001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/api/src/java/org/theospi/portfolio/shared/model/DateBean.java $
003: * $Id:DateBean.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.portfolio.shared.model;
021:
022: import java.text.MessageFormat;
023: import java.util.Calendar;
024: import java.util.Date;
025: import java.util.GregorianCalendar;
026: import java.util.List;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.metaobj.utils.mvc.intf.FieldValueWrapper;
031: import org.springframework.validation.Errors;
032:
033: public class DateBean implements FieldValueWrapper {
034: protected final Log logger = LogFactory.getLog(getClass());
035: private String month = "";
036: private String year = "";
037: private String day = "";
038: private String hour = "";
039: private String minute = "";
040: private String second = "";
041: boolean nullFlag = true;
042:
043: public DateBean() {
044: }
045:
046: public DateBean(Date date) {
047: if (date != null) {
048: setBackingDate(date);
049: }
050: }
051:
052: public void setBackingDate(Date date) {
053: Calendar cal = Calendar.getInstance();
054: cal.setTime(date);
055: setYear("" + cal.get(Calendar.YEAR));
056: setMonth("" + (cal.get(Calendar.MONTH) + 1));
057: setDay("" + cal.get(Calendar.DATE));
058: setHour("" + cal.get(Calendar.HOUR));
059: setMinute("" + cal.get(Calendar.MINUTE));
060: setSecond("" + cal.get(Calendar.SECOND));
061: nullFlag = false;
062: }
063:
064: public String getMonth() {
065: return month;
066: }
067:
068: public void setMonth(String month) {
069: this .month = month;
070: checkFlag(month);
071: }
072:
073: public String getYear() {
074: return year;
075: }
076:
077: public void setYear(String year) {
078: this .year = year;
079: checkFlag(year);
080: }
081:
082: public String getDay() {
083: return day;
084: }
085:
086: public void setDay(String day) {
087: this .day = day;
088: checkFlag(day);
089: }
090:
091: public String getHour() {
092: return hour;
093: }
094:
095: public void setHour(String hour) {
096: this .hour = hour;
097: checkFlag(hour);
098: }
099:
100: public String getMinute() {
101: return minute;
102: }
103:
104: public void setMinute(String minute) {
105: this .minute = minute;
106: checkFlag(minute);
107: }
108:
109: public String getSecond() {
110: return second;
111: }
112:
113: public void setSecond(String second) {
114: this .second = second;
115: checkFlag(second);
116: }
117:
118: public void setValue(Object value) {
119: setBackingDate((Date) value);
120: nullFlag = (value == null);
121: }
122:
123: public Object getValue() {
124: return getDate();
125: }
126:
127: public void validate(String fieldName, List errors, String label) {
128:
129: }
130:
131: public void validate(Errors errors) {
132: if (nullFlag) {
133: return;
134: }
135:
136: try {
137: Integer.parseInt(getYear());
138: } catch (NumberFormatException e) {
139: errors.rejectValue("year", "invalid year {0}",
140: new Object[] { getYear() }, MessageFormat.format(
141: "invalid year {0}",
142: new Object[] { getYear() }));
143: }
144: try {
145: Integer.parseInt(getMonth());
146: } catch (NumberFormatException e) {
147: errors.rejectValue("month", "invalid month {0}",
148: new Object[] { getYear() }, MessageFormat.format(
149: "invalid month {0}",
150: new Object[] { getYear() }));
151: }
152: try {
153: Integer.parseInt(getDay());
154: } catch (NumberFormatException e) {
155: errors.rejectValue("day", "invalid day {0}",
156: new Object[] { getYear() }, MessageFormat.format(
157: "invalid day {0}",
158: new Object[] { getYear() }));
159: }
160:
161: /*
162: try {
163: Integer.parseInt(getHour());
164: } catch (NumberFormatException e) {
165: ValidationError error = new ValidationError("invalid hour: {0}",
166: new Object[]{getHour()});
167: errors.add(error);
168: }
169: try {
170: Integer.parseInt(getMinute());
171: } catch (NumberFormatException e) {
172: ValidationError error = new ValidationError("invalid minute: {0}",
173: new Object[]{getMinute()});
174: errors.add(error);
175: }
176: try {
177: Integer.parseInt(getSecond());
178: } catch (NumberFormatException e) {
179: ValidationError error = new ValidationError("invalid second: {0}",
180: new Object[]{getSecond()});
181: errors.add(error);
182: }
183: */
184: }
185:
186: public Date getDate() {
187: if (nullFlag) {
188: return null;
189: }
190:
191: return new GregorianCalendar(
192: getValue(getYear()),
193: getValue(getMonth()) - 1, // month is zero indexed
194: getValue(getDay()), getValue(getHour()),
195: getValue(getMinute()), getValue(getSecond())).getTime();
196: }
197:
198: protected int getValue(String value) {
199: try {
200: return Integer.parseInt(value);
201: } catch (NumberFormatException e) {
202: return 0;
203: }
204: }
205:
206: protected void checkFlag(String value) {
207: nullFlag = (value == null || value.length() == 0);
208: }
209:
210: public Object clone() {
211: try {
212: return super .clone();
213: } catch (CloneNotSupportedException e) {
214: throw new RuntimeException(e);
215: }
216: }
217:
218: }
|