# HG changeset patch # User Raimund Renkert # Date 1327596822 0 # Node ID 28dbeb64ce7c7a7648fa65f22a35fdcab2e2b538 # Parent 65dac9cf6ff508e4722de3e2c264c6a0deebe82e Issue 138. PDF output for computed discharge calculation results. flys-artifacts/trunk@3776 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 65dac9cf6ff5 -r 28dbeb64ce7c flys-artifacts/ChangeLog --- 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 + + 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 Issue 138: PDF output for calculations. diff -r 65dac9cf6ff5 -r 28dbeb64ce7c flys-artifacts/doc/conf/artifacts/winfo.xml --- 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 @@ + diff -r 65dac9cf6ff5 -r 28dbeb64ce7c flys-artifacts/doc/conf/jasper/computed-discharge.jasper Binary file flys-artifacts/doc/conf/jasper/computed-discharge.jasper has changed diff -r 65dac9cf6ff5 -r 28dbeb64ce7c flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java --- 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()); diff -r 65dac9cf6ff5 -r 28dbeb64ce7c flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java --- 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 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 :