changeset 322:448d0dc64357

The inserted ranges (distance and WQ ranges) are validated in the feed() operation. flys-artifacts/trunk@1716 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 18 Apr 2011 12:36:08 +0000 (2011-04-18)
parents 9581b88f2920
children b33ba2cd4a3e
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java
diffstat 6 files changed, 247 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Apr 18 09:34:18 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Apr 18 12:36:08 2011 +0000
@@ -1,3 +1,28 @@
+2011-04-18  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java:
+	  The input data of feed() are validated using the DefaultStates
+	  validate() method.
+
+	* src/main/java/de/intevation/flys/artifacts/states/DefaultState.java:
+	  New method validate() that needs to be overidden by concrete subclasses.
+	  It should return true, if the data of the State is fine, otherwise it
+	  should raise an exception.
+
+	  NOTE: The exceptions are not translated in the server but in the client!
+
+	* src/main/java/de/intevation/flys/artifacts/states/RangeState.java: New.
+	  This abstract class exists to provide some methods for handling ranges.
+	  Currently, there is a method that validates a given range based on
+	  min/max values.
+
+	* src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java,
+	  src/main/java/de/intevation/flys/artifacts/states/WQSelect.java:
+	  Implemented input data validation for ranges.
+
+	  NOTE: The input validation of concrete values has not been implemented
+	  yet!
+
 2011-04-18  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Mon Apr 18 09:34:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Mon Apr 18 12:36:08 2011 +0000
@@ -33,6 +33,7 @@
 
 import de.intevation.flys.artifacts.context.FLYSContext;
 import de.intevation.flys.artifacts.model.RiverFactory;
+import de.intevation.flys.artifacts.states.DefaultState;
 
 
 /**
@@ -161,14 +162,12 @@
         doc.appendChild(result);
 
         try {
-            saveData(target, XPATH_FEED_INPUT);
+            saveData(target, XPATH_FEED_INPUT, context);
             return describe(target, context);
         }
         catch (IllegalArgumentException iae) {
             creator.addAttr(result, "type", OPERATION_FAILED, true);
 
-            // TODO I18N this message - getMessage() returns a lookup string, no
-            // human readable error message
             result.setTextContent(iae.getMessage());
         }
 
@@ -324,7 +323,7 @@
      * @param feed The FEED document.
      * @param xpath The XPath that points to the data nodes.
      */
-    public void saveData(Document feed, String xpath)
+    public void saveData(Document feed, String xpath, CallContext context)
     throws IllegalArgumentException
     {
         if (feed == null || xpath == null || xpath.length() == 0) {
@@ -355,10 +354,14 @@
             if (name != null && value != null) {
                 logger.debug("Save data item for '" + name + "' : " + value);
 
-                // TODO ADD INPUT VALIDATION!
                 addData(name, new DefaultStateData(name, null, null, value));
             }
         }
+
+        State current           = getCurrentState(context);
+        DefaultState toValidate = (DefaultState) fillState(current);
+
+        toValidate.validate(this, context);
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java	Mon Apr 18 09:34:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java	Mon Apr 18 12:36:08 2011 +0000
@@ -184,6 +184,22 @@
         CallContext context);
 
 
+    /**
+     * This method validates the inserted data and returns true, if everything
+     * was correct, otherwise an exception is thrown.
+     *
+     * @param artifact A reference to the owner artifact.
+     * @param context The CallContext object.
+     *
+     * @return true, if everything was fine.
+     */
+    public boolean validate(Artifact artifact, CallContext context)
+    throws IllegalArgumentException
+    {
+        return true;
+    }
+
+
     protected String getUIProvider() {
         return null;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java	Mon Apr 18 09:34:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java	Mon Apr 18 12:36:08 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.Map;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Element;
@@ -21,7 +23,7 @@
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class LocationDistanceSelect extends DefaultState {
+public class LocationDistanceSelect extends RangeState {
 
     /** The logger used in this class.*/
     private static Logger logger = Logger.getLogger(LocationDistanceSelect.class);
@@ -30,6 +32,15 @@
     /** The default step width.*/
     public static final String DEFAULT_STEP = "100";
 
+    /** The name of the 'from' field. */
+    public static final String FROM = "ld_from";
+
+    /** The name of the 'to' field. */
+    public static final String TO = "ld_to";
+
+    /** The name of the 'step' field. */
+    public static final String STEP = "ld_step";
+
     /**
      * The default constructor that initializes an empty State object.
      */
@@ -139,5 +150,32 @@
 
         return river != null ? river.determineMinMaxDistance() : null;
     }
+
+
+    @Override
+    public boolean validate(Artifact artifact, CallContext context)
+    throws IllegalArgumentException
+    {
+        logger.debug("LocationDistanceSelect.validate");
+
+        Map<String, StateData> data = getData();
+
+        String fromStr = (String) data.get(FROM).getValue();
+        String toStr   = (String) data.get(TO).getValue();
+        String stepStr = (String) data.get(STEP).getValue();
+
+        try {
+            double from = Double.parseDouble(fromStr);
+            double to   = Double.parseDouble(toStr);
+            double step = Double.parseDouble(stepStr);
+
+            double[] minmaxDist = getMinMaxDistance(artifact);
+
+            return validateRange(minmaxDist[0], minmaxDist[1], from, to, step);
+        }
+        catch (NumberFormatException nfe) {
+            throw new IllegalArgumentException("error_feed_number_format");
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java	Mon Apr 18 12:36:08 2011 +0000
@@ -0,0 +1,54 @@
+package de.intevation.flys.artifacts.states;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public abstract class RangeState extends DefaultState {
+
+    /** The logger that is used in this class.*/
+    private static Logger logger = Logger.getLogger(RangeState.class);
+
+
+    public RangeState() {
+    }
+
+
+    /**
+     * Validates a given range with a given valid range.
+     *
+     * @param fromValid Valid lower value of the range.
+     * @param toValid Valid upper value of the range.
+     * @param from The lower value.
+     * @param to The upper value.
+     * @param step The step width.
+     *
+     * @return true, if everything was fine, otherwise an exception is thrown.
+     */
+    protected boolean validateRange(
+        double fromValid, double toValid,
+        double from,      double to,      double step)
+    throws IllegalArgumentException
+    {
+        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");
+        }
+        else if (from > to) {
+            throw new IllegalArgumentException("error_feed_from_bigger_to");
+        }
+
+        return true;
+    }
+}
+// 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/WQSelect.java	Mon Apr 18 09:34:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java	Mon Apr 18 12:36:08 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.Map;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Element;
@@ -23,7 +25,7 @@
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class WQSelect extends DefaultState {
+public class WQSelect extends RangeState {
 
     /** The logger used in this class.*/
     private static Logger logger = Logger.getLogger(WQSelect.class);
@@ -35,6 +37,18 @@
     /** The default step width for Qs.*/
     public static final String DEFAULT_STEP_W = "30";
 
+    /** The name of the 'mode' field. */
+    public static final String WQ_MODE = "wq_mode";
+
+    /** The name of the 'from' field. */
+    public static final String WQ_FROM = "wq_from";
+
+    /** The name of the 'to' field. */
+    public static final String WQ_TO = "wq_to";
+
+    /** The name of the 'step' field. */
+    public static final String WQ_STEP = "wq_step";
+
     /**
      * The default constructor that initializes an empty State object.
      */
@@ -172,5 +186,95 @@
 
         return new double[] { minQ, maxQ };
     }
+
+
+    @Override
+    public boolean validate(Artifact artifact, CallContext context)
+    throws IllegalArgumentException
+    {
+        logger.debug("WQSelect.validate");
+
+        Map<String, StateData> data = getData();
+
+        String mode = (String) data.get(WQ_MODE).getValue();
+        logger.debug("WQ Mode: " + mode);
+
+        String fromStr = (String) data.get(WQ_FROM).getValue();
+        String toStr   = (String) data.get(WQ_TO).getValue();
+        String stepStr = (String) data.get(WQ_STEP).getValue();
+
+        double from = Double.parseDouble(fromStr);
+        double to   = Double.parseDouble(toStr);
+        double step = Double.parseDouble(stepStr);
+
+        try {
+            if (mode != null && mode.trim().toLowerCase().equals("w")) {
+                return validateW(artifact, context, from, to, step);
+            }
+            else if (mode != null && mode.trim().toLowerCase().equals("q")) {
+                return validateQ(artifact, context, from, to, step);
+            }
+            else {
+                throw new IllegalArgumentException("error_feed_invalid_wq_mode");
+            }
+        }
+        catch (NumberFormatException nfe) {
+            throw new IllegalArgumentException("error_feed_number_format");
+        }
+    }
+
+
+    /**
+     * Validates the inserted W values.
+     *
+     * @param artifact The owner artifact.
+     * @param context The CallContext
+     * @param from The lower value of the W range.
+     * @param to The upper value of the W range.
+     * @param step The step width.
+     *
+     * @return true, if everything was fine, otherwise an exception is thrown.
+     */
+    protected boolean validateW(
+        Artifact    artifact,
+        CallContext context,
+        double from,
+        double to,
+        double step)
+    throws    IllegalArgumentException
+    {
+        logger.debug("WQSelect.validateW");
+
+        double[] minmaxW = determineMinMaxW(artifact);
+
+        return validateRange(minmaxW[0], minmaxW[1], from, to, step);
+    }
+
+
+    /**
+     * Validates the inserted Q values.
+     *
+     * @param artifact The owner artifact.
+     * @param context The CallContext
+     * @param from The lower value of the Q range.
+     * @param to The upper value of the Q range.
+     * @param step The step width.
+     *
+     * @return true, if everything was fine, otherwise an exception is thrown.
+     */
+    protected boolean validateQ(
+        Artifact    artifact,
+        CallContext context,
+        double from,
+        double to,
+        double step)
+    throws    IllegalArgumentException
+    {
+        logger.debug("WQSelect.validateQ");
+
+        double[] minmaxQ = determineMinMaxQ(artifact);
+
+        return validateRange(minmaxQ[0], minmaxQ[1], from, to, step);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org