diff artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationExporter.java @ 9150:23945061daec

gigantic refactoring: exporter, result, results to support multiple jaspers -> collisions
author gernotbelger
date Thu, 14 Jun 2018 16:56:31 +0200
parents 41f4bc83aa7a
children
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationExporter.java	Thu Jun 14 15:12:25 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationExporter.java	Thu Jun 14 16:56:31 2018 +0200
@@ -10,18 +10,14 @@
 package org.dive4elements.river.artifacts.uinfo.inundationduration;
 
 import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
 
-import org.apache.log4j.Logger;
-import org.dive4elements.river.artifacts.common.GeneralResultType;
+import org.dive4elements.river.artifacts.common.AbstractCalculationExportableResult;
+import org.dive4elements.river.artifacts.common.AbstractCommonExporter;
+import org.dive4elements.river.artifacts.common.ExportContextCSV;
 import org.dive4elements.river.artifacts.common.I18NStrings;
 import org.dive4elements.river.artifacts.common.JasperReporter;
 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource;
-import org.dive4elements.river.artifacts.common.ResultRow;
-import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType;
 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
-import org.dive4elements.river.artifacts.uinfo.commons.AbstractUInfoExporter;
 
 import au.com.bytecode.opencsv.CSVWriter;
 import net.sf.jasperreports.engine.JRException;
@@ -30,113 +26,23 @@
  * @author Domenico Nardi Tironi
  *
  */
-public class InundationDurationExporter extends AbstractUInfoExporter<InundationDurationCalculationResult, InundationDurationCalculationResults> {
-
-    /** The log used in this exporter. */
-    private static Logger log = Logger.getLogger(InundationDurationExporter.class);
-
-    private static final String JASPER_FILE = "/jasper/templates/uinfo.inundationduration.jrxml";
-
-    @Override
-    protected Logger getLog() {
-        return log;
-    }
-
-    @Override
-    protected void writeCSVGlobalMetadata(final CSVWriter writer, final InundationDurationCalculationResults results) {
-        log.info("SalixLineExporter.writeCSVMeta");
-
-        super.writeCSVGlobalMetadataDefaults(writer, results);
-
-        // "# Höhensystem des Flusses: "
-        writeCSVMetaEntry(writer, I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, results.getRiver().getWstUnit());
-
-        writer.writeNext(new String[] { "" }); // break line
-    }
+public class InundationDurationExporter extends AbstractCommonExporter<InundationDurationCalculationResults> {
 
     @Override
-    protected void writeCSVResultMetadata(final CSVWriter writer, final InundationDurationCalculationResults results,
-            final InundationDurationCalculationResult result) {
-
-        /*
-         * final BedHeightInfo sounding = result.getSounding();
-         * super.writeCSVSoundingMetadata(writer, sounding);
-         * final WstInfo wst = result.getWst();
-         * super.writeCSVWaterlevelMetadata(writer, wst);
-         */
-    }
-
-    /**
-     * Write the header, with different headings depending on whether at a
-     * gauge or at a location.
-     *
-     * @param river
-     * @param useTkh
-     */
-    @Override
-    protected void writeCSVHeader(final CSVWriter writer, final InundationDurationCalculationResults results, final RiverInfo river) {
-        log.info("FlowDepthExporter.writeCSVHeader");
-
-        final Collection<String> header = new ArrayList<>(11);
-
-        header.add(msg(GeneralResultType.station.getCsvHeader()));
-        header.add(msgUnit(SInfoResultType.flowdepth.getCsvHeader(), SInfoResultType.flowdepth.getUnit()));
-
-        header.add(msgUnit(SInfoResultType.waterlevel.getCsvHeader(), river.getWstUnit()));
-        header.add(msgUnit(SInfoResultType.discharge.getCsvHeader(), SInfoResultType.discharge.getUnit()));
-        header.add(msg(SInfoResultType.waterlevelLabel.getCsvHeader()));
-        header.add(msg(SInfoResultType.gaugeLabel.getCsvHeader()));
-        header.add(msgUnit(SInfoResultType.meanBedHeight.getCsvHeader(), river.getWstUnit()));
-        header.add(msg(SInfoResultType.soundingLabel.getCsvHeader()));
-        header.add(msg(SInfoResultType.location.getCsvHeader()));
-
-        writer.writeNext(header.toArray(new String[header.size()]));
-    }
-
-    /**
-     * Format a row of a flow depth result into an array of string, both used by csv and pdf
-     *
-     * @param pdf
-     *
-     * @param useTkh
-     */
-    @Override
-    protected String[] formatRow(final InundationDurationCalculationResults results, final ResultRow row, final ExportMode mode) {
-
-        final Collection<String> lines = new ArrayList<>(11);
-
-        lines.add(row.exportValue(this.context, GeneralResultType.station));
-        lines.add(row.exportValue(this.context, SInfoResultType.flowdepth));
-
-        lines.add(row.exportValue(this.context, SInfoResultType.waterlevel));
-        lines.add(row.exportValue(this.context, SInfoResultType.discharge));
-        lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel));
-        lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel));
-        lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight));
-        lines.add(row.exportValue(this.context, SInfoResultType.soundingLabel));
-        lines.add(row.exportValue(this.context, SInfoResultType.location));
-
-        return lines.toArray(new String[lines.size()]);
-    }
-
-    @Override
-    protected final void addJRMetaData(final MetaAndTableJRDataSource source, final InundationDurationCalculationResults results) {
-
-        /* general metadata */
-        super.addJRMetaData(source, results);
-
-        /* column headings */
-        source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta()));
-
-    }
-
-    @Override
-    protected void writePDF(final OutputStream out) {
+    protected void doWritePdf(final OutputStream out, final InundationDurationCalculationResults results) {
+        // TODO: Move to super
         try {
-            final MetaAndTableJRDataSource source = createJRData(this.data);
+            final ExportContextCSV exportContextCSV = new ExportContextCSV(this.context, null);
 
             final JasperReporter reporter = new JasperReporter();
-            reporter.addReport(JASPER_FILE, source);
+
+            for (final AbstractCalculationExportableResult<InundationDurationCalculationResults> result : results.getResults()) {
+                final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource();
+                getHelper().addJRMetaDataUSINFO(source, results);
+
+                result.addReport(exportContextCSV, results, reporter, source);
+            }
+
             reporter.exportPDF(out);
         }
         catch (final JRException je) {
@@ -144,4 +50,36 @@
         }
     }
 
+    @Override
+    protected void doWriteCSVData(final CSVWriter writer, final InundationDurationCalculationResults results) {
+        // TODO: Diesen Ablauf in super? -> leichte abweichung
+
+        // TODO: move results into context?
+        final ExportContextCSV exportContextCSV = new ExportContextCSV(this.context, writer);
+
+        getLog().info("writeCSVData");
+
+        final RiverInfo river = results.getRiver();
+
+        /* write as csv */
+        exportContextCSV.writeCSVGlobalMetadataDefaults(results); // ggf auslagern innerhalb dieser Klasse
+        exportContextCSV.writeCSVMetaEntry(I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit());
+        // writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance
+
+        final Class<?> lastResultType = null;
+
+        for (final AbstractCalculationExportableResult<InundationDurationCalculationResults> result : results.getResults()) {
+
+            final Class<?> resultType = result.getClass();
+            if (lastResultType == null || lastResultType != resultType) {
+                exportContextCSV.writeBlankLine();
+                result.writeCSVHeader(exportContextCSV, results, river);
+                exportContextCSV.writeBlankLine();
+            } else
+                exportContextCSV.writeCSVLine(new String[] { "#" });
+
+            result.writeCsv(exportContextCSV, results);
+        }
+
+    }
 }
\ No newline at end of file

http://dive4elements.wald.intevation.org