changeset 6172:58a613798386

Implement PDF export for SQ Relation
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 03 Jun 2013 17:15:09 +0200
parents 4a1af135885a
children 5d88268f184d
files artifacts/src/main/java/org/dive4elements/river/exports/sq/SQRelationExporter.java artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java artifacts/src/main/resources/messages.properties artifacts/src/main/resources/messages_de.properties artifacts/src/main/resources/messages_de_DE.properties artifacts/src/main/resources/messages_en.properties
diffstat 6 files changed, 169 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQRelationExporter.java	Mon Jun 03 16:26:10 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQRelationExporter.java	Mon Jun 03 17:15:09 2013 +0200
@@ -11,21 +11,43 @@
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Date;
+import java.util.Locale;
+import java.text.DateFormat;
 
 import org.w3c.dom.Document;
 
+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 au.com.bytecode.opencsv.CSVWriter;
 
 import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.artifacts.CallMeta;
 
 import org.dive4elements.river.artifacts.model.CalculationResult;
-
 import org.dive4elements.river.artifacts.model.sq.SQFractionResult;
 import org.dive4elements.river.artifacts.model.sq.SQResult;
+import org.dive4elements.river.artifacts.model.sq.SQRelationJRDataSource;
 import org.dive4elements.river.artifacts.model.Parameters;
+import org.dive4elements.river.artifacts.model.DateRange;
+import org.dive4elements.river.artifacts.access.SQRelationAccess;
+
+import org.dive4elements.river.artifacts.resources.Resources;
+
+import org.dive4elements.river.artifacts.D4EArtifact;
 
 import org.dive4elements.river.exports.AbstractExporter;
 
+import org.dive4elements.river.utils.RiverUtils;
+import org.dive4elements.river.utils.Formatter;
+
+import org.dive4elements.artifacts.common.utils.Config;
+
 import org.apache.log4j.Logger;
 
 /**
@@ -80,6 +102,14 @@
     public static final String CSV_VARIANCE =
         "export.sqrelation.csv.header.variance";
 
+    public static final String PDF_TITLE=
+        "export.sqrelation.pdf.title";
+
+    public static final String PDF_HEADER_MODE =
+        "export.sqrelation.pdf.mode";
+
+    public static final String JASPER_FILE =
+        "export.sqrelation.pdf.file";
 
     protected List<SQResult []> data;
 
@@ -120,16 +150,14 @@
 
         for (SQResult [] results: data) {
             for (SQResult result: results) {
-                data2CSV(writer, result);
+                writer.writeAll(data2StringArrays(result));
             }
         }
     }
 
-    protected void data2CSV(CSVWriter writer, SQResult result) {
-        logger.debug("data2CSV");
-
-        // TODO: i18n
+    protected List<String[]> data2StringArrays(SQResult result) {
         String km = String.valueOf(result.getKm());
+        List<String[]> retval = new ArrayList<String[]>();
 
         for (int i = 0; i < SQResult.NUMBER_FRACTIONS; ++i) {
             SQFractionResult fraction = result.getFraction(i);
@@ -142,29 +170,95 @@
                 continue;
             }
 
-            double a  = parameters.getValue(0, "a");
-            double b  = parameters.getValue(0, "b");
-            double sd = Math.sqrt(parameters.getValue(0, "std_dev"));
-            int    o  = fraction.totalNumOutliers();
-            int    t  = fraction.numMeasurements() + o;
+            String a, b, sd, o, t;
+            a  = Formatter.getSQRelation(context).format(parameters.getValue(0, "a"));
+            b  = Formatter.getSQRelation(context).format(parameters.getValue(0, "b"));
+            sd = Formatter.getVariance(context).format(Math.sqrt(parameters.getValue(0, "std_dev")));
 
-            writer.writeNext(new String[] {
+            o  = String.valueOf(fraction.totalNumOutliers());
+            t  = String.valueOf(fraction.numMeasurements() + o);
+
+            retval.add(new String[] {
                 km,
                 name,
-                String.valueOf(a),
-                String.valueOf(b),
-                String.valueOf(t),
-                String.valueOf(o),
-                String.valueOf(sd)
+                a,
+                b,
+                t,
+                o,
+                sd
             });
         }
+        return retval;
     }
 
 
+    protected SQRelationJRDataSource createJRData() {
+        SQRelationJRDataSource source = new SQRelationJRDataSource();
+
+        addMetaData(source);
+        for (SQResult [] results: data) {
+            for (SQResult result: results) {
+                for (String[] res: data2StringArrays(result)) {
+                    source.addData(res);
+                }
+            }
+        }
+        return source;
+    }
+
+    protected void addMetaData(SQRelationJRDataSource source) {
+        CallMeta meta = context.getMeta();
+
+        D4EArtifact arti = (D4EArtifact) master;
+
+        source.addMetaData ("river", RiverUtils.getRivername(arti));
+
+        Locale locale = Resources.getLocale(meta);
+        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+
+        source.addMetaData("date", df.format(new Date()));
+
+        SQRelationAccess access = new SQRelationAccess(arti);
+        source.addMetaData("location", getKmFormatter().format(access.getLocation()));
+
+        DateRange period = access.getPeriod();
+        source.addMetaData("periods", df.format(period.getFrom()) + " - " +
+            df.format(period.getTo()));
+
+        source.addMetaData("outliertest", access.getOutlierMethod());
+        source.addMetaData("outliers", access.getOutliers().toString());
+
+        source.addMetaData("calculation", Resources.getMsg(
+                                            locale,
+                                            PDF_HEADER_MODE,
+                                            "SQRelation"));
+    }
+
     @Override
     protected void writePDF(OutputStream out) {
-        logger.debug("writePDF");
-        logger.error("NOT IMPLEMENTED: writePDF");
+        logger.debug("write PDF");
+        SQRelationJRDataSource source = createJRData();
+
+        String jasperFile = Resources.getMsg(
+                                context.getMeta(),
+                                JASPER_FILE,
+                                "/jasper/sqrelation_en.jasper");
+        String confPath = Config.getConfigDirectory().toString();
+
+
+        Map parameters = new HashMap();
+        parameters.put("ReportTitle", Resources.getMsg(
+                    context.getMeta(), PDF_TITLE, "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);
+        }
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Mon Jun 03 16:26:10 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Mon Jun 03 17:15:09 2013 +0200
@@ -11,6 +11,7 @@
 import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.text.SimpleDateFormat;
+import java.text.DecimalFormat;
 import java.util.Locale;
 
 import org.dive4elements.artifacts.CallContext;
@@ -87,6 +88,12 @@
     public static final int FIX_DELTA_W_DELTA_Q_MIN_DIGITS  = 0;
     public static final int FIX_DELTA_W_DELTA_Q_MAX_DIGITS  = 2;
 
+    public static final int VARIANCE_MIN_DIGITS = 0;
+    public static final int VARIANCE_MAX_DIGITS = 4;
+
+    // SQ Relation
+    public static final int SQ_RELATION_MAX_DIGITS = 8;
+    public static final int SQ_RELATION_MIN_DIGITS = 0;
 
     /**
      * Creates a localized NumberFormatter with given range of decimal digits.
@@ -106,7 +113,6 @@
         return nf;
     }
 
-
     public static NumberFormat getFormatter(CallContext c, int min, int max){
         return getFormatter(c.getMeta(), min, max);
     }
@@ -124,6 +130,32 @@
         return NumberFormat.getInstance(locale);
     }
 
+    /**
+     * Returns a formatter in engineering notation
+     */
+    public static NumberFormat getEngFormatter(CallContext c) {
+        NumberFormat nf = getRawFormatter(c);
+        if (nf instanceof DecimalFormat) {
+            DecimalFormat df = (DecimalFormat)nf;
+            df.applyPattern("##0.#####E0");
+        }
+        return nf;
+    }
+
+    /**
+     * Returns a number formatter that uses an exponent after max digits
+     */
+    public static NumberFormat getScientificFormater(CallContext c, int min, int max) {
+        NumberFormat nf = getRawFormatter(c);
+        if (nf instanceof DecimalFormat) {
+            DecimalFormat df = (DecimalFormat)nf;
+            df.applyPattern("0.0E0");
+            df.setMaximumFractionDigits(max);
+            df.setMinimumFractionDigits(min);
+        }
+        return nf;
+    }
+
 
     /**
      * Returns a date formatter with SHORT style.
@@ -398,6 +430,21 @@
                 FIX_DELTA_W_DELTA_W_MAX_DIGITS);
     }
 
+    public static NumberFormat getVariance(CallContext context) {
+        return getFormatter(
+                context,
+                VARIANCE_MIN_DIGITS,
+                VARIANCE_MAX_DIGITS);
+    }
+
+    public static NumberFormat getSQRelation(CallContext context) {
+        // Would be nice if E0 would not be shown
+        return getScientificFormater(
+                context,
+                SQ_RELATION_MIN_DIGITS,
+                SQ_RELATION_MAX_DIGITS);
+    }
+
     public static NumberFormat getMeterFormat(CallContext context) {
         return getFormatter(
                 context,
--- a/artifacts/src/main/resources/messages.properties	Mon Jun 03 16:26:10 2013 +0200
+++ b/artifacts/src/main/resources/messages.properties	Mon Jun 03 17:15:09 2013 +0200
@@ -390,6 +390,8 @@
 export.sqrelation.csv.header.c.duan = C (DUAN)
 export.sqrelation.csv.header.c.ferguson = C (FERGUSON)
 export.sqrelation.csv.header.variance = Standard variance
+export.sqrelation.pdf.file = /jasper/sqrelation_en.jasper
+export.sqrelation.pdf.mode = Load Discharge Relation
 export.minfo.bedquality.km = km
 export.minfo.bedquality.density_cap = Density Toplayer
 export.minfo.bedquality.density_sub = Density Sublayer
--- a/artifacts/src/main/resources/messages_de.properties	Mon Jun 03 16:26:10 2013 +0200
+++ b/artifacts/src/main/resources/messages_de.properties	Mon Jun 03 17:15:09 2013 +0200
@@ -391,6 +391,8 @@
 export.sqrelation.csv.header.c.duan = C (DUAN)
 export.sqrelation.csv.header.c.ferguson = C (FERGUSON)
 export.sqrelation.csv.header.variance = Standardabweichung
+export.sqrelation.pdf.file = /jasper/sqrelation.jasper
+export.sqrelation.pdf.mode = Transport-Abfluss Beziehung
 export.minfo.bedquality.km = km
 export.minfo.bedquality.density_cap = Sedimentdichte Deckschicht
 export.minfo.bedquality.density_sub = Sedimentdichte Unterschicht
--- a/artifacts/src/main/resources/messages_de_DE.properties	Mon Jun 03 16:26:10 2013 +0200
+++ b/artifacts/src/main/resources/messages_de_DE.properties	Mon Jun 03 17:15:09 2013 +0200
@@ -387,6 +387,8 @@
 export.sqrelation.csv.header.c.duan = C (DUAN)
 export.sqrelation.csv.header.c.ferguson = C (FERGUSON)
 export.sqrelation.csv.header.variance = Standardabweichung
+export.sqrelation.pdf.file = /jasper/sqrelation.jasper
+export.sqrelation.pdf.mode = Transport-Abfluss Beziehung
 export.minfo.bedquality.km = km
 export.minfo.bedquality.density_cap = Sedimentdichte Deckschicht
 export.minfo.bedquality.density_sub = Sedimentdichte Unterschicht
--- a/artifacts/src/main/resources/messages_en.properties	Mon Jun 03 16:26:10 2013 +0200
+++ b/artifacts/src/main/resources/messages_en.properties	Mon Jun 03 17:15:09 2013 +0200
@@ -392,6 +392,8 @@
 export.sqrelation.csv.header.c.duan = C (DUAN)
 export.sqrelation.csv.header.c.ferguson = C (FERGUSON)
 export.sqrelation.csv.header.variance = Standard variance
+export.sqrelation.pdf.file = /jasper/sqrelation_en.jasper
+export.sqrelation.pdf.mode = Load Discharge Relation
 export.minfo.bedquality.km = km
 export.minfo.bedquality.density_cap = Density Toplayer
 export.minfo.bedquality.density_sub = Density Sublayer

http://dive4elements.wald.intevation.org