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