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.Time;
008: import java.lang.annotation.Annotation;
009:
010: public class TimeRangeValidator implements Validator {
011: private Time from;
012: private Time until;
013: private boolean fromInclusive;
014: private boolean untilInclusive;
015: private boolean fromNow;
016: private boolean untilNow;
017:
018: public TimeRangeValidator() {
019: }
020:
021: public TimeRangeValidator(Annotation annotation) {
022: TimeRange dateRange = (TimeRange) annotation;
023: switch (dateRange.from()) {
024: case NOW_INCLUSIVE:
025: fromInclusive = true;
026: case NOW:
027: fromNow = true;
028: }
029: switch (dateRange.until()) {
030: case NOW_INCLUSIVE:
031: untilInclusive = true;
032: case NOW:
033: untilNow = true;
034: }
035: }
036:
037: public TimeRangeValidator(Time from, Time until) {
038: this .from = from;
039: this .until = until;
040: }
041:
042: public TimeRangeValidator(Time from, boolean fromInclusive,
043: Time until, boolean untilInclusive) {
044: this .from = from;
045: this .fromInclusive = fromInclusive;
046: this .until = until;
047: this .untilInclusive = untilInclusive;
048: }
049:
050: public Time getFrom() {
051: return from;
052: }
053:
054: public void setFrom(Time from) {
055: this .from = from;
056: }
057:
058: public Time getUntil() {
059: return until;
060: }
061:
062: public void setUntil(Time 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: Time time = (Time) value;
093:
094: if (fromNow)
095: from = new Time(System.currentTimeMillis());
096: if (untilNow)
097: until = new Time(System.currentTimeMillis());
098:
099: if (from != null
100: && ((fromInclusive && time.before(from)) || (!fromInclusive && !time
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 && time.after(until)) || (!untilInclusive && !time
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.timeFromInclusive"
119: : "validation.timeFrom";
120: }
121:
122: private String getUntilMessage(boolean untilInclusive) {
123: return untilInclusive ? "validation.timeUntilInclusive"
124: : "validation.timeUntil";
125: }
126:
127: public static Time time(int hour, int minute, int second) {
128: Calendar calendar = Calendar.getInstance();
129: calendar.set(Calendar.SECOND, second);
130: calendar.set(Calendar.MINUTE, minute);
131: calendar.set(Calendar.HOUR, hour);
132: Time time = new Time(calendar.getTime().getTime());
133: System.out.println("time = " + time);
134: return time;
135: }
136: }
|