changeset 252:f1e7ddeef5bc

Added Validation if a given maxvalue is greater than a given minvalue issue19 gnv-artifacts/trunk@323 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 11 Nov 2009 15:54:31 +0000
parents 113b06ab2110
children 07650fc6014c
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java
diffstat 3 files changed, 86 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Wed Nov 11 12:16:15 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Wed Nov 11 15:54:31 2009 +0000
@@ -1,5 +1,11 @@
 2009-11-11  Tim Englich  <tim.englich@intevation.de>
 
+	* src/main/java/de/intevation/gnv/transition/TransitionBase.java (putInputData): 
+	  Added Validation if a given maxvalue is greater than a given minvalue issue19
+	* src/main/java/de/intevation/gnv/utils/InputValidator.java (isInputValid): 
+	  Added new Method for validating if a maxvalue is greater than a minvalue. issue19
+	
+2009-11-11  Tim Englich  <tim.englich@intevation.de>
 	* src/main/java/de/intevation/gnv/transition/describedata/DefaultSingleValueDescribeData.java (setValue): 
 	* src/main/java/de/intevation/gnv/transition/describedata/SingleValueDescribeData.java: 
 	  Added setValue-Method for setting the Value which was sent back to the Servere 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java	Wed Nov 11 12:16:15 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java	Wed Nov 11 15:54:31 2009 +0000
@@ -246,9 +246,51 @@
                         this.inputData = new HashMap<String, InputData>(
                                 inputData.size());
                     }
+                    
                     boolean valid = iv.isInputValid(tmpItem.getValue(),
                             inputValue.getType());
                     if (valid) {
+                        if (tmpItem.getName().equals(MINVALUEFIELDNAME)){
+                            String minValue = tmpItem.getValue();
+                            String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME);
+                            valid = iv.isInputValid(maxValue,inputValue.getType());
+                            if (!valid){
+                                String errMsg = "Wrong input for " + tmpItem.getValue()
+                                                + " is not an " + inputValue.getType()
+                                                + " Value.";
+                                log.warn(errMsg);
+                                throw new TransitionException(errMsg);
+                            }
+                            
+                            valid = iv.isInputValid(minValue, 
+                                    maxValue,
+                                    inputValue.getType());
+                            if (!valid){
+                                String errMsg = "MaxValue-Input is less than MinValue-Input ";
+                                log.warn(errMsg);
+                                throw new TransitionException(errMsg);
+                            }
+                        }else if (tmpItem.getName().equals(MAXVALUEFIELDNAME)){
+                            String minValue = this.getInputValue4ID(inputData, MINVALUEFIELDNAME);
+                            String maxValue = tmpItem.getValue();
+                            valid = iv.isInputValid(minValue,inputValue.getType());
+                            if (!valid){
+                                String errMsg = "Wrong input for " + tmpItem.getValue()
+                                                + " is not an " + inputValue.getType()
+                                                + " Value.";
+                                log.warn(errMsg);
+                                throw new TransitionException(errMsg);
+                            }
+                            
+                            valid = iv.isInputValid(minValue, 
+                                                    maxValue,
+                                                    inputValue.getType());
+                            if (!valid){
+                                String errMsg = "MaxValue-Input is less than MinValue-Input ";
+                                log.warn(errMsg);
+                                throw new TransitionException(errMsg);
+                            }
+                        }
                         this.setSelection(tmpItem, uuid);
                         this.inputData.put(tmpItem.getName(), tmpItem);
                     } else {
@@ -270,6 +312,17 @@
             log.warn("No Inputdata given");
         }
     }
+    
+    private String getInputValue4ID(Collection<InputData> inputData, String inputName){
+        Iterator<InputData> it = inputData.iterator();
+        while (it.hasNext()) {
+            InputData tmpItem = it.next();
+            if (tmpItem.getName().equals(inputName)){
+                return tmpItem.getValue();
+            }
+        }
+        return null;
+    }
 
     private void setSelection(InputData inputData, String uuid) {
         log.debug("TransitionBase.setSelection");
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java	Wed Nov 11 12:16:15 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java	Wed Nov 11 15:54:31 2009 +0000
@@ -3,6 +3,8 @@
  */
 package de.intevation.gnv.utils;
 
+import java.util.Date;
+
 import org.apache.commons.validator.GenericValidator;
 import org.apache.log4j.Logger;
 
@@ -33,6 +35,31 @@
         super();
     }
 
+    public boolean isInputValid(String minInput, String maxInput, String type) {
+        log.debug("InputValidator.isInputValid " + minInput + " " + maxInput + " " +type);
+        boolean returnValue = false;
+        if ("Date".equalsIgnoreCase(type)) {
+            try {
+                Date min = DateUtils.getDateFromString(minInput,DateUtils.DATE_PATTERN);
+                Date max = DateUtils.getDateFromString(maxInput,DateUtils.DATE_PATTERN);
+                int value = max.compareTo(min);
+                returnValue = value >= 0;
+            } catch (Exception e) {
+                log.error(e,e);
+            }
+        } else if ("Double".equalsIgnoreCase(type)) {
+            try {
+                double min = Double.parseDouble(minInput);
+                double max = Double.parseDouble(maxInput);
+                returnValue = max >= min;
+            } catch (Exception e) {
+                log.error(e,e);
+            }
+        }
+        log.debug("Is valid? " + returnValue);
+        return returnValue;
+    }
+    
     public boolean isInputValid(String input, String type) {
         log.debug("InputValidator.isInputValid " + input + " " + type);
         boolean returnValue = false;

http://dive4elements.wald.intevation.org