# HG changeset patch # User Ingo Weinzierl # Date 1304440581 0 # Node ID 69d05357c177d3c2c372abbf26b7fb900d8d9e45 # Parent cc6840cbe503d68273f7ca09be5e66426e39621e Added an exporter (OutGenerator) for waterlevels which currently supports CSV exports. flys-artifacts/trunk@1810 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r cc6840cbe503 -r 69d05357c177 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Tue May 03 16:26:35 2011 +0000 +++ b/flys-artifacts/ChangeLog Tue May 03 16:36:21 2011 +0000 @@ -1,3 +1,17 @@ +2011-05-03 Ingo Weinzierl + + * doc/conf/artifacts/winfo.xml: Added a new output mode for the waterlevel + state (CSV export). + + * doc/conf/conf.xml: Added a new OutGenerator to export waterlevels. + + * src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: New. + This OutGenerator exports the data of a waterlevel computation. Note: + It is necessary to specify the desired facet (e.g. + 'waterlevel_export.csv'). + + * pom.xml: Added a dependency to OpenCSV. + 2011-05-03 Ingo Weinzierl * src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java: diff -r cc6840cbe503 -r 69d05357c177 flys-artifacts/doc/conf/artifacts/winfo.xml --- a/flys-artifacts/doc/conf/artifacts/winfo.xml Tue May 03 16:26:35 2011 +0000 +++ b/flys-artifacts/doc/conf/artifacts/winfo.xml Tue May 03 16:36:21 2011 +0000 @@ -101,6 +101,11 @@ + + + + + diff -r cc6840cbe503 -r 69d05357c177 flys-artifacts/doc/conf/conf.xml --- a/flys-artifacts/doc/conf/conf.xml Tue May 03 16:26:35 2011 +0000 +++ b/flys-artifacts/doc/conf/conf.xml Tue May 03 16:36:21 2011 +0000 @@ -52,6 +52,7 @@ de.intevation.flys.exports.DischargeCurveGenerator de.intevation.flys.exports.LongitudinalSectionGenerator de.intevation.flys.exports.DurationCurveGenerator + de.intevation.flys.exports.WaterlevelExporter diff -r cc6840cbe503 -r 69d05357c177 flys-artifacts/pom.xml --- a/flys-artifacts/pom.xml Tue May 03 16:26:35 2011 +0000 +++ b/flys-artifacts/pom.xml Tue May 03 16:36:21 2011 +0000 @@ -51,6 +51,11 @@ 2.1.1 + net.sf.opencsv + opencsv + 2.0 + + de.intevation.bsh.artifact-database artifact-database 1.0-SNAPSHOT diff -r cc6840cbe503 -r 69d05357c177 flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java Tue May 03 16:36:21 2011 +0000 @@ -0,0 +1,179 @@ +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; + +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.WQKms; + + +/** + * @author Ingo Weinzierl + */ +public class WaterlevelExporter implements OutGenerator { + + /** 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; + + + 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(); + } + + + /** + * 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)); + } + + + 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); + } + } + + + /** + * Returns the waterlevel data computed by the WINFOArtifact. + * + * @param artifact The WINFOArtifact. + * + * @return the computed waterlevel data. + */ + protected WQKms[] getWaterlevelData(Artifact artifact) { + WINFOArtifact winfoArtifact = (WINFOArtifact) artifact; + WQKms[] wqkms = winfoArtifact.getWaterlevelData(); + + logger.debug("Got " + wqkms.length + " WQKms objects."); + + return wqkms; + } + + + protected void generateCSV() + throws IOException + { + logger.info("WaterlevelExporter.generateCSV"); + + CSVWriter writer = new CSVWriter( + new OutputStreamWriter( + out, + DEFAULT_CSV_CHARSET), + DEFAULT_CSV_SEPARATOR); + + for (WQKms[] tmp: data) { + for (WQKms wqkms: tmp) { + wQKms2CSV(writer, wqkms); + } + } + + writer.close(); + } + + + protected void wQKms2CSV(CSVWriter writer, WQKms wqkms) { + logger.debug("WaterlevelExporter.wQKms2CSV"); + + int size = wqkms.size(); + double[] result = new double[3]; + + for (int i = 0; i < size; i ++) { + result = wqkms.get(i, result); + + writer.writeNext(new String[] { + Double.toString(result[2]), + Double.toString(result[0]), + Double.toString(result[1]) + }); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :