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 | 2 | super(nullable); |
66 | 2 | setRawRange(new Integer(0), new Integer(24)); |
67 | 2 | } |
68 | |
|
69 | |
protected boolean _canRepresent(SQLPoemType<?> other) { |
70 | 0 | return other instanceof HourPoemType; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public String toString() { |
78 | 0 | 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 | 2 | super(nullable); |
92 | 2 | setRawRange(new Integer(0), new Integer(60)); |
93 | 2 | } |
94 | |
|
95 | |
protected boolean _canRepresent(SQLPoemType<?> other) { |
96 | 0 | return other instanceof MinutePoemType; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public String toString() { |
104 | 0 | 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 | 2 | super(nullable); |
118 | 2 | setRawRange(new Integer(0), new Integer(60)); |
119 | 2 | } |
120 | |
|
121 | |
protected boolean _canRepresent(SQLPoemType<?> other) { |
122 | 0 | return other instanceof SecondPoemType; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public String toString() { |
130 | 0 | return super.toString() + " (seconds)"; |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 2 | public class YMDHMSTimestampAdaptor extends YMDDateAdaptor { |
139 | |
private static final String |
140 | |
hourSuffix = "-hour", |
141 | |
minuteSuffix = "-minute", |
142 | |
secondSuffix = "-second"; |
143 | |
|
144 | 1 | private static final YMDHMSTimestampAdaptor me = new YMDHMSTimestampAdaptor(); |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public static YMDHMSTimestampAdaptor getIt() { |
150 | 13 | return me; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public Object rawFrom(ServletTemplateContext context, String fieldName) { |
159 | 1 | String year = getFormOrDie(context, fieldName, yearSuffix); |
160 | 1 | String month = getFormOrDie(context, fieldName, monthSuffix); |
161 | 1 | String day = getFormOrDie(context, fieldName, daySuffix); |
162 | 1 | String hour = getFormOrDie(context, fieldName, hourSuffix); |
163 | 1 | String minute = getFormOrDie(context, fieldName, minuteSuffix); |
164 | 1 | String second = getFormOrDie(context, fieldName, secondSuffix); |
165 | |
|
166 | 1 | if (year.equals("") && month.equals("") && day.equals("") && |
167 | 1 | hour.equals("") && minute.equals("") && second.equals("")) |
168 | 1 | return null; |
169 | 0 | else if (!year.equals("") && !month.equals("") && !day.equals("") && |
170 | 0 | !hour.equals("") && !minute.equals("") && !second.equals("")) { |
171 | 0 | Calendar cal = Calendar.getInstance(); |
172 | 0 | cal.set(Integer.parseInt(year), |
173 | 0 | Integer.parseInt(month) - 1, |
174 | 0 | Integer.parseInt(day), |
175 | 0 | Integer.parseInt(hour), |
176 | 0 | Integer.parseInt(minute), |
177 | 0 | Integer.parseInt(second)); |
178 | 0 | return new Timestamp(cal.getTime().getTime()); |
179 | |
} else { |
180 | 0 | throw new PartlyNullException(fieldName); |
181 | |
} |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public Field<Integer> hourField(Field<Date> field) { |
189 | |
|
190 | 2 | Calendar when = when(field); |
191 | |
|
192 | |
|
193 | |
|
194 | 2 | String displayName = field.getDisplayName() + " (hour)"; |
195 | |
|
196 | 2 | return new Field<Integer>( |
197 | 0 | when == null ? null : new Integer(when.get(Calendar.HOUR_OF_DAY)), |
198 | |
new BaseFieldAttributes<Integer>( |
199 | 2 | field.getName() + hourSuffix, displayName, null, |
200 | 2 | 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 | 2 | Calendar when = when(field); |
213 | |
|
214 | |
|
215 | |
|
216 | 2 | String displayName = field.getDisplayName() + " (minutes)"; |
217 | |
|
218 | 2 | return new Field<Integer>( |
219 | 0 | when == null ? null : new Integer(when.get(Calendar.MINUTE)), |
220 | |
new BaseFieldAttributes<Integer>( |
221 | 2 | field.getName() + minuteSuffix, displayName, null, |
222 | 2 | 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 | 2 | Calendar when = when(field); |
235 | |
|
236 | |
|
237 | |
|
238 | 2 | String displayName = field.getDisplayName() + " (seconds)"; |
239 | |
|
240 | 2 | return new Field<Integer>( |
241 | 0 | when == null ? null : new Integer(when.get(Calendar.SECOND)), |
242 | |
new BaseFieldAttributes<Integer>( |
243 | 2 | field.getName() + secondSuffix, displayName, null, |
244 | 2 | field.getType().getNullable() ? new SecondPoemType(true) : |
245 | |
new SecondPoemType(false), |
246 | |
2, 1, |
247 | |
null, false, true, true)); |
248 | |
} |
249 | |
} |