changeset 233:2ceb4bf51cba

Added DataCollector which collects data used for exports. gnv-artifacts/trunk@301 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 09 Nov 2009 09:43:52 +0000
parents 3d6d89bcbf42
children a7fa7b404897
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultDataCollector.java gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultExport.java gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultProfile.java gnv-artifacts/src/main/java/de/intevation/gnv/exports/Export.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java
diffstat 6 files changed, 102 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Mon Nov 09 08:55:01 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Mon Nov 09 09:43:52 2009 +0000
@@ -1,3 +1,19 @@
+2009-11-09  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/exports/Export.java,
+	  src/main/java/de/intevation/gnv/exports/DefaultDataCollector.java:
+	  Implemented a DataCollector to collect all data used to create exports. 
+	  DefaultCollector fetches data with help of its headers. For special 
+	  exports, where a header returns more than one value, special 
+	  DataCollectors needs to be implemented (overwritung the 'getData' method).
+
+	* src/main/java/de/intevation/gnv/exports/DefaultProfile.java,
+	  src/main/java/de/intevation/gnv/exports/DefaultExport.java: Export now
+	  uses a DataCollector to get the data.
+
+	* src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java:
+	  Adjusted exports of csv export.	  
+
 2009-11-09  Tim Englich  <tim.englich@intevation.de>
 
 	* src/test/ressources/timeseries/timeseries_step_06_out_*.xml:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultDataCollector.java	Mon Nov 09 09:43:52 2009 +0000
@@ -0,0 +1,48 @@
+package de.intevation.gnv.exports;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+import de.intevation.gnv.transition.exception.TransitionException;
+
+
+/**
+ * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
+ */
+public class DefaultDataCollector
+implements Export.DataCollector
+{
+    private   Logger log = Logger.getLogger(DefaultDataCollector.class);
+
+    protected ResultDescriptor rd;
+    protected String []        names;
+
+
+    public DefaultDataCollector(String[] names) {
+        this.names = names;
+    }
+
+    public void init(Result res) {
+        rd = res.getResultDescriptor();
+    }
+
+    public String[] getData(Result result)
+    throws TransitionException {
+
+        if (rd == null)
+            init(result);
+
+        List entries = new ArrayList();
+
+        int[] indices = rd.getColumnIndices(names);
+        for (int i = 0; i < names.length; ++i) {
+            entries.add(result.getString(indices[i]));
+        }
+
+        return (String[]) entries.toArray((new String[entries.size()]));
+    }
+}
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultExport.java	Mon Nov 09 08:55:01 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultExport.java	Mon Nov 09 09:43:52 2009 +0000
@@ -29,35 +29,25 @@
 {
     private static Logger log = Logger.getLogger(DefaultExport.class);
 
+    private Export.DataCollector collector;
+
+    public DefaultExport(Export.DataCollector collector) {
+        this.collector = collector;
+    }
+
     protected void writeData(
         Profile    profile, 
         Collection result,
         CSVWriter  writer
-    ) {
+    )
+    throws TransitionException {
         log.debug("create content for export.");
         Iterator<Result> it = result.iterator();
 
-        ResultDescriptor rd      = null;
-        int []           indices = null;
-
-        String[] entries = new String[profile.numberColumns()];
-
         while (it.hasNext()) {
             Result res = it.next();
 
-            if (rd == null) {
-                rd = res.getResultDescriptor();
-                String [] names = new String[entries.length];
-                for (int i = 0; i < names.length; ++i) {
-                    names[i] = profile.getHeader(i);
-                }
-                indices = rd.getColumnIndices(names);
-            }
-            for (int i = 0; i < entries.length; ++i) {
-                entries[i] = profile.toString(
-                    i, res.getString(indices[i]));
-            }
-            writer.writeNext(entries);
+            writer.writeNext(collector.getData(res));
         }
     }
 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultProfile.java	Mon Nov 09 08:55:01 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultProfile.java	Mon Nov 09 09:43:52 2009 +0000
@@ -1,5 +1,8 @@
 package de.intevation.gnv.exports;
 
+import de.intevation.gnv.geobackend.base.Result;
+
+
 /**
  * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
  * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
@@ -7,25 +10,24 @@
 public class DefaultProfile
 implements   Export.Profile
 {
-    protected String [] names;
-    protected char      separator;
-    protected char      escapeCharacter;
-    protected char      quoteCharacter;
-    protected String    type;
-    protected String    encoding;
+    protected Export.DataCollector collector;
+    protected String []            names;
+    protected char                 separator;
+    protected char                 escapeCharacter;
+    protected char                 quoteCharacter;
+    protected String               type;
+    protected String               encoding;
 
     public DefaultProfile() {
     }
 
     public DefaultProfile(
-        String [] names, 
-        char      separator,
-        char      escapeCharacter,
-        char      quoteCharacter,
-        String    type,
-        String    encoding
+        char                 separator,
+        char                 escapeCharacter,
+        char                 quoteCharacter,
+        String               type,
+        String               encoding
     ) {
-        this.names           = names;
         this.separator       = separator;
         this.escapeCharacter = escapeCharacter;
         this.quoteCharacter  = quoteCharacter;
@@ -33,14 +35,6 @@
         this.encoding        = encoding;
     }
 
-    public int numberColumns() {
-        return names.length;
-    }
-
-    public String getHeader(int index) {
-        return names[index];
-    }
-
     public String toString(int index, String value) {
         return value;
     }
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/Export.java	Mon Nov 09 08:55:01 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/Export.java	Mon Nov 09 09:43:52 2009 +0000
@@ -7,6 +7,7 @@
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
+import de.intevation.gnv.geobackend.base.Result;
 import de.intevation.gnv.transition.exception.TransitionException;
 
 /**
@@ -16,8 +17,6 @@
 public interface Export {
 
     public interface Profile {
-        String getHeader(int column);
-        int    numberColumns();
         String toString(int column, String value);
         char   getSeparator();
         char   getEscapeCharacter();
@@ -26,6 +25,11 @@
         String getEncoding();
     }
 
+    public interface DataCollector {
+        String[] getData(Result result)
+        throws TransitionException;
+    }
+
     public void create(
         Profile      profile,
         OutputStream outputStream, 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Mon Nov 09 08:55:01 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Mon Nov 09 09:43:52 2009 +0000
@@ -52,6 +52,10 @@
 import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
 import de.intevation.gnv.transition.describedata.NamedCollection;
 import de.intevation.gnv.transition.exception.TransitionException;
+import de.intevation.gnv.exports.DefaultExport;
+import de.intevation.gnv.exports.DefaultDataCollector;
+import de.intevation.gnv.exports.DefaultProfile;
+import de.intevation.gnv.exports.Export.Profile;
 import de.intevation.gnv.utils.ArtifactXMLUtilities;
 
 
@@ -108,7 +112,6 @@
      */
     public static final Profile TIMESERIES_CSV_PROFILE =
         new DefaultProfile(
-            TIMESERIES_CSV_PROFILE_NAMES,
             ',',
             '"',
             '"',
@@ -121,7 +124,6 @@
      */
     public static final Profile TIMESERIES_ODV_PROFILE =
         new DefaultProfile(
-            TIMESERIES_ODV_PROFILE_NAMES,
             '\t',
             CSVWriter.NO_QUOTE_CHARACTER,
             CSVWriter.NO_ESCAPE_CHARACTER,
@@ -190,8 +192,6 @@
                 Document doc = this.writeStatistics2XML(statistics);
                 this.writeDocument2OutputStream(doc, outputStream);
             } else if (outputMode.equalsIgnoreCase("odv")) {
-                //TimeSeriesExport export = new TimeSeriesExport();
-                //export.createODV(outputStream, chartResult);
                 
                 Collection<Result> odvResult = this.getODVResult(uuid);
                 this.createODV(outputStream, odvResult);
@@ -221,7 +221,8 @@
                                                           throws UnsupportedEncodingException,
                                                           IOException,
                                                           TransitionException {
-        DefaultExport export = new DefaultExport();
+        DefaultExport export = new DefaultExport(new DefaultDataCollector(
+            TIMESERIES_CSV_PROFILE_NAMES));
 
         export.create(TIMESERIES_CSV_PROFILE, outputStream, chartResult);
     }
@@ -232,7 +233,8 @@
     protected void createODV(OutputStream outputStream, Collection result)
     throws IOException, TransitionException {
 
-        DefaultExport export = new DefaultExport();
+        DefaultExport export = new DefaultExport(new DefaultDataCollector(
+            TIMESERIES_CSV_PROFILE_NAMES));
 
         export.create(TIMESERIES_ODV_PROFILE, outputStream, result);
     }

http://dive4elements.wald.intevation.org