Mercurial > dive4elements > gnv-client
changeset 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 | 3509735619e7 |
children | e764cab8c885 |
files | gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java gnv-artifacts/src/main/resources/lang/artifactMessages.properties gnv-artifacts/src/main/resources/lang/artifactMessages_de.properties gnv-artifacts/src/main/resources/lang/artifactMessages_de_DE.properties gnv-artifacts/src/main/resources/lang/artifactMessages_en.properties |
diffstat | 7 files changed, 111 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/ChangeLog Tue Mar 30 09:17:53 2010 +0000 @@ -1,3 +1,22 @@ +2010-03-30 Ingo Weinzierl <ingo.weinzierl@intevation.de> + + Issue212 (Improved input validation) + + * src/main/java/de/intevation/gnv/state/MinMaxDateState.java: Improved date + input validation. The chosen date needs to be in range between upper and + lower date, otherwise an error message is displayed and the user needs to + correct his choice. + + * src/main/java/de/intevation/gnv/utils/InputValidator.java: Added a new + method to validate a given date being in range between an upper and a + lower date bound. + + * src/main/resources/lang/artifactMessages.properties, + src/main/resources/lang/artifactMessages_de_DE.properties, + src/main/resources/lang/artifactMessages_en.properties, + src/main/resources/lang/artifactMessages_de.propertie: Added error + messages for failures while date validation. + 2010-03-29 Ingo Weinzierl <ingo.weinzierl@intevation.de> Issue192
--- 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));
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java Tue Mar 30 09:17:53 2010 +0000 @@ -163,4 +163,22 @@ } } + + public boolean isDateValid(Date tmp, Date lo, Date up) { + // we need to transform the given dates into seconds, because + // they differ in milliseconds -> that's why we cannot use the + // Date.compareTo(Date) method. + long tmpTime = tmp.getTime() / 1000; + long tmpLow = lo.getTime() / 1000; + long tmpUp = up.getTime() / 1000; + + if (tmpTime < tmpLow || tmpTime > tmpUp) { + log.warn( + "Date [" + tmp.toString() + "] is out of range [" + + lo.toString() + " to "+ up.toString() + "]."); + return false; + } + + return true; + } }
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages.properties Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages.properties Tue Mar 30 09:17:53 2010 +0000 @@ -76,3 +76,5 @@ input.is.not.valid=Invalid input. Please try again. input.is.not.valid.date.required=Invalid input. A date is required. Please try again. start.date.after.end.date=Start date lies after end date. Please try again. +date.out.of.range=Date is out of range. +missing.data.field=Can't validate selected date. Missing date, lower or upper date bound.
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages_de.properties Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages_de.properties Tue Mar 30 09:17:53 2010 +0000 @@ -76,3 +76,5 @@ input.is.not.valid=Ung\u00fcltige Eingabe. Bitte versuchen Sie es erneut. input.is.not.valid.date.required=Ung\u00fcltige Eingabe. Ein Datum wird ben\u00f6tigt. Bitte versuchen Sie es erneut. start.date.after.end.date=Anfangsdatum liegt hinter dem Enddatum. Bitte versuchen Sie es erneut. +date.out.of.range=Das gew\u00e4hlte Datum liegt au\u00dferhalb des gültigen Wertebereiches. +missing.data.field=Validierung der Datumseingabe nicht m\u00f6glich. Datum, obere oder untere Grenze fehlt.
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages_de_DE.properties Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages_de_DE.properties Tue Mar 30 09:17:53 2010 +0000 @@ -76,3 +76,5 @@ input.is.not.valid=Ung\u00fcltige Eingabe. Bitte versuchen Sie es erneut. input.is.not.valid.date.required=Ung\u00fcltige Eingabe. Ein Datum wird ben\u00f6tigt. Bitte versuchen Sie es erneut. start.date.after.end.date=Anfangsdatum liegt nach dem Enddatum. Bitte versuchen Sie es erneut. +date.out.of.range=Das gew\u00e4hlte Datum liegt au\u00dferhalb des gültigen Wertebereiches. +missing.data.field=Validierung der Datumseingabe nicht m\u00f6glich. Datum, obere oder untere Grenze fehlt.
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages_en.properties Mon Mar 29 12:56:13 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages_en.properties Tue Mar 30 09:17:53 2010 +0000 @@ -77,3 +77,5 @@ input.is.not.valid=Invalid input. Please try again. input.is.not.valid.date.required=Invalid input. A date is required. Please try again. start.date.after.end.date=Start date lies after end date. Please try again. +date.out.of.range=Date is out of range. +missing.data.field=Can't validate selected date. Missing date, lower or upper date bound.