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.util.Calendar;
48 import java.sql.Date;
49 import java.sql.Timestamp;
50
51 import org.melati.poem.Field;
52 import org.melati.poem.IntegerPoemType;
53 import org.melati.poem.BaseFieldAttributes;
54 import org.melati.poem.SQLPoemType;
55
56
57
58
59 class HourPoemType extends IntegerPoemType {
60
61
62
63
64 public HourPoemType(boolean nullable) {
65 super(nullable);
66 setRawRange(new Integer(0), new Integer(24));
67 }
68
69 protected boolean _canRepresent(SQLPoemType<?> other) {
70 return other instanceof HourPoemType;
71 }
72
73
74
75
76
77 public String toString() {
78 return super.toString() + " (hour)";
79 }
80 }
81
82
83
84
85 class MinutePoemType extends IntegerPoemType {
86
87
88
89
90 public MinutePoemType(boolean nullable) {
91 super(nullable);
92 setRawRange(new Integer(0), new Integer(60));
93 }
94
95 protected boolean _canRepresent(SQLPoemType<?> other) {
96 return other instanceof MinutePoemType;
97 }
98
99
100
101
102
103 public String toString() {
104 return super.toString() + " (minutes)";
105 }
106 }
107
108
109
110
111 class SecondPoemType extends IntegerPoemType {
112
113
114
115
116 public SecondPoemType(boolean nullable) {
117 super(nullable);
118 setRawRange(new Integer(0), new Integer(60));
119 }
120
121 protected boolean _canRepresent(SQLPoemType<?> other) {
122 return other instanceof SecondPoemType;
123 }
124
125
126
127
128
129 public String toString() {
130 return super.toString() + " (seconds)";
131 }
132 }
133
134
135
136
137
138 public class YMDHMSTimestampAdaptor extends YMDDateAdaptor {
139 private static final String
140 hourSuffix = "-hour",
141 minuteSuffix = "-minute",
142 secondSuffix = "-second";
143
144 private static final YMDHMSTimestampAdaptor me = new YMDHMSTimestampAdaptor();
145
146
147
148
149 public static YMDHMSTimestampAdaptor getIt() {
150 return me;
151 }
152
153
154
155
156
157
158 public Object rawFrom(ServletTemplateContext context, String fieldName) {
159 String year = getFormOrDie(context, fieldName, yearSuffix);
160 String month = getFormOrDie(context, fieldName, monthSuffix);
161 String day = getFormOrDie(context, fieldName, daySuffix);
162 String hour = getFormOrDie(context, fieldName, hourSuffix);
163 String minute = getFormOrDie(context, fieldName, minuteSuffix);
164 String second = getFormOrDie(context, fieldName, secondSuffix);
165
166 if (year.equals("") && month.equals("") && day.equals("") &&
167 hour.equals("") && minute.equals("") && second.equals(""))
168 return null;
169 else if (!year.equals("") && !month.equals("") && !day.equals("") &&
170 !hour.equals("") && !minute.equals("") && !second.equals("")) {
171 Calendar cal = Calendar.getInstance();
172 cal.set(Integer.parseInt(year),
173 Integer.parseInt(month) - 1,
174 Integer.parseInt(day),
175 Integer.parseInt(hour),
176 Integer.parseInt(minute),
177 Integer.parseInt(second));
178 return new Timestamp(cal.getTime().getTime());
179 } else {
180 throw new PartlyNullException(fieldName);
181 }
182 }
183
184
185
186
187
188 public Field<Integer> hourField(Field<Date> field) {
189
190 Calendar when = when(field);
191
192
193
194 String displayName = field.getDisplayName() + " (hour)";
195
196 return new Field<Integer>(
197 when == null ? null : new Integer(when.get(Calendar.HOUR_OF_DAY)),
198 new BaseFieldAttributes<Integer>(
199 field.getName() + hourSuffix, displayName, null,
200 field.getType().getNullable() ? new HourPoemType(true) :
201 new HourPoemType(false),
202 2, 1,
203 null, false, true, true));
204 }
205
206
207
208
209
210 public Field<Integer> minuteField(Field<Date> field) {
211
212 Calendar when = when(field);
213
214
215
216 String displayName = field.getDisplayName() + " (minutes)";
217
218 return new Field<Integer>(
219 when == null ? null : new Integer(when.get(Calendar.MINUTE)),
220 new BaseFieldAttributes<Integer>(
221 field.getName() + minuteSuffix, displayName, null,
222 field.getType().getNullable() ? new MinutePoemType(true) :
223 new MinutePoemType(false),
224 2, 1,
225 null, false, true, true));
226 }
227
228
229
230
231
232 public Field<Integer> secondField(Field<Date> field) {
233
234 Calendar when = when(field);
235
236
237
238 String displayName = field.getDisplayName() + " (seconds)";
239
240 return new Field<Integer>(
241 when == null ? null : new Integer(when.get(Calendar.SECOND)),
242 new BaseFieldAttributes<Integer>(
243 field.getName() + secondSuffix, displayName, null,
244 field.getType().getNullable() ? new SecondPoemType(true) :
245 new SecondPoemType(false),
246 2, 1,
247 null, false, true, true));
248 }
249 }