changeset 300:6a3a02e004d9

Refactored process of chart generation. Charts will be generated via Chart-Interface from rev351 and no more via factory classes. gnv-artifacts/trunk@354 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 20 Nov 2009 13:51:14 +0000 (2009-11-20)
parents d6c75171f1e9
children 3ea030aafe65
files gnv-artifacts/ChangeLog gnv-artifacts/TODO gnv-artifacts/src/main/java/de/intevation/gnv/transition/OutputTransitionBase.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputTransition.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/vertical/VerticalProfileOutputTransition.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/verticalcrosssection/VerticalCrossSectionOutputTransition.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java
diffstat 8 files changed, 345 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Fri Nov 20 13:51:14 2009 +0000
@@ -1,3 +1,36 @@
+2009-11-20  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/transition/OutputTransitionBase.java:
+	  Added methods for caching charts.
+
+	* src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java,
+	  src/main/java/de/intevation/gnv/transition/profile/vertical/VerticalProfileOutputTransition.java,
+	  src/main/java/de/intevation/gnv/transition/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputTransition.java,
+	  src/main/java/de/intevation/gnv/transition/profile/verticalcrosssection/VerticalCrossSectionOutputTransition.java,
+	  src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java:
+	  Changed chart generation. Charts will now be generated in a central place
+	  in TimeSeriesOutputTransition which is the parent class of other
+	  OutputTransitions. Each OutputTransition got a new method to serve its own
+	  special chart class.
+
+	* TODO: Added hint for caching charts. Verticalcrosssection and
+	  orizontalcrosssection charts aren't implemented yet.
+
+	  NOTE: Charts can be cached. This could be useful if the user exports 
+	  charts as svg, png or pdf. The caching of charts is configured via system
+	  property 'cache.chart' at the moment.
+
+
+2009-11-19  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java,
+	  src/main/java/de/intevation/gnv/transition/OutputTransition.java,
+	  src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java:
+	  Changed parameter of 'out'-method of OutputTransition. Now, the
+	  OutputTransition gets the complete xml document of the request (before
+	  just the target name). The mime type of this xml ist used to distinguish
+	  between different output formats of charts (png, jpeg).
+
 2009-11-19  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java,
--- a/gnv-artifacts/TODO	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/TODO	Fri Nov 20 13:51:14 2009 +0000
@@ -0,0 +1,5 @@
+TODO:
+    * Caching of charts is configured via system property. This should be done
+      in another way. (Property: -Dcache.chart)
+    * charts for verticalcrosssection and horizontalcrosssection need to be
+      implemented
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/OutputTransitionBase.java	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/OutputTransitionBase.java	Fri Nov 20 13:51:14 2009 +0000
@@ -145,6 +145,20 @@
         }
         return null;
     }
+
+    protected Object getChartFromCache(String uuid) {
+        log.debug("Fetch chart [" + uuid + "] from cache");
+        CacheFactory cacheFactory = CacheFactory.getInstance();
+        if (cacheFactory.isInitialized()) {
+            String key = "chart_" + uuid + super.getID();
+            net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
+
+            if (object != null) {
+                return object.getObjectValue();
+            }
+        }
+        return null;
+    }
     
     protected Collection<Result> getODVResult(String uuid) {
         log.debug("OutputTransitionBase.getODVResult");
@@ -197,6 +211,16 @@
         }
     }
 
+
+    protected void purifyChart(Object chart, String uuid) {
+        log.debug("Prufify chart [" + uuid + "]");
+        CacheFactory cacheFactory = CacheFactory.getInstance();
+        if (cacheFactory.isInitialized()) {
+            String key = "chart_" + uuid + getID();
+            cacheFactory.getCache().put(new net.sf.ehcache.Element(key, chart));
+        }
+    }
+
     /**
      * @see de.intevation.gnv.transition.TransitionBase#putInputData(java.util.Collection, java.lang.String)
      */
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java	Fri Nov 20 13:51:14 2009 +0000
@@ -3,23 +3,31 @@
  */
 package de.intevation.gnv.transition.profile.horizontal;
 
+import java.awt.Dimension;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Locale;
+
+import javax.imageio.ImageIO;
 
 import org.apache.log4j.Logger;
 
+import org.jfree.chart.ChartTheme;
+
 import au.com.bytecode.opencsv.CSVWriter;
 
 import com.vividsolutions.jts.geom.Point;
 import com.vividsolutions.jts.io.ParseException;
 import com.vividsolutions.jts.io.WKTReader;
 
+import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.ChartStyle;
+import de.intevation.gnv.chart.HorizontalProfileChart;
 import de.intevation.gnv.chart.HorizontalProfileChartFactory;
 import de.intevation.gnv.chart.exception.TechnicalChartException;
 import de.intevation.gnv.geobackend.base.Result;
@@ -73,25 +81,47 @@
         super.domainLable = "Distance [km]";
     }
 
-    /**
-     * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#createChart(java.io.OutputStream,
-     *      java.util.Collection, java.util.Collection, java.lang.String,
-     *      de.intevation.gnv.chart.ChartStyle,
-     *      de.intevation.gnv.chart.ChartLabels)
-     */
-    @Override
-    protected void createChart(OutputStream outputStream,
-                               Collection<KeyValueDescibeData> parameters,
-                               Collection<KeyValueDescibeData> measurements,
-                               Collection<KeyValueDescibeData> dates,
-                               ChartStyle chartStyle, ChartLabels chartLables,
-                               String uuid) throws IOException,
-                                           TechnicalChartException {
-        HorizontalProfileChartFactory chartFactory = new HorizontalProfileChartFactory();
-        chartFactory.createProfileChart(chartLables, chartStyle, parameters,
-                measurements,dates, outputStream, this.getChartResult(uuid));
+    protected Chart getChart(
+        ChartLabels  chartLables,
+        ChartTheme   chartTheme,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   result,
+        Collection   dates,
+        Locale       locale,
+        String       uuid
+    ) {
+        Chart chart = null;
+
+        if (CACHE_CHART) {
+            log.info("Try to get horizontalprofile chart from cache.");
+            chart = (Chart) getChartFromCache(uuid);
+        }
+
+        if (chart != null)
+            return chart;
+
+        log.info("Chart not in cache yet.");
+        chart = new HorizontalProfileChart(
+            chartLables,
+            chartTheme,
+            parameters,
+            measurements,
+            result,
+            dates,
+            locale
+        );
+        chart.generateChart();
+
+        if (CACHE_CHART) {
+            log.info("Put chart into cache.");
+            purifyChart(chart, uuid);
+        }
+
+        return chart;
     }
 
+
     /**
      * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#getStatisticsGenerator()
      */
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputTransition.java	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputTransition.java	Fri Nov 20 13:51:14 2009 +0000
@@ -9,10 +9,14 @@
 import java.io.UnsupportedEncodingException;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Locale;
 
 import org.apache.log4j.Logger;
 
+import org.jfree.chart.ChartTheme;
+
 import au.com.bytecode.opencsv.CSVWriter;
+import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.ChartStyle;
 import de.intevation.gnv.chart.HorizontalCrossSectionChartFactory;
@@ -46,27 +50,52 @@
         super.domainLable = "Latitude [m]";
     }
 
-    /**
-     * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#createChart(java.io.OutputStream,
-     *      java.util.Collection, java.util.Collection, java.lang.String,
-     *      de.intevation.gnv.chart.ChartStyle,
-     *      de.intevation.gnv.chart.ChartLabels)
-     */
-    @Override
-    protected void createChart(OutputStream outputStream,
-                               Collection<KeyValueDescibeData> parameters,
-                               Collection<KeyValueDescibeData> measurements,
-                               Collection<KeyValueDescibeData> dates,
-                               ChartStyle chartStyle, ChartLabels chartLables,
-                               String uuid) throws IOException,
-                                           TechnicalChartException {
-       new HorizontalCrossSectionChartFactory().
-            createProfileChart(chartLables,  chartStyle, 
-                               parameters, measurements, 
-                               dates, outputStream, 
-                               this.getChartResult(uuid));
+
+    protected Chart getChart(
+        ChartLabels  chartLables,
+        ChartTheme   chartTheme,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   result,
+        Collection   dates,
+        Locale       locale,
+        String       uuid
+    ) {
+        Chart chart = null;
+
+        if (CACHE_CHART) {
+            log.info("Try to get horizontalcrosssection chart from cache.");
+            chart = (Chart) getChartFromCache(uuid);
+        }
+
+        if (chart != null)
+            return chart;
+
+        log.info("Chart not in cache yet.");
+        
+        log.warn("This sort of chart is not implemented yet.");
+        /* TODO Implement a special chart for this sort of charts.
+        chart = new HorizontalProfileChart(
+            chartLables,
+            chartTheme,
+            parameters,
+            measurements,
+            result,
+            dates,
+            locale
+        );
+        chart.generateChart();
+
+        if (CACHE_CHART) {
+            log.info("Put chart into cache.");
+            purifyChart(chart, uuid);
+        }
+        */
+
+        return chart;
     }
 
+
     /**
      * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#getStatisticsGenerator()
      */
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/vertical/VerticalProfileOutputTransition.java	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/vertical/VerticalProfileOutputTransition.java	Fri Nov 20 13:51:14 2009 +0000
@@ -3,12 +3,23 @@
  */
 package de.intevation.gnv.transition.profile.vertical;
 
+import java.awt.Dimension;
+
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Collection;
+import java.util.Locale;
 
+import javax.imageio.ImageIO;
+
+import org.apache.log4j.Logger;
+
+import org.jfree.chart.ChartTheme;
+
+import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.ChartStyle;
+import de.intevation.gnv.chart.VerticalProfileChart;
 import de.intevation.gnv.chart.VerticalProfileChartFactory;
 import de.intevation.gnv.chart.exception.TechnicalChartException;
 import de.intevation.gnv.statistics.Statistics;
@@ -26,6 +37,9 @@
      */
     private static final long serialVersionUID = 4401516087492028840L;
 
+    private static Logger log = Logger
+            .getLogger(TimeSeriesOutputTransition.class);
+
     /**
      * Constructor
      */
@@ -34,23 +48,44 @@
         super.domainLable = "Depth [m]";
     }
 
-    /**
-     * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#createChart(java.io.OutputStream,
-     *      java.util.Collection, java.util.Collection, java.lang.String,
-     *      de.intevation.gnv.chart.ChartStyle,
-     *      de.intevation.gnv.chart.ChartLabels)
-     */
-    @Override
-    protected void createChart(OutputStream outputStream,
-                               Collection<KeyValueDescibeData> parameters,
-                               Collection<KeyValueDescibeData> measurements,
-                               Collection<KeyValueDescibeData> dates,
-                               ChartStyle chartStyle, ChartLabels chartLables,
-                               String uuid) throws IOException,
-                                           TechnicalChartException {
-        VerticalProfileChartFactory chartFactory = new VerticalProfileChartFactory();
-        chartFactory.createProfileChart(chartLables, chartStyle, parameters,
-                measurements,dates, outputStream, this.getChartResult(uuid));
+    protected Chart getChart(
+        ChartLabels  chartLables,
+        ChartTheme   chartTheme,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   result,
+        Collection   dates,
+        Locale       locale,
+        String       uuid
+    ) {
+        Chart chart = null;
+
+        if (CACHE_CHART) {
+            log.info("Try to get verticalprofile chart from cache.");
+            chart = (Chart) getChartFromCache(uuid);
+        }
+
+        if (chart != null)
+            return chart;
+
+        log.info("Chart not in cache yet.");
+        chart = new VerticalProfileChart(
+            chartLables,
+            chartTheme,
+            parameters,
+            measurements,
+            result,
+            dates,
+            locale
+        );
+        chart.generateChart();
+
+        if (CACHE_CHART) {
+            log.info("Put chart into cache.");
+            purifyChart(chart, uuid);
+        }
+
+        return chart;
     }
 
     /**
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/verticalcrosssection/VerticalCrossSectionOutputTransition.java	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/verticalcrosssection/VerticalCrossSectionOutputTransition.java	Fri Nov 20 13:51:14 2009 +0000
@@ -9,15 +9,19 @@
 import java.io.UnsupportedEncodingException;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Locale;
 
 import org.apache.log4j.Logger;
 
+import org.jfree.chart.ChartTheme;
+
 import au.com.bytecode.opencsv.CSVWriter;
 
 import com.vividsolutions.jts.geom.Point;
 import com.vividsolutions.jts.io.ParseException;
 import com.vividsolutions.jts.io.WKTReader;
 
+import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.ChartStyle;
 import de.intevation.gnv.chart.VerticalCrossSectionChartFactory;
@@ -51,12 +55,58 @@
         super.domainLable = "Depth [m]";
     }
 
+
+    protected Chart getChart(
+        ChartLabels  chartLables,
+        ChartTheme   chartTheme,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   result,
+        Collection   dates,
+        Locale       locale,
+        String       uuid
+    ) {
+        Chart chart = null;
+
+        if (CACHE_CHART) {
+            log.info("Try to get verticalcrosssection chart from cache.");
+            chart = (Chart) getChartFromCache(uuid);
+        }
+
+        if (chart != null)
+            return chart;
+
+        log.info("Chart not in cache yet.");
+        
+        log.warn("This sort of chart is not implemented yet.");
+        /* TODO Implement a special chart for this sort of charts.
+        chart = new VerticalProfileChart(
+            chartLables,
+            chartTheme,
+            parameters,
+            measurements,
+            result,
+            dates,
+            locale
+        );
+        chart.generateChart();
+
+        if (CACHE_CHART) {
+            log.info("Put chart into cache.");
+            purifyChart(chart, uuid);
+        }
+        */
+
+        return chart;
+    }
+
     /**
      * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#createChart(java.io.OutputStream,
      *      java.util.Collection, java.util.Collection, java.lang.String,
      *      de.intevation.gnv.chart.ChartStyle,
      *      de.intevation.gnv.chart.ChartLabels)
      */
+    /*
     @Override
     protected void createChart(OutputStream outputStream,
                                Collection<KeyValueDescibeData> parameters,
@@ -69,6 +119,7 @@
                 chartStyle, parameters, measurements, dates, outputStream, this
                         .getChartResult(uuid));
     }
+    */
 
     /**
      * @see de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition#getStatisticsGenerator()
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Thu Nov 19 17:54:18 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Fri Nov 20 13:51:14 2009 +0000
@@ -12,6 +12,7 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 
 import javax.imageio.ImageIO;
 import javax.xml.transform.Transformer;
@@ -28,16 +29,19 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import org.jfree.chart.ChartTheme;
+
 import au.com.bytecode.opencsv.CSVWriter;
 import de.intevation.artifactdatabase.Config;
 import de.intevation.artifactdatabase.XMLUtils;
 import de.intevation.artifacts.CallMeta;
 import de.intevation.artifacts.PreferredLocale;
 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
-import de.intevation.gnv.chart.TestChart;
+import de.intevation.gnv.chart.Chart;
 import de.intevation.gnv.chart.ChartFactory;
 import de.intevation.gnv.chart.ChartLabels;
 import de.intevation.gnv.chart.ChartStyle;
+import de.intevation.gnv.chart.AbstractXYLineChart;
 import de.intevation.gnv.chart.TimeSeriesChart;
 import de.intevation.gnv.chart.exception.TechnicalChartException;
 import de.intevation.gnv.exports.DefaultExport;
@@ -70,6 +74,9 @@
  */
 public class TimeSeriesOutputTransition extends OutputTransitionBase {
 
+    protected static final boolean CACHE_CHART = 
+        Boolean.parseBoolean(System.getProperty("cache.chart", "false"));
+
     /**
      * The UID of this Class
      */
@@ -80,8 +87,7 @@
      */
     private static Logger log = Logger
             .getLogger(TimeSeriesOutputTransition.class);
-    
-    
+
     private static List<TimeGap> timeGapDefinitions = null;
 
     protected String domainLable = "Zeit [UTC]";
@@ -352,17 +358,82 @@
      * @throws IOException
      * @throws TechnicalChartException
      */
-    protected void createChart(OutputStream outputStream,
-                               Collection<KeyValueDescibeData> parameters,
-                               Collection<KeyValueDescibeData> measurements,
-                               Collection<KeyValueDescibeData> dates,
-                               ChartStyle chartStyle, ChartLabels chartLables,
-                               String uuid) throws IOException,
-                                           TechnicalChartException {
-        ChartFactory chartFactory = new ChartFactory();
-        chartFactory.createSimpleTimeSeriesChart(chartLables, chartStyle,
-                parameters, measurements, outputStream, this
-                        .getChartResult(uuid),timeGapDefinitions);
+    protected void createChart(
+        OutputStream outputStream,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   dates,
+        ChartStyle   chartStyle,
+        ChartLabels  chartLables,
+        String       uuid
+    )
+    throws IOException, TechnicalChartException
+    {
+        log.debug("Create chart.");
+        Chart chart = getChart(
+            chartLables,
+            null, // ChartTheme
+            parameters,
+            measurements,
+            getChartResult(uuid),
+            timeGapDefinitions,
+            null, // Locale
+            uuid
+        );
+
+        if (chart == null) {
+            log.error("Could not initialize chart.");
+            return;
+        }
+
+        // TODO generic format
+        Dimension dim    = chartStyle.getChartSize();
+        String    format = "PNG";
+        int       width  = new Double(dim.getWidth()).intValue();
+        int       height = new Double(dim.getHeight()).intValue();
+
+        log.debug("export chart as " + format + " in " + width + "x" + height);
+        ImageIO.write(chart.exportPNG(width, height), format, outputStream);
+    }
+
+    protected Chart getChart(
+        ChartLabels  chartLables,
+        ChartTheme   chartTheme,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   result,
+        Collection   dates,
+        Locale       locale,
+        String       uuid
+    ) {
+        Chart chart = null;
+
+        if (CACHE_CHART) {
+            log.info("Try to get timeseries chart from cache.");
+            chart = (Chart) getChartFromCache(uuid);
+        }
+
+        if (chart != null)
+            return chart;
+
+        log.info("Chart not in cache yet.");
+        chart = new TimeSeriesChart(
+            chartLables,
+            null,
+            parameters,
+            measurements,
+            result,
+            dates,
+            null
+        );
+        chart.generateChart();
+
+        if (CACHE_CHART) {
+            log.info("Put chart into cache.");
+            purifyChart(chart, uuid);
+        }
+
+        return chart;
     }
 
     protected ChartStyle creatStyle(int witdh, int height) {

http://dive4elements.wald.intevation.org