changeset 8613:39ce099a6551

(issue1755) Add good defaults for bedquality distance selection
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 24 Mar 2015 18:17:23 +0100
parents b5854a9b101c
children e7a2234e7c3f
files artifacts/doc/conf/artifacts/minfo.xml artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityDistanceSelect.java artifacts/src/main/java/org/dive4elements/river/utils/KMIndex.java
diffstat 3 files changed, 96 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/doc/conf/artifacts/minfo.xml	Tue Mar 24 15:22:16 2015 +0100
+++ b/artifacts/doc/conf/artifacts/minfo.xml	Tue Mar 24 18:17:23 2015 +0100
@@ -41,7 +41,7 @@
 
         <transition transition="org.dive4elements.river.artifacts.transitions.ValueCompareTransition">
             <from state="state.minfo.calculation_mode"/>
-            <to state="state.minfo.distance"/>
+            <to state="state.minfo.bed.distance"/>
             <condition data="calculation_mode" value="calc.bed.quality" operator="equal"/>
         </transition>
 
@@ -84,7 +84,7 @@
         </transition>
 
         <transition transition="org.dive4elements.river.artifacts.transitions.DefaultTransition">
-            <from state="state.minfo.distance"/>
+            <from state="state.minfo.bed.distance"/>
             <to state="state.minfo.bed.periods"/>
             <condition data="calculation_mode" value="calc.bed.quality" operator="equal"/>
         </transition>
@@ -113,6 +113,12 @@
             <data name="periods" type="String"/>
         </state>
 
+        <state id="state.minfo.bed.distance" description="state.minfo.distance" state="org.dive4elements.river.artifacts.states.minfo.BedQualityDistanceSelect" helpText="help.state.minfo.distance">
+            <data name="ld_from" type="Double" />
+            <data name="ld_to"   type="Double" />
+            <data name="ld_step" type="Double" />
+        </state>
+
         <transition transition="org.dive4elements.river.artifacts.transitions.DefaultTransition">
             <from state="state.minfo.dischargestate"/>
             <to state="state.minfo.flow_velocity"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityDistanceSelect.java	Tue Mar 24 18:17:23 2015 +0100
@@ -0,0 +1,80 @@
+/* Copyright (C) 2011, 2012, 2013, 2015 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * 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.states.minfo;
+
+import java.util.Date;
+import java.util.List;
+
+import org.w3c.dom.Element;
+
+import org.apache.log4j.Logger;
+
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.RiverAccess;
+import org.dive4elements.river.artifacts.states.DistanceSelect;
+import org.dive4elements.river.utils.KMIndex;
+
+import org.dive4elements.river.artifacts.model.minfo.BedOverview;
+import org.dive4elements.river.artifacts.model.minfo.BedOverviewFactory;
+
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+
+import org.dive4elements.artifactdatabase.data.StateData;
+
+/** Extended Distance Select with default values appropiate for BedQuality */
+public class BedQualityDistanceSelect extends DistanceSelect {
+
+    private static Logger log = Logger.getLogger(BedQualityDistanceSelect.class);
+
+    /**
+     * The default constructor that initializes an empty State object.
+     */
+    public BedQualityDistanceSelect() {
+    }
+
+    @Override
+    protected Element createData(
+        XMLUtils.ElementCreator cr,
+        Artifact    artifact,
+        StateData   data,
+        CallContext context)
+    {
+        Element ele = super.createData(cr, artifact, data, context);
+        if (!data.getName().equals("ld_from") && !data.getName().equals("ld_to")) {
+            return ele;
+        }
+
+        D4EArtifact arti = (D4EArtifact) artifact;
+        RiverAccess access = new RiverAccess(arti);
+
+        /* Bit of an overkill here but it will be cached and accessed
+         * in the next state anyway. */
+        BedOverview overview = BedOverviewFactory.getOverview(access.getRiverName());
+
+        if (overview == null) {
+            log.warn("No overview found for river '" + access.getRiverName() + "'");
+            return ele;
+        }
+
+        KMIndex<List<Date>> entries = overview.filter(BedOverview.ACCEPT);
+        entries.sort();
+
+        if (data.getName().equals("ld_from")) {
+            cr.addAttr(ele, "defaultLabel", data.getName(), true);
+            cr.addAttr(ele, "defaultValue", Double.toString(entries.get(0).getKm()), true);
+        } else if (data.getName().equals("ld_to")) {
+            cr.addAttr(ele, "defaultLabel", data.getName(), true);
+            cr.addAttr(ele, "defaultValue", Double.toString(entries.get(entries.size()-1).getKm()), true);
+        }
+
+        return ele;
+    }
+}
--- a/artifacts/src/main/java/org/dive4elements/river/utils/KMIndex.java	Tue Mar 24 15:22:16 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/KMIndex.java	Tue Mar 24 18:17:23 2015 +0100
@@ -80,6 +80,14 @@
         Collections.sort(entries);
     }
 
+    public int size() {
+        return entries.size();
+    }
+
+    public Entry<A> get(int idx) {
+        return entries.get(idx);
+    }
+
     /** Return the first entry at km. */
     public Entry<A> search(double km) {
         for (Entry<A> entry: entries) {

http://dive4elements.wald.intevation.org