Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.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 | 6f4a1f513e89 |
children | 4bd3d8bbb60c |
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.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.model.WQDay; import de.intevation.flys.artifacts.model.CalculationResult; 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 DurationCurveExporter extends AbstractExporter { /** The logger used in this exporter. */ private static Logger logger = Logger.getLogger(WaterlevelExporter.class); public static final String CSV_DURATION_HEADER = "export.duration.curve.csv.header.duration"; public static final String CSV_W_HEADER = "export.duration.curve.csv.header.w"; public static final String CSV_Q_HEADER = "export.duration.curve.csv.header.q"; public static final String DEFAULT_CSV_DURATION_HEADER = "D [Tagen]"; 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.duration.pdf.mode"; public static final String JASPER_FILE = "export.duration.pdf.file"; /** The storage that contains all WQKms objects for the different facets. */ protected List<WQDay> data; public void init(Document request, OutputStream out, CallContext context) { logger.debug("DurationCurveExporter.init"); super.init(request, out, context); this.data = new ArrayList<WQDay>(); } @Override protected void addData(Object d) { if (d instanceof CalculationResult) { d = ((CalculationResult)d).getData(); if (d instanceof WQDay) { data.add((WQDay)d); } } } protected void writeCSVData(CSVWriter writer) { logger.info("DurationCurveExporter.writeData"); writeCSVHeader(writer); for (WQDay wqday: data) { wQDay2CSV(writer, wqday); } } protected void writeCSVHeader(CSVWriter writer) { logger.info("DurationCurveExporter.writeCSVHeader"); writer.writeNext(new String[] { msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER), msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER), msg(CSV_DURATION_HEADER, DEFAULT_CSV_DURATION_HEADER) }); } protected void wQDay2CSV(CSVWriter writer, WQDay wqday) { logger.debug("DurationCurveExporter.wQDay2CSV"); int size = wqday.size(); NumberFormat wf = getWFormatter(); NumberFormat qf = getQFormatter(); NumberFormat df = getDFormatter(); if (wqday.isIncreasing()) { for (int i = size-1; i >= 0; i --) { writer.writeNext(new String[] { wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), df.format(wqday.getDay(i)) }); } } else { for (int i = 0; i < size; i ++) { writer.writeNext(new String[] { wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), df.format(wqday.getDay(i)) }); } } } /** * Returns the number formatter for W values. * * @return the number formatter for W values. */ @Override protected NumberFormat getWFormatter() { return Formatter.getDurationW(context); } /** * Returns the number formatter for Q values. * * @return the number formatter for Q values. */ @Override protected NumberFormat getQFormatter() { return Formatter.getDurationQ(context); } /** * Returns the number formatter for duration values. * * @return the number formatter for duration values. */ protected NumberFormat getDFormatter() { return Formatter.getDurationD(context); } @Override protected void writePDF(OutputStream out) { WKmsJRDataSource source = createJRData(); String jasperFile = Resources.getMsg( context.getMeta(), JASPER_FILE, "/jasper/duration_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); for (WQDay wqday: data) { addWQDayData(source, wqday); } 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, "Duration")); } protected void addWQDayData(WKmsJRDataSource source, WQDay wqday) { int size = wqday.size(); NumberFormat wf = getWFormatter(); NumberFormat qf = getQFormatter(); NumberFormat df = getDFormatter(); if (wqday.isIncreasing()) { for (int i = size-1; i >= 0; i --) { source.addData(new String[] { "", wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), "", "", "", df.format(wqday.getDay(i)) }); } } else { for (int i = 0; i < size; i ++) { source.addData(new String[] { "", wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), "", "", "", df.format(wqday.getDay(i)) }); } } } }