Mercurial > hg > ismi-richfaces
comparison src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/Calendar.java @ 1:2e911857a759
(none)
author | jurzua |
---|---|
date | Wed, 29 Oct 2014 14:00:28 +0000 |
parents | |
children | 6d82e03fb2ee |
comparison
equal
deleted
inserted
replaced
0:74df02964906 | 1:2e911857a759 |
---|---|
1 package de.mpiwg.itgroup.ismi.util.guiComponents; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import javax.faces.event.ActionEvent; | |
6 | |
7 import org.apache.commons.lang.StringUtils; | |
8 import org.joda.time.DateTime; | |
9 import org.joda.time.chrono.GregorianChronology; | |
10 import org.joda.time.chrono.IslamicChronology; | |
11 import org.joda.time.chrono.JulianChronology; | |
12 import org.json.JSONException; | |
13 import org.json.JSONObject; | |
14 | |
15 import de.mpiwg.itgroup.ismi.json.utils.JSONUtils; | |
16 | |
17 public class Calendar extends AbstractCalendar implements Serializable { | |
18 private static final long serialVersionUID = 1L; | |
19 | |
20 private String inputForm; | |
21 private String calendarType; | |
22 | |
23 private Date fromGregorian = new Date(); | |
24 private Date untilGregorian = new Date(); | |
25 private String additionalInfo; | |
26 | |
27 private Integer currentYear; | |
28 private Date currentFrom = new Date(); | |
29 private Date currentUntil = new Date(); | |
30 | |
31 private String dateInText; | |
32 private String state; | |
33 | |
34 public Calendar(){ | |
35 this.calendarType = TYPE_GREGORIAN; | |
36 this.inputForm = INPUT_FORM_YEAR; | |
37 this.state = STATE_NOT_CHECKED; | |
38 } | |
39 | |
40 public Calendar(String jsonString){ | |
41 if(StringUtils.isNotEmpty(jsonString)){ | |
42 try { | |
43 JSONObject json = new JSONObject(jsonString); | |
44 this.state = json.getString(STATE); | |
45 | |
46 if(StringUtils.isNotEmpty(state)){ | |
47 if(state.equals(STATE_KNOWN)){ | |
48 | |
49 this.additionalInfo = json.getString(ADD_INF); | |
50 this.calendarType = json.getString(CALENDAR_TYPE); | |
51 this.inputForm = json.getString(INPUT_FORM); | |
52 | |
53 if(inputForm.equals(INPUT_FORM_YEAR)){ | |
54 this.fromGregorian = new Date(json.getJSONObject(FROM)); | |
55 this.untilGregorian = new Date(json.getJSONObject(UNTIL)); | |
56 this.currentYear = json.getInt(YEAR); | |
57 }else if(inputForm.equals(INPUT_FORM_RANGE)){ | |
58 this.fromGregorian = new Date(json.getJSONObject(FROM)); | |
59 this.untilGregorian = new Date(json.getJSONObject(UNTIL)); | |
60 if(calendarType.equals(TYPE_GREGORIAN)){ | |
61 this.currentFrom = new Date(json.getJSONObject(FROM)); | |
62 this.currentUntil = new Date(json.getJSONObject(UNTIL)); | |
63 }else if(calendarType.equals(TYPE_ISLAMIC)){ | |
64 this.currentFrom = new Date(this.fromGregorian.getIslamicDateTime()); | |
65 this.currentUntil = new Date(this.untilGregorian.getIslamicDateTime()); | |
66 }else if(calendarType.equals(TYPE_JULIAN)){ | |
67 this.currentFrom = new Date(this.fromGregorian.getJulianDateTime()); | |
68 this.currentUntil = new Date(this.untilGregorian.getJulianDateTime()); | |
69 } | |
70 }else if(inputForm.equals(INPUT_FORM_DATE)){ | |
71 this.fromGregorian = new Date(json.getJSONObject(DATE)); | |
72 if(calendarType.equals(TYPE_GREGORIAN)){ | |
73 this.currentFrom = new Date(json.getJSONObject(DATE)); | |
74 }else if(calendarType.equals(TYPE_ISLAMIC)){ | |
75 this.currentFrom = new Date(this.fromGregorian.getIslamicDateTime()); | |
76 }else if(calendarType.equals(TYPE_JULIAN)){ | |
77 this.currentFrom = new Date(this.fromGregorian.getJulianDateTime()); | |
78 } | |
79 } | |
80 | |
81 if(json.has("dayOfWeekFrom")){ | |
82 this.currentFrom.setDayOfWeek(json.getInt("dayOfWeekFrom")); | |
83 } | |
84 if(json.has("dayOfWeekUntil")){ | |
85 this.currentUntil.setDayOfWeek(json.getInt("dayOfWeekUntil")); | |
86 } | |
87 if(json.has("dayOfWeek")){ | |
88 this.currentFrom.setDayOfWeek(json.getInt("dayOfWeek")); | |
89 } | |
90 | |
91 }else if(state.equals(STATE_NOT_CHECKED) || state.equals(STATE_UNKNOWN)){ | |
92 if(json.has(DATE_IN_TEXT)){ | |
93 this.dateInText = json.getString(DATE_IN_TEXT); | |
94 } | |
95 this.calendarType = TYPE_GREGORIAN; | |
96 this.inputForm = INPUT_FORM_YEAR; | |
97 } | |
98 } | |
99 | |
100 } catch (Exception e) { | |
101 e.printStackTrace(); | |
102 this.state = STATE_NOT_CHECKED; | |
103 this.calendarType = TYPE_GREGORIAN; | |
104 this.inputForm = INPUT_FORM_YEAR; | |
105 } | |
106 }else{ | |
107 this.calendarType = TYPE_GREGORIAN; | |
108 this.inputForm = INPUT_FORM_YEAR; | |
109 this.state = STATE_NOT_CHECKED; | |
110 } | |
111 } | |
112 | |
113 public void update(){ | |
114 | |
115 if(inputForm.equals(INPUT_FORM_RANGE)){ | |
116 if(isUntilOlderThanFrom()){ | |
117 if(calendarType.equals(TYPE_GREGORIAN)){ | |
118 this.inputGregorianRange(); | |
119 }else if(calendarType.equals(TYPE_ISLAMIC)){ | |
120 this.inputIslamicRange(); | |
121 }else if(calendarType.equals(TYPE_JULIAN)){ | |
122 this.inputJulianRange(); | |
123 } | |
124 }else{ | |
125 addGeneralMsg("The until date must be older than the from date."); | |
126 } | |
127 | |
128 }else if(inputForm.equals(INPUT_FORM_YEAR)){ | |
129 if(calendarType.equals(TYPE_GREGORIAN)){ | |
130 this.inputGregorianYear(); | |
131 }else if(calendarType.equals(TYPE_ISLAMIC)){ | |
132 this.inputIslamicYear(); | |
133 }else if(calendarType.equals(TYPE_JULIAN)){ | |
134 this.inputJulianYear(); | |
135 } | |
136 }else if(inputForm.equals(INPUT_FORM_DATE)){ | |
137 if(calendarType.equals(TYPE_GREGORIAN)){ | |
138 this.inputGregorianFrom(); | |
139 }else if(calendarType.equals(TYPE_ISLAMIC)){ | |
140 this.inputIslamicFrom(); | |
141 }else if(calendarType.equals(TYPE_JULIAN)){ | |
142 this.inputJulianFrom(); | |
143 } | |
144 } | |
145 } | |
146 | |
147 public boolean isDeployable(){ | |
148 return (!this.state.equals(STATE_NOT_CHECKED) && !this.state.equals(STATE_UNKNOWN)); | |
149 } | |
150 | |
151 public String getFormattedRange(){ | |
152 if(inputForm != null){ | |
153 String from = this.fromGregorian.toString(); | |
154 if(this.inputForm.equals(INPUT_FORM_DATE)){ | |
155 return "[" + from + "]"; | |
156 }else{ | |
157 | |
158 String until = this.untilGregorian.toString(); | |
159 if(StringUtils.isNotEmpty(from) && StringUtils.isNotEmpty(until)){ | |
160 return "[FROM: " + from + " TO: " + until + "]"; | |
161 } | |
162 } | |
163 } | |
164 return null; | |
165 } | |
166 | |
167 public String getFormattedIslamicRange(){ | |
168 if(inputForm != null){ | |
169 String from = this.fromGregorian.toIslamicString(); | |
170 if(this.inputForm.equals(INPUT_FORM_DATE)){ | |
171 return "[" + from + "]"; | |
172 }else{ | |
173 String until = this.untilGregorian.toIslamicString(); | |
174 if(StringUtils.isNotEmpty(from) && StringUtils.isNotEmpty(until)){ | |
175 return "[FROM: " + from + " TO: " + until + "]"; | |
176 } | |
177 } | |
178 } | |
179 return null; | |
180 } | |
181 | |
182 public String getFormattedJulianRange(){ | |
183 if(inputForm != null){ | |
184 String from = this.fromGregorian.toJulianString(); | |
185 if(this.inputForm.equals(INPUT_FORM_DATE)){ | |
186 return "[" + from + "]"; | |
187 }else{ | |
188 String until = this.untilGregorian.toJulianString(); | |
189 if(StringUtils.isNotEmpty(from) && StringUtils.isNotEmpty(until)){ | |
190 return "[FROM: " + from + " TO: " + until + "]"; | |
191 } | |
192 } | |
193 } | |
194 return null; | |
195 } | |
196 | |
197 public void listenerUpdate(ActionEvent e){ | |
198 this.update(); | |
199 } | |
200 | |
201 | |
202 public String getCalendarAsHtml(){ | |
203 StringBuilder sb = new StringBuilder(); | |
204 | |
205 if(isDeployable()){ | |
206 sb.append("<table align=\"left\">"); | |
207 | |
208 sb.append("<tr>"); | |
209 sb.append("<th align=\"left\">Gregorian:</th>"); | |
210 sb.append("<th align=\"left\">" + this.getFormattedRange() + "</th>"); | |
211 sb.append("</tr>"); | |
212 | |
213 sb.append("<tr>"); | |
214 sb.append("<th align=\"left\">Islamic:</th>"); | |
215 sb.append("<th align=\"left\">" + this.getFormattedIslamicRange() + "</th>"); | |
216 sb.append("</tr>"); | |
217 | |
218 sb.append("<tr>"); | |
219 sb.append("<th align=\"left\">Julian:</th>"); | |
220 sb.append("<th align=\"left\">" + this.getFormattedJulianRange() + "</th>"); | |
221 sb.append("</tr>"); | |
222 | |
223 sb.append("</table>"); | |
224 }else if(this.state.equals(STATE_UNKNOWN)){ | |
225 sb.append("Unkknown"); | |
226 }else{ | |
227 sb.append("Not checked"); | |
228 if(StringUtils.isNotEmpty(this.dateInText)){ | |
229 sb.append("=" + this.dateInText); | |
230 } | |
231 } | |
232 | |
233 return sb.toString(); | |
234 } | |
235 | |
236 | |
237 private void inputGregorianRange(){ | |
238 this.inputGregorianFrom(); | |
239 try{ | |
240 DateTime gregrorian = | |
241 new DateTime(currentUntil.getYear(), currentUntil.getMonth(), currentUntil.getDayOfMonth(), 0, 0, 0, 0, GregorianChronology.getInstance()); | |
242 this.untilGregorian = new Date(gregrorian.withChronology(GregorianChronology.getInstance())); | |
243 if(isDayInWeek(currentUntil.getDayOfWeek())){ | |
244 this.untilGregorian.setDayOfWeek(currentUntil.getDayOfWeek()); | |
245 } | |
246 }catch (Exception e) { | |
247 this.addGeneralMsg("In until date - " + e.getMessage()); | |
248 } | |
249 } | |
250 | |
251 private void inputGregorianFrom(){ | |
252 try{ | |
253 DateTime gregorian = | |
254 new DateTime(currentFrom.getYear(), currentFrom.getMonth(), currentFrom.getDayOfMonth(), 0, 0, 0, 0, GregorianChronology.getInstance()); | |
255 this.fromGregorian = new Date(gregorian); | |
256 if(isDayInWeek(currentFrom.getDayOfWeek())){ | |
257 this.fromGregorian.setDayOfWeek(currentFrom.getDayOfWeek()); | |
258 } | |
259 }catch (Exception e) { | |
260 this.addGeneralMsg("In from date - " + e.getMessage()); | |
261 } | |
262 } | |
263 | |
264 private void inputIslamicRange(){ | |
265 this.inputIslamicFrom(); | |
266 try{ | |
267 DateTime islamic = | |
268 new DateTime(currentUntil.getYear(), currentUntil.getMonth(), currentUntil.getDayOfMonth(), 0, 0, 0, 0, IslamicChronology.getInstance()); | |
269 DateTime gregorian = islamic.withChronology(GregorianChronology.getInstance()); | |
270 this.untilGregorian = new Date(gregorian); | |
271 if(isDayInWeek(currentUntil.getDayOfWeek())){ | |
272 this.untilGregorian.setDayOfWeek(gregorian.getDayOfWeek()); | |
273 }else{ | |
274 this.untilGregorian.setAmbiguity(2); | |
275 } | |
276 }catch (Exception e) { | |
277 this.addGeneralMsg("In until date - " + e.getMessage()); | |
278 } | |
279 } | |
280 | |
281 private void inputIslamicFrom(){ | |
282 try{ | |
283 DateTime islamic = | |
284 new DateTime(currentFrom.getYear(), currentFrom.getMonth(), currentFrom.getDayOfMonth(), 0, 0, 0, 0, IslamicChronology.getInstance()); | |
285 DateTime gregorian = islamic.withChronology(GregorianChronology.getInstance()); | |
286 this.fromGregorian = new Date(gregorian); | |
287 if(isDayInWeek(currentFrom.getDayOfWeek())){ | |
288 this.fromGregorian.setDayOfWeek(gregorian.getDayOfWeek()); | |
289 }else{ | |
290 this.fromGregorian.setAmbiguity(2); | |
291 } | |
292 }catch (Exception e) { | |
293 this.addGeneralMsg("In from date - " + e.getMessage()); | |
294 } | |
295 } | |
296 | |
297 | |
298 private void inputJulianRange(){ | |
299 this.inputJulianFrom(); | |
300 DateTime julian = null; | |
301 try{ | |
302 julian = | |
303 new DateTime(currentUntil.getYear(), currentUntil.getMonth(), currentUntil.getDayOfMonth(), 0, 0, 0, 0, JulianChronology.getInstance()); | |
304 DateTime gregorian = julian.withChronology(GregorianChronology.getInstance()); | |
305 this.untilGregorian = new Date(gregorian); | |
306 if(isDayInWeek(currentUntil.getDayOfWeek())){ | |
307 this.untilGregorian.setDayOfWeek(gregorian.getDayOfWeek()); | |
308 } | |
309 }catch (Exception e) { | |
310 this.addGeneralMsg("In until date - " + e.getMessage()); | |
311 } | |
312 } | |
313 | |
314 private void inputJulianFrom(){ | |
315 DateTime julian = null; | |
316 try{ | |
317 julian = | |
318 new DateTime(currentFrom.getYear(), currentFrom.getMonth(), currentFrom.getDayOfMonth(), 0, 0, 0, 0, JulianChronology.getInstance()); | |
319 DateTime gregorian = julian.withChronology(GregorianChronology.getInstance()); | |
320 this.fromGregorian = new Date(gregorian); | |
321 if(isDayInWeek(currentFrom.getDayOfWeek())){ | |
322 this.fromGregorian.setDayOfWeek(gregorian.getDayOfWeek()); | |
323 } | |
324 }catch (Exception e) { | |
325 addGeneralMsg("In from date - " + e.getMessage()); | |
326 } | |
327 } | |
328 | |
329 public void inputGregorianYear(){ | |
330 if(inputForm.equals(INPUT_FORM_YEAR) && currentYear != null){ | |
331 this.fromGregorian = new Date(new DateTime(currentYear, 1, 1, 0, 0, 0, 0, GregorianChronology.getInstance())); | |
332 this.untilGregorian = new Date(new DateTime(currentYear, 12, 31, 0, 0, 0, 0, GregorianChronology.getInstance())); | |
333 } | |
334 } | |
335 | |
336 public void inputIslamicYear(){ | |
337 if(this.currentYear < 1 || this.currentYear > 292271022){ | |
338 this.addGeneralMsg("year must be in the range [1,292271022]"); | |
339 }else { | |
340 DateTime islamic = | |
341 new DateTime(currentYear, 1, 1, 0, 0, 0, 0, IslamicChronology.getInstance()); | |
342 DateTime gregorian = islamic.withChronology(GregorianChronology.getInstance()); | |
343 this.fromGregorian = new Date(gregorian); | |
344 this.fromGregorian.setAmbiguity(2); | |
345 this.fromGregorian.setDayOfWeek(null); | |
346 | |
347 islamic = | |
348 new DateTime(currentYear, 12, 29, 0, 0, 0, 0, IslamicChronology.getInstance()); | |
349 gregorian = islamic.withChronology(GregorianChronology.getInstance()); | |
350 this.untilGregorian = new Date(gregorian); | |
351 this.untilGregorian.setAmbiguity(2); | |
352 this.untilGregorian.setDayOfWeek(null); | |
353 } | |
354 } | |
355 | |
356 public void inputJulianYear(){ | |
357 if(this.currentYear < 1){ | |
358 this.addGeneralMsg("Value 0 for year is not supported"); | |
359 }else{ | |
360 DateTime julian = | |
361 new DateTime(currentYear, 1, 1, 0, 0, 0, 0, JulianChronology.getInstance()); | |
362 DateTime gregorian = julian.withChronology(GregorianChronology.getInstance()); | |
363 this.fromGregorian = new Date(gregorian); | |
364 this.fromGregorian.setDayOfWeek(null); | |
365 | |
366 julian = | |
367 new DateTime(currentYear, 12, 31, 0, 0, 0, 0, JulianChronology.getInstance()); | |
368 gregorian = julian.withChronology(GregorianChronology.getInstance()); | |
369 this.untilGregorian = new Date(gregorian); | |
370 this.untilGregorian.setDayOfWeek(null); | |
371 } | |
372 } | |
373 | |
374 | |
375 public String toJSONString(){ | |
376 this.update(); | |
377 JSONObject json = this.toJSON(); | |
378 if(json != null){ | |
379 return json.toString(); | |
380 } | |
381 return ""; | |
382 } | |
383 | |
384 public JSONObject toJSON(){ | |
385 JSONObject json = new JSONObject(); | |
386 if(state.equals(STATE_KNOWN)){ | |
387 try { | |
388 | |
389 if(inputForm.equals(INPUT_FORM_YEAR)){ | |
390 json.put(YEAR, this.currentYear); | |
391 json.put(FROM, this.fromGregorian.toJSON()); | |
392 json.put(UNTIL, this.untilGregorian.toJSON()); | |
393 }else if(inputForm.equals(INPUT_FORM_RANGE)){ | |
394 json.put(FROM, this.fromGregorian.toJSON()); | |
395 json.put(UNTIL, this.untilGregorian.toJSON()); | |
396 if(isDayInWeek(currentFrom.getDayOfWeek())){ | |
397 json.put("dayOfWeekFrom", currentFrom.getDayOfWeek()); | |
398 } | |
399 if(isDayInWeek(currentUntil.getDayOfWeek())){ | |
400 json.put("dayOfWeekUntil", currentUntil.getDayOfWeek()); | |
401 } | |
402 }else if(inputForm.equals(INPUT_FORM_DATE)){ | |
403 json.put(DATE, this.fromGregorian.toJSON()); | |
404 if(isDayInWeek(currentFrom.getDayOfWeek())){ | |
405 json.put("dayOfWeek", currentFrom.getDayOfWeek()); | |
406 } | |
407 } | |
408 | |
409 json.put(STATE, this.state); | |
410 json.put(ADD_INF, additionalInfo); | |
411 json.put(CALENDAR_TYPE, this.calendarType); | |
412 json.put(INPUT_FORM, this.inputForm); | |
413 | |
414 } catch (JSONException e) { | |
415 e.printStackTrace(); | |
416 } | |
417 return json; | |
418 }else if(state.equals(STATE_UNKNOWN)){ | |
419 try { | |
420 json.put(STATE, this.state); | |
421 } catch (JSONException e) { | |
422 e.printStackTrace(); | |
423 } | |
424 return json; | |
425 }else if(state.equals(STATE_NOT_CHECKED) && StringUtils.isNotEmpty(dateInText)){ | |
426 try { | |
427 json.put(STATE, this.state); | |
428 json.put(DATE_IN_TEXT, this.dateInText); | |
429 } catch (JSONException e) { | |
430 e.printStackTrace(); | |
431 } | |
432 return json; | |
433 } | |
434 return null; | |
435 } | |
436 | |
437 public boolean isUntilOlderThanFrom(){ | |
438 if(this.currentFrom.getYear() != null && this.currentUntil.getYear() != null){ | |
439 if(this.currentFrom.getYear() < this.currentUntil.getYear()){ | |
440 return true; | |
441 }else if(this.currentFrom.getYear().equals(this.currentUntil.getYear())){ | |
442 if(this.currentFrom.getMonth() != null && this.currentUntil.getMonth() != null){ | |
443 if(this.currentFrom.getMonth() < this.currentUntil.getMonth()){ | |
444 return true; | |
445 }else if(this.currentFrom.getMonth().equals(this.currentUntil.getMonth())){ | |
446 if(this.currentFrom.getDayOfMonth() != null && this.currentUntil.getDayOfMonth() != null){ | |
447 if(this.currentFrom.getDayOfMonth() < this.currentUntil.getDayOfMonth()){ | |
448 return true; | |
449 } | |
450 } | |
451 } | |
452 } | |
453 } | |
454 } | |
455 return false; | |
456 } | |
457 | |
458 private boolean isDayInWeek(Integer day){ | |
459 if(day == null) | |
460 return false; | |
461 if(day >= 1 && day <= 7){ | |
462 return true; | |
463 } | |
464 return false; | |
465 } | |
466 | |
467 public Date getFromGregorian() { | |
468 return fromGregorian; | |
469 } | |
470 public void setFromGregorian(Date fromGregorian) { | |
471 this.fromGregorian = fromGregorian; | |
472 } | |
473 public Date getUntilGregorian() { | |
474 return untilGregorian; | |
475 } | |
476 public void setUntilGregorian(Date untilGregorian) { | |
477 this.untilGregorian = untilGregorian; | |
478 } | |
479 | |
480 public String getInputForm() { | |
481 return inputForm; | |
482 } | |
483 | |
484 public void setInputForm(String inputForm) { | |
485 this.inputForm = inputForm; | |
486 } | |
487 | |
488 public String getCalendarType() { | |
489 return calendarType; | |
490 } | |
491 | |
492 public void setCalendarType(String calendarType) { | |
493 this.calendarType = calendarType; | |
494 } | |
495 | |
496 public String getAdditionalInfo() { | |
497 return additionalInfo; | |
498 } | |
499 | |
500 public void setAdditionalInfo(String additionalInfo) { | |
501 this.additionalInfo = additionalInfo; | |
502 } | |
503 | |
504 public Integer getCurrentYear() { | |
505 return currentYear; | |
506 } | |
507 | |
508 public void setCurrentYear(Integer currentYear) { | |
509 this.currentYear = currentYear; | |
510 } | |
511 | |
512 public Date getCurrentFrom() { | |
513 return currentFrom; | |
514 } | |
515 | |
516 public void setCurrentFrom(Date currentFrom) { | |
517 this.currentFrom = currentFrom; | |
518 } | |
519 | |
520 public Date getCurrentUntil() { | |
521 return currentUntil; | |
522 } | |
523 | |
524 public void setCurrentUntil(Date currentUntil) { | |
525 this.currentUntil = currentUntil; | |
526 } | |
527 | |
528 public String getState() { | |
529 return state; | |
530 } | |
531 | |
532 public void setState(String state) { | |
533 this.state = state; | |
534 } | |
535 | |
536 public String getDateInText() { | |
537 return dateInText; | |
538 } | |
539 | |
540 public void setDateInText(String dateInText) { | |
541 this.dateInText = dateInText; | |
542 } | |
543 } |