changeset 8625:362abb64e897

Add default value to BedQuality period selection
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 26 Mar 2015 11:51:47 +0100
parents 4f702ea1fd3a
children 3b85c8cee94b
files artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityPeriodsSelect.java
diffstat 1 files changed, 83 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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";

http://dive4elements.wald.intevation.org