Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/ReferenceCurveExporter.java @ 2311:b8df8d1476ba
Compute differences between discharge table values and reference discharge table values in historical discharge curve calculation.
flys-artifacts/trunk@3991 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 09 Feb 2012 10:57:35 +0000 |
parents | ecb76933362f |
children | 6bbb8295fd64 |
line wrap: on
line source
package de.intevation.flys.exports; import java.io.IOException; import java.io.OutputStream; 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; 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.CallContext; import de.intevation.artifacts.CallMeta; import de.intevation.artifacts.common.utils.Config; import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; import de.intevation.flys.artifacts.model.CalculationResult; import de.intevation.flys.artifacts.model.WWQQ; import de.intevation.flys.artifacts.model.WWQQJRDataSource; import de.intevation.flys.artifacts.resources.Resources; import de.intevation.flys.utils.Formatter; import de.intevation.flys.utils.FLYSUtils; /** * (CSV)Exporter for Reference Curves. */ public class ReferenceCurveExporter extends AbstractExporter { /** The logger used in this exporter. */ private static Logger logger = Logger.getLogger(ReferenceCurveExporter.class); public static final String RC_CSV_KM_HEADER = "export.reference_curve.csv.header.km"; public static final String RC_CSV_W_HEADER = "export.reference_curve.csv.header.w"; public static final String RC_CSV_Q_HEADER = "export.reference_curve.csv.header.q"; public static final String CSV_LOCATION_HEADER = "export.waterlevel.csv.header.location"; public static final String DEFAULT_CSV_LOCATION_HEADER = "Lage"; public static final String RC_DEFAULT_CSV_KM_HEADER = "Fluss-Km"; public static final String RC_DEFAULT_CSV_W_HEADER = "m"; public static final String RC_DEFAULT_CSV_Q_HEADER = "Q"; public static final String PDF_HEADER_MODE = "export.reference_curve.pdf.mode"; public static final String JASPER_FILE = "export.reference_curve.pdf.file"; /** The storage that contains all WKms objects for the different facets. */ protected List<WWQQ[]> data; public void init(Document request, OutputStream out, CallContext context) { logger.debug("ReferenceCurveExporter.init"); super.init(request, out, context); this.data = new ArrayList<WWQQ[]>(); } /** * Genereate data in csv format. */ @Override public void generate() throws IOException { logger.debug("ReferenceCurveExporter.generate"); if (facet == null) { throw new IOException("invalid (null) facet for exporter"); } 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"); } } /** * Adds given data. * @param d a WWQQ[]. */ @Override protected void addData(Object d) { logger.debug("ReferenceCurveExporter.addData"); if (d instanceof CalculationResult) { d = ((CalculationResult)d).getData(); if (d instanceof WWQQ []) { data.add((WWQQ [])d); logger.debug("ReferenceCurveExporter.addData wwqq[]."); } else { logger.warn("ReferenceCurveExporter.addData/1 unknown type (" + d + ")."); } } else { logger.warn("ReferenceCurveExporter.addData/2 unknown type (" + d + ")."); } } /** * Lets writer write all data (including header). * @param writer Writer to write data with. */ @Override protected void writeCSVData(CSVWriter writer) { logger.info("ReferenceCurveExporter.writeData"); writeCSVHeader(writer); for (WWQQ[] tmp: data) { for (WWQQ ww: tmp) { wWQQ2CSV(writer, ww); } } } /** * Lets csvwriter write the header (first line in file). * @param write Writer to write header with. */ protected void writeCSVHeader(CSVWriter writer) { logger.info("ReferenceCurveExporter.writeCSVHeader"); // TODO missing 'relative' W(cm). writer.writeNext(new String[] { msg(RC_CSV_KM_HEADER, RC_DEFAULT_CSV_KM_HEADER), msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER), msg(RC_CSV_W_HEADER, RC_DEFAULT_CSV_W_HEADER), msg(RC_CSV_Q_HEADER, RC_DEFAULT_CSV_Q_HEADER), msg(RC_CSV_KM_HEADER, RC_DEFAULT_CSV_KM_HEADER), msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER), msg(RC_CSV_W_HEADER, RC_DEFAULT_CSV_W_HEADER), msg(RC_CSV_Q_HEADER, RC_DEFAULT_CSV_Q_HEADER), }); } protected void wWQQ2CSV(CSVWriter writer, WWQQ ww) { logger.debug("ReferenceCurveExporter.wWQQ2CSV"); NumberFormat kmf = getKmFormatter(); NumberFormat wf = getWFormatter(); NumberFormat qf = getQFormatter(); int size = ww.size(); FLYSArtifact flys = (FLYSArtifact) master; String startLocationDescription = FLYSUtils.getLocationDescription( flys, ww.getStartKm()); String endLocationDescription = FLYSUtils.getLocationDescription( flys, ww.getEndKm()); // TODO missing 'relative' W(cm). for (int i = 0; i < size; i ++) { writer.writeNext(new String[] { kmf.format(ww.getStartKm()), startLocationDescription, wf.format(ww.getW1(i)), qf.format(ww.getQ1(i)), // "Q" kmf.format(ww.getEndKm()), endLocationDescription, wf.format(ww.getW2(i)), qf.format(ww.getQ2(i)) // "Q" }); } } @Override protected void writePDF(OutputStream out) { WWQQJRDataSource source = createJRData(); String jasperFile = Resources.getMsg( context.getMeta(), JASPER_FILE, "/jasper/reference_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 WWQQJRDataSource createJRData() { WWQQJRDataSource source = new WWQQJRDataSource(); addMetaData(source); for (WWQQ[] tmp: data) { for (WWQQ ww: tmp) { addWWQQData(source, ww); } } return source; } protected void addMetaData(WWQQJRDataSource 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())); source.addMetaData("calculation", Resources.getMsg( locale, PDF_HEADER_MODE, "Reference Curve")); } protected void addWWQQData(WWQQJRDataSource source, WWQQ ww) { NumberFormat kmf = getKmFormatter(); NumberFormat wf = getWFormatter(); NumberFormat qf = getQFormatter(); int size = ww.size(); FLYSArtifact flys = (FLYSArtifact) master; String startLocationDescription = FLYSUtils.getLocationDescription( flys, ww.getStartKm()); String endLocationDescription = FLYSUtils.getLocationDescription( flys, ww.getEndKm()); // TODO missing 'relative' W(cm). for (int i = 0; i < size; i ++) { source.addData(new String[] { kmf.format(ww.getStartKm()), startLocationDescription, wf.format(ww.getW1(i)), qf.format(ww.getQ1(i)), // "Q" kmf.format(ww.getEndKm()), endLocationDescription, wf.format(ww.getW2(i)), qf.format(ww.getQ2(i)) // "Q" }); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :