diff artifacts/src/main/java/org/dive4elements/river/artifacts/common/EpochYearAccessHelper.java @ 9070:611a523fc42f

VegetationZoneAccessHelper, VegetationTablePanels verbessert
author gernotbelger
date Tue, 15 May 2018 18:04:36 +0200
parents artifacts/src/main/java/org/dive4elements/river/artifacts/common/AccessHelper.java@1ffd38826175
children 9b4f5e61c02e
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/EpochYearAccessHelper.java	Tue May 15 18:04:36 2018 +0200
@@ -0,0 +1,106 @@
+/** 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 org.apache.log4j.Logger;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.Access;
+
+import gnu.trove.TIntArrayList;
+
+/**
+ * @author Domenico Nardi Tironi
+ *
+ */
+// TODO: rename?
+public class EpochYearAccessHelper extends Access {
+
+    private static Logger log = Logger.getLogger(EpochYearAccessHelper.class);
+
+    // private final Artifact artifact;
+
+    public EpochYearAccessHelper(final Artifact artifact) {
+        this.artifact = (D4EArtifact) artifact;
+    }
+
+    public String getYearEpoch() {
+        return getString("ye_select"); // ACHTUNG, Ergebniswerte wurden geändert in state.sinfo.epoch und state.sinfo.year
+    }
+
+    /** [year1, years2,..] if its about years. */
+    public int[] getYears(final String yearSelectedValue, final String yearKey) {
+        int[] years = null;
+        // if (years != null) {
+        // return years;
+        // }
+        if (getYearEpoch().equals(yearSelectedValue)) {
+            final TIntArrayList ints = new TIntArrayList();
+            final String yearsData = getString(yearKey);
+            if (yearsData == null || yearsData.isEmpty()) {
+                log.warn("No years provided");
+                return null;
+            }
+            for (final String sValue : yearsData.split(" ")) {
+                try {
+                    ints.add(Integer.parseInt(sValue));
+                }
+                catch (final NumberFormatException e) {
+                    /* Client should prevent this */
+                    log.warn("Invalid year value: " + sValue);
+                    continue;
+                }
+            }
+
+            if (!ints.isEmpty()) {
+                ints.sort();
+                years = ints.toNativeArray();
+            }
+            return years;
+        }
+        return null;
+    }
+
+    public int[][] getEpochs(final String epochSelectedValue, final String epochKey) {
+        int epochs[][] = null;
+        // if (epochs != null) {
+        // return epochs;
+        // }
+
+        if (!getYearEpoch().equals(epochSelectedValue)) {
+            return null;
+        }
+
+        final String data = getString(epochKey);
+
+        if (data == null) {
+            log.warn("No 'epochs' parameter specified!");
+            return null;
+        }
+
+        final String[] parts = data.split(";");
+
+        epochs = new int[parts.length][];
+
+        for (int i = 0; i < parts.length; i++) {
+            final String[] values = parts[i].split(",");
+            final TIntArrayList ints = new TIntArrayList();
+            try {
+                ints.add(Integer.parseInt(values[0]));
+                ints.add(Integer.parseInt(values[1]));
+                epochs[i] = ints.toNativeArray();
+            }
+            catch (final NumberFormatException nfe) {
+                log.warn("Cannot parse int from string: '" + values + "'");
+            }
+        }
+        return epochs;
+    }
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org