Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.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 | 84cdde4bc482 |
children | 7dd45896e941 |
line wrap: on
line source
package de.intevation.flys.exports; import java.io.OutputStream; import java.text.NumberFormat; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.HashMap; 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.Timerange; import de.intevation.flys.artifacts.model.WQTimerange; import de.intevation.flys.artifacts.model.WQTJRDataSource; 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 HistoricalDischargeCurveExporter extends AbstractExporter { private static final Logger logger = Logger.getLogger(HistoricalDischargeCurveExporter.class); public static final String CSV_TIMERANGE_HEADER = "export.historical.discharge.csv.header.timerange"; public static final String CSV_WATERLEVEL_HEADER = "export.historical.discharge.csv.header.waterlevel"; public static final String CSV_DISCHARGE_HEADER = "export.historical.discharge.csv.header.discharge"; public static final String CSV_DIFF_HEADER = "export.historical.discharge.csv.header.diff"; public static final String CSV_GAUGENAME_HEADER = "export.historical.discharge.csv.header.gaugename"; public static final String PDF_HEADER_MODE = "export.historical.discharge.pdf.mode"; public static final String JASPER_FILE = "export.historical.discharge.pdf.file"; protected List<WQTimerange[]> data; public void init(Document request, OutputStream out, CallContext cc) { super.init(request, out, cc); data = new ArrayList<WQTimerange[]>(); } @Override protected void addData(Object d) { logger.debug("Add data of class: " + d.getClass()); if (d instanceof CalculationResult) { d = ((CalculationResult) d).getData(); logger.debug("Internal data of CalculationResult: " + d.getClass()); if (d instanceof WQTimerange[]) { logger.debug("Add new data of type WQTimerange"); data.add((WQTimerange[]) d); } } } @Override protected void writeCSVData(CSVWriter writer) { logger.info("HistoricalDischargeCurveExporter.writeCSVData"); logger.debug("CSV gets " + data.size() + " WQTimerange[] objects."); writeCSVHeader(writer); for (WQTimerange[] arr: data) { for (WQTimerange wqt: arr) { wqt2CSV(writer, wqt); } } } @Override protected void writePDF(OutputStream out) { WQTJRDataSource source = createJRData(); String jasperFile = Resources.getMsg( context.getMeta(), JASPER_FILE, "/jasper/historical-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 void writeCSVHeader(CSVWriter writer) { writer.writeNext(new String[] { msg(CSV_TIMERANGE_HEADER, CSV_TIMERANGE_HEADER), msg(CSV_WATERLEVEL_HEADER, CSV_WATERLEVEL_HEADER), msg(CSV_DISCHARGE_HEADER, CSV_DISCHARGE_HEADER), msg(CSV_DIFF_HEADER, CSV_DIFF_HEADER), msg(CSV_GAUGENAME_HEADER, CSV_GAUGENAME_HEADER) }); } protected void wqt2CSV(CSVWriter writer, WQTimerange wqt) { logger.debug("Add next WQTimerange to CSV"); DateFormat df = Formatter.getShortDateFormat(context); NumberFormat wf = Formatter.getHistoricalDischargeW(context); NumberFormat qf = Formatter.getHistoricalDischargeQ(context); double[] wq = new double[3]; String gaugeName = getReferenceGaugename(); for (int i = 0, n = wqt.size(); i < n; i++) { Timerange tr = wqt.getTimerange(i); Date start = new Date(tr.getStart()); Date end = new Date(tr.getEnd()); wqt.get(i, wq); writer.writeNext(new String[] { df.format(start) + " - " + df.format(end), wf.format(wq[0]), qf.format(wq[1]), qf.format(wq[2]), gaugeName }); } } protected WQTJRDataSource createJRData() { WQTJRDataSource source = new WQTJRDataSource(); addMetaData(source); for (WQTimerange[] arr: data) { for (WQTimerange wqt: arr) { addWQTData(source, wqt); } } return source; } protected void addMetaData(WQTJRDataSource 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())); source.addMetaData("calculation", Resources.getMsg( locale, PDF_HEADER_MODE, "Historical Discharge")); } protected void addWQTData(WQTJRDataSource source, WQTimerange wqt) { DateFormat df = Formatter.getShortDateFormat(context); NumberFormat wf = Formatter.getHistoricalDischargeW(context); NumberFormat qf = Formatter.getHistoricalDischargeQ(context); double[] wq = new double[3]; String gaugeName = getReferenceGaugename(); for (int i = 0, n = wqt.size(); i < n; i++) { Timerange tr = wqt.getTimerange(i); Date start = new Date(tr.getStart()); Date end = new Date(tr.getEnd()); wqt.get(i, wq); source.addData(new String[] { df.format(start) + " - " + df.format(end), wf.format(wq[0]), qf.format(wq[1]), qf.format(wq[2]), gaugeName }); } } public String getReferenceGaugename() { return FLYSUtils.getReferenceGaugeName((FLYSArtifact) master); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :