[PATCH 4 of 9] Add default value to BedQuality period selection

Wald Commits scm-commit at wald.intevation.org
Thu Mar 26 12:11:58 CET 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1427367107 -3600
# Node ID 362abb64e8977c0b96ce4c3f7114a967db40aaa6
# Parent  4f702ea1fd3a99a9e33d40774e563699d6a7ab17
Add default value to BedQuality period selection

diff -r 4f702ea1fd3a -r 362abb64e897 artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityPeriodsSelect.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityPeriodsSelect.java	Thu Mar 26 11:46:03 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityPeriodsSelect.java	Thu Mar 26 11:51:47 2015 +0100
@@ -8,10 +8,32 @@
 
 package org.dive4elements.river.artifacts.states.minfo;
 
+import java.util.List;
+import java.util.Date;
+import java.util.TreeSet;
+
 import org.apache.log4j.Logger;
 
 import org.dive4elements.river.artifacts.states.DefaultState;
 
+import org.w3c.dom.Element;
+
+import org.dive4elements.river.artifacts.access.RangeAccess;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.model.minfo.BedOverview;
+import org.dive4elements.river.artifacts.model.minfo.BedOverviewFactory;
+import org.dive4elements.river.artifacts.model.minfo.BedloadOverview;
+import org.dive4elements.river.artifacts.model.minfo.BedloadOverviewFactory;
+
+import org.dive4elements.river.utils.KMIndex;
+
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+
+import org.dive4elements.artifactdatabase.data.StateData;
+
+
 public class BedQualityPeriodsSelect extends DefaultState {
 
     /** The log used in this class. */
@@ -24,6 +46,67 @@
     public BedQualityPeriodsSelect() {
     }
 
+    /** Get either the start and end date of the data at the current position. */
+    protected Long[] getDataMinMaxDate(Artifact artifact) {
+        D4EArtifact arti = (D4EArtifact) artifact;
+        RangeAccess access = new RangeAccess(arti);
+        double a = access.getFrom();
+        double b = access.getTo();
+
+        if (a > b) {
+            double buf = a;
+            a = b;
+            b = buf;
+        }
+
+        BedOverview overview = BedOverviewFactory.getOverview(access.getRiverName());
+        BedloadOverview overview2 = BedloadOverviewFactory.getOverview(access.getRiverName());
+
+        /* Filter is not implemented and only checks if a complete
+         * KMIndex list is acceptable or not. So KMFiltering wont work */
+        KMIndex<List<Date>> entries = overview.filter(BedOverview.ACCEPT);
+        KMIndex<List<Date>> loads = overview2.filter(BedloadOverview.ACCEPT);
+        TreeSet<Date> allDates = new TreeSet<Date>();
+
+        for (int i = 0; i < entries.size(); i++) {
+            if (entries.get(i).getKm() >= a && entries.get(i).getKm() <= b) {
+                allDates.addAll(entries.get(i).getValue());
+            }
+        }
+        for (int i = 0; i < loads.size(); i++) {
+            if (loads.get(i).getKm() >= a && loads.get(i).getKm() <= b) {
+                allDates.addAll(loads.get(i).getValue());
+            }
+        }
+        if (allDates.size() < 2) {
+            return null;
+        }
+
+        return new Long[] {allDates.first().getTime(),
+                           allDates.last().getTime()};
+    }
+
+    @Override
+    protected Element[] createItems(
+        XMLUtils.ElementCreator cr,
+        Artifact    artifact,
+        String      name,
+        CallContext context)
+    {
+        if (!name.equals("periods")) {
+            return null;
+        }
+        Long[] values = getDataMinMaxDate(artifact);
+        if (values == null) {
+            return null;
+        }
+        Element def = createItem(
+            cr,
+            new String[] {"default", values[0].toString() + "," + values[1].toString()});
+
+        return new Element[] { def };
+    }
+
     @Override
     protected String getUIProvider() {
         return "bedquality_periods_select";


More information about the Dive4Elements-commits mailing list