From 21c93e2a68beb8b3b92c1f4b55a15008b7db86f3 Mon Sep 17 00:00:00 2001 From: strentini <none@none> Date: Fri, 13 Apr 2012 09:41:34 +0200 Subject: [PATCH] OO-154 : two new subclasses of DatePropertyHandler. (futureDate* and pastDate*). replace DatePropertyHandler for userPopertyBirthday with PastDatePropertyHandler ( in userPropertiesHandlersContext.xml ). two new i18nkeys for error-message added (in en and de) --- .../FutureDatePropertyHandler.java | 69 ++++++++++++++++++ .../PastDatePropertyHandler.java | 70 +++++++++++++++++++ .../_i18n/LocalStrings_de.properties | 2 + .../_i18n/LocalStrings_en.properties | 2 + .../userPropertriesHandlersContext.xml | 2 +- 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/olat/user/propertyhandlers/FutureDatePropertyHandler.java create mode 100644 src/main/java/org/olat/user/propertyhandlers/PastDatePropertyHandler.java diff --git a/src/main/java/org/olat/user/propertyhandlers/FutureDatePropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/FutureDatePropertyHandler.java new file mode 100644 index 00000000000..aae9e94ec5f --- /dev/null +++ b/src/main/java/org/olat/user/propertyhandlers/FutureDatePropertyHandler.java @@ -0,0 +1,69 @@ +/** + * <a href="http://www.openolat.org"> + * OpenOLAT - Online Learning and Training</a><br> + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at the + * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * Initial code contributed and copyrighted by<br> + * frentix GmbH, http://www.frentix.com + * <p> + */ +package org.olat.user.propertyhandlers; + +import java.util.Date; +import java.util.Map; + +import org.apache.commons.lang.time.DateUtils; +import org.olat.core.gui.components.form.flexible.FormItem; +import org.olat.core.gui.components.form.flexible.elements.DateChooser; + +/** + * <h3>Description:</h3> The FutureDatePropertyHandler offers the functionality + * of a date which lies in the future. (i.e. does not validate on dates that are + * in the past !) + * <p> + * Initial Date: 12.04.2012 <br> + * + * @author strentini, sergio.trentini@frentix.com, http://www.frentix.com + */ +public class FutureDatePropertyHandler extends DatePropertyHandler { + + /** + * @see org.olat.user.propertyhandlers.UserPropertyHandler#isValid(org.olat.core.gui.components.form.flexible.FormItem, + * java.util.Map) + */ + public boolean isValid(FormItem formItem, Map formContext) { + boolean isValidDate = super.isValid(formItem, formContext); + if (!isValidDate) + return false; + + if (isDateInTheFuture(((DateChooser) formItem).getDate())) { + return true; + } else { + formItem.setErrorKey("form.name.date.future.error", null); + return false; + } + } + + /** + * checks whether the given date is in the future. + * if the two dates are on the same date, false is returned! + * + * @param dateToCheck + * @return true if the given date is in the future + */ + private static boolean isDateInTheFuture(Date dateToCheck) { + Date now = new Date(); + return (!DateUtils.isSameDay(now, dateToCheck)) && dateToCheck.after(now); + } + +} diff --git a/src/main/java/org/olat/user/propertyhandlers/PastDatePropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/PastDatePropertyHandler.java new file mode 100644 index 00000000000..33978d48f7e --- /dev/null +++ b/src/main/java/org/olat/user/propertyhandlers/PastDatePropertyHandler.java @@ -0,0 +1,70 @@ +/** + * <a href="http://www.openolat.org"> + * OpenOLAT - Online Learning and Training</a><br> + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at the + * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * Initial code contributed and copyrighted by<br> + * frentix GmbH, http://www.frentix.com + * <p> + */ +package org.olat.user.propertyhandlers; + +import java.util.Date; +import java.util.Map; + +import org.apache.commons.lang.time.DateUtils; +import org.olat.core.gui.components.form.flexible.FormItem; +import org.olat.core.gui.components.form.flexible.elements.DateChooser; + +/** + * <h3>Description:</h3> The PastDatePropertyHandler offers the functionality of + * a date which lies in the past. (i.e. does not validate on future dates!) + * <p> + * Initial Date: 12.04.2012 <br> + * + * @author strentini, sergio.trentini@frentix.com, http://www.frentix.com + */ +public class PastDatePropertyHandler extends DatePropertyHandler { + + /** + * @see org.olat.user.propertyhandlers.UserPropertyHandler#isValid(org.olat.core.gui.components.form.flexible.FormItem, + * java.util.Map) + */ + public boolean isValid(FormItem formItem, Map formContext) { + boolean isValidDate = super.isValid(formItem, formContext); + if (!isValidDate) + return false; + + // check for date in the past + if (isDateInThePast(((DateChooser) formItem).getDate())) { + return true; + } else { + formItem.setErrorKey("form.name.date.past.error", null); + return false; + } + + } + + /** + * checks whether the given date is in the past. if the two dates are on + * the same date, false is returned! + * + * @param dateToCheck + * @return true if the given date is in the past + */ + private static boolean isDateInThePast(Date dateToCheck) { + Date now = new Date(); + return (!DateUtils.isSameDay(now, dateToCheck)) && dateToCheck.before(now); + } + +} diff --git a/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_de.properties index 81eb3e3d8bf..55cf6b8508d 100644 --- a/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_de.properties +++ b/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_de.properties @@ -17,6 +17,8 @@ form.group.person=Person form.name.birthDay=Geburtsdatum form.name.birthDay.error=Bitte geben Sie ein g\u00FCltiges Datum an. form.name.birthDay.error.empty=Das Feld "Geburtsdatum" darf nicht leer sein. +form.name.date.future.error=Das gewählte Datum muss in der Zukunft liegen +form.name.date.past.error=Das gewählte Datum muss in der Vergangenheit liegen form.name.city=Stadt form.name.city.error.empty=Das Feld "Stadt" darf nicht leer sein. form.name.country=Land diff --git a/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_en.properties index 72014ccbbf9..c1d4a57e3ef 100644 --- a/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_en.properties +++ b/src/main/java/org/olat/user/propertyhandlers/_i18n/LocalStrings_en.properties @@ -17,6 +17,8 @@ form.group.person=Person form.name.birthDay=Date of birth form.name.birthDay.error=Please provide a valid date. form.name.birthDay.error.empty=The field "Date of birth" is mandatory. +form.name.date.future.error=The chosen Date must lie in the future +form.name.date.past.error=The chosen Date must lie in the past form.name.city=City form.name.city.error.empty=The field "City" is mandatory. form.name.country=Country diff --git a/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml b/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml index a77d2adb7e7..bc716941d46 100644 --- a/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml +++ b/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml @@ -30,7 +30,7 @@ <property name="deletable" value="false" /> </bean> - <bean name="userPropertyBirthDay" class="org.olat.user.propertyhandlers.DatePropertyHandler"> + <bean name="userPropertyBirthDay" class="org.olat.user.propertyhandlers.PastDatePropertyHandler"> <property name="name" value="birthDay" /> <property name="group" value="person" /> <property name="deletable" value="true" /> -- GitLab