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