Mercurial > dive4elements > river
changeset 2177:28dbeb64ce7c
Issue 138.
PDF output for computed discharge calculation results.
flys-artifacts/trunk@3776 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Thu, 26 Jan 2012 16:53:42 +0000 |
parents | 65dac9cf6ff5 |
children | 1d7fb2cabfc7 |
files | flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/jasper/computed-discharge.jasper flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java |
diffstat | 5 files changed, 105 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Thu Jan 26 15:38:46 2012 +0000 +++ b/flys-artifacts/ChangeLog Thu Jan 26 16:53:42 2012 +0000 @@ -1,3 +1,17 @@ +2012-01-26 Raimund Renkert <raimund.renkert@intevation.de> + + Issue 138: PDF output for calculations. + + * src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java: + Create the PDF datasource and generate the PDF report output. + + * src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java, + doc/conf/artifacts/winfo.xml: + Added PDF facet to state. + + * doc/conf/jasper/computed-discharge.jasper: + New. Template for computed discharge 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 15:38:46 2012 +0000 +++ b/flys-artifacts/doc/conf/artifacts/winfo.xml Thu Jan 26 16:53:42 2012 +0000 @@ -232,6 +232,7 @@ <outputmode name="computed_dischargecurve_export" description="output.computed_dischargecurve_export" mime-type="text/plain" type="export"> <facets> <facet name="csv" description="facet.computed_dischargecurve_export.csv" /> + <facet name="pdf" description="facet.computed_dischargecurve_export.pdf" /> </facets> </outputmode> <outputmode name="computed_dischargecurve_report" description="output.computed_dischargecurve_report" mime-type="text/xml" type="report">
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java Thu Jan 26 15:38:46 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java Thu Jan 26 16:53:42 2012 +0000 @@ -79,6 +79,7 @@ } 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/ComputedDischargeCurveExporter.java Thu Jan 26 15:38:46 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java Thu Jan 26 16:53:42 2012 +0000 @@ -5,6 +5,11 @@ import java.util.ArrayList; import java.util.Arrays; 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,23 @@ import au.com.bytecode.opencsv.CSVWriter; +import net.sf.jasperreports.engine.JRDataSource; +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.CalculationResult; import de.intevation.flys.artifacts.model.WQKms; +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; @@ -39,6 +56,8 @@ 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.computed.discharge.pdf.mode"; + protected List<WQKms> data; @@ -119,6 +138,75 @@ @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/computed-discharge.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); + addWQData(source); + + 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, + "Computed Discharge")); + } + + protected void addWQData(WKmsJRDataSource source) { + NumberFormat wf = getWFormatter(); + NumberFormat qf = getQFormatter(); + + double[] res = new double[3]; + + for (WQKms wqkms: data) { + int size = wqkms.size(); + + for (int i = 0; i < size; i++) { + res = wqkms.get(i, res); + + source.addData(new String[] { + "", // Empty, the WKmsJRDtasource stores km here. + wf.format(res[0]), + qf.format(res[1]) + }); + } + } + + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :