changeset 2286:89ca1e8572e4

Added PDF export for historical discharge curve calculations. flys-artifacts/trunk@3949 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 07 Feb 2012 10:29:39 +0000
parents 5a00269406f3
children dac387274390
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/jasper/historical-discharge.jasper flys-artifacts/doc/conf/jasper/historical-discharge_en.jasper flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQTJRDataSource.java flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java flys-artifacts/src/main/resources/messages.properties flys-artifacts/src/main/resources/messages_de.properties flys-artifacts/src/main/resources/messages_de_DE.properties flys-artifacts/src/main/resources/messages_en.properties
diffstat 10 files changed, 229 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/ChangeLog	Tue Feb 07 10:29:39 2012 +0000
@@ -1,3 +1,26 @@
+2012-02-07  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Added PDF export for historical discharge curve calculations.
+
+	* src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java:
+	  Implemented PDF export for calculation results.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WQTJRDataSource.java:
+	  New. Datasource for JasperReports.
+
+	* doc/conf/artifacts/winfo.xml:
+	  Added facet for PDF export to historical discharge curve output mode.
+
+	* src/main/resources/messages.properties,
+	  src/main/resources/messages_de_DE.properties,
+	  src/main/resources/messages_en.properties,
+	  src/main/resources/messages_de.properties:
+	  Added i18n strings for historical discharge curve export.
+
+	* doc/conf/jasper/historical-discharge.jasper,
+	  doc/conf/jasper/historical-discharge_en.jasper:
+	  New. Templates for JasperReports PDF creation.
+
 2012-02-07  Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/ReferenceCurveExporter.java:
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Tue Feb 07 10:29:39 2012 +0000
@@ -511,6 +511,7 @@
                 <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" />
+                        <facet name="pdf" description="facet.historical_discharge.pdf" />
                     </facets>
                 </outputmode>
                 <outputmode name="historical_discharge_report" description="output.historical_discharge_report" mime-type="text/xml" type="report">
Binary file flys-artifacts/doc/conf/jasper/historical-discharge.jasper has changed
Binary file flys-artifacts/doc/conf/jasper/historical-discharge_en.jasper has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQTJRDataSource.java	Tue Feb 07 10:29:39 2012 +0000
@@ -0,0 +1,99 @@
+package de.intevation.flys.artifacts.model;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.apache.log4j.Logger;
+
+import net.sf.jasperreports.engine.JRDataSource;
+import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JRField;
+
+
+/**
+ * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
+ */
+public class WQTJRDataSource implements JRDataSource
+{
+    /** The logger used in this exporter.*/
+    private static Logger logger = Logger.getLogger(WQTJRDataSource.class);
+
+    /**
+     *
+     */
+    private ArrayList<String[]> data;
+    private HashMap<String, String> metaData;
+
+    private int index = -1;
+
+    /**
+     *
+     */
+    public WQTJRDataSource()
+    {
+        data = new ArrayList<String[]>();
+        metaData = new HashMap<String, String>();
+    }
+
+
+    /**
+     *
+     */
+    public void addData(String[] data) {
+        this.data.add(data);
+    }
+
+
+    /**
+     *
+     */
+    public void addMetaData(String key, String value) {
+        this.metaData.put(key, value);
+    }
+
+
+    /**
+     *
+     */
+    public boolean next() throws JRException
+    {
+        index++;
+
+        return (index < data.size());
+    }
+
+
+    /**
+     *
+     */
+    public Object getFieldValue(JRField field) throws JRException
+    {
+        Object value = "";
+        String fieldName = field.getName();
+        if ("river".equals(fieldName)) {
+            value = metaData.get("river");
+        }
+        if ("date".equals(fieldName)) {
+            value = metaData.get("date");
+        }
+        if ("calculation".equals(fieldName)) {
+            value = metaData.get("calculation");
+        }
+        if ("range".equals(fieldName)) {
+            value = data.get(index)[0];
+        }
+        if ("W".equals(fieldName)) {
+            value = data.get(index)[1];
+        }
+        if ("Q".equals(fieldName)) {
+            value = data.get(index)[2];
+        }
+        if ("delta".equals(fieldName)) {
+            value = data.get(index)[3];
+        }
+        if ("gaugename".equals(fieldName)) {
+            value = data.get(index)[4];
+        }
+        return value;
+    }
+}
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java	Tue Feb 07 10:29:39 2012 +0000
@@ -6,6 +6,9 @@
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Locale;
 
 import org.w3c.dom.Document;
 
@@ -13,6 +16,13 @@
 
 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.common.utils.Config;
+
 import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallMeta;
 
@@ -20,6 +30,7 @@
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.Timerange;
 import de.intevation.flys.artifacts.model.WQTimerange;
+import de.intevation.flys.artifacts.model.WQTJRDataSource;
 import de.intevation.flys.artifacts.resources.Resources;
 import de.intevation.flys.utils.FLYSUtils;
 import de.intevation.flys.utils.Formatter;
@@ -49,6 +60,11 @@
     public static final String CSV_GAUGENAME_HEADER =
         "export.historical.discharge.csv.header.gaugename";
 
+    public static final String PDF_HEADER_MODE =
+        "export.historical.discharge.pdf.mode";
+
+    public static final String JASPER_FILE =
+        "export.historical.discharge.pdf.file";
 
     protected List<WQTimerange[]> data;
 
@@ -94,7 +110,28 @@
 
     @Override
     protected void writePDF(OutputStream out) {
-        logger.warn("TODO: IMPLEMENT ME!");
+        WQTJRDataSource source = createJRData();
+
+        String jasperFile = Resources.getMsg(
+                                context.getMeta(),
+                                JASPER_FILE,
+                                "/jasper/historical-discharge_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();
+        }
+
     }
 
 
@@ -138,6 +175,66 @@
     }
 
 
+    protected WQTJRDataSource createJRData() {
+        WQTJRDataSource source = new WQTJRDataSource();
+
+        addMetaData(source);
+        for (WQTimerange[] arr: data) {
+            for (WQTimerange wqt: arr) {
+                addWQTData(source, wqt);
+            }
+        }
+
+        return source;
+    }
+
+
+    protected void addMetaData(WQTJRDataSource source) {
+        CallMeta meta = context.getMeta();
+
+        FLYSArtifact flys = (FLYSArtifact) 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()));
+
+        source.addMetaData("calculation", Resources.getMsg(
+                                            locale,
+                                            PDF_HEADER_MODE,
+                                            "Historical Discharge"));
+    }
+
+
+    protected void addWQTData(WQTJRDataSource source, WQTimerange wqt) {
+        DateFormat   df = Formatter.getShortDateFormat(context);
+        NumberFormat wf = Formatter.getHistoricalDischargeW(context);
+        NumberFormat qf = Formatter.getHistoricalDischargeQ(context);
+
+        double[] wq = new double[2];
+
+        String gaugeName = getReferenceGaugename();
+
+        for (int i = 0, n = wqt.size(); i < n; i++) {
+            Timerange tr = wqt.getTimerange(i);
+            Date   start = new Date(tr.getStart());
+            Date     end = new Date(tr.getEnd());
+
+            wqt.get(i, wq);
+
+            source.addData(new String[] {
+                df.format(start) + " - " + df.format(end),
+                wf.format(wq[0]),
+                qf.format(wq[1]),
+                "TODO: Difference to reference curve",
+                gaugeName
+            });
+        }
+    }
+
+
     public String getReferenceGaugename() {
         return FLYSUtils.getReferenceGaugeName((FLYSArtifact) master);
     }
--- a/flys-artifacts/src/main/resources/messages.properties	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/src/main/resources/messages.properties	Tue Feb 07 10:29:39 2012 +0000
@@ -134,10 +134,12 @@
 export.computed.discharge.pdf.mode = Computed Dischargecurve
 export.duration.pdf.mode = Durationcurve
 export.wdifferences.pdf.mode = W Differences
+export.historical.discharge.pdf.mode = Historical Discharge
 export.computed.discharge.pdf.file = /jasper/computed-discharge_en.jasper
 export.duration.pdf.file = /jasper/duration_en.jasper
 export.waterlevel.pdf.file = /jasper/waterlevel_en.jasper
 export.wdifferences.pdf.file = /jasper/wdifferences_en.jasper
+export.historical.discharge.pdf.file = /jasper/historical-discharge_en.jasper
 
 floodmap.wmsbackground = Background Map
 floodmap.riveraxis = River Axis
--- a/flys-artifacts/src/main/resources/messages_de.properties	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/src/main/resources/messages_de.properties	Tue Feb 07 10:29:39 2012 +0000
@@ -134,10 +134,12 @@
 export.computed.discharge.pdf.mode = Abflusskurve
 export.duration.pdf.mode = Dauerlinie
 export.wdifferences.pdf.mode = W Differenzen
+export.historical.discharge.pdf.mode = Historischer Abfluss
 export.computed.discharge.pdf.file = /jasper/computed-discharge.jasper
 export.duration.pdf.file = /jasper/duration.jasper
 export.waterlevel.pdf.file = /jasper/waterlevel.jasper
 export.wdifferences.pdf.file = /jasper/wdifferences.jasper
+export.historical.discharge.pdf.file = /jasper/historical-discharge.jasper
 
 floodmap.wmsbackground = Hintergrundkarte
 floodmap.riveraxis = Flussachse
--- a/flys-artifacts/src/main/resources/messages_de_DE.properties	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/src/main/resources/messages_de_DE.properties	Tue Feb 07 10:29:39 2012 +0000
@@ -134,10 +134,12 @@
 export.computed.discharge.pdf.mode = Abflusskurve
 export.duration.pdf.mode = Dauerline
 export.wdifferences.pdf.mode = W Differenzen
+export.historical.discharge.pdf.mode = Historischer Abfluss
 export.computed.discharge.pdf.file = /jasper/computed-discharge.jasper
 export.duration.pdf.file = /jasper/duration.jasper
 export.waterlevel.pdf.file = /jasper/waterlevel.jasper
 export.wdifferences.pdf.file = /jasper/wdifferences.jasper
+export.historical.discharge.pdf.file = /jasper/historical-discharge.jasper
 
 floodmap.wmsbackground = Hintergrundkarte
 floodmap.riveraxis = Flussachse
--- a/flys-artifacts/src/main/resources/messages_en.properties	Tue Feb 07 09:58:15 2012 +0000
+++ b/flys-artifacts/src/main/resources/messages_en.properties	Tue Feb 07 10:29:39 2012 +0000
@@ -130,10 +130,12 @@
 export.computed.discharge.pdf.mode = Computed Dischargecurve
 export.duration.pdf.mode = Durationcurve
 export.wdifferences.pdf.mode = W Differences
+export.historical.discharge.pdf.mode = Historical Discharge
 export.computed.discharge.pdf.file = /jasper/computed-discharge_en.jasper
 export.duration.pdf.file = /jasper/duration_en.jasper
 export.waterlevel.pdf.file = /jasper/waterlevel_en.jasper
 export.wdifferences.pdf.file = /jasper/wdifferences_en.jasper
+export.historical.discharge.pdf.file = /jasper/historical-discharge_en.jasper
 
 floodmap.wmsbackground = Background Map
 floodmap.riveraxis = River Axis

http://dive4elements.wald.intevation.org