Source code: com/RuntimeCollective/webapps/bean/FormDate.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/FormDate.java,v 1.6 2003/09/30 15:13:09 joe Exp $
2 * $Revision: 1.6 $
3 * $Date: 2003/09/30 15:13:09 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.bean;
31
32 import com.RuntimeCollective.webapps.RuntimeParameters;
33
34 import java.util.Date;
35 import java.util.Calendar;
36 import java.util.GregorianCalendar;
37
38 /**
39 * A time stamp with bean style accessors which is the counterpart of
40 * ideas.tag.DateInputTag.
41 *
42 * @version $Id: FormDate.java,v 1.6 2003/09/30 15:13:09 joe Exp $
43 */
44 public class FormDate {
45
46 // == Contstructors ====================================================
47
48 /** Default constructor sets the date to today and the time to 00:00. */
49 public FormDate() {
50 setDate(new Date());
51 }
52
53 /** Constructor sets the date from <code>date</code>.
54 * Date may be null, to explicitly represent a null date.
55 */
56 public FormDate(Date date) {
57 setDate(date);
58 }
59
60
61 // == Properties =======================================================
62
63 private String day;
64 /** get the day. */
65 public String getDay() { return nullToStr(day); }
66 /** set the day. */
67 public void setDay(String day) {
68 this.day = day;
69 dateIsDirty = true;
70 }
71
72 private String month;
73 /** get the month. */
74 public String getMonth() { return nullToStr(month); }
75 /** set the month. */
76 public void setMonth(String month) {
77 this.month = month;
78 dateIsDirty = true;
79 }
80
81 private String year;
82 /** get the year. */
83 public String getYear() { return nullToStr(year); }
84 /** set the year. */
85 public void setYear(String year) {
86 this.year = year;
87 dateIsDirty = true;
88 }
89
90 private String hour;
91 /** get the hour. */
92 public String getHour() { return nullToStr(hour); }
93 /** set the hour. */
94 public void setHour(String hour) {
95 this.hour = hour;
96 dateIsDirty = true;
97 }
98
99 private String minute;
100 /** get the minute. */
101 public String getMinute() { return nullToStr(minute); }
102 /** set the minute. */
103 public void setMinute(String minute) {
104 this.minute = minute;
105 dateIsDirty = true;
106 }
107
108 private boolean dateIsDirty;
109 private Date date;
110
111 /** get the date.
112 *
113 * @throws IllegalArgumentException if the date bean properties have been
114 * initialised with invalid arguments.
115 */
116 public Date getDate() {
117 if (dateIsDirty) {
118 synchroniseDate();
119 }
120
121 return date;
122 }
123
124 /** set the date. */
125 public void setDate(Date date) {
126 if (date != null) {
127 Calendar calendar = Calendar.getInstance();
128 calendar.setTime(date);
129
130 setDay(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
131 setMonth(Integer.toString(calendar.get(Calendar.MONTH)));
132 setYear(Integer.toString(calendar.get(Calendar.YEAR)));
133
134 setHour(Integer.toString(calendar.get(Calendar.HOUR)));
135 setMinute(Integer.toString(calendar.get(Calendar.MINUTE)));
136 } else {
137 setDay("");
138 setMonth("");
139 setYear("");
140
141 setHour("");
142 setMinute("");
143 }
144
145 dateIsDirty = false;
146 }
147
148
149 // == Utility Methods ==================================================
150
151 /** test if this bean represents a valid date. */
152 public boolean isValid() {
153 try {
154 synchroniseDate();
155 } catch (IllegalArgumentException e) {
156 return false;
157 }
158
159 return true;
160 }
161
162
163 // == Private Methods ==================================================
164
165 /* Synchronises the value of the date property with the String bean
166 * properties.
167 *
168 * @throws IllegalArgumentException if the bean properties contain an
169 * invalid date.
170 */
171 private void synchroniseDate() {
172
173 if (!validNullValues()) {
174 throw new IllegalArgumentException("invalid date");
175 }
176
177 try {
178 if (strToNull(getYear()) == null) {
179 date = null;
180
181 } else {
182 Calendar calendar = Calendar.getInstance();
183 calendar.setLenient(false);
184
185 // change 2-digit years to 4 digits
186 if (getYear().length() == 2)
187 setYear("19"+getYear());
188
189 calendar.set(Calendar.YEAR, Integer.parseInt(getYear()));
190
191 if (strToNull(getMonth()) != null) {
192 calendar.set(Calendar.MONTH, Integer.parseInt(getMonth()));
193 }
194
195 if (strToNull(getDay()) != null) {
196 calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(getDay()));
197 }
198
199 if (strToNull(getHour()) != null) {
200 calendar.set(Calendar.HOUR, Integer.parseInt(getHour()));
201 }
202
203 if (strToNull(getMinute()) != null) {
204 calendar.set(Calendar.MINUTE, Integer.parseInt(getMinute()));
205 }
206
207 date = calendar.getTime();
208 dateIsDirty = false;
209 }
210
211 } catch (Exception e) {
212 throw new IllegalArgumentException("invalid date");
213 }
214 }
215
216 /* Converts null strings to empty strings. */
217 private String nullToStr(String str) {
218 if (str == null) {
219 return "";
220 } else {
221 return str;
222 }
223 }
224
225 /* Converts empty strings to null strings. */
226 private String strToNull(String str) {
227 if (str == null ||
228 str.equals("")) {
229 return null;
230 } else {
231 return str;
232 }
233 }
234
235 /* @returns false if a null value exists between the most significant
236 * and least significant non-null values; where YEAR is the most
237 * significant value and MINUTE is the least significant.
238 */
239 private boolean validNullValues() {
240 boolean hasNull = false;
241
242 if (strToNull(getYear()) == null) {
243 hasNull = true;
244 }
245
246 if (strToNull(getMonth()) == null) {
247 hasNull = true;
248 } else if (hasNull) {
249 return false;
250 }
251
252 if (strToNull(getDay()) == null) {
253 hasNull = true;
254 } else if (hasNull) {
255 return false;
256 }
257
258 if (strToNull(getHour()) == null) {
259 hasNull = true;
260 } else if (hasNull) {
261 return false;
262 }
263
264 if (strToNull(getMinute()) == null) {
265 hasNull = true;
266 } else if (hasNull) {
267 return false;
268 }
269
270 return true;
271 }
272 }