# HG changeset patch # User Ingo Weinzierl # Date 1304444276 0 # Node ID 5d6988836f017d1485c8f6ee466bf68e87feccf8 # Parent a67748ad4d617937366989686b169bc2c51b583b Added an exporter to export the computed data of a duration curve. flys-artifacts/trunk@1814 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r a67748ad4d61 -r 5d6988836f01 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Tue May 03 17:34:29 2011 +0000 +++ b/flys-artifacts/ChangeLog Tue May 03 17:37:56 2011 +0000 @@ -1,3 +1,21 @@ +2011-05-03 Ingo Weinzierl + + * doc/conf/artifacts/winfo.xml: Added a new output mode for the duration + curve state (CSV export). + + * doc/conf/conf.xml: Added a new OutGenerator to export duration curve + computations. + + * src/main/java/de/intevation/flys/exports/AbstractExporter.java: New. + This abstract OutGenerator represents the base class for exporting + computed data. Currently, the CSV export is supported. + + * src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: Moved + the most code to export to CSV into the AbstractExporter. + + * src/main/java/de/intevation/flys/exports/DurationCurveExporter.java: + New. This exporter exports the computed data of a duration computation. + 2011-05-03 Ingo Weinzierl * src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java: diff -r a67748ad4d61 -r 5d6988836f01 flys-artifacts/doc/conf/artifacts/winfo.xml --- a/flys-artifacts/doc/conf/artifacts/winfo.xml Tue May 03 17:34:29 2011 +0000 +++ b/flys-artifacts/doc/conf/artifacts/winfo.xml Tue May 03 17:37:56 2011 +0000 @@ -84,6 +84,11 @@ + + + + + @@ -103,7 +108,7 @@ - + diff -r a67748ad4d61 -r 5d6988836f01 flys-artifacts/doc/conf/conf.xml --- a/flys-artifacts/doc/conf/conf.xml Tue May 03 17:34:29 2011 +0000 +++ b/flys-artifacts/doc/conf/conf.xml Tue May 03 17:37:56 2011 +0000 @@ -53,6 +53,7 @@ de.intevation.flys.exports.LongitudinalSectionGenerator de.intevation.flys.exports.DurationCurveGenerator de.intevation.flys.exports.WaterlevelExporter + de.intevation.flys.exports.DurationCurveExporter diff -r a67748ad4d61 -r 5d6988836f01 flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java Tue May 03 17:37:56 2011 +0000 @@ -0,0 +1,167 @@ +package de.intevation.flys.exports; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; + +import org.w3c.dom.Document; + +import org.apache.log4j.Logger; + +import au.com.bytecode.opencsv.CSVWriter; + +import de.intevation.artifacts.Artifact; +import de.intevation.artifacts.CallContext; + + +/** + * An abstract exporter that implements some basic methods for exporting data of + * artifacts. + * + * @author Ingo Weinzierl + */ +public abstract class AbstractExporter implements OutGenerator { + + /** The logger used in this exporter.*/ + private static Logger logger = Logger.getLogger(AbstractExporter.class); + + + /** The name of the CSV facet which triggers the CSV creation.*/ + public static final String FACET_CSV = "csv"; + + /** The default charset for the CSV export.*/ + public static final String DEFAULT_CSV_CHARSET = "UTF-8"; + + /** The default separator for the CSV export.*/ + public static final char DEFAULT_CSV_SEPARATOR = ','; + + + /** The document of the incoming out() request.*/ + protected Document request; + + /** The output stream where the data should be written to.*/ + protected OutputStream out; + + /** The CallContext object.*/ + protected CallContext context; + + /** The selected facet.*/ + protected String facet; + + + /** + * Concrete subclasses need to use this method to write their special data + * objects into the CSV document. + * + * @param writer The CSVWriter. + */ + protected abstract void writeCSVData(CSVWriter writer); + + + /** + * This method enables concrete subclasses to collected its own special + * data. + * + * @param artifacts The artifact that stores the data that has to be + * exported. + */ + protected abstract void addData(Artifact artifact); + + + public void init(Document request, OutputStream out, CallContext context) { + logger.debug("AbstractExporter.init"); + + this.request = request; + this.out = out; + this.context = context; + } + + + /** + * This doOut() just collects the data of multiple artifacts. Therefore, it + * makes use of the addData() method which enables concrete subclasses to + * store its data on its own. The real output creation takes place in the + * concrete generate() methods. + * + * @param artifact The artifact. + * @param facet The facet to add - NOTE: the facet needs to fit to the first + * facet inserted into this exporter. Otherwise this artifact/facet is + * skipped. + * @param attr The attr document. + */ + public void doOut(Artifact artifact, String facet, Document attr) { + logger.debug("AbstractExporter.doOut: " + facet); + + if (!isFacetValid(facet)) { + logger.warn("Facet '" + facet + "' not valid. No output created!"); + return; + } + + addData(artifact); + } + + + /** + * Generates an export based on a specified facet. + */ + public void generate() + throws IOException + { + logger.debug("AbstractExporter.generate"); + + if (facet != null && facet.equals(FACET_CSV)) { + generateCSV(); + } + else { + throw new IOException("invalid facet for exporter."); + } + } + + + /** + * Determines if the desired facet is valid for this exporter. If no facet + * is currently set, facet is set. + * + * @param facet The desired facet. + * + * @return true, if facet is valid, otherwise false. + */ + protected boolean isFacetValid(String facet) { + logger.debug("AbstractExporter.isFacetValid"); + + if (facet == null || facet.length() == 0) { + return false; + } + else if (this.facet == null || this.facet.length() == 0) { + logger.debug("Set the facet of this export: " + facet); + this.facet = facet; + + return true; + } + else { + return this.facet.equals(facet); + } + } + + + /** + * This method starts CSV creation. It makes use of writeCSVData() which has + * to be implemented by concrete subclasses. + */ + protected void generateCSV() + throws IOException + { + logger.info("AbstractExporter.generateCSV"); + + CSVWriter writer = new CSVWriter( + new OutputStreamWriter( + out, + DEFAULT_CSV_CHARSET), + DEFAULT_CSV_SEPARATOR); + + writeCSVData(writer); + + writer.close(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r a67748ad4d61 -r 5d6988836f01 flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java Tue May 03 17:37:56 2011 +0000 @@ -0,0 +1,70 @@ +package de.intevation.flys.exports; + +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +import org.w3c.dom.Document; + +import org.apache.log4j.Logger; + +import au.com.bytecode.opencsv.CSVWriter; + +import de.intevation.artifacts.Artifact; +import de.intevation.artifacts.CallContext; + +import de.intevation.flys.artifacts.WINFOArtifact; +import de.intevation.flys.artifacts.model.WQDay; + + +/** + * @author Ingo Weinzierl + */ +public class DurationCurveExporter extends AbstractExporter { + + /** The logger used in this exporter.*/ + private static Logger logger = Logger.getLogger(WaterlevelExporter.class); + + + /** The storage that contains all WQKms objects for the different facets.*/ + protected List data; + + + public void init(Document request, OutputStream out, CallContext context) { + logger.debug("DurationCurveExporter.init"); + + super.init(request, out, context); + + this.data = new ArrayList(); + } + + + protected void addData(Artifact artifact) { + WINFOArtifact winfoArtifact = (WINFOArtifact) artifact; + data.add(winfoArtifact.getDurationCurveData()); + } + + + protected void writeCSVData(CSVWriter writer) { + logger.info("DurationCurveExporter.writeData"); + + for (WQDay wqday: data) { + wQDay2CSV(writer, wqday); + } + } + + + protected void wQDay2CSV(CSVWriter writer, WQDay wqday) { + logger.debug("DurationCurveExporter.wQDay2CSV"); + + int size = wqday.size(); + + for (int i = 0; i < size; i ++) { + writer.writeNext(new String[] { + Double.toString(wqday.getW(i)), + Double.toString(wqday.getQ(i)), + Double.toString(wqday.getDay(i)) + }); + } + } +} diff -r a67748ad4d61 -r 5d6988836f01 flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java Tue May 03 17:34:29 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java Tue May 03 17:37:56 2011 +0000 @@ -1,8 +1,6 @@ package de.intevation.flys.exports; -import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; @@ -22,31 +20,12 @@ /** * @author Ingo Weinzierl */ -public class WaterlevelExporter implements OutGenerator { +public class WaterlevelExporter extends AbstractExporter { /** The logger used in this exporter.*/ private static Logger logger = Logger.getLogger(WaterlevelExporter.class); - public static final String WATERLEVEL_FACET_CSV = "waterlevel_export.csv"; - - public static final String DEFAULT_CSV_CHARSET = "UTF-8"; - - public static final char DEFAULT_CSV_SEPARATOR = ','; - - - /** The document of the incoming out() request.*/ - protected Document request; - - /** The output stream where the data should be written to.*/ - protected OutputStream out; - - /** The CallContext object.*/ - protected CallContext context; - - /** The selected facet.*/ - protected String facet; - /** The storage that contains all WQKms objects for the different facets.*/ protected List data; @@ -54,70 +33,14 @@ public void init(Document request, OutputStream out, CallContext context) { logger.debug("WaterlevelExporter.init"); - this.request = request; - this.out = out; - this.context = context; - this.data = new ArrayList(); - } - + super.init(request, out, context); - /** - * This doOut() just collects the data of multiple artifacts. The real data - * generation takes place in the concrete generate() methods. - * - * @param artifact The artifact. - * @param facet The facet to add - NOTE: the facet needs to fit to the first - * facet inserted into this exporter. Otherwise this artifact/facet is - * skipped. - * @param attr The attr document. - */ - public void doOut(Artifact artifact, String facet, Document attr) { - logger.debug("WaterlevelExporter.doOut: " + facet); - - if (!isFacetValid(facet)) { - logger.warn("Facet '" + facet + "' not valid. No output created!"); - return; - } - - data.add(getWaterlevelData(artifact)); + this.data = new ArrayList(); } - public void generate() - throws IOException - { - if (facet != null && facet.equals(WATERLEVEL_FACET_CSV)) { - generateCSV(); - } - else { - throw new IOException("invalid facet for exporter."); - } - } - - - /** - * Determines if the desired facet is valid for this exporter. If no facet - * is currently set, facet is set. - * - * @param facet The desired facet. - * - * @return true, if facet is valid, otherwise false. - */ - protected boolean isFacetValid(String facet) { - logger.debug("WaterlevelExporter.isFacetValid"); - - if (facet == null || facet.length() == 0) { - return false; - } - else if (this.facet == null || this.facet.length() == 0) { - logger.debug("Set the facet of this export: " + facet); - this.facet = facet; - - return true; - } - else { - return this.facet.equals(facet); - } + protected void addData(Artifact artifact) { + data.add(getWaterlevelData(artifact)); } @@ -138,24 +61,14 @@ } - protected void generateCSV() - throws IOException - { - logger.info("WaterlevelExporter.generateCSV"); - - CSVWriter writer = new CSVWriter( - new OutputStreamWriter( - out, - DEFAULT_CSV_CHARSET), - DEFAULT_CSV_SEPARATOR); + protected void writeCSVData(CSVWriter writer) { + logger.info("WaterlevelExporter.writeData"); for (WQKms[] tmp: data) { for (WQKms wqkms: tmp) { wQKms2CSV(writer, wqkms); } } - - writer.close(); }