1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.schedule.model;
21
22 import java.io.Serializable;
23 import java.util.Calendar;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.SortedSet;
28 import java.util.TimeZone;
29 import java.util.TreeSet;
30
31 import org.apache.myfaces.custom.schedule.util.ScheduleUtil;
32
33
34
35
36
37
38
39
40
41 public abstract class AbstractScheduleModel implements ScheduleModel,
42 Serializable
43 {
44
45
46
47 private TreeSet days;
48
49 private Date selectedDate = new Date();
50
51 private ScheduleEntry selectedEntry;
52
53 private int mode;
54
55 protected final TimeZone timeZone;
56
57
58
59
60
61
62
63 public AbstractScheduleModel()
64 {
65 this(TimeZone.getDefault());
66 }
67
68 public AbstractScheduleModel(TimeZone timeZone)
69 {
70 this.timeZone = timeZone;
71 }
72
73
74
75
76
77
78
79
80 protected SortedSet getDays()
81 {
82 if (days == null)
83 {
84 days = new TreeSet();
85
86 switch (mode)
87 {
88 case DAY:
89 setDay(this.selectedDate);
90
91 break;
92
93 case WORKWEEK:
94 setWorkWeek(this.selectedDate);
95
96 break;
97
98 case WEEK:
99 setWeek(this.selectedDate);
100
101 break;
102
103 case MONTH:
104 setMonth(this.selectedDate);
105
106 break;
107
108 default:
109 setDay(this.selectedDate);
110 }
111 }
112
113 return days;
114 }
115
116
117
118
119 public TimeZone getTimeZone ()
120 {
121 return this.timeZone;
122 }
123
124
125
126
127 public boolean isEmpty()
128 {
129 return getDays().isEmpty();
130 }
131
132
133
134
135 public void setMode(int mode)
136 {
137 this.mode = mode;
138 }
139
140
141
142
143 public int getMode()
144 {
145 return mode;
146 }
147
148
149
150
151 public void setSelectedDate(Date date)
152 {
153 if (date == null)
154 {
155
156 return;
157 }
158
159 this.selectedDate = date;
160 this.days = null;
161 }
162
163
164
165
166 public Date getSelectedDate()
167 {
168 return selectedDate;
169 }
170
171
172
173
174 public void setSelectedEntry(ScheduleEntry selectedEntry)
175 {
176 this.selectedEntry = selectedEntry;
177 }
178
179
180
181
182 public ScheduleEntry getSelectedEntry()
183 {
184 return selectedEntry;
185 }
186
187
188
189
190 public boolean isEntrySelected() {
191 return getSelectedEntry() != null;
192 }
193
194
195
196
197 public boolean containsDate(Date date)
198 {
199 for (Iterator iterator = iterator(); iterator.hasNext();)
200 {
201 ScheduleDay day = (ScheduleDay) iterator.next();
202
203 if (day.equalsDate(date))
204 {
205 return true;
206 }
207 }
208
209 return false;
210 }
211
212
213
214
215 public Object get(int index)
216 {
217 Object[] dayArray = getDays().toArray();
218
219 Object returnObject = dayArray[index];
220
221 return returnObject;
222 }
223
224
225
226
227 public Iterator iterator()
228 {
229 return getDays().iterator();
230 }
231
232
233
234
235 public int size()
236 {
237 return getDays().size();
238 }
239
240
241
242
243
244
245
246
247
248 protected void setDay(Date date)
249 {
250 if (date == null)
251 {
252 return;
253 }
254
255 clear();
256
257 ScheduleDay day = add(date);
258 load(day.getDayStart(), day.getDayEnd());
259 }
260
261
262
263
264
265
266
267
268
269 protected void setMonth(Date date)
270 {
271 if (date == null)
272 {
273 return;
274 }
275
276 clear();
277
278 Calendar cal = getCalendarInstance(date);
279
280
281 cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH));
282
283 Date temp = cal.getTime();
284 cal.add(Calendar.MONTH, 1);
285 int nextMonth = cal.get(Calendar.MONTH);
286 cal.setTime(temp);
287
288 ScheduleDay firstDay = null;
289 ScheduleDay lastDay = null;
290
291
292 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
293
294 while (true)
295 {
296 ScheduleDay addedDay = add(cal.getTime());
297
298 if (firstDay == null)
299 {
300 firstDay = addedDay;
301 }
302
303 lastDay = addedDay;
304 cal.add(Calendar.DATE, 1);
305
306
307 if (cal.get(Calendar.MONTH) == nextMonth && cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)
308 {
309 break;
310 }
311 }
312
313 load(firstDay.getDayStart(), lastDay.getDayEnd());
314 }
315
316
317
318
319
320
321
322
323
324 protected void setWeek(Date date)
325 {
326 if (date == null)
327 {
328 return;
329 }
330
331 clear();
332
333 Calendar cal = getCalendarInstance(date);
334
335
336 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
337
338 ScheduleDay firstDay = null;
339 ScheduleDay lastDay = null;
340
341 for (int i = 0; i < 7; i++)
342 {
343 ScheduleDay addedDay = add(cal.getTime());
344
345 if (firstDay == null)
346 {
347 firstDay = addedDay;
348 }
349
350 lastDay = addedDay;
351 cal.add(Calendar.DATE, 1);
352 }
353
354 load(firstDay.getDayStart(), lastDay.getDayEnd());
355 }
356
357
358
359
360
361
362
363
364
365 protected void setWorkWeek(Date date)
366 {
367 if (date == null)
368 {
369 return;
370 }
371
372 clear();
373
374 Calendar cal = getCalendarInstance(date);
375
376
377 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
378
379 ScheduleDay firstDay = null;
380 ScheduleDay lastDay = null;
381
382 for (int i = 0; i < 5; i++)
383 {
384 ScheduleDay addedDay = add(cal.getTime());
385
386 if (firstDay == null)
387 {
388 firstDay = addedDay;
389 }
390
391 lastDay = addedDay;
392 cal.add(Calendar.DATE, 1);
393 }
394
395 load(firstDay.getDayStart(), lastDay.getDayEnd());
396 }
397
398
399
400
401
402
403
404
405
406
407
408 protected ScheduleDay add(Date date)
409 {
410 if (date == null)
411 {
412 return null;
413 }
414
415 ScheduleDay day = new ScheduleDay(date, getTimeZone());
416 loadDayAttributes(day);
417 getDays().add(day);
418
419 return day;
420 }
421
422
423
424
425
426
427 protected void clear()
428 {
429 if (days != null)
430 {
431 for (Iterator dayIterator = days.iterator(); dayIterator.hasNext();)
432 {
433 ScheduleDay day = (ScheduleDay) dayIterator.next();
434 day.clear();
435 }
436
437 days.clear();
438 }
439 }
440
441
442
443
444 public void refresh()
445 {
446 setSelectedDate(selectedDate);
447 }
448
449
450
451
452
453
454
455
456
457
458
459
460 protected abstract Collection loadEntries(Date startDate, Date endDate);
461
462
463
464
465
466
467
468
469
470
471 protected abstract void loadDayAttributes(Day day);
472
473
474
475
476 public boolean containsRepeatedIntervals() {
477 boolean firstDay = true;
478 TreeSet firstDayIntervals = null;
479
480 if (mode == DAY)
481 {
482 return true;
483 }
484
485 for (Iterator dayIterator = iterator(); dayIterator.hasNext(); )
486 {
487 Day day = (Day) dayIterator.next();
488
489 if (firstDay)
490 {
491 firstDayIntervals = day.getIntervals();
492 firstDay = false;
493 }
494 else
495 {
496 if (firstDayIntervals == null || firstDayIntervals.size() == 0) {
497 if (day.getIntervals() != null && day.getIntervals().size() > 0)
498 {
499 return false;
500 }
501 }
502 else if (!ScheduleUtil.areEquivalentIntervals(firstDayIntervals, day.getIntervals()))
503 {
504
505 return false;
506 }
507 }
508 }
509
510 return true;
511 }
512
513 private void load(Date startDate, Date endDate)
514 {
515 Collection entries = loadEntries(startDate, endDate);
516
517 for (Iterator entryIterator = entries.iterator(); entryIterator
518 .hasNext();)
519 {
520 ScheduleEntry entry = (ScheduleEntry) entryIterator.next();
521
522 for (Iterator dayIterator = getDays().iterator(); dayIterator.hasNext();)
523 {
524 ScheduleDay day = (ScheduleDay) dayIterator.next();
525
526 day.addEntry(entry);
527 }
528 }
529 }
530
531 protected Calendar getCalendarInstance(Date date) {
532
533 return ScheduleUtil.getCalendarInstance(date, getTimeZone());
534 }
535 }
536