changeset 2178:1d7fb2cabfc7

Issue 138. PDF output for durationcurve calculation results. flys-artifacts/trunk@3777 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 27 Jan 2012 07:53:03 +0000
parents 28dbeb64ce7c
children c3e523a76340
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/jasper/duration.jasper flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java
diffstat 6 files changed, 118 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jan 26 16:53:42 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jan 27 07:53:03 2012 +0000
@@ -1,3 +1,20 @@
+2012-01-27  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Issue 138: PDF output for calculations.
+
+	* src/main/java/de/intevation/flys/exports/DurationCurveExporter.java:
+	  Create the PDF data source and generate the PDF report output.
+
+	* src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java,
+	  doc/conf/artifacts/winfo.xml:
+	  Added PDF facet to state.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java:
+	  Added new field type 'day'.
+
+	* doc/conf/jasper/duration.jasper:
+	  New. Template for duration curve PDF report.
+
 2012-01-26  Raimund Renkert <raimund.renkert@intevation.de>
 
 	Issue 138: PDF output for calculations.
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Thu Jan 26 16:53:42 2012 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Jan 27 07:53:03 2012 +0000
@@ -206,6 +206,7 @@
                 <outputmode name="durationcurve_export" description="output.durationcurve_export" mime-type="text/plain" type="export">
                     <facets>
                         <facet name="csv" description="facet.durationcurve_export.csv" />
+                        <facet name="pdf" description="facet.durationcurve_export.pdf" />
                     </facets>
                 </outputmode>
                 <outputmode name="durationcurve_report" description="output.durationcurve_report" mime-type="text/xml" type="report">
Binary file flys-artifacts/doc/conf/jasper/duration.jasper has changed
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java	Thu Jan 26 16:53:42 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java	Fri Jan 27 07:53:03 2012 +0000
@@ -103,6 +103,9 @@
         if ("gaugename".equals(fieldName)) {
             value = data.get(index)[5];
         }
+        if ("day".equals(fieldName)) {
+            value = data.get(index)[6];
+        }
         return value;
     }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java	Thu Jan 26 16:53:42 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java	Fri Jan 27 07:53:03 2012 +0000
@@ -89,6 +89,7 @@
                 facets.add(q);
 
                 facets.add(new DataFacet(CSV, "CSV data"));
+                facets.add(new DataFacet(PDF, "PDF data"));
 
                 if (res.getReport().hasProblems()) {
                     facets.add(new ReportFacet());
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java	Thu Jan 26 16:53:42 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java	Fri Jan 27 07:53:03 2012 +0000
@@ -4,6 +4,11 @@
 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;
 
@@ -11,11 +16,22 @@
 
 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.flys.artifacts.FLYSArtifact;
 
 import de.intevation.flys.artifacts.model.WQDay;
 import de.intevation.flys.artifacts.model.CalculationResult;
+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;
 
 
@@ -41,6 +57,7 @@
     public static final String DEFAULT_CSV_W_HEADER  = "W [NN + m]";
     public static final String DEFAULT_CSV_Q_HEADER  = "Q [m\u00b3/s]";
 
+    public static final String PDF_HEADER_MODE = "export.duration.pdf.mode";
 
     /** The storage that contains all WQKms objects for the different facets. */
     protected List<WQDay> data;
@@ -149,6 +166,84 @@
 
 
     @Override
-    protected void writePDF(OutputStream out) {}
+    protected void writePDF(OutputStream out) {
+        WKmsJRDataSource source = createJRData();
+        Map parameters = new HashMap();
+        parameters.put("ReportTitle", "Exported Data");
+        try {
+            JasperPrint print = JasperFillManager.fillReport(
+                "conf/jasper/duration.jasper",
+                parameters,
+                source);
+            JasperExportManager.exportReportToPdfStream(print, out);
+        }
+        catch(JRException je) {
+            logger.warn("Error generating PDF Report!");
+            je.printStackTrace();
+        }
+    }
 
+    protected WKmsJRDataSource createJRData() {
+        WKmsJRDataSource source = new WKmsJRDataSource();
+
+        addMetaData(source);
+        for (WQDay wqday: data) {
+            addWQDayData(source, wqday);
+        }
+
+        return source;
+    }
+
+
+    protected void addMetaData(WKmsJRDataSource 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()));
+
+        double[] kms = FLYSUtils.getKmRange(flys);
+        source.addMetaData("range", String.valueOf(kms[0]));
+
+        source.addMetaData("calculation", Resources.getMsg(
+                                            locale,
+                                            PDF_HEADER_MODE,
+                                            "Duration"));
+    }
+
+    protected void addWQDayData(WKmsJRDataSource source, WQDay wqday) {
+        int size = wqday.size();
+
+        NumberFormat wf  = getWFormatter();
+        NumberFormat qf  = getQFormatter();
+        NumberFormat df  = getDFormatter();
+
+        if (wqday.isIncreasing()) {
+            for (int i = size-1; i >= 0; i --) {
+                source.addData(new String[] {
+                    "",
+                    wf.format(wqday.getW(i)),
+                    qf.format(wqday.getQ(i)),
+                    "", "", "",
+                    df.format(wqday.getDay(i))
+                });
+            }
+        }
+        else {
+            for (int i = 0; i < size; i ++) {
+                source.addData(new String[] {
+                    "",
+                    wf.format(wqday.getW(i)),
+                    qf.format(wqday.getQ(i)),
+                    "", "", "",
+                    df.format(wqday.getDay(i))
+                });
+            }
+        }
+    }
 }

http://dive4elements.wald.intevation.org