changeset 2264:d974e4589f5a

Primitive, incomplete CSV export for reference curves. flys-artifacts/trunk@3920 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 06 Feb 2012 13:07:19 +0000
parents bdd032bfd978
children 1dad713412a7
files flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/exports/ReferenceCurveExporter.java
diffstat 3 files changed, 283 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Mon Feb 06 13:04:52 2012 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Mon Feb 06 13:07:19 2012 +0000
@@ -198,6 +198,11 @@
                         <facet name="reference_curve" description="A ... reference curve!"/>
                     </facets>
                 </outputmode>
+                <outputmode name="reference_curve_export" mime-type="text/plain" type="export">
+                    <facets>
+                        <facet name="csv" description="facet.reference_curve_export.csv" />
+                    </facets>
+                </outputmode>
             </outputmodes>
         </state>
 
@@ -493,7 +498,7 @@
                         <facet name="historical_discharge.historicalq"/>
                     </facets>
                 </outputmode>
-                <outputmode name="historical_discharge_export" description="output.w_differences.export" mime-type="text/plain" type="export">
+                <outputmode name="historical_discharge_export" description="output.historical_discharge.export" mime-type="text/plain" type="export">
                     <facets>
                         <facet name="csv" description="facet.historical_discharge.csv" />
                     </facets>
--- a/flys-artifacts/doc/conf/conf.xml	Mon Feb 06 13:04:52 2012 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Mon Feb 06 13:07:19 2012 +0000
@@ -179,6 +179,7 @@
         <output-generator name="map">de.intevation.flys.exports.MapGenerator</output-generator>
         <output-generator name="reference_curve">de.intevation.flys.exports.ReferenceCurveGenerator</output-generator>
         <output-generator name="reference_curve_chartinfo">de.intevation.flys.exports.ReferenceCurveInfoGenerator</output-generator>
+        <output-generator name="reference_curve_export">de.intevation.flys.exports.ReferenceCurveExporter</output-generator>
         <output-generator name="historical_discharge">de.intevation.flys.exports.HistoricalDischargeCurveGenerator</output-generator>
         <output-generator name="historical_discharge_info">de.intevation.flys.exports.HistoricalDischargeCurveInfoGenerator</output-generator>
         <output-generator name="historical_discharge_export">de.intevation.flys.exports.HistoricalDischargeCurveExporter</output-generator>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ReferenceCurveExporter.java	Mon Feb 06 13:07:19 2012 +0000
@@ -0,0 +1,276 @@
+package de.intevation.flys.exports;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Date;
+import java.text.DateFormat;
+import java.util.Locale;
+
+import org.w3c.dom.Document;
+
+import org.apache.log4j.Logger;
+
+import au.com.bytecode.opencsv.CSVWriter;
+
+import net.sf.jasperreports.engine.JasperExportManager;
+import net.sf.jasperreports.engine.JasperFillManager;
+import net.sf.jasperreports.engine.JasperPrint;
+import net.sf.jasperreports.engine.JRException;
+
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.common.utils.Config;
+
+import de.intevation.flys.artifacts.WINFOArtifact;
+
+import de.intevation.flys.artifacts.model.CalculationResult;
+import de.intevation.flys.artifacts.model.WW;
+import de.intevation.flys.artifacts.model.WKmsJRDataSource;
+import de.intevation.flys.artifacts.resources.Resources;
+
+import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.Formatter;
+
+/**
+ * (CSV)Exporter for Reference Curves.
+ */
+public class ReferenceCurveExporter extends AbstractExporter {
+
+    /** The logger used in this exporter. */
+    private static Logger logger = Logger.getLogger(ReferenceCurveExporter.class);
+
+
+    public static final String RC_CSV_KM_HEADER =
+        "export.reference_curve.csv.header.km";
+
+    public static final String RC_CSV_W_HEADER =
+        "export.reference_curve.csv.header.w";
+
+    /*
+        For each Bezugs and Ziel-ort (of which there might be multiples):
+          Lage, Bezeichnung, Q, W (cm), W(NN+m).
+     */
+
+    public static final String RC_DEFAULT_CSV_KM_HEADER = "Fluss-Km";
+    public static final String RC_DEFAULT_CSV_W_HEADER  = "m";
+
+    public static final String PDF_HEADER_MODE = "export.reference_curve.pdf.mode";
+    public static final String JASPER_FILE = "export.reference_curve.pdf.file";
+
+    /** The storage that contains all WKms objects for the different facets. */
+    /* TODO these shall be the function objects, probably. */
+    protected List<WW[]> data;
+
+
+    public void init(Document request, OutputStream out, CallContext context) {
+        logger.debug("ReferenceCurveExporter.init");
+
+        super.init(request, out, context);
+
+        this.data = new ArrayList<WW[]>();
+    }
+
+
+    /**
+     * Genereate data in csv format.
+     */
+    @Override
+    public void generate()
+    throws IOException
+    {
+        logger.debug("ReferenceCurveExporter.generate");
+
+        if (facet == null) {
+            throw new IOException("invalid (null) facet for exporter");
+        }
+        else if (facet.equals(AbstractExporter.FACET_CSV)) {
+            generateCSV();
+        }
+        else if (facet.equals(AbstractExporter.FACET_PDF)) {
+            generatePDF();
+        }
+        else {
+            throw new IOException("invalid facet (" + facet + ") for exporter");
+        }
+    }
+
+
+    /**
+     * Adds given data.
+     * @param d either a WKms or a CalculationResult to add to data.
+     */
+    @Override
+    protected void addData(Object d) {
+        if (d instanceof CalculationResult) {
+            d = ((CalculationResult)d).getData();
+            if (d instanceof WW []) {
+                data.add((WW [])d);
+            }
+        }
+        else if (d instanceof WW) {
+            data.add(new WW[] { (WW) d });
+        }
+        else {
+            logger.warn("ReferenceCurveExporter.addData unknown data type.");
+        }
+    }
+
+
+    /**
+     * Lets writer write all data (including header).
+     * @param writer Writer to write data with.
+     */
+    @Override
+    protected void writeCSVData(CSVWriter writer) {
+        logger.info("ReferenceCurveExporter.writeData");
+
+        writeCSVHeader(writer);
+
+        for (WW[] tmp: data) {
+            for (WW ww: tmp) {
+                ww2CSV(writer, ww);
+            }
+        }
+    }
+
+
+    /**
+     * Lets csvwriter write the header (first line in file).
+     * @param write Writer to write header with.
+     */
+    protected void writeCSVHeader(CSVWriter writer) {
+        logger.info("ReferenceCurveExporter.writeCSVHeader");
+
+        writer.writeNext(new String[] {
+            msg(RC_CSV_KM_HEADER, RC_DEFAULT_CSV_KM_HEADER),
+            msg(RC_CSV_W_HEADER, RC_DEFAULT_CSV_W_HEADER)
+        });
+    }
+
+
+    protected void ww2CSV(CSVWriter writer, WW ww) {
+        logger.debug("ReferenceCurveExporter.ww2CSV");
+
+        NumberFormat kmf  = getKmFormatter();
+        NumberFormat wf   = getWFormatter();
+        int          size = ww.size();
+
+        for (int i = 0; i < size; i ++) {
+
+            // TODO: start/end ... start/end ..
+            writer.writeNext(new String[] {
+                kmf.format(ww.getStartKm()),
+                wf.format(ww.getW(i))
+            });
+        }
+    }
+
+
+    /**
+     * Returns the number formatter for kilometer values.
+     *
+     * @return the number formatter for kilometer values.
+     */
+    protected NumberFormat getKmFormatter() {
+        return Formatter.getWaterlevelKM(context);
+    }
+
+
+    /**
+     * Returns the number formatter for W values.
+     *
+     * @return the number formatter for W values.
+     */
+    protected NumberFormat getWFormatter() {
+        return Formatter.getWaterlevelW(context);
+    }
+
+
+    @Override
+    protected void writePDF(OutputStream out) {
+        /*
+        WKmsJRDataSource source = createJRData();
+
+        String jasperFile = Resources.getMsg(
+                                context.getMeta(),
+                                JASPER_FILE,
+                                "/jasper/wdifferences_en.jasper");
+        String confPath = Config.getConfigDirectory().toString();
+
+        Map parameters = new HashMap();
+        parameters.put("ReportTitle", "Exported Data");
+        try {
+            JasperPrint print = JasperFillManager.fillReport(
+                confPath + jasperFile,
+                parameters,
+                source);
+            JasperExportManager.exportReportToPdfStream(print, out);
+        }
+        catch(JRException je) {
+            logger.warn("Error generating PDF Report!");
+            je.printStackTrace();
+        }
+        */
+        logger.error("ReferenceCurveExporter: PDF export not implemented.");
+    }
+
+    protected WKmsJRDataSource createJRData() {
+        /*
+        WKmsJRDataSource source = new WKmsJRDataSource();
+
+        addMetaData(source);
+        for (WKms[] tmp: data) {
+            for (WKms wkms: tmp) {
+                addWKmsData(source, wkms);
+            }
+        }
+        return source;
+        */
+        return null;
+    }
+
+
+    // TODO implement for pdf export.
+    /*
+    protected void addMetaData(WKmsJRDataSource source) {
+        CallMeta meta = context.getMeta();
+
+        WINFOArtifact flys = (WINFOArtifact) master;
+
+        source.addMetaData ("river", FLYSUtils.getRivername(flys));
+
+        Locale locale = Resources.getLocale(meta);
+        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+
+        source.addMetaData("date", df.format(new Date()));
+
+        String differences = FLYSUtils.getWDifferences(flys, context);
+        source.addMetaData("differences", differences);
+
+        source.addMetaData("calculation", Resources.getMsg(
+                                            locale,
+                                            PDF_HEADER_MODE,
+                                            "W Differences"));
+    }
+
+    protected void addWKmsData(WKmsJRDataSource source, WKms wkms) {
+        NumberFormat kmf  = getKmFormatter();
+        NumberFormat wf   = getWFormatter();
+        int          size = wkms.size();
+
+        for (int i = 0; i < size; i ++) {
+
+            source.addData(new String[] {
+                kmf.format(wkms.getKm(i)),
+                wf.format(wkms.getW(i))
+            });
+        }
+    }
+    */
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org