ingo@2258: package de.intevation.flys.exports;
ingo@2258:
ingo@2258: import java.io.OutputStream;
ingo@2258: import java.text.NumberFormat;
ingo@2258: import java.text.DateFormat;
ingo@2258: import java.util.ArrayList;
ingo@2258: import java.util.Date;
ingo@2258: import java.util.List;
raimund@2286: import java.util.Map;
raimund@2286: import java.util.HashMap;
raimund@2286: import java.util.Locale;
ingo@2258:
ingo@2258: import org.w3c.dom.Document;
ingo@2258:
ingo@2258: import org.apache.log4j.Logger;
ingo@2258:
ingo@2258: import au.com.bytecode.opencsv.CSVWriter;
ingo@2258:
raimund@2286: import net.sf.jasperreports.engine.JasperExportManager;
raimund@2286: import net.sf.jasperreports.engine.JasperFillManager;
raimund@2286: import net.sf.jasperreports.engine.JasperPrint;
raimund@2286: import net.sf.jasperreports.engine.JRException;
raimund@2286:
raimund@2286: import de.intevation.artifacts.common.utils.Config;
raimund@2286:
ingo@2258: import de.intevation.artifacts.CallContext;
ingo@2258: import de.intevation.artifacts.CallMeta;
ingo@2258:
ingo@2258: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@2258: import de.intevation.flys.artifacts.model.CalculationResult;
ingo@2258: import de.intevation.flys.artifacts.model.Timerange;
ingo@2258: import de.intevation.flys.artifacts.model.WQTimerange;
raimund@2286: import de.intevation.flys.artifacts.model.WQTJRDataSource;
ingo@2258: import de.intevation.flys.artifacts.resources.Resources;
ingo@2258: import de.intevation.flys.utils.FLYSUtils;
ingo@2258: import de.intevation.flys.utils.Formatter;
ingo@2258:
ingo@2258:
ingo@2258: /**
ingo@2258: * @author Ingo Weinzierl
ingo@2258: */
ingo@2258: public class HistoricalDischargeCurveExporter extends AbstractExporter {
ingo@2258:
ingo@2258: private static final Logger logger =
ingo@2258: Logger.getLogger(HistoricalDischargeCurveExporter.class);
ingo@2258:
ingo@2258:
ingo@2258: public static final String CSV_TIMERANGE_HEADER =
ingo@2258: "export.historical.discharge.csv.header.timerange";
ingo@2258:
ingo@2258: public static final String CSV_WATERLEVEL_HEADER =
ingo@2258: "export.historical.discharge.csv.header.waterlevel";
ingo@2258:
ingo@2258: public static final String CSV_DISCHARGE_HEADER =
ingo@2258: "export.historical.discharge.csv.header.discharge";
ingo@2258:
ingo@2258: public static final String CSV_DIFF_HEADER =
ingo@2258: "export.historical.discharge.csv.header.diff";
ingo@2258:
ingo@2258: public static final String CSV_GAUGENAME_HEADER =
ingo@2258: "export.historical.discharge.csv.header.gaugename";
ingo@2258:
raimund@2286: public static final String PDF_HEADER_MODE =
raimund@2286: "export.historical.discharge.pdf.mode";
raimund@2286:
raimund@2286: public static final String JASPER_FILE =
raimund@2286: "export.historical.discharge.pdf.file";
ingo@2258:
ingo@2258: protected List data;
ingo@2258:
ingo@2258:
ingo@2258: public void init(Document request, OutputStream out, CallContext cc) {
ingo@2258: super.init(request, out, cc);
ingo@2258:
ingo@2258: data = new ArrayList();
ingo@2258: }
ingo@2258:
ingo@2258:
ingo@2258: @Override
ingo@2258: protected void addData(Object d) {
ingo@2258: logger.debug("Add data of class: " + d.getClass());
ingo@2258:
ingo@2258: if (d instanceof CalculationResult) {
ingo@2258: d = ((CalculationResult) d).getData();
ingo@2258:
ingo@2258: logger.debug("Internal data of CalculationResult: " + d.getClass());
ingo@2258:
ingo@2258: if (d instanceof WQTimerange[]) {
ingo@2258: logger.debug("Add new data of type WQTimerange");
ingo@2258: data.add((WQTimerange[]) d);
ingo@2258: }
ingo@2258: }
ingo@2258: }
ingo@2258:
ingo@2258:
ingo@2258: @Override
ingo@2258: protected void writeCSVData(CSVWriter writer) {
ingo@2258: logger.info("HistoricalDischargeCurveExporter.writeCSVData");
ingo@2258: logger.debug("CSV gets " + data.size() + " WQTimerange[] objects.");
ingo@2258:
ingo@2258: writeCSVHeader(writer);
ingo@2258:
ingo@2258: for (WQTimerange[] arr: data) {
ingo@2258: for (WQTimerange wqt: arr) {
ingo@2258: wqt2CSV(writer, wqt);
ingo@2258: }
ingo@2258: }
ingo@2258: }
ingo@2258:
ingo@2258:
ingo@2258: @Override
ingo@2258: protected void writePDF(OutputStream out) {
raimund@2286: WQTJRDataSource source = createJRData();
raimund@2286:
raimund@2286: String jasperFile = Resources.getMsg(
raimund@2286: context.getMeta(),
raimund@2286: JASPER_FILE,
raimund@2286: "/jasper/historical-discharge_en.jasper");
raimund@2286: String confPath = Config.getConfigDirectory().toString();
raimund@2286:
raimund@2286: Map parameters = new HashMap();
raimund@2286: parameters.put("ReportTitle", "Exported Data");
raimund@2286: try {
raimund@2286: JasperPrint print = JasperFillManager.fillReport(
raimund@2286: confPath + jasperFile,
raimund@2286: parameters,
raimund@2286: source);
raimund@2286: JasperExportManager.exportReportToPdfStream(print, out);
raimund@2286: }
raimund@2286: catch(JRException je) {
raimund@2286: logger.warn("Error generating PDF Report!");
raimund@2286: je.printStackTrace();
raimund@2286: }
raimund@2286:
ingo@2258: }
ingo@2258:
ingo@2258:
ingo@2258: protected void writeCSVHeader(CSVWriter writer) {
ingo@2258: writer.writeNext(new String[] {
ingo@2258: msg(CSV_TIMERANGE_HEADER, CSV_TIMERANGE_HEADER),
ingo@2258: msg(CSV_WATERLEVEL_HEADER, CSV_WATERLEVEL_HEADER),
ingo@2258: msg(CSV_DISCHARGE_HEADER, CSV_DISCHARGE_HEADER),
ingo@2258: msg(CSV_DIFF_HEADER, CSV_DIFF_HEADER),
ingo@2258: msg(CSV_GAUGENAME_HEADER, CSV_GAUGENAME_HEADER)
ingo@2258: });
ingo@2258: }
ingo@2258:
ingo@2258:
ingo@2258: protected void wqt2CSV(CSVWriter writer, WQTimerange wqt) {
ingo@2258: logger.debug("Add next WQTimerange to CSV");
ingo@2258:
ingo@2258: DateFormat df = Formatter.getShortDateFormat(context);
ingo@2258: NumberFormat wf = Formatter.getHistoricalDischargeW(context);
ingo@2258: NumberFormat qf = Formatter.getHistoricalDischargeQ(context);
ingo@2258:
ingo@2258: double[] wq = new double[2];
ingo@2258:
ingo@2258: String gaugeName = getReferenceGaugename();
ingo@2258:
ingo@2258: for (int i = 0, n = wqt.size(); i < n; i++) {
ingo@2258: Timerange tr = wqt.getTimerange(i);
ingo@2258: Date start = new Date(tr.getStart());
ingo@2258: Date end = new Date(tr.getEnd());
ingo@2258:
ingo@2258: wqt.get(i, wq);
ingo@2258:
ingo@2258: writer.writeNext(new String[] {
ingo@2258: df.format(start) + " - " + df.format(end),
ingo@2258: wf.format(wq[0]),
ingo@2258: qf.format(wq[1]),
ingo@2258: "TODO: Difference to reference curve",
ingo@2258: gaugeName
ingo@2258: });
ingo@2258: }
ingo@2258: }
ingo@2258:
ingo@2258:
raimund@2286: protected WQTJRDataSource createJRData() {
raimund@2286: WQTJRDataSource source = new WQTJRDataSource();
raimund@2286:
raimund@2286: addMetaData(source);
raimund@2286: for (WQTimerange[] arr: data) {
raimund@2286: for (WQTimerange wqt: arr) {
raimund@2286: addWQTData(source, wqt);
raimund@2286: }
raimund@2286: }
raimund@2286:
raimund@2286: return source;
raimund@2286: }
raimund@2286:
raimund@2286:
raimund@2286: protected void addMetaData(WQTJRDataSource source) {
raimund@2286: CallMeta meta = context.getMeta();
raimund@2286:
raimund@2286: FLYSArtifact flys = (FLYSArtifact) master;
raimund@2286:
raimund@2286: source.addMetaData ("river", FLYSUtils.getRivername(flys));
raimund@2286:
raimund@2286: Locale locale = Resources.getLocale(meta);
raimund@2286: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
raimund@2286:
raimund@2286: source.addMetaData("date", df.format(new Date()));
raimund@2286:
raimund@2286: source.addMetaData("calculation", Resources.getMsg(
raimund@2286: locale,
raimund@2286: PDF_HEADER_MODE,
raimund@2286: "Historical Discharge"));
raimund@2286: }
raimund@2286:
raimund@2286:
raimund@2286: protected void addWQTData(WQTJRDataSource source, WQTimerange wqt) {
raimund@2286: DateFormat df = Formatter.getShortDateFormat(context);
raimund@2286: NumberFormat wf = Formatter.getHistoricalDischargeW(context);
raimund@2286: NumberFormat qf = Formatter.getHistoricalDischargeQ(context);
raimund@2286:
raimund@2286: double[] wq = new double[2];
raimund@2286:
raimund@2286: String gaugeName = getReferenceGaugename();
raimund@2286:
raimund@2286: for (int i = 0, n = wqt.size(); i < n; i++) {
raimund@2286: Timerange tr = wqt.getTimerange(i);
raimund@2286: Date start = new Date(tr.getStart());
raimund@2286: Date end = new Date(tr.getEnd());
raimund@2286:
raimund@2286: wqt.get(i, wq);
raimund@2286:
raimund@2286: source.addData(new String[] {
raimund@2286: df.format(start) + " - " + df.format(end),
raimund@2286: wf.format(wq[0]),
raimund@2286: qf.format(wq[1]),
raimund@2286: "TODO: Difference to reference curve",
raimund@2286: gaugeName
raimund@2286: });
raimund@2286: }
raimund@2286: }
raimund@2286:
raimund@2286:
ingo@2258: public String getReferenceGaugename() {
ingo@2258: return FLYSUtils.getReferenceGaugeName((FLYSArtifact) master);
ingo@2258: }
ingo@2258: }
ingo@2258: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :