diff artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculationResult.java @ 9205:3dae6b78e1da

inundationDuration/floodDuration multiple columns+chartLines refactoring
author gernotbelger
date Mon, 02 Jul 2018 19:01:09 +0200
parents b4402594213b
children 53cc5b496692
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculationResult.java	Mon Jul 02 17:40:39 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculationResult.java	Mon Jul 02 19:01:09 2018 +0200
@@ -9,16 +9,17 @@
  */
 package org.dive4elements.river.artifacts.sinfo.flood_duration;
 
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
 import org.dive4elements.river.artifacts.common.AbstractCalculationExportableResult;
+import org.dive4elements.river.artifacts.common.AbstractExportContext;
 import org.dive4elements.river.artifacts.common.ExportContextCSV;
 import org.dive4elements.river.artifacts.common.ExportContextPDF;
 import org.dive4elements.river.artifacts.common.GeneralResultType;
-import org.dive4elements.river.artifacts.common.IExportContext;
 import org.dive4elements.river.artifacts.common.IResultType;
 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource;
 import org.dive4elements.river.artifacts.common.ResultRow;
@@ -37,24 +38,31 @@
 
     private static final long serialVersionUID = 1L;
 
-    private static final String JASPER_FILE = "/jasper/templates/sinfo.floodduration.jrxml";
+    private final String[] mainvalueLabels;
 
-    private final String[] mainvalueLabels;
+    private final int waterlevelCount;
+    // private final WstInfo wstInfo;
+    private final int maxWaterlevelPdf = 3;
+
+    private enum ExportMode {
+        pdf, csv
+    }
 
     public FloodDurationCalculationResult(final String label, final String[] mainvalueLabels, final Collection<ResultRow> rows) {
         super(label, rows);
-        this.mainvalueLabels = mainvalueLabels;
+        this.mainvalueLabels = mainvalueLabels; // TODO: löschen (?)
+        this.waterlevelCount = mainvalueLabels.length;
     }
 
     /**
      * The label of one of the optional main values, or null
      */
-    public String getMainValueLabel(final int index) {
-        if (index <= this.mainvalueLabels.length - 1)
-            return this.mainvalueLabels[index];
-        else
-            return null;
-    }
+    // public String getMainValueLabel(final int index) {
+    // if (index <= this.mainvalueLabels.length - 1)
+    // return this.mainvalueLabels[index];
+    // else
+    // return null;
+    // }
 
     /**
      * Collection of the result rows containing only the rows describing an infrastructure
@@ -105,7 +113,8 @@
         if (this.mainvalueLabels.length >= 1) {
             // "##METADATEN WASSERSPIEGELLAGE"
             exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL);
-            for (int i = 1; i <= this.mainvalueLabels.length; i++) {
+            for (int i = 1; i <= getWaterlevelCount(); // this.mainvalueLabels.length;
+                    i++) {
                 // "# Bezeichnung der Wasserspiegellage: "
                 final String label = this.getMainValueLabel(i - 1);
                 exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, String.format("%d: %s", i, label));
@@ -113,15 +122,18 @@
             // "# Bezugspegel: "
             exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, "TODO: gauge");
         }
+        exportContextCSV.writeBlankLine();
     }
 
     @Override
     protected String getJasperFile() {
-        // TODO Variante mit Wasserspiegellage(n)
-        return JASPER_FILE;
+        if (this.waterlevelCount <= 1)
+            return "/jasper/templates/sinfo.floodduration.jrxml";
+        else
+            return "/jasper/templates/sinfo.floodduration2.jrxml";
     }
 
-    protected String[] formatRow(final IExportContext exportContextCSV, final ResultRow row) {
+    protected String[] formatRow(final AbstractExportContext exportContextCSV, final ResultRow row, final ExportMode mode) {
 
         final Collection<String> lines = new ArrayList<>(10);
 
@@ -132,22 +144,31 @@
         lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.infrastructureHeight));
         lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.infrastructuretype));
 
-        if (this.getMainValueLabel(0) != null) {
-            lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.waterlevel1));
-            lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.mainValue1Duration));
-            lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.discharge1));
-            if (this.getMainValueLabel(1) != null) {
-                lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.waterlevel2));
-                lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.mainValue2Duration));
-                lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.discharge2));
-                if (this.getMainValueLabel(2) != null) {
-                    lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.waterlevel3));
-                    lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.mainValue3Duration));
-                    lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.discharge3));
-                }
+        final List<DurationWaterlevel> waterlevelList = (List<DurationWaterlevel>) row.getValue(SInfoResultType.customMultiRowColWaterlevel);
+        if (waterlevelList != null) {
+            final NumberFormat wFormatter = exportContextCSV.getFlowDepthFormatter();
+            final NumberFormat qFormatter = exportContextCSV.getQFormatter();
+
+            for (int i = 0; i < waterlevelList.size(); i++) {
+
+                if (i == this.maxWaterlevelPdf && mode == ExportMode.pdf)
+                    break;
+
+                final DurationWaterlevel item = waterlevelList.get(i);
+                lines.add(item.getWFormatted(wFormatter));
+                lines.add(item.getFloodDurDaysPerYearFormatted());
+                lines.add(item.getQFormatted(qFormatter));
+                lines.add(item.getBezeichnung());
             }
         }
 
+        if ((this.waterlevelCount == 0 || this.waterlevelCount == 2) && mode == ExportMode.pdf) {
+            // lines.add("dummy1");
+            // lines.add("dummy2");
+            // lines.add("dummy3");
+            // lines.add("dummy4");
+        }
+
         lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.gaugeLabel));
         lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.location));
 
@@ -166,20 +187,18 @@
         header.add(exportContextCSV.msgUnitCSV(SInfoResultType.infrastructureHeight, SInfoResultType.infrastructureHeight.getUnit()));
         header.add(exportContextCSV.formatCsvHeader(SInfoResultType.infrastructuretype));
 
-        if (this.getMainValueLabel(0) != null) {
-            header.add(exportContextCSV.msgUnitCSV(SInfoResultType.waterlevel1, SInfoResultType.waterlevel.getUnit()));
-            header.add(exportContextCSV.formatCsvHeader(SInfoResultType.mainValue1Duration));
-            header.add(exportContextCSV.msgUnitCSV(SInfoResultType.discharge1, SInfoResultType.discharge.getUnit()));
-            if (this.getMainValueLabel(1) != null) {
-                header.add(exportContextCSV.msgUnitCSV(SInfoResultType.waterlevel2, SInfoResultType.waterlevel2.getUnit()));
-                header.add(exportContextCSV.formatCsvHeader(SInfoResultType.mainValue2Duration));
-                header.add(exportContextCSV.msgUnitCSV(SInfoResultType.discharge2, SInfoResultType.discharge2.getUnit()));
-                if (this.getMainValueLabel(2) != null) {
-                    header.add(exportContextCSV.msgUnitCSV(SInfoResultType.waterlevel3, SInfoResultType.waterlevel3.getUnit()));
-                    header.add(exportContextCSV.formatCsvHeader(SInfoResultType.mainValue3Duration));
-                    header.add(exportContextCSV.msgUnitCSV(SInfoResultType.discharge3, SInfoResultType.discharge3.getUnit()));
-                }
-            }
+        // add dynamic headers
+        final int waterlevelCount = // results.
+                getWaterlevelCount();
+        for (int i = 0; i < waterlevelCount; i++) {
+            final int naturalIndex = i + 1;
+            final String appendIndex = new StringBuilder().append("_").append(naturalIndex).toString();
+            final Object[] args = new Object[] { appendIndex };
+            // new StringBuilder().append('\u2081').toString(); // schlechter UTF-8-Support für subscript ints
+            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderW(), new Object[] { appendIndex, "results.getRiver().getWstUnit()" }));
+            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderFloodDurPerYear(), args));
+            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderQ(), args));
+            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderBezeichn(), args));
         }
 
         header.add(exportContextCSV.formatCsvHeader(SInfoResultType.gaugeLabel));
@@ -192,13 +211,13 @@
     @Override
     protected String[] formatCSVRow(final ExportContextCSV exportContextCSV, final ResultRow row) {
 
-        return this.formatRow(exportContextCSV, row);
+        return this.formatRow(exportContextCSV, row, ExportMode.csv);
     }
 
     @Override
     protected String[] formatPDFRow(final ExportContextPDF exportContextPDF, final ResultRow row) {
 
-        return this.formatRow(exportContextPDF, row);
+        return this.formatRow(exportContextPDF, row, ExportMode.pdf);
     }
 
     @Override
@@ -212,24 +231,45 @@
         exportContextPDF.addJRMetadata(source, "infrastructure_height_header", SInfoResultType.infrastructureHeight);
         exportContextPDF.addJRMetadata(source, "infrastructure_type_header", SInfoResultType.infrastructuretype);
 
-        // TODO Feldnamen ergaenzen und aktivieren wenn Report fertig
-        // if (this.getMainValueLabel(0) != null) {
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.waterlevel);
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.mainValue1Duration);
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.discharge);
-        // if (this.getMainValueLabel(1) != null) {
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.waterlevel2);
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.mainValue2Duration);
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.discharge2);
-        // if (this.getMainValueLabel(2) != null) {
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.waterlevel3);
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.mainValue3Duration);
-        // exportContextPDF.addJRMetadata(source, "?", SInfoResultType.discharge3);
-        // }
-        // }
-        // }
+        // add dynamic headers
+
+        if (this.waterlevelCount == 0 || this.waterlevelCount == 2) {
+            // source.addMetaData("dummy1", "dummy1");
+            // source.addMetaData("dummy2", "dummy2");
+            // source.addMetaData("dummy3", "dummy3");
+            // source.addMetaData("dummy4", "dummy4");
+        }
+
+        for (int i = 0; i < this.waterlevelCount; i++) {
+            final int naturalIndex = i + 1;
+
+            final Object[] args = new String[] { new StringBuilder().append("_").append(naturalIndex).toString() };
+            exportContextPDF.addJRMetadata(source, getPdfHeader("w", naturalIndex), exportContextPDF.msg(DurationWaterlevel.getHeaderW(), args));
+            exportContextPDF.addJRMetadata(source, getPdfHeader("duration", naturalIndex),
+                    exportContextPDF.msg(DurationWaterlevel.getHeaderFloodDurPerYear(), args));
+            exportContextPDF.addJRMetadata(source, getPdfHeader("q", naturalIndex), exportContextPDF.msg(DurationWaterlevel.getHeaderQ(), args));
+            exportContextPDF.addJRMetadata(source, getPdfHeader("bezeichnung", naturalIndex),
+                    exportContextPDF.msg(DurationWaterlevel.getHeaderBezeichn(), args));
+        }
 
         exportContextPDF.addJRMetadata(source, "gauge_header", SInfoResultType.gaugeLabel);
         exportContextPDF.addJRMetadata(source, "location_header", SInfoResultType.location);
     }
+
+    public final int getWaterlevelCount() {
+        return this.waterlevelCount;
+    }
+
+    private final String getPdfHeader(final String rootStr, final int index) {
+        final String hd = "_header";
+        final StringBuilder builder = new StringBuilder();
+        return builder.append(rootStr).append("_").append(index).append(hd).toString();
+    }
+
+    public String getMainValueLabel(final int j) {
+
+        if (this.mainvalueLabels != null && j < this.mainvalueLabels.length)
+            return this.mainvalueLabels[j];
+        return "";
+    }
 }
\ No newline at end of file

http://dive4elements.wald.intevation.org