changeset 67:0e9762ebd18d

Refactor ChartFactory so that it is able to be compiled without errors. gnv-artifacts/trunk@50 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 09 Sep 2009 13:14:12 +0000
parents bf883222b675
children d117fd4b82e5
files gnv-artifacts/Changelog gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/exception/TechnicalChartException.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/describedata/DefaultKeyValueDescribeData.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/describedata/KeyValueDescibeData.java
diffstat 5 files changed, 193 insertions(+), 128 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/Changelog	Wed Sep 09 12:03:00 2009 +0000
+++ b/gnv-artifacts/Changelog	Wed Sep 09 13:14:12 2009 +0000
@@ -1,3 +1,17 @@
+2009-09-09  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/java/de/intevation/gnv/chart/exception/TechnicalChartException.java Added, 
+      src/main/java/de/intevation/gnv/chart/ChartFactory.java Edited:
+      Refactoring of the ChartFactory so that it colud be use and it is possible to compile 
+      this class.
+      
+    * src/main/java/de/intevation/gnv/transition/describedata/KeyValueDescibeData.java Added, 
+      src/main/java/de/intevation/gnv/transition/describedata/DefaultKeyValueDescribeData.java Added:
+      Added an new Representation of Describedata for Key Value Pairs using for the Parameter Lists
+      of the Transitions an put it into the Chartgeneration 
+   
+       
+    
 2009-09-09  Tim Englich  <tim.englich@intevation.de>
 
     * pom.xml Edited:
@@ -5,6 +19,14 @@
     
 2009-09-09  Tim Englich  <tim.englich@intevation.de>
 
+    * src/main/java/de/intevation/gnv/chart/Insets.java Added, 
+      src/main/java/de/intevation/gnv/chart/ChartStyle.java Added, 
+      src/main/java/de/intevation/gnv/chart/ChartLabels.java Added, 
+      src/main/java/de/intevation/gnv/chart/ChartFactory.java Added:
+      Insert Chart-Classes from OLD-Repository Revision: 3101 Does not compile
+
+2009-09-09  Tim Englich  <tim.englich@intevation.de>
+
     * src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java Added:
       The specifed OutputTransition for the Product TimeSeries. 
     
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java	Wed Sep 09 12:03:00 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/ChartFactory.java	Wed Sep 09 13:14:12 2009 +0000
@@ -22,7 +22,10 @@
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Collection;
 import java.util.Date;
+import java.util.Iterator;
 
 import org.apache.log4j.Logger;
 import org.jfree.chart.JFreeChart;
@@ -41,16 +44,9 @@
 import org.jfree.data.xy.XYDataset;
 import org.jfree.ui.RectangleInsets;
 
-import au.com.bytecode.opencsv.CSVReader;
-import de.conterra.bsh.gdi.gnviewer.data.filter.IdValue;
-import de.conterra.bsh.gdi.gnviewer.data.filter.MeasurementId;
-import de.conterra.bsh.gdi.gnviewer.data.filter.ParameterId;
-import de.conterra.bsh.gdi.gnviewer.datasources.ResultSet;
-import de.conterra.bsh.gdi.gnviewer.datasources.Row;
-import de.conterra.bsh.gdi.gnviewer.exception.TechnicalException;
-import de.conterra.bsh.gdi.gnviewer.renderer.DiagramRenderer;
-import de.conterra.bsh.gdi.gnviewer.util.TempFile;
-import de.conterra.bsh.gdi.gnviewer.util.TemporaryFileDirectory;
+import de.intevation.gnv.chart.exception.TechnicalChartException;
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
 
 /**
  * The class <code>ChartFactory</code> fulfills the following purposes:
@@ -71,20 +67,9 @@
 	 */
 	private static Logger sLogger = Logger.getLogger(ChartFactory.class);
 	private static boolean sDebug = sLogger.isDebugEnabled();
-	private final TemporaryFileDirectory mTmpImageDir;
 
-	/**
-	 * 
-	 */
-	public ChartFactory(TemporaryFileDirectory pTmpImageDir) {
-		mTmpImageDir = pTmpImageDir;
-	}
 
-	public synchronized TempFile createSimpleTimeSeriesChart(
-			ChartLabels pLabels, ChartStyle pStyle, String pTimeSeriesName,
-			TempFile tempF, IdValue[] pParameterId,
-			IdValue[] pMeasurementId, IdValue[] pFeatureId
-			) throws IOException, TechnicalException {
+	public synchronized void createSimpleTimeSeriesChart(ChartLabels pLabels, ChartStyle pStyle, String pTimeSeriesName, KeyValueDescibeData[] pParameterId,KeyValueDescibeData[] pMeasurementId, KeyValueDescibeData[] pFeatureId , OutputStream outputStream, Collection<Result> resultSet) throws IOException, TechnicalChartException {
 		if (sDebug)
 			sLogger.debug("createSimpleTimeSeriesChart()");
 		int lLowerLevel = Integer.MIN_VALUE;
@@ -99,7 +84,7 @@
 		}
 		if (sDebug)
 			sLogger.debug("  vor createDataset()");
-		XYDataset lSet = createDataset(pTimeSeriesName, tempF, lUpperLevel,
+		XYDataset lSet = createDataset(pTimeSeriesName, resultSet, lUpperLevel,
 				lLowerLevel,pParameterId,pMeasurementId,pFeatureId);
 		if (sDebug)
 			sLogger.debug("  nach createDataset()");
@@ -119,10 +104,10 @@
 		plot.setDomainAxis(domain);
 		plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
 		if (pParameterId.length == 1) {
-			axis = new NumberAxis(pParameterId[0].getTitle());
-			if(pParameterId[0].getTitle().contains("richtung")){
+			axis = new NumberAxis(pParameterId[0].getValue());
+			if(pParameterId[0].getValue().contains("richtung")){
 				NumberAxis axis1 = new NumberAxis(
-						((String) pParameterId[0].getTitle()));//,new Range(0.0,360.0));
+						((String) pParameterId[0].getValue()));//,new Range(0.0,360.0));
 			    axis1.setTickUnit(new NumberTickUnit(30.0));
 				axis1.setUpperBound(360.0);
 				axis1.setLowerBound(0.0);
@@ -190,20 +175,15 @@
 		if (sDebug)
 			sLogger.debug("  vor encodeChart()");
 		
-		return encodeChart(chart, pStyle);
+		encodeChart(chart, pStyle, outputStream);
 	}
-    private static XYDataset getDataset(TimeSeriesCollection T, int pIndex){ //throws TechnicalException{
-    	//if (T.getSeriesCount() < pIndex) throw TechnicalException();
+    private static XYDataset getDataset(TimeSeriesCollection T, int pIndex){ //throws TechnicalChartException{
+    	//if (T.getSeriesCount() < pIndex) throw TechnicalChartException();
     	TimeSeriesCollection TSC = new TimeSeriesCollection();
     	TSC.addSeries(T.getSeries(pIndex));
     	return (XYDataset) TSC;
     }
-	public synchronized TempFile createScatterPlot(ChartStyle pStyle,
-			ResultSet pResults) throws TechnicalException, IOException {
-		ScatterPlot lPlot = new ScatterPlot("ScatterPlot", pResults);
-		return encodeChart(lPlot.getChart(), pStyle);
-	}
-
+    
 	private void configureRenderingOptions(JFreeChart pJfreechart) {
 		org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = ((XYPlot) pJfreechart
 				.getPlot()).getRenderer();
@@ -233,33 +213,28 @@
 	}
 	
 	
-	private TimeSeries createTimeSeries(String pTimeSeriesName, TempFile tempF, 
-			int lUpperCut, int lLowerCut,int pStart,int pEnd, Date dStart, Date dEnd) throws TechnicalException{
+	private TimeSeries createTimeSeries(String pTimeSeriesName,  Collection<Result> resultSet, 
+			int lUpperCut, int lLowerCut,int pStart,int pEnd, Date dStart, Date dEnd) throws TechnicalChartException{
 		if (sDebug)
 			sLogger.debug("createTimeSeries()");
-		Row lRow0, lRow1, lRowVorEnd;
+		Result lRow0, lRow1, lRowVorEnd;
 		
 		Date lDate=null, lDate0=null;
-        DiagramRenderer mDiagramRenderer = new DiagramRenderer();
         
-        CSVReader reader = null;
-		TimeSeries lTimeseries = new TimeSeries(pTimeSeriesName,   
+        TimeSeries lTimeseries = new TimeSeries(pTimeSeriesName,   
 				org.jfree.data.time.Minute.class);
 		try {
-			reader = new CSVReader (new FileReader(tempF.getFile()), ';');
-			
-			
-			
 			long maxGap=0, lDateDiff=0;
 			double lValue=0;
-			String [] lRow = null;
+			
 
 			int i = 0;
-            while ((lRow = reader.readNext()) != null){
-            	
+			Iterator<Result> resultIterator = resultSet.iterator();
+            while (resultIterator.hasNext()){
+            	Result lRow = resultIterator.next();
             	if (i >= pStart+1 && i <= pEnd){
             		if (i == pStart+1){
-                		lRow0 = new Row(lRow);//
+                		lRow0 = lRow;//
             			
             			// Handle Gaps > 0.5% timeserieslength, i.e do not draw here
             			// +TODO mache maxGap variabel über diagram options
@@ -271,18 +246,18 @@
             			if (sDebug)
             				sLogger.debug("MaxGap : "+maxGap/1000+" Länge : "+(dEnd.getTime() - dStart.getTime())/1000+
             						      "Intervall "+(dEnd.getTime() - dStart.getTime())/(pEnd-pStart)/1000);
-            			lDate = lRow0.getDateValue(0);
+            			lDate = lRow0.getDate("PARAMETERID");
             			lDate0 = lDate;
-            			lValue = lRow0.getDoubleValue(1);
+            			lValue = lRow0.getDouble("DATAVALUE");
             			if (lValue > lLowerCut && lValue < lUpperCut){
             				//lTimeseries.addOrUpdate(new Minute(lDate), lValue);
             				lTimeseries.add(new Minute(lDate), lValue);
             			}
                 	}
 				//for (int i = pStart+1; i <= pEnd; i++) {
-					lRow1 = new Row (lRow);
-					lDate = lRow1.getDateValue(0);
-					lValue = lRow1.getDoubleValue(1);
+					lRow1 =lRow;
+					lDate = lRow1.getDate("PARAMETERID");
+					lValue = lRow1.getDouble("DATAVALUE");
 					lDateDiff = lDate.getTime() - lDate0.getTime();
 					if (lDateDiff > maxGap) {
 						// add 1 minute in millisecs to left hand side Date
@@ -310,94 +285,77 @@
 			sLogger.error(e.getMessage(), e);
 			return lTimeseries;
 		
-		} catch (Exception e) { //TechnicalException
+		} catch (Exception e) { //TechnicalChartException
 			sLogger.error(e.getMessage(), e);
 		}
 		finally {
-			try{
-				reader.close();
-			}
-			catch (Exception e){
-				sLogger.error(e.getMessage(), e);
-			}
 		}
 		
 		return lTimeseries;
 	}
 
-	private XYDataset createDataset(String pTimeseriesName, TempFile tempF,
-			int lUpperCut, int lLowerCut,IdValue[] pParameterId,
-			IdValue[] pMeasurementId, IdValue[] pFeatureID) throws TechnicalException {
+	private XYDataset createDataset(String pTimeseriesName, Collection<Result> resultSet,
+			int lUpperCut, int lLowerCut,KeyValueDescibeData[] pParameterId,
+			KeyValueDescibeData[] pMeasurementId, KeyValueDescibeData[] pFeatureID) throws TechnicalChartException {
 		
-		CSVReader reader=null;
 		TimeSeriesCollection lTimeSeriesCollection = new TimeSeriesCollection();
 		try{
-			for (int i=0;i<pMeasurementId.length;i++) System.out.println("Measurement Depth "+
-				            pMeasurementId[i].getTitle() );
-			for (int i=0;i<pParameterId.length;i++) System.out.println("Parameter "+
-		            pParameterId[i].getTitle() );
-			
 			Date dStart = null, dEnd= null;
 			int break1, break2, break3;
 			int mStart = 0;
 			int mEnd = 0;
 			
-			reader = new CSVReader (new FileReader(tempF.getFile()), ';');
 	
-			String [] sArrayStrLine = reader.readNext();
-			
-			Row row = new Row(sArrayStrLine);
+			Iterator<Result> resultIterator = resultSet.iterator();
+			if (resultIterator.hasNext()){
+    //			Row row = new Row(sArrayStrLine);
+    			Result row = resultIterator.next();
+    			
+    			break1 = row.getInteger("PARAMETERID").intValue(); // 2
+    			break2 = row.getInteger("MEASUREMENTID").intValue();  //3
+    			break3 = row.getInteger("TIMESERIESID").intValue(); // 4
+    			dStart = row.getDate("TIMEVALUE");
+    			int i = 1;
+    			while (resultIterator.hasNext()) {
+    			    row = resultIterator.next();
+    				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";
+    					
+    					lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName,
+    							resultSet, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd));
+    					mStart = i;
+    					dStart = row.getDate("TIMEVALUE");
+    					break1 = row.getInteger("PARAMETERID").intValue();
+    					break2 = row.getInteger("MEASUREMENTID").intValue();  //3
+    					break3 = row.getInteger("TIMESERIESID").intValue(); // 4
+    	
+    				}
+    				mEnd = i;
+    				//mEnd ++;
+    				dEnd = row.getDate("TIMEVALUE");
+    				i = i + 1;
+    			}
 			
-			break1 = new Integer (sArrayStrLine[2]).intValue();
-			break2 = new Integer (sArrayStrLine[3]).intValue();
-			break3 = new Integer (sArrayStrLine[4]).intValue();
-			dStart = row.getDateValue(0);
-			int i = 1;
-			while ((sArrayStrLine = reader.readNext()) != null) {
-				row = new Row(sArrayStrLine);
-				if (break1 != new Integer (sArrayStrLine[2]).intValue()
-						|| break2 != new Integer (sArrayStrLine[3]).intValue()
-					    || break3 != new Integer (sArrayStrLine[4]).intValue()){
-					String mTimeSeriesName = findValueTitle(pParameterId,break1)+" "+
-					                         findValueTitle(pMeasurementId,break2)+"m";
-					
-					lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName,
-							tempF, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd));
-					mStart = i;
-					dStart = row.getDateValue(0);
-					break1 = new Integer (sArrayStrLine[2]).intValue();
-					break2 = new Integer (sArrayStrLine[3]).intValue();
-					break3 = new Integer (sArrayStrLine[4]).intValue();
-	
-				}
-				mEnd = i;
-				//mEnd ++;
-				dEnd = row.getDateValue(0);
-				i = i + 1;
-			}
 			String mTimeSeriesName = findValueTitle(pParameterId,break1)+" "+
 	                                 findValueTitle(pMeasurementId,break2)+"m";
 			lTimeSeriesCollection.addSeries(createTimeSeries(mTimeSeriesName,
-					tempF, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd));
-			
+					resultSet, lUpperCut, lLowerCut, mStart, mEnd, dStart, dEnd));
+			}
 		}
 		catch (Exception e){
 			sLogger.error(e.getMessage(), e);
 		}
 		finally{
-			try{
-			reader.close();
-			}
-			catch (Exception e){
-				sLogger.error(e.getMessage(), e);
-			}
 		}
 		return lTimeSeriesCollection;
 	}
 
 
 
-	private TempFile encodeChart(JFreeChart pChart, ChartStyle pStyle)
+	private void encodeChart(JFreeChart pChart, ChartStyle pStyle, OutputStream outputStream)
 			throws IOException {
 		if (sDebug)
 			sLogger.debug("encodeChart()");
@@ -410,29 +368,17 @@
 		FileOutputStream lFileOutputStream = null;
 		BufferedOutputStream lBufferedOutputStream = null;
 
-		TempFile lImageFile = mTmpImageDir.createFile(".png");
 
 		BufferedImage lImage = pChart.createBufferedImage(lWidth, lHeight,
 				BufferedImage.BITMASK, null);
-		lFileOutputStream = new FileOutputStream(lImageFile.getFile());
-		lBufferedOutputStream = new BufferedOutputStream(lFileOutputStream);
-		lEncoder.encode(lImage, lBufferedOutputStream);
-		lBufferedOutputStream.close();
-		lFileOutputStream.close();
-
-		return lImageFile;
+	
+		lEncoder.encode(lImage, outputStream);
+	
+}
 
-	}
-
-    private String findValueTitle(IdValue[] pValueId,int pMmtId){
+    private String findValueTitle(KeyValueDescibeData[] pValueId,int pMmtId){
     	for (int i=0;i<pValueId.length;i++){
-    		if ((long) pMmtId ==pValueId[i].getValue())return pValueId[i].getTitle();
-    	}
-    	return "";
-    }
-    private String findValueTitle(IdValue[] pValueId,long pMmtId){
-    	for (int i=0;i<pValueId.length;i++){
-    		if ( pMmtId ==pValueId[i].getValue())return pValueId[i].getTitle();
+    		if ((long) pMmtId ==pValueId[i].getKey())return pValueId[i].getValue();
     	}
     	return "";
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/exception/TechnicalChartException.java	Wed Sep 09 13:14:12 2009 +0000
@@ -0,0 +1,47 @@
+/**
+ *
+ */
+package de.intevation.gnv.chart.exception;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class TechnicalChartException extends Exception {
+
+    /**
+     * The UID of this Class
+     */
+    private static final long serialVersionUID = -5325863742368006109L;
+
+    /**
+     * Constructor
+     */
+    public TechnicalChartException() {
+    }
+
+    /**
+     * Constructor
+     * @param message
+     */
+    public TechnicalChartException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructor
+     * @param cause
+     */
+    public TechnicalChartException(Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * Constructor
+     * @param message
+     * @param cause
+     */
+    public TechnicalChartException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/describedata/DefaultKeyValueDescribeData.java	Wed Sep 09 13:14:12 2009 +0000
@@ -0,0 +1,35 @@
+/**
+ *
+ */
+package de.intevation.gnv.transition.describedata;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class DefaultKeyValueDescribeData implements KeyValueDescibeData {
+
+    private int key;
+    
+    private String value = null;
+    
+    public DefaultKeyValueDescribeData(int key, String value) {
+        super();
+        this.key = key;
+        this.value = value;
+    }
+
+    /**
+     * @see de.intevation.gnv.transition.describedata.KeyValueDescibeData#getKey()
+     */
+    public int getKey() {
+       return this.key;
+    }
+
+    /**
+     * @see de.intevation.gnv.transition.describedata.KeyValueDescibeData#getValue()
+     */
+    public String getValue() {
+        return this.value;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/describedata/KeyValueDescibeData.java	Wed Sep 09 13:14:12 2009 +0000
@@ -0,0 +1,15 @@
+/**
+ *
+ */
+package de.intevation.gnv.transition.describedata;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public interface KeyValueDescibeData {
+    
+    public int getKey();
+    
+    public String getValue();
+
+}

http://dive4elements.wald.intevation.org