changeset 1630:cafd8af6734a

Improved bounds validation for distance only states. flys-artifacts/trunk@2806 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 22 Sep 2011 07:58:24 +0000
parents 4aec853a2f85
children 26e19cdaed5e
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DistanceOnlySelect.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java
diffstat 3 files changed, 68 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Sep 21 14:28:39 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Sep 22 07:58:24 2011 +0000
@@ -1,3 +1,13 @@
+2011-09-22  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/states/DistanceOnlySelect.java:
+	  Override validate() of parent classes to suppress "step" validation
+	  which is not present in this state.
+
+	* src/main/java/de/intevation/flys/artifacts/states/RangeState.java: Added
+	  new method validateBounds() which really just validates a boundary
+	  without "step" parameter.
+
 2011-09-21  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
  Added WaterlevelArtifact to accompany WDifferencesPlots.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DistanceOnlySelect.java	Wed Sep 21 14:28:39 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DistanceOnlySelect.java	Thu Sep 22 07:58:24 2011 +0000
@@ -2,6 +2,12 @@
 
 import org.apache.log4j.Logger;
 
+import de.intevation.artifacts.Artifact;
+
+import de.intevation.artifactdatabase.data.StateData;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
 
 public class DistanceOnlySelect extends DistanceSelect {
 
@@ -11,5 +17,35 @@
     protected String getUIProvider() {
         return "distance_only_panel";
     }
+
+
+    @Override
+    public boolean validate(Artifact artifact)
+    throws IllegalArgumentException
+    {
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+
+        StateData dFrom = getData(flys, getLowerField());
+        StateData dTo   = getData(flys, getUpperField());
+
+        String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
+        String toStr   = dTo   != null ? (String) dTo.getValue()   : null;
+
+        if (fromStr == null || toStr == null) {
+            throw new IllegalArgumentException("error_empty_state");
+        }
+
+        try {
+            double from = Double.parseDouble(fromStr);
+            double to   = Double.parseDouble(toStr);
+
+            double[] minmax = getMinMax(flys);
+
+            return validateBounds(minmax[0], minmax[1], from, to);
+        }
+        catch (NumberFormatException nfe) {
+            throw new IllegalArgumentException("error_invalid_double_value");
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java	Wed Sep 21 14:28:39 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java	Thu Sep 22 07:58:24 2011 +0000
@@ -28,6 +28,26 @@
     protected abstract double[] getMinMax(Artifact artifact);
 
 
+    protected boolean validateBounds(
+        double fromValid, double toValid,
+        double from,      double to)
+    throws IllegalArgumentException
+    {
+        if (from < fromValid) {
+            logger.error(
+                "Invalid 'from'. " + from + " is smaller than " + fromValid);
+            throw new IllegalArgumentException("error_feed_from_out_of_range");
+        }
+        else if (to > toValid) {
+            logger.error(
+                "Invalid 'to'. " + to + " is bigger than " + toValid);
+            throw new IllegalArgumentException("error_feed_to_out_of_range");
+        }
+
+        return true;
+    }
+
+
     /**
      * Validates a given range with a given valid range.
      *
@@ -46,18 +66,8 @@
     {
         logger.debug("RangeState.validateRange");
 
-        if (from < fromValid) {
-            logger.error(
-                "Invalid 'from'. " + from + " is smaller than " + fromValid);
-            throw new IllegalArgumentException("error_feed_from_out_of_range");
-        }
-        else if (to > toValid) {
-            logger.error(
-                "Invalid 'to'. " + to + " is bigger than " + toValid);
-            throw new IllegalArgumentException("error_feed_to_out_of_range");
-        }
-
-        return true;
+        // XXX The step width is not validated at the moment!
+        return validateBounds(fromValid, toValid, from, to);
     }
 
 

http://dive4elements.wald.intevation.org