diff artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 9151:bb929b444ea5

vergessen
author gernotbelger
date Thu, 14 Jun 2018 16:57:14 +0200
parents
children a4121ec450d6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java	Thu Jun 14 16:57:14 2018 +0200
@@ -0,0 +1,96 @@
+/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by
+ *  Björnsen Beratende Ingenieure GmbH
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+package org.dive4elements.river.artifacts.common;
+
+import java.io.OutputStream;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.river.artifacts.model.CalculationResult;
+import org.dive4elements.river.exports.AbstractExporter;
+import org.w3c.dom.Document;
+
+import au.com.bytecode.opencsv.CSVWriter;
+
+/**
+ * @author Gernot Belger
+ */
+public abstract class AbstractCommonExporter<RESULTS extends AbstractCalculationResults> extends AbstractExporter {
+
+    /** The log used in this exporter. */
+    private final Logger log = Logger.getLogger(getClass());
+
+    /** The log used in this exporter. */
+    protected final Logger getLog() {
+        return this.log;
+    }
+
+    private RESULTS results = null;
+
+    private ExportContextPDF helper;
+
+    @Override
+    public void init(final String outName, final Document request, final OutputStream out, final CallContext context) {
+        super.init(outName, request, out, context);
+
+        this.helper = new ExportContextPDF(context); // bleibt so asynchron zu ExportContextCSV - ist so beabsichtigt
+    }
+
+    protected final ExportContextPDF getHelper() {
+        return this.helper;
+    }
+
+    @Override
+    protected final void addData(final Object d) {
+        /* reset */
+        this.results = null;
+
+        if (d instanceof CalculationResult) {
+
+            final Object dat = ((CalculationResult) d).getData();
+            if (dat != null) {
+                @SuppressWarnings("unchecked")
+                final RESULTS result = (RESULTS) dat;
+                this.results = result;
+            }
+        }
+    }
+
+    protected final RESULTS getResults() {
+        return this.results;
+    }
+
+    protected final AbstractCalculationResults getResults2() {
+        return this.results;
+    }
+
+    @Override
+    protected final void writeCSVData(final CSVWriter writer) {
+        doWriteCSVData(writer, this.results);
+    }
+
+    protected abstract void doWriteCSVData(CSVWriter writer, RESULTS results);
+
+    /**
+     * Formats header with unit and label: msg [unit] (label)
+     */
+    // TODO: REMOVE, because it has moved to ExportContextCSV
+    protected final String msgUnitLabel(final String key, final String unit, final String label) {
+        final String msg = msg(key);
+        return String.format("%s [%s] (%s)", msg, unit, label);
+    }
+
+    @Override
+    protected final void writePDF(final OutputStream out) {
+        doWritePdf(out, this.results);
+    }
+
+    protected abstract void doWritePdf(OutputStream out, RESULTS results);
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org