changeset 8933:05b5588bdd94

Extracting some winfo logic without breaking old code
author gernotbelger
date Tue, 06 Mar 2018 17:05:13 +0100
parents 8731c3dabb56
children cef37cc093f2
files artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelDescriptionBuilder.java
diffstat 1 files changed, 148 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelDescriptionBuilder.java	Tue Mar 06 17:05:13 2018 +0100
@@ -0,0 +1,148 @@
+/** 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.exports;
+
+import java.text.NumberFormat;
+
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.WINFOArtifact;
+import org.dive4elements.river.artifacts.model.WQKms;
+import org.dive4elements.river.artifacts.resources.Resources;
+import org.dive4elements.river.utils.Formatter;
+import org.dive4elements.river.utils.RiverUtils;
+import org.dive4elements.river.utils.RiverUtils.WQ_MODE;
+
+/**
+ * Helper that encapsulates the logic how the 'description' column in waterlevel exporting is generated.
+ * TODO: this class should also be used in WaterlevelExport (all code is copied from there), but this would involve
+ * heavy testing and we leave this to the one who is responsible to clean up this mess.
+ *
+ * @author Gernot Belger
+ */
+public final class WaterlevelDescriptionBuilder {
+
+    private static final String CSV_Q_DESC_HEADER = "export.waterlevel.csv.header.q.desc";
+
+    // FIXME: missing in resource-files! hence always the default is used...
+    private static final String CSV_W_DESC_HEADER = "export.waterlevel.csv.header.w.desc";
+
+    private static final String DEFAULT_CSV_Q_DESC_HEADER = "Bezeichnung";
+
+    private static final String DEFAULT_CSV_W_DESC_HEADER = "W/Pegel [cm]";
+
+    private final boolean isQ;
+
+    private final boolean atGauge;
+
+    private final CallContext context;
+
+    private final WQ_MODE mode;
+
+    private final D4EArtifact artifact;
+
+    public WaterlevelDescriptionBuilder(final D4EArtifact artifact, final CallContext context) {
+        this.context = context;
+
+        // REMARK: taken from WaterlevelExporter, should be moved into WInfoArtifact
+        this.mode = RiverUtils.getWQMode(artifact);
+        this.atGauge = this.mode == WQ_MODE.QGAUGE || this.mode == WQ_MODE.WGAUGE;
+        this.isQ = this.mode == WQ_MODE.QGAUGE || this.mode == WQ_MODE.QFREE;
+
+        this.artifact = artifact;
+    }
+
+    public boolean isAtGauge() {
+        return this.atGauge;
+    }
+
+    public String getColumnHeader() {
+
+        if (!this.atGauge)
+            return null;
+
+        // REMARK: bad inter-dependency to WaterlevelExporter, but we want to really copy the logic from WInfo
+        if (this.isQ)
+            return Resources.getMsg(this.context.getMeta(), CSV_Q_DESC_HEADER, DEFAULT_CSV_Q_DESC_HEADER);
+
+        return Resources.getMsg(this.context.getMeta(), CSV_W_DESC_HEADER, DEFAULT_CSV_W_DESC_HEADER);
+    }
+
+    public String getDesc(final WQKms wqkms) {
+        return getDesc(wqkms, this.isQ);
+    }
+
+    public String getDesc(final WQKms wqkms, final boolean isQoverride) {
+        String colDesc = "";
+
+        if (this.artifact instanceof WINFOArtifact && isQoverride) {
+            colDesc = getCSVRowTitle((WINFOArtifact) this.artifact, wqkms);
+        } else if (!isQoverride) {
+            final Double value = RiverUtils.getValueFromWQ(wqkms);
+            colDesc = (value != null) ? Formatter.getWaterlevelW(this.context).format(value) : null;
+        }
+
+        if (this.artifact instanceof WINFOArtifact) {
+            if (wqkms != null && wqkms.getRawValue() != null) {
+                final WINFOArtifact winfo = (WINFOArtifact) this.artifact;
+                colDesc = RiverUtils.getNamedMainValue(winfo, wqkms.getRawValue());
+                // For 'W am Pegel' s
+                if (colDesc == null) {
+                    final Double value = RiverUtils.getValueFromWQ(wqkms);
+                    colDesc = (value != null) ? Formatter.getWaterlevelW(this.context).format(value) : null;
+                }
+            }
+        }
+
+        if (colDesc == null)
+            return "";
+
+        /*
+         * Quick hack. Can be removed when database strings are
+         * adapted or left in here as it should never be harmful.
+         */
+        return colDesc.replace("Amtl.Festlegung_", "Amtl. ");
+    }
+
+    private String getCSVRowTitle(final WINFOArtifact winfo, final WQKms wqkms) {
+
+        if (this.mode == WQ_MODE.WFREE || this.mode == WQ_MODE.QGAUGE)
+            return localizeWQKms(wqkms);
+
+        final Double v = wqkms.getRawValue();
+
+        final String nmv = RiverUtils.getNamedMainValue(winfo, v);
+
+        if (nmv != null && nmv.length() > 0) {
+            return RiverUtils.stripNamedMainValue(nmv);
+        }
+
+        return localizeWQKms(wqkms);
+    }
+
+    /**
+     * Get a string like 'W=' or 'Q=' with a number following in localized
+     * format.
+     */
+    private String localizeWQKms(final WQKms wqkms) {
+        final Double rawValue = wqkms.getRawValue();
+
+        if (rawValue == null) {
+            return wqkms.getName();
+        }
+
+        final NumberFormat nf = Formatter.getRawFormatter(this.context);
+
+        if (this.mode == WQ_MODE.WFREE || this.mode == WQ_MODE.WGAUGE)
+            return "W=" + nf.format(rawValue);
+
+        return "Q=" + nf.format(rawValue);
+    }
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org