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