Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java @ 2414:b5f5af53a526
Make it compileable again
flys-artifacts/trunk@4045 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 13 Feb 2012 17:48:14 +0000 |
parents | a9a8df1473fd |
children | b4a95290ec63 |
line wrap: on
line source
package de.intevation.flys.exports; import java.io.OutputStream; import java.text.NumberFormat; 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; import org.apache.log4j.Logger; 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.common.utils.Config; 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; /** * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class ComputedDischargeCurveExporter extends AbstractExporter { /** The logger used in this exporter.*/ private static Logger logger = Logger.getLogger(ComputedDischargeCurveExporter.class); public static final String CSV_W_HEADER = "export.computed.discharge.curve.csv.header.w"; public static final String CSV_Q_HEADER = "export.computed.discharge.curve.csv.header.q"; 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"; public static final String JASPER_FILE = "export.computed.discharge.pdf.file"; protected List<WQKms> data; public void init(Document request, OutputStream out, CallContext context) { logger.debug("ComputedDischargeCurveExporter.init"); super.init(request, out, context); this.data = new ArrayList<WQKms>(); } @Override protected void addData(Object d) { if (d instanceof CalculationResult) { d = ((CalculationResult)d).getData(); if (d instanceof WQKms []) { data.addAll(Arrays.asList((WQKms [])d)); } } } protected void writeCSVData(CSVWriter writer) { logger.info("ComputedDischargeCurveExporter.writeData"); writeCSVHeader(writer); 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); writer.writeNext(new String[] { wf.format(res[0]), qf.format(res[1]) }); } } } protected void writeCSVHeader(CSVWriter writer) { logger.debug("ComputedDischargeCurveExporter.writeCSVHeader"); writer.writeNext(new String[] { msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER), msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER) }); } /** * Returns the number formatter for W values. * * @return the number formatter for W values. */ protected NumberFormat getWFormatter() { return Formatter.getComputedDischargeW(context); } /** * Returns the number formatter for Q values. * * @return the number formatter for Q values. */ protected NumberFormat getQFormatter() { return Formatter.getComputedDischargeQ(context); } @Override protected void writePDF(OutputStream out) { WKmsJRDataSource source = createJRData(); String jasperFile = Resources.getMsg( context.getMeta(), JASPER_FILE, "/jasper/computed-discharge_en.jasper"); String confPath = Config.getConfigDirectory().toString(); Map parameters = new HashMap(); parameters.put("ReportTitle", "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.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 :