001: /*
002: * Copyright 2006 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.validators;
017:
018: import java.util.Date;
019:
020: import org.iscreen.FailureMessage;
021: import org.iscreen.Validator;
022: import org.iscreen.ValidatorContext;
023:
024: /**
025: * The date range validator checks a "from" date to a "to" date to ensure
026: * that the "to" date is equal to or after the "from" date. If not,
027: * the "rangeFailure" is reported. In addition,
028: * a third check is done if the "date" property is mapped (not null), where
029: * if the "date" property is before the "from" date or after the "to" date,
030: * then the "betweenFailure" is reported. The mapping
031: * properties are: 'to', 'from' and 'date'.
032: *
033: * @author Shellman, Dan
034: */
035: public class DateRangeValidator implements Validator {
036: private FailureMessage rangeFailure;
037: private FailureMessage beforeFailure;
038: private FailureMessage afterFailure;
039:
040: /**
041: * Default constructor.
042: */
043: public DateRangeValidator() {
044: } //end DateRangeValidator()
045:
046: public void validate(ValidatorContext context,
047: Object objectToValidate) {
048: BeanToValidate bean;
049:
050: bean = (BeanToValidate) objectToValidate;
051: if (bean.getTo() != null && bean.getFrom() != null
052: && bean.getFrom().after(bean.getTo())) {
053: context.reportFailure(rangeFailure);
054: return;
055: }
056:
057: if (bean.getFrom() != null && bean.getDate() != null
058: && bean.getFrom().after(bean.getDate())) {
059: context.reportFailure(beforeFailure, bean.getFrom());
060: } else if (bean.getTo() != null && bean.getDate() != null
061: && bean.getTo().before(bean.getDate())) {
062: context.reportFailure(afterFailure, bean.getTo());
063: }
064: } //end validate()
065:
066: public Object constructBeanToValidate() {
067: return new BeanToValidate();
068: } //end constructBeanToValidate()
069:
070: /**
071: * This is the only failure that this validator reports.
072: *
073: * @param failure The failure message to report.
074: */
075: public void setRangeFailure(FailureMessage failure) {
076: rangeFailure = failure;
077: } //end setRangeFailure()
078:
079: public void setBeforeFailure(FailureMessage failure) {
080: beforeFailure = failure;
081: } //end setBeforeFailure()
082:
083: public void setAfterFailure(FailureMessage failure) {
084: afterFailure = failure;
085: } //end setAfterFailure()
086:
087: protected class BeanToValidate {
088: private Date from;
089: private Date to;
090: private Date date;
091:
092: public void setFrom(Date fromDate) {
093: from = fromDate;
094: } //end setFrom()
095:
096: public Date getFrom() {
097: return from;
098: } //end getFrom()
099:
100: public void setTo(Date toDate) {
101: to = toDate;
102: } //end setTo()
103:
104: public Date getTo() {
105: return to;
106: } //end getTo()
107:
108: public void setDate(Date theDate) {
109: date = theDate;
110: } //end setDate()
111:
112: public Date getDate() {
113: return date;
114: } //end getDate()
115: } //end BeanToValidate
116: } //end DateRangeValidator
|