changeset 2180:65b32220f197

Issue 138. PDF output for differences calculations. flys-artifacts/trunk@3784 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 27 Jan 2012 09:49:11 +0000
parents c3e523a76340
children 38207b820dca
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/jasper/wdifferences.jasper flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesExporter.java
diffstat 6 files changed, 117 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jan 27 09:42:15 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jan 27 09:49:11 2012 +0000
@@ -1,3 +1,22 @@
+2012-01-27  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Issue 138: PDF output for calculations.
+
+	* src/main/java/de/intevation/flys/exports/WDifferencesExporter.java:
+	  Create the PDF data source and generate the PDF report output.
+
+	* src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java:
+	  Add the PDF facet to state. Add export facets only once.
+
+	* doc/conf/artifacts/winfo.xml:
+	  Added PDF facet to state.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java:
+	  Added new field type 'differences'.
+
+	* doc/conf/jasper/wdifferences.jasper:
+	  New. Template for w-differences PDF export.
+
 2012-01-27  Raimund Renkert <raimund.renkert@intevation.de>
 
 	* src/main/java/de/intevation/flys/utils/FLYSUtils.java:
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Jan 27 09:42:15 2012 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Jan 27 09:49:11 2012 +0000
@@ -188,6 +188,7 @@
                 <outputmode name="w_differences_export" description="output.w_differences.export" mime-type="text/plain" type="export">
                     <facets>
                         <facet name="csv" description="facet.w_differences.csv" />
+                        <facet name="pdf" description="facet.w_differences.pdf" />
                     </facets>
                 </outputmode>
             </outputmodes>
Binary file flys-artifacts/doc/conf/jasper/wdifferences.jasper has changed
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java	Fri Jan 27 09:42:15 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsJRDataSource.java	Fri Jan 27 09:49:11 2012 +0000
@@ -85,6 +85,9 @@
         if ("calculation".equals(fieldName)) {
             value = metaData.get("calculation");
         }
+        if ("differences".equals(fieldName)) {
+            value = metaData.get("differences");
+        }
         if ("km".equals(fieldName)) {
             value = data.get(index)[0];
         }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java	Fri Jan 27 09:42:15 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java	Fri Jan 27 09:49:11 2012 +0000
@@ -164,16 +164,19 @@
                 wkmss.add(wkms);
                 logger.debug("WKMSSubtraction happened");
             }
-    
             if (facets != null) {
                 facets.add(new DifferenceCurveFacet(i/2, W_DIFFERENCES, facetName,
                     ComputeType.ADVANCE, id, hash));
-                facets.add(new DataFacet(CSV, "CSV data"));
-                logger.debug("Adding facets in WDifferencesState.");
             }
-            else {
-                logger.debug("Not adding facets in WDifferencesState.");
-            }
+        }
+
+        if (facets != null) {
+            facets.add(new DataFacet(CSV, "CSV data"));
+            facets.add(new DataFacet(PDF, "PDF data"));
+            logger.debug("Adding facets in WDifferencesState.");
+        }
+        else {
+            logger.debug("Not adding facets in WDifferencesState.");
         }
 
         // TODO Evaluate whether null is okay as reports.
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesExporter.java	Fri Jan 27 09:42:15 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesExporter.java	Fri Jan 27 09:49:11 2012 +0000
@@ -5,6 +5,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;
 
@@ -12,11 +17,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.WINFOArtifact;
 
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.WKms;
+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;
 
 /**
@@ -37,6 +53,7 @@
     public static final String WDIFF_DEFAULT_CSV_KM_HEADER = "Fluss-Km";
     public static final String WDIFF_DEFAULT_CSV_W_HEADER  = "m";
 
+    public static final String PDF_HEADER_MODE = "export.wdifferences.pdf.mode";
 
     /** The storage that contains all WKms objects for the different facets. */
     protected List<WKms[]> data;
@@ -66,6 +83,9 @@
         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");
         }
@@ -160,6 +180,70 @@
 
 
     @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/wdifferences.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 (WKms[] tmp: data) {
+            for (WKms wkms: tmp) {
+                addWKmsData(source, wkms);
+            }
+        }
+        return source;
+    }
+
+
+    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