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 | 12 | super(nullable); |
75 | 12 | setRawRange(new Integer(low), new Integer(limit)); |
76 | 12 | } |
77 | |
|
78 | |
protected boolean _canRepresent(SQLPoemType other) { |
79 | 0 | return other instanceof YearPoemType; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public String toString() { |
87 | 0 | 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 | 12 | super(nullable); |
102 | 12 | setRawRange(new Integer(1), new Integer(13)); |
103 | 12 | } |
104 | |
|
105 | |
protected boolean _canRepresent(SQLPoemType other) { |
106 | 0 | return other instanceof MonthPoemType; |
107 | |
} |
108 | |
|
109 | |
protected String _stringOfCooked(Object raw, |
110 | |
PoemLocale locale, int style) { |
111 | 144 | int m = ((Integer)raw).intValue(); |
112 | 144 | switch (style) { |
113 | |
case DateFormat.FULL: case DateFormat.LONG: |
114 | 0 | return locale.monthName(m); |
115 | |
case DateFormat.MEDIUM: |
116 | 144 | return locale.shortMonthName(m); |
117 | |
default: |
118 | 0 | return "" + m; |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public String toString() { |
127 | 0 | 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 | 12 | super(nullable); |
142 | 12 | setRawRange(new Integer(1), new Integer(32)); |
143 | 12 | } |
144 | |
|
145 | |
protected boolean _canRepresent(SQLPoemType other) { |
146 | 0 | return other instanceof DayPoemType; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public String toString() { |
154 | 0 | return super.toString() + " (day)"; |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | 8 | public class YMDDateAdaptor implements TempletAdaptor { |
163 | |
|
164 | |
protected static final String |
165 | |
yearSuffix = "-year", |
166 | |
monthSuffix = "-month", |
167 | |
daySuffix = "-day"; |
168 | |
|
169 | |
|
170 | 2 | public static final YMDDateAdaptor it = new YMDDateAdaptor(); |
171 | |
|
172 | |
protected String getFormOrDie(ServletTemplateContext context, |
173 | |
String fieldName, String suffix) { |
174 | 24 | String fullName = fieldName + suffix; |
175 | 24 | String value = context.getFormField(fullName); |
176 | 24 | if (value == null) |
177 | 0 | throw new MissingFieldException(this, fieldName, fullName); |
178 | 24 | return value; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
public Object rawFrom(ServletTemplateContext context, String fieldName) { |
187 | 4 | String year = getFormOrDie(context, fieldName, yearSuffix); |
188 | 4 | String month = getFormOrDie(context, fieldName, monthSuffix); |
189 | 4 | String day = getFormOrDie(context, fieldName, daySuffix); |
190 | |
|
191 | 4 | if (year.equals("") && month.equals("") && day.equals("")) |
192 | 4 | return null; |
193 | 0 | else if (!year.equals("") && !month.equals("") ) { |
194 | 0 | if (day.equals("")) |
195 | 0 | day = "1"; |
196 | 0 | Calendar cal = Calendar.getInstance(); |
197 | 0 | cal.set(Integer.parseInt(year), |
198 | |
Integer.parseInt(month) - 1, |
199 | |
Integer.parseInt(day)); |
200 | 0 | return new Date(cal.getTime().getTime()); |
201 | |
} else { |
202 | 0 | throw new PartlyNullException(fieldName); |
203 | |
} |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public Field yearField(Field dateField) { |
211 | |
|
212 | 12 | Calendar when = when(dateField); |
213 | |
|
214 | |
|
215 | 12 | String displayName = dateField.getDisplayName() + " (year)"; |
216 | |
|
217 | 12 | 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 | 12 | Calendar when = when(dateField); |
236 | |
|
237 | |
|
238 | 12 | String displayName = dateField.getDisplayName() + " (month)"; |
239 | |
|
240 | 12 | 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 | 12 | Calendar when = when(dateField); |
257 | |
|
258 | |
|
259 | 12 | String displayName = dateField.getDisplayName() + " (day)"; |
260 | |
|
261 | 12 | 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 | 48 | if (dateField.getRaw() == null) return null; |
273 | 0 | Calendar when = Calendar.getInstance(); |
274 | 0 | when.setTime((java.util.Date)dateField.getRaw()); |
275 | 0 | return when; |
276 | |
} |
277 | |
} |