UISchedule.java
22.8 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package org.richfaces.component;
import org.richfaces.component.event.ScheduleViewChangedEvent;
import org.richfaces.component.event.ScheduleDateRangeChangedEvent;
import org.richfaces.component.event.ScheduleItemResizeEvent;
import org.richfaces.component.event.ScheduleItemSelectedEvent;
import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.FacesEvent;
import javax.faces.model.*;
import javax.servlet.jsp.jstl.sql.Result;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.richfaces.component.event.ScheduleDateRangeChangedListener;
import org.richfaces.component.event.ScheduleDateSelectedEvent;
import org.richfaces.component.event.ScheduleDateSelectedListener;
import org.richfaces.component.event.ScheduleItemSelectedListener;
import org.richfaces.component.event.ScheduleItemMoveEvent;
import org.richfaces.component.event.ScheduleItemMoveListener;
import org.richfaces.component.event.ScheduleItemResizeListener;
import org.richfaces.component.event.ScheduleListenerEventsProducer;
import org.richfaces.component.event.ScheduleViewChangedListener;
import org.richfaces.component.model.DateRange;
public abstract class UISchedule extends UIComponentBase implements ScheduleCommonViewAttributes, ScheduleListenerEventsProducer {
public static final String COMPONENT_TYPE = "org.richfaces.Schedule";
public static final String COMPONENT_FAMILY = "org.richfaces.Schedule";
public static final String VIEW_MONTH = "month";
public static final String VIEW_BASIC_WEEK = "basicWeek";
public static final String VIEW_AGENDA_WEEK = "agendaWeek";
public static final String VIEW_BASIC_DAY = "basicDay";
public static final String VIEW_AGENDA_DAY = "agendaDay";
public static final String WEEK_MODE_FIXED = "fixed";
private DataModel model;
public abstract Object getValue();
public abstract void setValue(Object value);
public abstract String getVar();
public abstract void setVar(String value);
public abstract Date getDate();
public abstract void setDate(Date date);
public abstract String getSwitchType();
public abstract void setSwitchType(String switchType);
public abstract String getView();
public abstract void setView(String defaultView);
public abstract String getHeaderLeft();
public abstract void setHeaderLeft(String headerLeft);
public abstract String getHeaderCenter();
public abstract void setHeaderCenter(String headerCenter);
public abstract String getHeaderRight();
public abstract void setHeaderRight(String headerRight);
public abstract Integer getFirstDay();
public abstract void setFirstDay(Integer firstDay);
public abstract Boolean getIsRTL();
public abstract void setIsRTL(Boolean isrtl);
public abstract Boolean getShowWeekends();
public abstract void setShowWeekends(Boolean showWeekends);
public abstract String getWeekMode();
public abstract void setWeekMode(String weekMode);
public abstract Integer getHeight();
public abstract void setHeight(Integer height);
public abstract Integer getContentHeight();
public abstract void setContentHeight(Integer contentHeight);
public abstract Double getAspectRatio();
public abstract void setAspectRatio(Double aspectRatio);
public abstract Boolean getAllDayByDefault();
public abstract void setAllDayByDefault(Boolean allDayByDefault);
public abstract Boolean getAllDaySlot();
public abstract void setAllDaySlot(Boolean allDaySlot);
public abstract String getAllDayText();
public abstract void setAllDayText(String allDayText);
public abstract String getAxisFormat();
public abstract void setAxisFormat(String axisFormat);
public abstract Integer getSlotMinutes();
public abstract void setSlotMinutes(Integer slotMinutes);
public abstract Integer getDefaultEventMinutes();
public abstract void setDefaultEventMinutes(Integer defaultEventMinutes);
public abstract Integer getFirstHour();
public abstract void setFirstHour(Integer firstHour);
public abstract String getMinTime();
public abstract void setMinTime(String minTime);
public abstract String getMaxTime();
public abstract void setMaxTime(String maxTime);
public abstract Boolean getEditable();
public abstract void setEditable(Boolean editable);
public abstract Boolean getDisableDragging();
public abstract void setDisableDragging(Boolean disableDragging);
public abstract Boolean getDisableResizing();
public abstract void setDisableResizing(Boolean disableResizing);
public abstract Integer getDragRevertDuration();
public abstract void setDragRevertDuration(Integer dragRevertDuration);
public abstract Double getDragOpacity();
public abstract void setDragOpacity(Double dragOpacity);
public abstract String getStyleClass();
public abstract void setStyleClass(String styleClass);
public abstract String getOnItemSelected();
public abstract void setOnItemSelected(String onItemSelect);
public abstract String getOnItemDragStart();
public abstract void setOnItemDragStart(String onItemDragStart);
public abstract String getOnItemDragStop();
public abstract void setOnItemDragStop(String onItemDragStop);
public abstract String getOnItemDrop();
public abstract void setOnItemDrop(String onItemDrop);
public abstract String getOnItemResizeStart();
public abstract void setOnItemResizeStart(String onItemResizeStart);
public abstract String getOnItemResizeStop();
public abstract void setOnItemResizeStop(String onItemResizeStop);
public abstract String getOnItemResized();
public abstract void setOnItemResized(String onItemMouseover);
public abstract String getOnItemMouseover();
public abstract void setOnItemMouseover(String onItemMouseover);
public abstract String getOnItemMouseout();
public abstract void setOnItemMouseout(String onItemMouseout);
public abstract String getOnViewDisplay();
public abstract void setOnViewDisplay(String onViewDisplay);
public abstract String getOnDateSelected();
public abstract void setOnDateSelected(String onDaySelected);
// TODO do we use MethodBinding or MethodExpression?
public abstract MethodBinding getItemMoveListener();
public abstract void setItemMoveListener(MethodBinding listener);
public abstract MethodBinding getItemSelectedListener();
public abstract void setItemSelectedListener(MethodBinding listener);
public abstract MethodBinding getItemResizeListener();
public abstract void setItemResizeListener(MethodBinding listener);
public abstract MethodBinding getViewChangedListener();
public abstract void setViewChangedListener(MethodBinding listener);
public abstract MethodBinding getDateRangeChangedListener();
public abstract void setDateRangeChangedListener(MethodBinding listener);
public abstract MethodBinding getDateSelectedListener();
public abstract void setDateSelectedListener(MethodBinding listener);
@Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
if (event instanceof ScheduleDateRangeChangedEvent) {
super.broadcast(event);
ScheduleDateRangeChangedEvent calendarAjaxEvent = (ScheduleDateRangeChangedEvent) event;
FacesContext facesContext = getFacesContext();
AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
MethodBinding expression = getDateRangeChangedListener();
if (expression != null) {
expression.invoke(facesContext, new Object[]{event});
}
try {
ajaxContext.getResponseDataMap().put("_ajax:scheduleData", getScheduleData(calendarAjaxEvent.getStartDate(), calendarAjaxEvent.getEndDate()));
} catch (IOException ex) {
getFacesContext().getExternalContext().log("Cannot get schedule data", ex);
}
} else if (event instanceof ScheduleItemMoveEvent) {
FacesContext facesContext = getFacesContext();
AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
MethodBinding expression = getItemMoveListener();
boolean allow = true;
if (expression != null) {
Object result = expression.invoke(facesContext, new Object[]{event});
allow = ((Boolean) result);
}
ajaxContext.setResponseData(allow);
super.broadcast(event);
} else if (event instanceof ScheduleItemResizeEvent) {
FacesContext facesContext = getFacesContext();
AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
MethodBinding expression = getItemResizeListener();
boolean allow = true;
if (expression != null) {
Object result = expression.invoke(facesContext, new Object[]{event});
allow = ((Boolean) result);
}
ajaxContext.setResponseData(allow);
super.broadcast(event);
} else if (event instanceof ScheduleItemSelectedEvent) {
super.broadcast(event);
FacesContext facesContext = getFacesContext();
MethodBinding expression = getItemSelectedListener();
if (expression != null) {
expression.invoke(facesContext, new Object[]{event});
}
} else if (event instanceof ScheduleViewChangedEvent) {
super.broadcast(event);
FacesContext facesContext = getFacesContext();
MethodBinding expression = getViewChangedListener();
if (expression != null) {
expression.invoke(facesContext, new Object[]{event});
}
} else if (event instanceof ScheduleDateSelectedEvent) {
super.broadcast(event);
FacesContext facesContext = getFacesContext();
MethodBinding expression = getDateSelectedListener();
if (expression != null) {
expression.invoke(facesContext, new Object[]{event});
}
}
}
/**
* Range is [startDate;endDate), which means that start date is inclusive
* and end date is exclusive.
*
* @param startDate
* @param endDate
* @return
* @throws IOException
*/
public List<Map<String, Object>> getScheduleData(Date startDate, Date endDate) throws IOException {
/**
* Locale must be US because this is the format the javascript widget supports
*/
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
format.setLenient(false);
DataModel dataModel = getDataModel();
if (dataModel instanceof ExtendedDataModel) {
DataVisitor visitor = new DataVisitor() {
//TODO Is this fine? or should we stack rowKeys and not use dataModel later on. I'don't know business rules of extendedDataModel, just used it to did once pagination with underlying EntityQuery from Seam
public void process(FacesContext context, Object rowKey, Object argument) throws IOException {
}
};
((ExtendedDataModel) dataModel).walk(getFacesContext(), visitor, new DateRange(startDate, endDate), null);
}
ELContext elContext = getFacesContext().getELContext();
ValueExpression valueExpression = getFacesContext().getApplication().getExpressionFactory().createValueExpression(getFacesContext().getELContext(), "#{" + getVar() + "}", Object.class);
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < dataModel.getRowCount(); i++) {
dataModel.setRowIndex(i);
valueExpression.setValue(elContext, dataModel.getRowData());
Map<String, Object> firstDataElement = new HashMap<String, Object>();
// TODO shouldn't we check earlier if there is at most one renderable UIScheduleItem child?
for (UIComponent child : getChildren()) {
if (child instanceof UIScheduleItem) {
UIScheduleItem item = (UIScheduleItem) child;
if (!item.isRendered()) {
continue;
}
firstDataElement.put("id", item.getEventId());
firstDataElement.put("title", item.getTitle());
if (item.getAllDay() != null) {
firstDataElement.put("allDay", item.getAllDay());
}
firstDataElement.put("start", format.format(item.getStartDate()));
if (item.getEndDate() != null) {
firstDataElement.put("end", format.format(item.getEndDate()));
}
if (item.getUrl() != null) {
firstDataElement.put("url", item.getUrl());
}
if (item.getStyleClass() != null) {
firstDataElement.put("className", item.getStyleClass());
}
if (item.getEditable() != null) {
firstDataElement.put("editable", item.getEditable());
}
if (item.getData() != null) {
firstDataElement.put("data", item.getData());
}
data.add(firstDataElement);
}
}
}
valueExpression.setValue(elContext, null);
return data;
}
public static Date getFirstDisplayedDay(UISchedule schedule) {
Calendar calendar = Calendar.getInstance();
Date date = schedule.getDate();
if (date != null) {
calendar.setTime(date);
}
int firstDayOfWeek = getFirstDay(schedule);
calendar.setFirstDayOfWeek(firstDayOfWeek);
String view = getView(schedule);
boolean showWeekends = isShowWeekends(schedule);
if (VIEW_MONTH.equals(view)) {
calendar.set(Calendar.DATE, 1);
if (!showWeekends) {
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY) {
calendar.add(Calendar.DATE, 1);
} else if (dayOfWeek == Calendar.SATURDAY) {
calendar.add(Calendar.DATE, 2);
}
}
if (!showWeekends && firstDayOfWeek == Calendar.SUNDAY) {
firstDayOfWeek++;
}
/**
* Following 1 line is a fix to what i believe is a bug of java.util.Calendar
*/
calendar.get(Calendar.DAY_OF_WEEK);
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
return calendar.getTime();
} else if (VIEW_AGENDA_WEEK.equals(view) || VIEW_BASIC_WEEK.equals(view)) {
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
if (!showWeekends) {
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY) {
calendar.add(Calendar.DATE, 1);
} else if (dayOfWeek == Calendar.SATURDAY) {
calendar.add(Calendar.DATE, 2);
}
}
return calendar.getTime();
} else if (VIEW_AGENDA_DAY.equals(view) || VIEW_BASIC_DAY.equals(view)) {
return calendar.getTime();
} else {
throw new IllegalStateException("Invalid view attribute: " + view);
}
}
public static Date getLastDisplayedDate(UISchedule schedule) {
Calendar calendar = Calendar.getInstance();
int firstDayOfWeek = getFirstDay(schedule);
String view = getView(schedule);
boolean showWeekends = isShowWeekends(schedule);
if (VIEW_MONTH.equals(view)) {
if (WEEK_MODE_FIXED.equals(getWeekMode(schedule))) {
calendar.setTime(getFirstDisplayedDay(schedule));
calendar.add(Calendar.DAY_OF_YEAR, 7 * 6);
} else {
Date date = schedule.getDate();
if (date != null) {
calendar.setTime(date);
}
if (!showWeekends && firstDayOfWeek == Calendar.SUNDAY) {
firstDayOfWeek++;
}
calendar.setFirstDayOfWeek(firstDayOfWeek);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
int dayOfWeek = firstDayOfWeek + 6;
if (dayOfWeek > Calendar.SATURDAY) {
dayOfWeek -= 7;
}
/**
* Following 1 line is a fix to what i believe is a bug of java.util.Calendar
*/
calendar.get(Calendar.DAY_OF_WEEK);
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.add(Calendar.DATE, 1);
}
return calendar.getTime();
} else if (VIEW_AGENDA_WEEK.equals(view) || VIEW_BASIC_WEEK.equals(view)) {
calendar.setFirstDayOfWeek(firstDayOfWeek);
calendar.setTime(getFirstDisplayedDay(schedule));
calendar.add(Calendar.DATE, 7);
if (!showWeekends) {
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek - 1 == Calendar.SUNDAY) {
calendar.add(Calendar.DATE, -2);
} else if (dayOfWeek - 1 == Calendar.SATURDAY) {
calendar.add(Calendar.DATE, -1);
}
}
return calendar.getTime();
} else if (VIEW_AGENDA_DAY.equals(view) || VIEW_BASIC_DAY.equals(view)) {
calendar.setTime(getFirstDisplayedDay(schedule));
calendar.add(Calendar.DATE, 1);
return calendar.getTime();
} else {
throw new IllegalStateException("Invalid view attribute: " + view);
}
}
/**
* Here go static methods for getting value or default value of attributes.
*/
/**
* @param schedule
* @return
*/
public static boolean isShowWeekends(UISchedule schedule) {
Boolean showWeekends = schedule.getShowWeekends();
return showWeekends == null || schedule.getShowWeekends();
}
public static int getFirstDay(UISchedule schedule) {
Integer firstDay = schedule.getFirstDay();
if (firstDay == null) {
firstDay = Calendar.SUNDAY;
}
return firstDay;
}
public static String getWeekMode(UISchedule schedule) {
String weekMode = schedule.getWeekMode();
if (weekMode == null) {
weekMode = WEEK_MODE_FIXED;
}
return weekMode;
}
public static String getView(UISchedule schedule) {
String view = schedule.getView();
if (view == null) {
view = VIEW_MONTH;
}
return view;
}
protected DataModel getDataModel() {
// Return any previously cached DataModel instance
if (this.model != null) {
return (model);
}
// Synthesize a DataModel around our current value if possible
Object current = getValue();
if (current == null) {
setDataModel(new ListDataModel(Collections.EMPTY_LIST));
} else if (current instanceof DataModel) {
setDataModel((DataModel) current);
} else if (current instanceof List) {
setDataModel(new ListDataModel((List) current));
} else if (Object[].class.isAssignableFrom(current.getClass())) {
setDataModel(new ArrayDataModel((Object[]) current));
} else if (current instanceof ResultSet) {
setDataModel(new ResultSetDataModel((ResultSet) current));
} else if (current instanceof Result) {
setDataModel(new ResultDataModel((Result) current));
} else {
setDataModel(new ScalarDataModel(current));
}
return (model);
}
public void setDataModel(DataModel model) {
this.model = model;
}
public void addItemSelectedListener(ScheduleItemSelectedListener listener) {
addFacesListener(listener);
}
public void removeItemSelectedListener(ScheduleItemSelectedListener listener) {
removeFacesListener(listener);
}
public ScheduleItemSelectedListener[] getItemSelectedListeners() {
return (ScheduleItemSelectedListener[]) getFacesListeners(ScheduleItemSelectedListener.class);
}
public void addItemMoveListener(ScheduleItemMoveListener listener) {
addFacesListener(listener);
}
public void removeItemMoveListener(ScheduleItemMoveListener listener) {
removeFacesListener(listener);
}
public ScheduleItemMoveListener[] getItemMoveListeners() {
return (ScheduleItemMoveListener[]) getFacesListeners(ScheduleItemMoveListener.class);
}
public void addItemResizeListener(ScheduleItemResizeListener listener) {
addFacesListener(listener);
}
public void removeItemResizeListener(ScheduleItemResizeListener listener) {
removeFacesListener(listener);
}
public ScheduleItemResizeListener[] getItemResizeListeners() {
return (ScheduleItemResizeListener[]) getFacesListeners(ScheduleItemResizeListener.class);
}
public void addViewChangedListener(ScheduleViewChangedListener listener) {
addFacesListener(listener);
}
public void removeViewChangedListener(ScheduleViewChangedListener listener) {
removeFacesListener(listener);
}
public ScheduleViewChangedListener[] getViewChangedListeners() {
return (ScheduleViewChangedListener[]) getFacesListeners(ScheduleViewChangedListener.class);
}
public void addDateRangeChangedListener(ScheduleDateRangeChangedListener listener) {
addFacesListener(listener);
}
public void removeDateRangeChangedListener(ScheduleDateRangeChangedListener listener) {
removeFacesListener(listener);
}
public ScheduleDateRangeChangedListener[] getDateRangeChangedListeners() {
return (ScheduleDateRangeChangedListener[]) getFacesListeners(ScheduleDateRangeChangedListener.class);
}
public void addDateSelectedListener(ScheduleDateSelectedListener listener) {
addFacesListener(listener);
}
public void removeDateSelectedListener(ScheduleDateSelectedListener listener) {
removeFacesListener(listener);
}
public ScheduleDateSelectedListener[] getDateSelectedListeners() {
return (ScheduleDateSelectedListener[]) getFacesListeners(ScheduleDateSelectedListener.class);
}
}