Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java @ 784:7b81d563c8c0
Improved date input validation. A given date needs to be in a specific range (issue212).
gnv-artifacts/trunk@866 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 30 Mar 2010 09:17:53 +0000 |
parents | c4156275c1e1 |
children | a5526908f92f |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java Tue Mar 30 09:17:53 2010 +0000 @@ -4,14 +4,19 @@ import de.intevation.gnv.artifacts.ressource.RessourceFactory; +import de.intevation.gnv.geobackend.util.DateUtils; + import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData; import de.intevation.gnv.state.describedata.DescribeData; +import de.intevation.gnv.state.describedata.MinMaxDescribeData; import de.intevation.gnv.state.exception.StateException; import de.intevation.gnv.utils.InputValidator; +import java.util.GregorianCalendar; import java.util.Collection; +import java.util.Date; import java.util.Iterator; import java.util.Locale; @@ -31,6 +36,12 @@ public static final String EXCEPTION_START_AFTER_END = "start.date.after.end.date"; + public static final String EXCEPTION_DATE_OUT_OF_RANGE = + "date.out.of.range"; + + public static final String EXCEPTION_MISSING_DATE = + "missing.data.field"; + private static Logger logger = Logger.getLogger(MinMaxDateState.class); @@ -63,8 +74,13 @@ InputValidator iv = new InputValidator(); Iterator iter = inputData.iterator(); - Object min = null; - Object max = null; + MinMaxDescribeData data = + (MinMaxDescribeData) getDescibeData(uuid).get(0); + Object min = data.getMinValue(); + Object max = data.getMaxValue(); + + Object tmpMin = null; + Object tmpMax = null; while (iter.hasNext()) { InputData tmp = (InputData) iter.next(); @@ -79,24 +95,65 @@ return feedFailure(msg); } - boolean valid = iv.isInputValid(value, type); - if (!valid) { + if (!iv.isInputValid(value, type)) { String msg = resFactory.getRessource( locale, EXCEPTION_DATE_REQUIRED, EXCEPTION_DATE_REQUIRED); logger.error(msg); return feedFailure(msg); } + Date lower = null; + if (min instanceof GregorianCalendar) { + lower = ((GregorianCalendar)min).getTime(); + } + + Date upper = null; + if (max instanceof GregorianCalendar) { + upper = ((GregorianCalendar)max).getTime(); + } + + Date d = null; + try { + d = DateUtils.getDateFromString(value,DateUtils.DATE_PATTERN); + } + catch (Exception e) { + logger.warn(e, e); + } + + if (d == null || lower == null || upper == null) { + String msg = resFactory.getRessource( + locale, + EXCEPTION_MISSING_DATE, + EXCEPTION_MISSING_DATE); + logger.warn(msg); + } + else { + if (logger.isDebugEnabled()) { + logger.debug("Date to validate: " + d.toString()); + logger.debug("Lower date bound: " + lower.toString()); + logger.debug("Upper date bound: " + upper.toString()); + } + + if (!iv.isDateValid(d, lower, upper)) { + String msg = resFactory.getRessource( + locale, + EXCEPTION_DATE_OUT_OF_RANGE, + EXCEPTION_DATE_OUT_OF_RANGE); + logger.error(msg); + return feedFailure(msg); + } + } + if (name.equals(MINVALUEFIELDNAME)) { - min = value; + tmpMin = value; } if (name.equals(MAXVALUEFIELDNAME)) { - max = value; + tmpMax = value; } - if (min != null && max != null) { - if (!iv.isInputValid((String) min, (String) max, type)) { + if (tmpMin != null && tmpMax != null) { + if (!iv.isInputValid((String) tmpMin, (String) tmpMax, type)) { String msg = resFactory.getRessource( locale, EXCEPTION_START_AFTER_END, @@ -108,7 +165,7 @@ } DescribeData values = new DefaultMinMaxDescribeData( - dataName, min, max, getID()); + dataName, tmpMin, tmpMax, getID()); this.inputData.put(dataName, new DefaultInputData(dataName, values));