001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/shared/model/DateBean.java $
003: * $Id: DateBean.java 22417 2007-03-12 13:49:39Z john.ellis@rsmart.com $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.shared.model;
021:
022: import java.text.MessageFormat;
023: import java.text.Format;
024: import java.text.ParseException;
025: import java.util.Calendar;
026: import java.util.Date;
027: import java.util.GregorianCalendar;
028: import java.util.List;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.sakaiproject.metaobj.utils.mvc.intf.FieldValueWrapper;
033: import org.sakaiproject.metaobj.utils.xml.NormalizationException;
034: import org.sakaiproject.util.DateWidgetFormat;
035:
036: public class DateBean implements FieldValueWrapper {
037: protected final Log logger = LogFactory.getLog(getClass());
038: private String month = "";
039: private String year = "";
040: private String day = "";
041: private String hour = "";
042: private String minute = "";
043: private String second = "";
044: private String fullDate = null;
045: boolean nullFlag = true;
046:
047: private Format dateFormat;
048: private DateWidgetFormat dateFormatUtil = new DateWidgetFormat();
049:
050: public DateBean() {
051: dateFormat = dateFormatUtil.getLocaleDateFormat();
052: }
053:
054: public DateBean(Date date) {
055: dateFormat = dateFormatUtil.getLocaleDateFormat();
056: if (date != null) {
057: setBackingDate(date);
058: }
059: }
060:
061: public void setBackingDate(Date date) {
062: Calendar cal = Calendar.getInstance();
063: cal.setTime(date);
064: setYear("" + cal.get(Calendar.YEAR));
065: setMonth("" + (cal.get(Calendar.MONTH) + 1));
066: setDay("" + cal.get(Calendar.DATE));
067: setHour("" + cal.get(Calendar.HOUR));
068: setMinute("" + cal.get(Calendar.MINUTE));
069: setSecond("" + cal.get(Calendar.SECOND));
070: nullFlag = false;
071: }
072:
073: public String getMonth() {
074: return month;
075: }
076:
077: public void setMonth(String month) {
078: this .month = month;
079: checkFlag(month);
080: }
081:
082: public String getYear() {
083: return year;
084: }
085:
086: public void setYear(String year) {
087: this .year = year;
088: checkFlag(year);
089: }
090:
091: public String getDay() {
092: return day;
093: }
094:
095: public void setDay(String day) {
096: this .day = day;
097: checkFlag(day);
098: }
099:
100: public String getHour() {
101: return hour;
102: }
103:
104: public void setHour(String hour) {
105: this .hour = hour;
106: checkFlag(hour);
107: }
108:
109: public String getMinute() {
110: return minute;
111: }
112:
113: public void setMinute(String minute) {
114: this .minute = minute;
115: checkFlag(minute);
116: }
117:
118: public String getSecond() {
119: return second;
120: }
121:
122: public void setSecond(String second) {
123: this .second = second;
124: checkFlag(second);
125: }
126:
127: public void setValue(Object value) {
128: setBackingDate((Date) value);
129: nullFlag = (value == null);
130: }
131:
132: public Object getValue() {
133: return getDate();
134: }
135:
136: public void validate(String fieldName, List errors, String label) {
137: if (nullFlag) {
138: return;
139: }
140:
141: if (fullDate != null) {
142: try {
143: setValue(dateFormat.parseObject(getFullDate()));
144: } catch (ParseException e) {
145: errors
146: .add(new ValidationError(
147: label,
148: fieldName,
149: NormalizationException.DATE_INVALID_ERROR_CODE,
150: new Object[] { getFullDate() },
151: MessageFormat.format(
152: "invalid date {0}",
153: new Object[] { getFullDate() })));
154: nullFlag = true;
155: }
156:
157: return;
158: }
159:
160: try {
161: Integer.parseInt(getYear());
162: } catch (NumberFormatException e) {
163: errors.add(new ValidationError(label, fieldName + ".year",
164: "invalid year {0}", new Object[] { getYear() },
165: MessageFormat.format("invalid year {0}",
166: new Object[] { getYear() })));
167: }
168: try {
169: Integer.parseInt(getMonth());
170: } catch (NumberFormatException e) {
171: errors.add(new ValidationError(label, fieldName + ".month",
172: "invalid month {0}", new Object[] { getYear() },
173: MessageFormat.format("invalid month {0}",
174: new Object[] { getYear() })));
175: }
176: try {
177: Integer.parseInt(getDay());
178: } catch (NumberFormatException e) {
179: errors.add(new ValidationError(label, fieldName + ".day",
180: "invalid day {0}", new Object[] { getYear() },
181: MessageFormat.format("invalid day {0}",
182: new Object[] { getYear() })));
183: }
184:
185: /*
186: try {
187: Integer.parseInt(getHour());
188: } catch (NumberFormatException e) {
189: ValidationError error = new ValidationError("invalid hour: {0}",
190: new Object[]{getHour()});
191: errors.add(error);
192: }
193: try {
194: Integer.parseInt(getMinute());
195: } catch (NumberFormatException e) {
196: ValidationError error = new ValidationError("invalid minute: {0}",
197: new Object[]{getMinute()});
198: errors.add(error);
199: }
200: try {
201: Integer.parseInt(getSecond());
202: } catch (NumberFormatException e) {
203: ValidationError error = new ValidationError("invalid second: {0}",
204: new Object[]{getSecond()});
205: errors.add(error);
206: }
207: */
208: }
209:
210: public Date getDate() {
211: if (nullFlag) {
212: return null;
213: }
214:
215: return new GregorianCalendar(
216: getValue(getYear()),
217: getValue(getMonth()) - 1, // month is zero indexed
218: getValue(getDay()), getValue(getHour()),
219: getValue(getMinute()), getValue(getSecond())).getTime();
220: }
221:
222: protected int getValue(String value) {
223: try {
224: return Integer.parseInt(value);
225: } catch (NumberFormatException e) {
226: return 0;
227: }
228: }
229:
230: protected void checkFlag(String value) {
231: nullFlag = (value == null || value.length() == 0);
232: }
233:
234: public String getFullDate() {
235: return fullDate;
236: }
237:
238: public void setFullDate(String fullDate) {
239: this .fullDate = fullDate;
240: checkFlag(fullDate);
241: }
242:
243: public Object clone() {
244: try {
245: return super .clone();
246: } catch (CloneNotSupportedException e) {
247: throw new RuntimeException(e);
248: }
249: }
250: }
|