001: /* SimpleDateConstraint.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Dec 25 12:06:30 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import java.util.Date;
022: import java.text.SimpleDateFormat;
023: import java.text.ParseException;
024:
025: import org.zkoss.mesg.Messages;
026: import org.zkoss.util.Dates;
027:
028: import org.zkoss.zk.ui.Component;
029: import org.zkoss.zk.ui.UiException;
030: import org.zkoss.zk.ui.WrongValueException;
031:
032: import org.zkoss.zul.mesg.MZul;
033:
034: /**
035: * A simple date constraint.
036: *
037: * @author tomyeh
038: * @since 3.0.2
039: */
040: public class SimpleDateConstraint extends SimpleConstraint {
041: private static final SimpleDateFormat _df = new SimpleDateFormat(
042: "yyyyMMdd");
043: private Date _beg, _end;
044:
045: public SimpleDateConstraint(int flags) {
046: super (flags);
047: fixConstraint();
048: }
049:
050: /** Constraints a constraint.
051: *
052: * @param flags a combination of {@link #NO_POSITIVE}, {@link #NO_NEGATIVE},
053: * {@link #NO_ZERO}, and so on.
054: * @param errmsg the error message to display. Ignored if null or empty.
055: */
056: public SimpleDateConstraint(int flags, String errmsg) {
057: super (flags, errmsg);
058: fixConstraint();
059: }
060:
061: /** Constructs a regular-expression constraint.
062: *
063: * @param regex ignored if null or empty
064: * @param errmsg the error message to display. Ignored if null or empty.
065: */
066: public SimpleDateConstraint(String regex, String errmsg) {
067: super (regex, errmsg);
068: fixConstraint();
069: }
070:
071: /** Constructs a constraint combining regular expression.
072: *
073: * @param flags a combination of {@link #NO_POSITIVE}, {@link #NO_NEGATIVE},
074: * {@link #NO_ZERO}, and so on.
075: * @param regex ignored if null or empty
076: * @param errmsg the error message to display. Ignored if null or empty.
077: */
078: public SimpleDateConstraint(int flags, String regex, String errmsg) {
079: super (flags, regex, errmsg);
080: fixConstraint();
081: }
082:
083: /** Constructs a constraint with beginning and ending date.
084: *
085: * @param flags a combination of {@link #NO_POSITIVE}, {@link #NO_NEGATIVE},
086: * {@link #NO_ZERO}, and so on.
087: * @param begin the beginning date, or null if no constraint at the beginning
088: * date.
089: * @param end the ending date, or null if no constraint at the ending
090: * date.
091: * @param errmsg the error message to display. Ignored if null or empty.
092: */
093: public SimpleDateConstraint(int flags, Date begin, Date end,
094: String errmsg) {
095: super (flags, errmsg);
096: _beg = begin;
097: _end = end;
098: fixConstraint();
099: }
100:
101: /** Constructs a constraint with a list of constraints separated by comma.
102: *
103: * @param constraint a list of constraints separated by comma.
104: * Example: "between 20071012 and 20071223", "before 20080103"
105: */
106: public SimpleDateConstraint(String constraint) {
107: super (constraint);
108: fixConstraint();
109: }
110:
111: private void fixConstraint() {
112: if ((_flags & NO_FUTURE) != 0 && _end == null)
113: _end = Dates.today();
114: if ((_flags & NO_PAST) != 0 && _beg == null)
115: _beg = Dates.today();
116: }
117:
118: /** Returns the beginning date, or null if there is no constraint of
119: * the beginning date.
120: */
121: public Date getBeginDate() {
122: return _beg;
123: }
124:
125: /** Returns the ending date, or null if therer is no constraint of
126: * the ending date.
127: */
128: public Date getEndDate() {
129: return _end;
130: }
131:
132: //super//
133: protected int parseConstraint(String constraint) throws UiException {
134: if (constraint.startsWith("between")) {
135: final int j = constraint.indexOf("and", 7);
136: if (j < 0)
137: throw new UiException("Constraint syntax error: "
138: + constraint);
139: _beg = parseDate(constraint.substring(7, j));
140: _end = parseDate(constraint.substring(j + 3));
141: if (_beg.compareTo(_end) > 0) {
142: final Date d = _beg;
143: _beg = _end;
144: _end = d;
145: }
146: return 0;
147: } else if (constraint.startsWith("before")) {
148: _end = parseDate(constraint.substring(6));
149: return 0;
150: } else if (constraint.startsWith("after")) {
151: _beg = parseDate(constraint.substring(5));
152: return 0;
153: }
154: return super .parseConstraint(constraint);
155: }
156:
157: private static Date parseDate(String val) throws UiException {
158: try {
159: return _df.parse(val.trim());
160: } catch (ParseException ex) {
161: throw new UiException("Not a date: " + val
162: + ". Format: yyyyMMdd", ex);
163: }
164: }
165:
166: public void validate(Component comp, Object value)
167: throws WrongValueException {
168: if (value instanceof Date) {
169: final Date d = (Date) value;
170: if (_beg != null && _beg.compareTo(d) > 0)
171: throw outOfRangeValue(comp);
172: if (_end != null && _end.compareTo(d) < 0)
173: throw outOfRangeValue(comp);
174: }
175: super .validate(comp, value);
176: }
177:
178: private WrongValueException outOfRangeValue(Component comp) {
179: final String errmsg = getErrorMessage(comp);
180: return errmsg != null ? new WrongValueException(comp, errmsg)
181: : new WrongValueException(comp, MZul.OUT_OF_RANGE,
182: new Object[] { dateToString(comp, _beg),
183: dateToString(comp, _end) });
184: }
185:
186: private static String dateToString(Component comp, Date d) {
187: if (d == null)
188: return "";
189: if (comp instanceof Datebox)
190: return ((Datebox) comp).coerceToString(d);
191: return _df.format(d);
192: }
193: }
|