# HG changeset patch # User Tim Englich # Date 1257954871 0 # Node ID f1e7ddeef5bc8ad7a1a67f53ee373d6243584dac # Parent 113b06ab211026151de08ed1ddc2d46f6aa980fc Added Validation if a given maxvalue is greater than a given minvalue issue19 gnv-artifacts/trunk@323 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 113b06ab2110 -r f1e7ddeef5bc gnv-artifacts/ChangeLog --- 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 + * 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 * 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 diff -r 113b06ab2110 -r f1e7ddeef5bc gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java --- 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( 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, String inputName){ + Iterator 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"); diff -r 113b06ab2110 -r f1e7ddeef5bc gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java --- 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;