diff gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/WaterlevelSoundingYearFilter.java @ 9227:84397da33d17

Allow to control specific behaviour in TwinDatacagePanel Implemented client logic of 'intelligent datacage filtering' for SINFO
author gernotbelger
date Wed, 04 Jul 2018 18:28:08 +0200
parents
children 082e8e808902
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/WaterlevelSoundingYearFilter.java	Wed Jul 04 18:28:08 2018 +0200
@@ -0,0 +1,77 @@
+/** 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.client.client.ui.sinfo;
+
+import org.dive4elements.river.client.client.ui.DatacageWidget.DatacageFilter;
+import org.dive4elements.river.client.shared.model.AttrList;
+import org.dive4elements.river.client.shared.model.DataCageNode;
+
+/**
+ * @author Gernot Belger
+ */
+final class WaterlevelSoundingYearFilter implements DatacageFilter {
+
+    private final int soundingYear;
+
+    public WaterlevelSoundingYearFilter(final int soundingYear) {
+        this.soundingYear = soundingYear;
+    }
+
+    @Override
+    public boolean accept(final DataCageNode node) {
+
+        final AttrList attributes = node.getAttributes();
+        if (attributes == null)
+            return true;
+
+        if (!attributes.hasAttribute("factory"))
+            return true;
+
+        final Integer waterlevelYear = getYear(attributes);
+        if (waterlevelYear == null) {
+            // should never happen, we should only show waterlevels that have a year
+            return false;
+        }
+
+        final int diff = Math.abs(this.soundingYear - waterlevelYear);
+
+        return diff <= getMaxYearDifference();
+    }
+
+    private int getMaxYearDifference() {
+
+        if (this.soundingYear < 1918)
+            return 25;
+
+        if (1918 <= this.soundingYear && this.soundingYear < 1958)
+            return 12;
+
+        if (1958 <= this.soundingYear && this.soundingYear < 1998)
+            return 6;
+
+        // > 1998
+        return 3;
+    }
+
+    private static Integer getYear(final AttrList attributes) {
+
+        final String year = attributes.getValue(WaterlevelSoundingSelectionController.ATTRIBUTE_YEAR);
+        if (year == null || year.trim().isEmpty())
+            return null;
+
+        try {
+            return Integer.valueOf(year);
+        }
+        catch (final NumberFormatException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org