From 23c18ca9bcc2a87eb61e8e2ad2021ec149140d82 Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Thu, 15 Apr 2010 17:24:59 +0000
Subject: [PATCH] LMS-1483 Handling of configuration parameters for graph
 generation.

SVN: 15490
---
 screening/etc/tabular-data-graph.properties   | 42 +++++++++++++++++++
 .../server/TabularDataGraphServlet.java       | 40 ++++++++++++++++--
 .../server/graph/ITabularDataGraph.java       | 10 +++++
 ...bularDataGraphCollectionConfiguration.java | 31 ++++++++++----
 .../graph/TabularDataGraphConfiguration.java  | 15 ++++++-
 .../server/graph/TabularDataHeatmap.java      |  3 --
 .../TabularDataHeatmapConfiguration.java      |  4 +-
 .../TabularDataHistogramConfiguration.java    |  2 +-
 .../server/graph/TabularDataScatterplot.java  |  5 ++-
 .../TabularDataScatterplotConfiguration.java  | 41 ++++++++++++++++++
 ...rDataGraphCollectionConfigurationTest.java |  7 +++-
 .../server/graph/TabularDataHeatmapTest.java  |  2 +-
 .../graph/TabularDataScatterplotTest.java     | 14 ++++---
 .../dss/generic/server/graph/graph.properties |  4 +-
 14 files changed, 190 insertions(+), 30 deletions(-)
 create mode 100644 screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotConfiguration.java

diff --git a/screening/etc/tabular-data-graph.properties b/screening/etc/tabular-data-graph.properties
index 9c3080b9185..04cb84acd09 100644
--- a/screening/etc/tabular-data-graph.properties
+++ b/screening/etc/tabular-data-graph.properties
@@ -1,3 +1,45 @@
 # ------------------------------------------------------------------
 # Properties File For Generating Graphs From Tabular Data
 # ------------------------------------------------------------------
+# ------------------------------------------------------------------
+# Properties File For Generating Graphs From Tabular Data
+# ------------------------------------------------------------------
+
+# The width of the full image
+full-width = 800
+
+# The height of the full image
+full-height = 600
+
+# The width of the table column
+column-width = 300
+
+# The height of the table row
+column-height = 200
+
+# The graphs shown per row
+graphs = scatter1, hist, heat, scatter2
+
+# The properties for the first scatter plot
+scatter1.graph-type = scatterplot
+scatter1.x-axis = TotalCells
+scatter1.y-axis = InfectedCells
+scatter1.title = Total vs. Infected
+
+# The properties for the histogram
+hist.graph-type = histogram
+hist.column = TotalCells
+hist.title = Total Cells Histogram
+
+# The properties for the heatmap
+heat.graph-type = heatmap
+heat.x-axis = WellName
+heat.y-axis = WellName
+heat.column = InfectionIndex
+heat.title = Infection Index
+
+# The properties for the second scatter plot
+scatter2.graph-type = scatterplot
+scatter2.x-axis = TotalCells
+scatter2.y-axis = RelativeInfectionIndex
+scatter2.title = Total vs. Rel Infection
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java
index 42a0ac587b5..0e9ce3ec0fc 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/TabularDataGraphServlet.java
@@ -64,6 +64,11 @@ public class TabularDataGraphServlet extends AbstractDatasetDownloadServlet
      */
     private static class RequestParams
     {
+        // optional parameters
+        public final static String WIDTH_PARAM = "w";
+
+        public final static String HEIGHT_PARAM = "h";
+
         private final String sessionId;
 
         private final String datasetCode;
@@ -72,12 +77,35 @@ public class TabularDataGraphServlet extends AbstractDatasetDownloadServlet
 
         private final String graphName;
 
+        private final int width;
+
+        private final int height;
+
         public RequestParams(HttpServletRequest request)
         {
             sessionId = getRequiredParameter(request, SESSION_ID_PARAM);
             datasetCode = getRequiredParameter(request, DATASET_CODE_PARAM);
             filePath = getRequiredParameter(request, FILE_PATH_PARAM);
             graphName = getRequiredParameter(request, GRAPH_TYPE_CODE);
+            width = getIntParam(request, WIDTH_PARAM, 0);
+            height = getIntParam(request, HEIGHT_PARAM, 0);
+        }
+
+        private static int getIntParam(HttpServletRequest request, String paramName,
+                int defaultValue)
+        {
+            String value = request.getParameter(paramName);
+            if (value == null)
+                return defaultValue;
+
+            try
+            {
+                return Integer.valueOf(value);
+            } catch (NumberFormatException e)
+            {
+                throw new UserFailureException("parameter " + paramName
+                        + " should be an integer, but is: " + value);
+            }
         }
 
         private static String getRequiredParameter(final HttpServletRequest request,
@@ -135,9 +163,15 @@ public class TabularDataGraphServlet extends AbstractDatasetDownloadServlet
 
             // Generate an image image into the stream
             ITabularDataGraph generator =
-                    configuration.getGraph(params.graphName, fileLines, response
-                            .getOutputStream());
-            generator.generateImage();
+                    configuration.getGraph(params.graphName, fileLines, response.getOutputStream());
+
+            if (params.height > 0 && params.width > 0)
+            {
+                generator.generateImage(params.width, params.height);
+            } else
+            {
+                generator.generateImage();
+            }
 
         } catch (Exception e)
         {
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/ITabularDataGraph.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/ITabularDataGraph.java
index 59c01edd377..724bbd49ae7 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/ITabularDataGraph.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/ITabularDataGraph.java
@@ -32,4 +32,14 @@ public interface ITabularDataGraph
      */
     public abstract void generateImage() throws IOException;
 
+    /**
+     * Create an image,overriding the width and height in the configuration from the file lines and
+     * write it to the output stream.
+     * 
+     * @param imageWidth The desired width of the image
+     * @param imageHeight The desired height of the image
+     * @throws IOException
+     */
+    public abstract void generateImage(int imageWidth, int imageHeight) throws IOException;
+
 }
\ No newline at end of file
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java
index 3e6bbdee632..77b57e02b1d 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfiguration.java
@@ -28,6 +28,7 @@ import java.util.Properties;
 
 import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
 import ch.systemsx.cisd.common.utilities.PropertyUtils;
+import ch.systemsx.cisd.openbis.dss.generic.server.graph.TabularDataGraphConfiguration.GraphType;
 import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.DatasetFileLines;
 import ch.systemsx.cisd.openbis.dss.generic.shared.utils.PropertyParametersUtil;
 import ch.systemsx.cisd.openbis.dss.generic.shared.utils.PropertyParametersUtil.SectionProperties;
@@ -89,11 +90,6 @@ public class TabularDataGraphCollectionConfiguration
 
     private final HashMap<String, TabularDataGraphConfiguration> graphTypeMap;
 
-    private enum GraphType
-    {
-        SCATTERPLOT, HISTOGRAM, HEATMAP
-    }
-
     /**
      * Create a configuration from the properties file located at path.
      * 
@@ -192,9 +188,10 @@ public class TabularDataGraphCollectionConfiguration
                         .getMandatoryProperty(props, COLUMN_KEY), getThumbnailWidth(),
                         getThumbnailHeight(), PropertyUtils.getInt(props, NUMBER_OF_BINS_KEY, 10));
             case SCATTERPLOT:
-                return new TabularDataGraphConfiguration(title, PropertyUtils.getMandatoryProperty(
-                        props, X_AXIS_KEY), PropertyUtils.getMandatoryProperty(props, Y_AXIS_KEY),
-                        getThumbnailWidth(), getThumbnailHeight());
+                return new TabularDataScatterplotConfiguration(title, PropertyUtils
+                        .getMandatoryProperty(props, X_AXIS_KEY), PropertyUtils
+                        .getMandatoryProperty(props, Y_AXIS_KEY), getThumbnailWidth(),
+                        getThumbnailHeight());
         }
 
         // should never get here
@@ -231,7 +228,23 @@ public class TabularDataGraphCollectionConfiguration
         {
             throw new IllegalArgumentException("No graph associated with code " + graphName);
         }
-        return new TabularDataScatterplot(config, fileLines, out);
+        GraphType type = config.getGraphType();
+        switch (type)
+        {
+            case HEATMAP:
+                return new TabularDataHeatmap((TabularDataHeatmapConfiguration) config, fileLines,
+                        out);
+            case HISTOGRAM:
+                return new TabularDataHistogram((TabularDataHistogramConfiguration) config,
+                        fileLines, out);
+            case SCATTERPLOT:
+                return new TabularDataScatterplot((TabularDataScatterplotConfiguration) config,
+                        fileLines, out);
+
+        }
+
+        // should never get here
+        return null;
     }
 
     public char getColumnDelimiter()
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphConfiguration.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphConfiguration.java
index 5a45d0bcde3..b60d7ccea51 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphConfiguration.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphConfiguration.java
@@ -23,6 +23,13 @@ package ch.systemsx.cisd.openbis.dss.generic.server.graph;
  */
 public class TabularDataGraphConfiguration
 {
+    public enum GraphType
+    {
+        SCATTERPLOT, HISTOGRAM, HEATMAP
+    }
+
+    private final GraphType graphType;
+
     private final String title;
 
     // the column that is used as the x-axis
@@ -44,9 +51,10 @@ public class TabularDataGraphConfiguration
      * @param imageWidth The desired width of the resulting image
      * @param imageHeight The desired height of the resulting image
      */
-    public TabularDataGraphConfiguration(String title, String xAxisColumn,
+    protected TabularDataGraphConfiguration(GraphType graphType, String title, String xAxisColumn,
             String yAxisColumn, int imageWidth, int imageHeight)
     {
+        this.graphType = graphType;
         this.title = title;
         this.xAxisColumn = xAxisColumn;
         this.yAxisColumn = yAxisColumn;
@@ -62,6 +70,11 @@ public class TabularDataGraphConfiguration
         return title;
     }
 
+    public GraphType getGraphType()
+    {
+        return graphType;
+    }
+
     /**
      * The name of the column from which the x values come.
      */
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmap.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmap.java
index ed8e3c3e07f..f58c5d45cb0 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmap.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmap.java
@@ -165,9 +165,6 @@ public class TabularDataHeatmap extends AbstractTabularDataGraph<TabularDataHeat
             {
                 heatmapData.maxY = element.y;
             }
-
-            // DEBUG
-            System.out.println("" + element.x + "," + element.y + "," + element.z);
         }
 
         return heatmapData;
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapConfiguration.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapConfiguration.java
index 3a4a0d7234e..7fe7b85aa3d 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapConfiguration.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapConfiguration.java
@@ -40,7 +40,7 @@ public class TabularDataHeatmapConfiguration extends TabularDataGraphConfigurati
     protected TabularDataHeatmapConfiguration(String title, String xAxisColumn, String yAxisColumn,
             String zAxisColumn, int imageWidth, int imageHeight)
     {
-        super(title, xAxisColumn, yAxisColumn, imageWidth, imageHeight);
+        super(GraphType.HISTOGRAM, title, xAxisColumn, yAxisColumn, imageWidth, imageHeight);
         this.zAxisColumn = zAxisColumn;
         isXYSplit = true;
     }
@@ -58,7 +58,7 @@ public class TabularDataHeatmapConfiguration extends TabularDataGraphConfigurati
     protected TabularDataHeatmapConfiguration(String title, String indexColumn, String zAxisColumn,
             int imageWidth, int imageHeight)
     {
-        super(title, indexColumn, indexColumn, imageWidth, imageHeight);
+        super(GraphType.HEATMAP, title, indexColumn, indexColumn, imageWidth, imageHeight);
         this.zAxisColumn = zAxisColumn;
         isXYSplit = false;
     }
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHistogramConfiguration.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHistogramConfiguration.java
index 58eaefbdc80..2fd73e71d85 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHistogramConfiguration.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHistogramConfiguration.java
@@ -36,7 +36,7 @@ public class TabularDataHistogramConfiguration extends TabularDataGraphConfigura
     protected TabularDataHistogramConfiguration(String title, String histogramColumn,
             int imageWidth, int imageHeight, int numberOfBins)
     {
-        super(title, histogramColumn, histogramColumn, imageWidth, imageHeight);
+        super(GraphType.HISTOGRAM, title, histogramColumn, histogramColumn, imageWidth, imageHeight);
         assert numberOfBins > 0;
         this.numberOfBins = numberOfBins;
     }
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplot.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplot.java
index bf775ba3eab..ac9f36ec863 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplot.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplot.java
@@ -31,13 +31,14 @@ import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.DatasetFileLine
 /**
  * @author Chandrasekhar Ramakrishnan
  */
-public class TabularDataScatterplot extends AbstractTabularDataGraph<TabularDataGraphConfiguration>
+public class TabularDataScatterplot extends
+        AbstractTabularDataGraph<TabularDataScatterplotConfiguration>
 {
 
     /**
      * @param configuration
      */
-    public TabularDataScatterplot(TabularDataGraphConfiguration configuration,
+    public TabularDataScatterplot(TabularDataScatterplotConfiguration configuration,
             DatasetFileLines fileLines, OutputStream out)
     {
         super(configuration, fileLines, out);
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotConfiguration.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotConfiguration.java
new file mode 100644
index 00000000000..05d91f218d0
--- /dev/null
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotConfiguration.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.dss.generic.server.graph;
+
+
+/**
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class TabularDataScatterplotConfiguration extends TabularDataGraphConfiguration
+{
+
+    /**
+     * Construct a configuration for a scatterplot.
+     * 
+     * @param title
+     * @param xAxisColumn
+     * @param yAxisColumn
+     * @param imageWidth
+     * @param imageHeight
+     */
+    public TabularDataScatterplotConfiguration(String title, String xAxisColumn,
+            String yAxisColumn, int imageWidth, int imageHeight)
+    {
+        super(GraphType.SCATTERPLOT, title, xAxisColumn, yAxisColumn, imageWidth, imageHeight);
+    }
+
+}
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java
index 75ce8c95693..09c7360fb3e 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataGraphCollectionConfigurationTest.java
@@ -60,8 +60,13 @@ public class TabularDataGraphCollectionConfigurationTest extends AssertJUnit
         assertEquals(30, graphConfig.getImageWidth());
 
         assertEquals("hist", graphNames.get(1));
+        graphConfig = configuration.getGraphConfiguration(graphNames.get(1));
+        assertEquals("Total Cells Histogram", graphConfig.getTitle());
+
         assertEquals("heat", graphNames.get(2));
-        assertEquals("scatter2", graphNames.get(3));
+        graphConfig = configuration.getGraphConfiguration(graphNames.get(2));
+        assertEquals("Infected Cells", graphConfig.getTitle());
 
+        assertEquals("scatter2", graphNames.get(3));
     }
 }
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapTest.java
index b8ca69d7222..94f1bd136e5 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataHeatmapTest.java
@@ -29,7 +29,7 @@ public class TabularDataHeatmapTest extends AbstractTabularDataGraphTest
     @Test
     public void testHeatmap() throws IOException
     {
-        File outputFile = getImageOutputFile();
+        File outputFile = getTestImageOutputFile();
 
         TabularDataHeatmapConfiguration config =
                 new TabularDataHeatmapConfiguration("Test", "WellName", "InfectionIndex", 300, 200);
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotTest.java
index e77a0b0e146..ae7be7358d1 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/TabularDataScatterplotTest.java
@@ -31,9 +31,10 @@ public class TabularDataScatterplotTest extends AbstractTabularDataGraphTest
     {
         File outputFile = getImageOutputFile();
 
-        TabularDataGraphConfiguration config =
-                new TabularDataGraphConfiguration("Test", "TotalCells", "InfectedCells", 300, 200);
-        AbstractTabularDataGraph<TabularDataGraphConfiguration> graph =
+        TabularDataScatterplotConfiguration config =
+                new TabularDataScatterplotConfiguration("Test", "TotalCells", "InfectedCells", 300,
+                        200);
+        TabularDataScatterplot graph =
                 new TabularDataScatterplot(config, getDatasetFileLines(),
                         getOutputStream(outputFile));
         assertNotSame(graph.tryXColumnNumber(), graph.tryYColumnNumber());
@@ -48,9 +49,10 @@ public class TabularDataScatterplotTest extends AbstractTabularDataGraphTest
     {
         File outputFile = getImageOutputFile();
 
-        TabularDataGraphConfiguration config =
-                new TabularDataGraphConfiguration("Test", "TotalCells", "Non-existant", 300, 200);
-        AbstractTabularDataGraph<TabularDataGraphConfiguration> graph =
+        TabularDataScatterplotConfiguration config =
+                new TabularDataScatterplotConfiguration("Test", "TotalCells", "Non-existant", 300,
+                        200);
+        TabularDataScatterplot graph =
                 new TabularDataScatterplot(config, getDatasetFileLines(),
                         getOutputStream(outputFile));
         assertTrue(graph.tryYColumnNumber() < 0);
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties
index 534639217f9..6605628eba3 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/graph/graph.properties
@@ -31,14 +31,16 @@ scatter1.y-axis = InfectedCells
 # The properties for the histogram
 hist.graph-type = histogram
 hist.column = TotalCells
+hist.title = Total Cells Histogram
 
 # The properties for the heatmap
 heat.graph-type = heatmap
 heat.x-axis = WellName
 heat.y-axis = WellName
 heat.column = InfectedCells
+heat.title = Infected Cells
 
 # The properties for the second scatter plot
 scatter2.graph-type = scatterplot
 scatter2.x-axis = TotalCells
-scatter2.y-axis = RelativeInfectionIndex
\ No newline at end of file
+scatter2.y-axis = RelativeInfectionIndex
-- 
GitLab