1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.melati.template;
46
47 import java.sql.Date;
48 import java.text.DateFormat;
49 import java.util.Calendar;
50
51 import org.melati.poem.BaseFieldAttributes;
52 import org.melati.poem.Field;
53 import org.melati.poem.IntegerPoemType;
54 import org.melati.poem.PoemLocale;
55 import org.melati.poem.SQLPoemType;
56
57
58
59
60
61 class YearPoemType extends IntegerPoemType {
62
63 static final int firstYear = 2000;
64
65 static final int limitYear = 2023;
66
67
68
69
70
71
72
73 public YearPoemType(boolean nullable, int low, int limit) {
74 super(nullable);
75 setRawRange(new Integer(low), new Integer(limit));
76 }
77
78 protected boolean _canRepresent(SQLPoemType other) {
79 return other instanceof YearPoemType;
80 }
81
82
83
84
85
86 public String toString() {
87 return super.toString() + " (year)";
88 }
89 }
90
91
92
93
94 class MonthPoemType extends IntegerPoemType {
95
96
97
98
99
100 public MonthPoemType(boolean nullable) {
101 super(nullable);
102 setRawRange(new Integer(1), new Integer(13));
103 }
104
105 protected boolean _canRepresent(SQLPoemType other) {
106 return other instanceof MonthPoemType;
107 }
108
109 protected String _stringOfCooked(Object raw,
110 PoemLocale locale, int style) {
111 int m = ((Integer)raw).intValue();
112 switch (style) {
113 case DateFormat.FULL: case DateFormat.LONG:
114 return locale.monthName(m);
115 case DateFormat.MEDIUM:
116 return locale.shortMonthName(m);
117 default:
118 return "" + m;
119 }
120 }
121
122
123
124
125
126 public String toString() {
127 return super.toString() + " (month)";
128 }
129 }
130
131
132
133
134 class DayPoemType extends IntegerPoemType {
135
136
137
138
139
140 public DayPoemType(boolean nullable) {
141 super(nullable);
142 setRawRange(new Integer(1), new Integer(32));
143 }
144
145 protected boolean _canRepresent(SQLPoemType other) {
146 return other instanceof DayPoemType;
147 }
148
149
150
151
152
153 public String toString() {
154 return super.toString() + " (day)";
155 }
156 }
157
158
159
160
161
162 public class YMDDateAdaptor implements TempletAdaptor {
163
164 protected static final String
165 yearSuffix = "-year",
166 monthSuffix = "-month",
167 daySuffix = "-day";
168
169
170 public static final YMDDateAdaptor it = new YMDDateAdaptor();
171
172 protected String getFormOrDie(ServletTemplateContext context,
173 String fieldName, String suffix) {
174 String fullName = fieldName + suffix;
175 String value = context.getFormField(fullName);
176 if (value == null)
177 throw new MissingFieldException(this, fieldName, fullName);
178 return value;
179 }
180
181
182
183
184
185
186 public Object rawFrom(ServletTemplateContext context, String fieldName) {
187 String year = getFormOrDie(context, fieldName, yearSuffix);
188 String month = getFormOrDie(context, fieldName, monthSuffix);
189 String day = getFormOrDie(context, fieldName, daySuffix);
190
191 if (year.equals("") && month.equals("") && day.equals(""))
192 return null;
193 else if (!year.equals("") && !month.equals("") ) {
194 if (day.equals(""))
195 day = "1";
196 Calendar cal = Calendar.getInstance();
197 cal.set(Integer.parseInt(year),
198 Integer.parseInt(month) - 1,
199 Integer.parseInt(day));
200 return new Date(cal.getTime().getTime());
201 } else {
202 throw new PartlyNullException(fieldName);
203 }
204 }
205
206
207
208
209
210 public Field yearField(Field dateField) {
211
212 Calendar when = when(dateField);
213
214
215 String displayName = dateField.getDisplayName() + " (year)";
216
217 return new Field(
218 when == null ? null : new Integer(when.get(Calendar.YEAR)),
219 new BaseFieldAttributes(
220 dateField.getName() + yearSuffix,
221 displayName,
222 null,
223 new YearPoemType(dateField.getType().getNullable(),
224 YearPoemType.firstYear, YearPoemType.limitYear),
225 5, 1,
226 null, false, true, true));
227 }
228
229
230
231
232
233 public Field monthField(Field dateField) {
234
235 Calendar when = when(dateField);
236
237
238 String displayName = dateField.getDisplayName() + " (month)";
239
240 return new Field(
241 when == null ? null : new Integer(when.get(Calendar.MONTH) + 1),
242 new BaseFieldAttributes(
243 dateField.getName() + monthSuffix, displayName, null,
244 dateField.getType().getNullable() ? new MonthPoemType(true) :
245 new MonthPoemType(false),
246 3, 1,
247 null, false, true, true));
248 }
249
250
251
252
253
254 public Field dayField(Field dateField) {
255
256 Calendar when = when(dateField);
257
258
259 String displayName = dateField.getDisplayName() + " (day)";
260
261 return new Field(
262 when == null ? null : new Integer(when.get(Calendar.DAY_OF_MONTH)),
263 new BaseFieldAttributes(
264 dateField.getName() + daySuffix, displayName, null,
265 dateField.getType().getNullable() ? new DayPoemType(true) :
266 new DayPoemType(false),
267 2, 1,
268 null, false, true, true));
269 }
270
271 protected Calendar when(Field dateField) {
272 if (dateField.getRaw() == null) return null;
273 Calendar when = Calendar.getInstance();
274 when.setTime((java.util.Date)dateField.getRaw());
275 return when;
276 }
277 }