001: package org.conform.validator;
002:
003: import org.conform.Validator;
004: import org.conform.ValidationException;
005:
006: import java.util.Calendar;
007: import java.sql.Date;
008: import java.lang.annotation.Annotation;
009:
010: public class DateRangeValidator implements Validator {
011: private Date from;
012: private Date until;
013: private boolean fromInclusive;
014: private boolean untilInclusive;
015: private boolean fromToday;
016: private boolean untilToday;
017:
018: public DateRangeValidator() {
019: }
020:
021: public DateRangeValidator(Annotation annotation) {
022: DateRange dateRange = (DateRange) annotation;
023: switch (dateRange.from()) {
024: case TODAY_INCLUSIVE:
025: fromInclusive = true;
026: case TODAY:
027: fromToday = true;
028: }
029: switch (dateRange.until()) {
030: case TODAY_INCLUSIVE:
031: untilInclusive = true;
032: case TODAY:
033: untilToday = true;
034: }
035: }
036:
037: public DateRangeValidator(Date from, Date until) {
038: this .from = from;
039: this .until = until;
040: }
041:
042: public DateRangeValidator(Date from, boolean fromInclusive,
043: Date until, boolean untilInclusive) {
044: this .from = from;
045: this .fromInclusive = fromInclusive;
046: this .until = until;
047: this .untilInclusive = untilInclusive;
048: }
049:
050: public Date getFrom() {
051: return from;
052: }
053:
054: public void setFrom(Date from) {
055: this .from = from;
056: }
057:
058: public Date getUntil() {
059: return until;
060: }
061:
062: public void setUntil(Date until) {
063: this .until = until;
064: }
065:
066: public boolean isFromInclusive() {
067: return fromInclusive;
068: }
069:
070: public void setFromInclusive(boolean fromInclusive) {
071: this .fromInclusive = fromInclusive;
072: }
073:
074: public boolean isUntilInclusive() {
075: return untilInclusive;
076: }
077:
078: public void setUntilInclusive(boolean untilInclusive) {
079: this .untilInclusive = untilInclusive;
080: }
081:
082: /**
083: * ...[...]...
084: * *****
085: * ...]...[...
086: * ***
087: */
088: public Object validate(Object value) {
089: if (value == null)
090: return null;
091:
092: Date date = (Date) value;
093:
094: if (fromToday)
095: from = today();
096: if (untilToday)
097: until = today();
098:
099: if (from != null
100: && ((fromInclusive && date.before(from)) || (!fromInclusive && !date
101: .after(from))))
102: throw new ValidationException(
103: new ValidationException.Message(
104: getFromMessage(fromInclusive),
105: new Object[] { value, from, until }));
106: if (until != null
107: && ((untilInclusive && date.after(until)) || (!untilInclusive && !date
108: .before(until))))
109: throw new ValidationException(
110: new ValidationException.Message(
111: getUntilMessage(untilInclusive),
112: new Object[] { value, from, until }));
113:
114: return value;
115: }
116:
117: private String getFromMessage(boolean fromInclusive) {
118: return fromInclusive ? "validation.dateFromInclusive"
119: : "validation.dateFrom";
120: }
121:
122: private String getUntilMessage(boolean untilInclusive) {
123: return untilInclusive ? "validation.dateUntilInclusive"
124: : "validation.dateUntil";
125: }
126:
127: public static Date today() {
128: Calendar calendar = Calendar.getInstance();
129: calendar.clear(Calendar.MILLISECOND);
130: calendar.clear(Calendar.SECOND);
131: calendar.clear(Calendar.MINUTE);
132: calendar.clear(Calendar.HOUR);
133: Date today = new Date(calendar.getTime().getTime());
134: System.out.println("today = " + today);
135: return today;
136: }
137: }
|