changeset 68:d117fd4b82e5

Chartrendering integrated in Timeseries Transition gnv-artifacts/trunk@53 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 09 Sep 2009 15:28:18 +0000
parents 0e9762ebd18d
children f24c825144bc
files gnv-artifacts/Changelog gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java gnv-artifacts/src/main/java/de/intevation/gnv/timeseries/TimeSeriesArtifact.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/OutputTransition.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java
diffstat 7 files changed, 162 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/Changelog	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/Changelog	Wed Sep 09 15:28:18 2009 +0000
@@ -1,3 +1,15 @@
+2009-09-09  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java: 
+      Chart Rendering completed with some Fixes to do
+    * src/main/java/de/intevation/gnv/transition/TransitionBase.java: 
+      Integrated usage of KeyValueDescibeData
+    * src/main/java/de/intevation/gnv/transition/OutputTransition.java (getOutputModes): 
+     Errorhandling integrated
+    * src/main/java/de/intevation/gnv/timeseries/TimeSeriesArtifact.java Edited, 
+    * src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java Edited:
+    Refactored out-Method from Extending Class
+    
 2009-09-09  Tim Englich  <tim.englich@intevation.de>
 
     * src/main/java/de/intevation/gnv/chart/exception/TechnicalChartException.java Added, 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Wed Sep 09 15:28:18 2009 +0000
@@ -309,4 +309,24 @@
         }
         return returnValue;
     }
+    /**
+     * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, java.lang.Object)
+     */
+    public byte[] out(Document format, Object context) {
+        log.debug("TGNVArtifactBase.out");
+        try {
+            
+            if (current != null && current instanceof OutputTransition){
+                return ((OutputTransition)current).out(this.readOutputType(format));
+            }
+        } catch (TransitionException e) {
+            log.error(e,e);
+        }
+        return null;
+    }
+    
+    protected String readOutputType(Document document){
+        String value = Config.getStringXPath(document,"action/out/@name");
+        return value;
+    }
 }
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java	Wed Sep 09 15:28:18 2009 +0000
@@ -18,9 +18,6 @@
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.image.BufferedImage;
-import java.io.BufferedOutputStream;
-import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Collection;
@@ -69,7 +66,7 @@
 	private static boolean sDebug = sLogger.isDebugEnabled();
 
 
-	public synchronized void createSimpleTimeSeriesChart(ChartLabels pLabels, ChartStyle pStyle, String pTimeSeriesName, KeyValueDescibeData[] pParameterId,KeyValueDescibeData[] pMeasurementId, KeyValueDescibeData[] pFeatureId , OutputStream outputStream, Collection<Result> resultSet) throws IOException, TechnicalChartException {
+	public synchronized void createSimpleTimeSeriesChart(ChartLabels pLabels, ChartStyle pStyle, String pTimeSeriesName, Collection<KeyValueDescibeData> parameters, Collection<KeyValueDescibeData> measurements, OutputStream outputStream, Collection<Result> resultSet) throws IOException, TechnicalChartException {
 		if (sDebug)
 			sLogger.debug("createSimpleTimeSeriesChart()");
 		int lLowerLevel = Integer.MIN_VALUE;
@@ -85,7 +82,7 @@
 		if (sDebug)
 			sLogger.debug("  vor createDataset()");
 		XYDataset lSet = createDataset(pTimeSeriesName, resultSet, lUpperLevel,
-				lLowerLevel,pParameterId,pMeasurementId,pFeatureId);
+				lLowerLevel,parameters,measurements);
 		if (sDebug)
 			sLogger.debug("  nach createDataset()");
 		final Color[] color = {Color.black, Color.red, Color.green, Color.blue};
@@ -103,11 +100,12 @@
 		//plot.getRangeAxis().setFixedDimension(10.0);
 		plot.setDomainAxis(domain);
 		plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
-		if (pParameterId.length == 1) {
-			axis = new NumberAxis(pParameterId[0].getValue());
-			if(pParameterId[0].getValue().contains("richtung")){
+		if (parameters.size() == 1) {
+		    KeyValueDescibeData parameter = parameters.iterator().next();
+			axis = new NumberAxis(parameter.getValue());
+			if(parameter.getValue().contains("richtung")){
 				NumberAxis axis1 = new NumberAxis(
-						((String) pParameterId[0].getValue()));//,new Range(0.0,360.0));
+						((String) parameter.getValue()));//,new Range(0.0,360.0));
 			    axis1.setTickUnit(new NumberTickUnit(30.0));
 				axis1.setUpperBound(360.0);
 				axis1.setLowerBound(0.0);
@@ -246,7 +244,7 @@
             			if (sDebug)
             				sLogger.debug("MaxGap : "+maxGap/1000+" Länge : "+(dEnd.getTime() - dStart.getTime())/1000+
             						      "Intervall "+(dEnd.getTime() - dStart.getTime())/(pEnd-pStart)/1000);
-            			lDate = lRow0.getDate("PARAMETERID");
+            			lDate = lRow0.getDate("TIMEVALUE");
             			lDate0 = lDate;
             			lValue = lRow0.getDouble("DATAVALUE");
             			if (lValue > lLowerCut && lValue < lUpperCut){
@@ -256,7 +254,7 @@
                 	}
 				//for (int i = pStart+1; i <= pEnd; i++) {
 					lRow1 =lRow;
-					lDate = lRow1.getDate("PARAMETERID");
+					lDate = lRow1.getDate("TIMEVALUE");
 					lValue = lRow1.getDouble("DATAVALUE");
 					lDateDiff = lDate.getTime() - lDate0.getTime();
 					if (lDateDiff > maxGap) {
@@ -295,8 +293,7 @@
 	}
 
 	private XYDataset createDataset(String pTimeseriesName, Collection<Result> resultSet,
-			int lUpperCut, int lLowerCut,KeyValueDescibeData[] pParameterId,
-			KeyValueDescibeData[] pMeasurementId, KeyValueDescibeData[] pFeatureID) throws TechnicalChartException {
+			int lUpperCut, int lLowerCut,Collection<KeyValueDescibeData> parameters, Collection<KeyValueDescibeData> measurements) throws TechnicalChartException {
 		
 		TimeSeriesCollection lTimeSeriesCollection = new TimeSeriesCollection();
 		try{
@@ -321,8 +318,8 @@
     				if (break1 != row.getInteger("PARAMETERID").intValue()
     						|| break2 != row.getInteger("MEASUREMENTID").intValue()
     					    || break3 != row.getInteger("TIMESERIESID").intValue()){
-    					String mTimeSeriesName = findValueTitle(pParameterId,break1)+" "+
-    					                         findValueTitle(pMeasurementId,break2)+"m";
+    					String mTimeSeriesName = findValueTitle(parameters,break1)+" "+
+    					                         findValueTitle(measurements,break2)+"m";
     					
     					lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName,
     							resultSet, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd));
@@ -339,8 +336,8 @@
     				i = i + 1;
     			}
 			
-			String mTimeSeriesName = findValueTitle(pParameterId,break1)+" "+
-	                                 findValueTitle(pMeasurementId,break2)+"m";
+			String mTimeSeriesName = findValueTitle(parameters,break1)+" "+
+	                                 findValueTitle(measurements,break2)+"m";
 			lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName,
 					resultSet, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd));
 			}
@@ -365,10 +362,6 @@
 		int lWidth = (int) pStyle.getChartSize().getWidth();
 		int lHeight = (int) pStyle.getChartSize().getHeight();
 
-		FileOutputStream lFileOutputStream = null;
-		BufferedOutputStream lBufferedOutputStream = null;
-
-
 		BufferedImage lImage = pChart.createBufferedImage(lWidth, lHeight,
 				BufferedImage.BITMASK, null);
 	
@@ -376,9 +369,13 @@
 	
 }
 
-    private String findValueTitle(KeyValueDescibeData[] pValueId,int pMmtId){
-    	for (int i=0;i<pValueId.length;i++){
-    		if ((long) pMmtId ==pValueId[i].getKey())return pValueId[i].getValue();
+    private String findValueTitle(Collection<KeyValueDescibeData> values,int pMmtId){
+    	Iterator<KeyValueDescibeData> it = values.iterator();
+    	while(it.hasNext()){
+    	    KeyValueDescibeData data = it.next();
+    		if ((long) pMmtId ==data.getKey()){
+    		    return data.getValue();
+    		}
     	}
     	return "";
     }
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/timeseries/TimeSeriesArtifact.java	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/timeseries/TimeSeriesArtifact.java	Wed Sep 09 15:28:18 2009 +0000
@@ -45,15 +45,6 @@
     }
 
     /**
-     * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, java.lang.Object)
-     */
-    @Override
-    public byte[] out(Document format, Object context) {
-        log.debug("TimeSeriesArtifact.out");
-        return super.out(format, context);
-    }
-
-    /**
      * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, java.lang.Object)
      */
     @Override
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/OutputTransition.java	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/OutputTransition.java	Wed Sep 09 15:28:18 2009 +0000
@@ -5,6 +5,8 @@
 
 import java.util.Collection;
 
+import de.intevation.gnv.transition.exception.TransitionException;
+
 /**
  * @author Tim Englich <tim.englich@intevation.de>
  *
@@ -15,8 +17,9 @@
      * Returns the Rendered Result of an Transition.
      * @param outputMode The Mode that should be produces (Chart, Map, ...)
      * @return The Bytecode of the Result
+     * @throws TransitionException
      */
-    public byte[] out( String outputMode);
+    public byte[] out( String outputMode) throws TransitionException;
     
     /**
      * Delivers the provided OutputModes of an Transition
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java	Wed Sep 09 15:28:18 2009 +0000
@@ -24,6 +24,8 @@
 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
 import de.intevation.gnv.geobackend.util.DateUtils;
+import de.intevation.gnv.transition.describedata.DefaultKeyValueDescribeData;
+import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
 import de.intevation.gnv.transition.describedata.MinMaxDescribeData;
 import de.intevation.gnv.transition.exception.TransitionException;
 
@@ -219,7 +221,14 @@
         if (this.descibeData == null){
             this.descibeData = new ArrayList<Object>();
         }
-        this.descibeData.add(result);
+        
+        Iterator<Result> it = result.iterator();
+        Collection<KeyValueDescibeData> keyValueDescibeData = new ArrayList<KeyValueDescibeData>(result.size());
+        while(it.hasNext()){
+            Result resultValue = it.next();
+            keyValueDescibeData.add(new DefaultKeyValueDescribeData(resultValue.getInteger("KEY").intValue(), resultValue.getString("VALUE")));
+        }
+        this.descibeData.add(keyValueDescibeData);
     }
     
     /**
@@ -244,19 +253,19 @@
                     lableNode.setTextContent(name);
                     Element choiceNode = this.createXFormElement(document, "choices");
                     
-                    Collection<Result> values = (Collection)o;
-                    Iterator<Result> resultIt = values.iterator();
+                    Collection<KeyValueDescibeData> values = (Collection)o;
+                    Iterator<KeyValueDescibeData> resultIt = values.iterator();
                     while (resultIt.hasNext()){
-                        Result result = resultIt.next();
+                        KeyValueDescibeData result = resultIt.next();
                         Element itemNode = this.createXFormElement(document, "item");
                         
                         
                         Element choiceLableNode = this.createXFormElement(document, "label");
-                        choiceLableNode.setTextContent(result.getString("VALUE"));
+                        choiceLableNode.setTextContent(result.getValue());
                         itemNode.appendChild(choiceLableNode);
                         
                         Element choicValueNode = this.createXFormElement(document, "value");
-                        choicValueNode.setTextContent(result.getString("KEY"));
+                        choicValueNode.setTextContent(""+result.getKey());
                         itemNode.appendChild(choicValueNode);
                         
                         choiceNode.appendChild(itemNode);
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Wed Sep 09 13:14:12 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Wed Sep 09 15:28:18 2009 +0000
@@ -3,14 +3,35 @@
  */
 package de.intevation.gnv.transition.timeseries;
 
+import java.awt.Color;
+import java.awt.Dimension;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.gnv.chart.ChartFactory;
+import de.intevation.gnv.chart.ChartLabels;
+import de.intevation.gnv.chart.ChartStyle;
+import de.intevation.gnv.chart.exception.TechnicalChartException;
+import de.intevation.gnv.geobackend.base.Result;
 import de.intevation.gnv.transition.OutputTransitionBase;
+import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
+import de.intevation.gnv.transition.exception.TransitionException;
 
 /**
  * @author Tim Englich <tim.englich@intevation.de>
  *
  */
 public class TimeSeriesOutputTransition extends OutputTransitionBase{
-
+    
+    protected Collection<Result> chartResult = null;
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(TimeSeriesOutputTransition.class);
     /**
      * Constructor
      */
@@ -28,8 +49,74 @@
     /**
      * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String)
      */
-    public byte[] out(String outputMode) {
+    public byte[] out(String outputMode) throws TransitionException {
+        log.debug("TimeSeriesOutputTransition");
+        try {
+            this.advance(); // TODO nur neu holen wenn hash auf chartResult sich geändert hat
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            ChartFactory chartFactory = new ChartFactory();
+            
+            Collection<KeyValueDescibeData> parameters = this.getParameters();
+            Collection<KeyValueDescibeData> measurements = this.getMeasurements();
+            String timeSeriesName = "BLA";
+            ChartStyle chartStyle = this.creatStyle(600,400);
+            StringBuffer lDiagramTitle = new StringBuffer();
+            lDiagramTitle.append(/*mSelectedFis.getTitle()*/ "FIS-TITLE").append(" - ").append(/*mSelectedFeatureId[0].getTitle()*/"FeatureTitle"); // TODO: FIXME
+            String domainLable = "BLA"; // TODO woher bekommen wir das
+            ChartLabels chartLables = new ChartLabels(lDiagramTitle.toString(),domainLable , /*mSelectedParams[0].getTitle()*/ "Selected Params Title");
+            chartFactory.createSimpleTimeSeriesChart(chartLables, chartStyle, timeSeriesName, 
+                                                     parameters, measurements, 
+                                                     os, this.chartResult);
+            return os.toByteArray();
+        } catch (IOException e) {
+            log.error(e,e);
+            throw new TransitionException(e);
+        } catch (TechnicalChartException e) {
+            log.error(e,e);
+            throw new TransitionException(e);
+        }
+    }
+
+    /**
+     * @see de.intevation.gnv.transition.TransitionBase#purifyResult(java.util.Collection)
+     */
+    @Override
+    protected void purifyResult(Collection<Result> result) {
+        this.chartResult = result;
+    }
+    
+    protected ChartStyle creatStyle(int witdh, int height){
+        // TODO Konfigurierbar machen
+        de.intevation.gnv.chart.Insets lInsets = new de.intevation.gnv.chart.Insets(5d, 5d, 5d, 5d);
+        Dimension lChartSize = new Dimension(witdh, height);
+        return new ChartStyle(Color.white, new Color(230, 230, 230), Color.white, Color.white, true, true, lInsets, lChartSize);
+    }
+    
+    protected Collection<KeyValueDescibeData> getParameters(){
+        Iterator<Object> it = this.descibeData.iterator();
+        int i = 0;
+        while (it.hasNext()){
+           
+           Object o = it.next();
+           if (i == 1){ // TODO über Identifier arbeiten This is just a hack
+               return (Collection)o;
+           }
+           i++;
+        }
         return null;
     }
-
+    
+    protected Collection<KeyValueDescibeData> getMeasurements(){
+        Iterator<Object> it = this.descibeData.iterator();
+        int i = 0;
+        while (it.hasNext()){
+           
+           Object o = it.next();
+           if (i == 2){ // TODO über Identifier arbeiten This is just a hack
+               return (Collection)o;
+           }
+           i++;
+        }
+        return null;
+    }
 }

http://dive4elements.wald.intevation.org