changeset 230:f68ffbe974a0

Implemented an exporter for odv and csv exports. gnv-artifacts/trunk@298 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 06 Nov 2009 10:56:18 +0000
parents a610c0a01afc
children 74255064a4c5
files gnv-artifacts/ChangeLog 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 5 files changed, 286 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Fri Nov 06 10:23:55 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Fri Nov 06 10:56:18 2009 +0000
@@ -1,20 +1,36 @@
+2009-11-06  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/exports,
+	  src/main/java/de/intevation/gnv/exports/Export.java,
+	  src/main/java/de/intevation/gnv/exports/DefaultProfile.java,
+	  src/main/java/de/intevation/gnv/exports/DefaultExport.java: Implemented an
+	  Exporter for odv and csv exports. Each exports just needs a Profile, which
+	  describes the output. 
+
+	* src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java:
+	  Added an exemplary export for CSV and ODV formats of
+	  TimeSeriesOutputTransition.
+
+	  TODO: ODV exporter exports the same data as CSV exporter does. This needs
+	  to be adapted.
+
 2009-11-06  Tim Englich  <tim.englich@intevation.de>
 
-    * src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCase.java: 
-      Added and moved Workflowsteps because of changes in the Workflow of these Artifacts.
-      Now the Region-Filter is added to the Workflow an can be tested using
-      this UnitTestCases.
-      
-    * src/test/ressources/horizontalcrosssection_mesh/horizontalcrosssection_step_*.xml, 
-      src/test/ressources/horizontalProfile_mesh/horizontalprofile_step_*.xml,
-      src/test/ressources/timeseries/timeseries_step_*.xml,
-      src/test/ressources/timeseries_mesh/timeseries_step_*.xml,
-      src/test/ressources/verticalprofile/verticalprofile_step_*.xml,
-      src/test/ressources/verticalprofile_mesh/verticalprofile_step_*.xml,
-      Added and moved Files because of changes in the Workflow of these Artifacts.
-      Now the Region-Filter is added to the Workflow an can be tested using
-      the UnitTestCases.
-      
+	* src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCase.java: 
+	  Added and moved Workflowsteps because of changes in the Workflow of these Artifacts.
+	  Now the Region-Filter is added to the Workflow an can be tested using
+	  this UnitTestCases.
+
+	* src/test/ressources/horizontalcrosssection_mesh/horizontalcrosssection_step_*.xml, 
+	  src/test/ressources/horizontalProfile_mesh/horizontalprofile_step_*.xml,
+	  src/test/ressources/timeseries/timeseries_step_*.xml,
+	  src/test/ressources/timeseries_mesh/timeseries_step_*.xml,
+	  src/test/ressources/verticalprofile/verticalprofile_step_*.xml,
+	  src/test/ressources/verticalprofile_mesh/verticalprofile_step_*.xml:
+	  Added and moved Files because of changes in the Workflow of these Artifacts.
+	  Now the Region-Filter is added to the Workflow an can be tested using
+	  the UnitTestCases.
+
 2009-11-06  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* doc/conf/products/horizontalcrosssection/conf_mesh.xml,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultExport.java	Fri Nov 06 10:56:18 2009 +0000
@@ -0,0 +1,93 @@
+package de.intevation.gnv.exports;
+
+import org.apache.log4j.Logger;
+
+import au.com.bytecode.opencsv.CSVWriter;
+
+import java.util.List;
+import java.util.Iterator;
+import java.util.Collection;
+
+import java.io.UnsupportedEncodingException;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.OutputStream;
+
+import de.intevation.gnv.exports.Export.Profile;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+
+import de.intevation.gnv.transition.exception.TransitionException;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
+ */
+public class DefaultExport
+implements   Export
+{
+    private static Logger log = Logger.getLogger(DefaultExport.class);
+
+    protected void writeData(
+        Profile    profile, 
+        Collection result,
+        CSVWriter  writer
+    ) {
+        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);
+        }
+    }
+
+    public void create(
+        Profile      profile,
+        OutputStream outputStream, 
+        Collection   result
+    )
+    throws
+        IOException,
+        UnsupportedEncodingException,
+        TransitionException 
+    {
+        if (result == null) {
+            String msg = "No data given for generation of " +
+                profile.getType() + " file.";
+            log.error(msg);
+            throw new TransitionException(msg);
+        }
+
+        CSVWriter writer = new CSVWriter(
+            new OutputStreamWriter(
+                outputStream,
+                profile.getEncoding()), 
+            profile.getSeparator(),
+            profile.getQuoteCharacter(),
+            profile.getEscapeCharacter()); 
+
+        writeData(profile, result, writer);
+
+        writer.close();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultProfile.java	Fri Nov 06 10:56:18 2009 +0000
@@ -0,0 +1,67 @@
+package de.intevation.gnv.exports;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
+ */
+public class DefaultProfile
+implements   Export.Profile
+{
+    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
+    ) {
+        this.names           = names;
+        this.separator       = separator;
+        this.escapeCharacter = escapeCharacter;
+        this.quoteCharacter  = quoteCharacter;
+        this.type            = type;
+        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;
+    }
+
+    public char getSeparator() {
+        return separator;
+    }
+
+    public char getEscapeCharacter() {
+        return escapeCharacter;
+    }
+
+    public char getQuoteCharacter() {
+        return quoteCharacter;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public String getEncoding() {
+        return encoding;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/Export.java	Fri Nov 06 10:56:18 2009 +0000
@@ -0,0 +1,36 @@
+package de.intevation.gnv.exports;
+
+import java.util.Collection;
+import java.util.List;
+
+import java.io.OutputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import de.intevation.gnv.transition.exception.TransitionException;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
+ */
+public interface Export {
+
+    public interface Profile {
+        String getHeader(int column);
+        int    numberColumns();
+        String toString(int column, String value);
+        char   getSeparator();
+        char   getEscapeCharacter();
+        char   getQuoteCharacter();
+        String getType();
+        String getEncoding();
+    }
+
+    public void create(
+        Profile      profile,
+        OutputStream outputStream, 
+        Collection   result
+    )
+    throws IOException, UnsupportedEncodingException, TransitionException;
+
+}
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Fri Nov 06 10:23:55 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Fri Nov 06 10:56:18 2009 +0000
@@ -8,6 +8,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -50,8 +51,14 @@
 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.DefaultProfile;
+import de.intevation.gnv.exports.Export.Profile;
+
 import de.intevation.gnv.utils.ArtifactXMLUtilities;
 
+
 /**
  * @author Tim Englich <tim.englich@intevation.de>
  * 
@@ -78,6 +85,39 @@
     protected String parameterValuesName = "parameterid";
     protected String measuremenValueName = "measurementid";
     protected String dateValueName = "dateid";
+    
+    public static final String [] TIMESERIES_CSV_PROFILE_NAMES = {
+        "XORDINATE",
+        "YORDINATE",
+        "GROUP1",
+        "GROUP2",
+        "GROUP3"
+    };
+
+    /**
+     * Profile for exporting data to cvs
+     */
+    public static final Profile TIMESERIES_CSV_PROFILE =
+        new DefaultProfile(
+            TIMESERIES_CSV_PROFILE_NAMES,
+            ',',
+            '"',
+            '"',
+            "CSV",
+            "ISO-8859-1");
+
+    /**
+     * Profile for exporting data to odv
+     * TODO Change TIMESERIES_PROFILE_NAMES, which belong to CSV exports
+     */
+    public static final Profile TIMESERIES_ODV_PROFILE =
+        new DefaultProfile(
+            TIMESERIES_CSV_PROFILE_NAMES,
+            '\t',
+            CSVWriter.NO_QUOTE_CHARACTER,
+            CSVWriter.NO_ESCAPE_CHARACTER,
+            "ODV",
+            "ISO-8859-1");
 
     /**
      * Constructor
@@ -138,6 +178,10 @@
                         .calculateStatistics(chartResult);
                 Document doc = this.writeStatistics2XML(statistics);
                 this.writeDocument2OutputStream(doc, outputStream);
+            } else if (outputMode.equalsIgnoreCase("odv")) {
+                //TimeSeriesExport export = new TimeSeriesExport();
+                //export.createODV(outputStream, chartResult);
+                createODV(outputStream, chartResult);
             }
         } catch (IOException e) {
             log.error(e, e);
@@ -151,6 +195,7 @@
         }
     }
 
+    
     /**
      * @param outputStream
      * @param chartResult
@@ -163,29 +208,20 @@
                                                           throws UnsupportedEncodingException,
                                                           IOException,
                                                           TransitionException {
-        if (chartResult != null) {
-            CSVWriter writer = new CSVWriter(new OutputStreamWriter(
-                    outputStream, "ISO-8859-1"), ','); 
-            // USE THIS ENCODING BECAUSE OF
-            // PROBLEMS WITH EXCEL AND UTF-8
-            Iterator<Result> it = chartResult.iterator();
-            while (it.hasNext()) {
-                Result result = it.next();
-                int i = 0;
-                String[] entries = new String[5];
-                entries[i++] = result.getString("XORDINATE");
-                entries[i++] = result.getString("YORDINATE");
-                entries[i++] = result.getString("GROUP1");
-                entries[i++] = result.getString("GROUP2");
-                entries[i++] = result.getString("GROUP3");
-                writer.writeNext(entries);
-            }
-            writer.close();
-        } else {
-            log.error("No Data given for generation an CSV-File.");
-            throw new TransitionException(
-                    "No Data given for generation an CSV-File.");
-        }
+        DefaultExport export = new DefaultExport();
+
+        export.create(TIMESERIES_CSV_PROFILE, outputStream, chartResult);
+    }
+
+    /**
+     * TODO Result is not used at the moment. Change result with correct data.
+     */
+    protected void createODV(OutputStream outputStream, Collection result)
+    throws IOException, TransitionException {
+
+        DefaultExport export = new DefaultExport();
+
+        export.create(TIMESERIES_ODV_PROFILE, outputStream, result);
     }
 
     /**

http://dive4elements.wald.intevation.org