001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/calendar/tags/sakai_2-4-1/calendar-summary-tool/tool/src/java/org/sakaiproject/tool/summarycalendar/ui/CalendarBean.java $
003: * $Id: CalendarBean.java 29637 2007-04-26 15:04:53Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.summarycalendar.ui;
021:
022: import java.text.DateFormat;
023: import java.text.SimpleDateFormat;
024: import java.util.ArrayList;
025: import java.util.Calendar;
026: import java.util.Collection;
027: import java.util.Date;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.ListIterator;
032: import java.util.Map;
033:
034: import javax.faces.context.ExternalContext;
035: import javax.faces.context.FacesContext;
036: import javax.faces.event.ActionEvent;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040: import org.sakaiproject.authz.api.SecurityService;
041: import org.sakaiproject.calendar.api.CalendarEvent;
042: import org.sakaiproject.calendar.api.CalendarEventVector;
043: import org.sakaiproject.calendar.api.CalendarService;
044: import org.sakaiproject.component.cover.ComponentManager;
045: import org.sakaiproject.component.cover.ServerConfigurationService;
046: import org.sakaiproject.exception.IdUnusedException;
047: import org.sakaiproject.exception.PermissionException;
048: import org.sakaiproject.site.api.Group;
049: import org.sakaiproject.site.api.Site;
050: import org.sakaiproject.site.api.SiteService;
051: import org.sakaiproject.site.api.ToolConfiguration;
052: import org.sakaiproject.time.api.Time;
053: import org.sakaiproject.time.api.TimeRange;
054: import org.sakaiproject.time.api.TimeService;
055: import org.sakaiproject.tool.api.Placement;
056: import org.sakaiproject.tool.api.ToolManager;
057: import org.sakaiproject.util.MergedList;
058: import org.sakaiproject.util.ResourceLoader;
059:
060: public class CalendarBean {
061:
062: public static final String MODE_MONTHVIEW = "month";
063: public static final String MODE_WEEKVIEW = "week";
064: public static final String PRIORITY_HIGH = "priority_high";
065: public static final String PRIORITY_MEDIUM = "priority_medium";
066: public static final String PRIORITY_LOW = "priority_low";
067: public static final String DATE_FORMAT = "MMM dd, yyyy";
068: private static final String imgLocation = "../../../library/image/sakai/";
069: private static final String SCHEDULE_TOOL_ID = "sakai.schedule";
070:
071: /** Our log (commons). */
072: private static Log LOG = LogFactory.getLog(CalendarBean.class);
073:
074: /** Resource bundle */
075: private transient ResourceLoader msgs = new ResourceLoader(
076: "org.sakaiproject.tool.summarycalendar.bundle.Messages");
077:
078: /** Bean members */
079: private String viewMode = MODE_MONTHVIEW;
080: private String prevViewMode = null;
081: private Date today = null;
082: private Date viewingDate = null;
083: private Date selectedDay = null;
084: private boolean selectedDayHasEvents = false;
085: private String selectedEventRef = null;
086: private String selectedCalendarRef = null;
087: private EventSummary selectedEvent = null;
088:
089: /** Private members */
090: private boolean updateEventList = true;
091: private List weeks = new ArrayList();
092: private MonthWeek week1 = new MonthWeek();
093: private MonthWeek week2 = new MonthWeek();
094: private MonthWeek week3 = new MonthWeek();
095: private MonthWeek week4 = new MonthWeek();
096: private MonthWeek week5 = new MonthWeek();
097: private MonthWeek week6 = new MonthWeek();
098: private List calendarReferences = new ArrayList();
099: private CalendarEventVector calendarEventVector = null;
100: private String siteId = null;
101: private String[] months = { "mon_jan", "mon_feb", "mon_mar",
102: "mon_apr", "mon_may", "mon_jun", "mon_jul", "mon_aug",
103: "mon_sep", "mon_oct", "mon_nov", "mon_dec" };
104: private Map eventImageMap = new HashMap();
105:
106: private long lastModifiedPrefs = 0l;
107: private Map priorityColorsMap = null;
108: private String highPrCSSProp = "";
109: private String mediumPrCSSProp = "";
110: private String lowPrCSSProp = "";
111: private Map priorityEventsMap = null;
112: private List highPriorityEvents = null;
113: private List mediumPriorityEvents = null;
114: private List lowPriorityEvents = null;
115:
116: /** Sakai services */
117: private transient CalendarService M_ca = (CalendarService) ComponentManager
118: .get(CalendarService.class.getName());
119: private transient TimeService M_ts = (TimeService) ComponentManager
120: .get(TimeService.class.getName());
121: private transient SiteService M_ss = (SiteService) ComponentManager
122: .get(SiteService.class.getName());
123: private transient SecurityService M_as = (SecurityService) ComponentManager
124: .get(SecurityService.class.getName());
125: private transient ToolManager M_tm = (ToolManager) ComponentManager
126: .get(ToolManager.class.getName());
127:
128: // ######################################################################################
129: // Main methods
130: // ######################################################################################
131: public CalendarBean() {
132: // go to today events it is first time loading
133: selectedDay = getToday();
134: }
135:
136: public String getInitValues() {
137: long lastModified = PrefsBean.getPreferenceLastModified();
138: if (lastModifiedPrefs != lastModified)
139: readPreferences();
140:
141: // re-read events from API for selected month/week
142: calendarEventVector = null;
143:
144: return "";
145: }
146:
147: // ######################################################################################
148: // Private methods
149: // ######################################################################################
150: private void readPreferences() {
151: LOG.debug("Reading preferences...");
152: lastModifiedPrefs = PrefsBean.getPreferenceLastModified();
153:
154: // view mode
155: prevViewMode = viewMode;
156: viewMode = PrefsBean.getPreferenceViewMode();
157:
158: // priority colors (CSS properties)
159: priorityColorsMap = PrefsBean.getPreferencePriorityColors();
160: highPrCSSProp = (String) priorityColorsMap
161: .get(PrefsBean.PREFS_HIGHPRIORITY_COLOR);
162: mediumPrCSSProp = (String) priorityColorsMap
163: .get(PrefsBean.PREFS_MEDIUMPRIORITY_COLOR);
164: lowPrCSSProp = (String) priorityColorsMap
165: .get(PrefsBean.PREFS_LOWPRIORITY_COLOR);
166:
167: highPrCSSProp = highPrCSSProp.equals("") ? ""
168: : "background-color: " + highPrCSSProp;
169: mediumPrCSSProp = mediumPrCSSProp.equals("") ? ""
170: : "background-color: " + mediumPrCSSProp;
171: lowPrCSSProp = lowPrCSSProp.equals("") ? ""
172: : "background-color: " + lowPrCSSProp;
173:
174: // priority events
175: priorityEventsMap = PrefsBean.getPreferencePriorityEvents();
176: highPriorityEvents = (List) priorityEventsMap
177: .get(PrefsBean.PREFS_HIGHPRIORITY_EVENTS);
178: mediumPriorityEvents = (List) priorityEventsMap
179: .get(PrefsBean.PREFS_MEDIUMPRIORITY_EVENTS);
180: lowPriorityEvents = (List) priorityEventsMap
181: .get(PrefsBean.PREFS_LOWPRIORITY_EVENTS);
182: }
183:
184: private List getCalendarReferences() {
185: if (calendarReferences == null
186: || calendarReferences.size() == 0) {
187: MergedList mergedCalendarList = new MergedList();
188:
189: String[] channelArray = null;
190: boolean isOnWorkspaceTab = M_ss.isUserSite(getSiteId());
191: boolean isSuperUser = M_as.isSuperUser();
192:
193: // Figure out the list of channel references that we'll be using.
194: // If we're on the workspace tab, we get everything.
195: // Don't do this if we're the super-user, since we'd be
196: // overwhelmed.
197: calendarReferences = new ArrayList();
198: if (isOnWorkspaceTab && !isSuperUser) {
199: channelArray = mergedCalendarList
200: .getAllPermittedChannels(new CalendarChannelReferenceMaker());
201: if (channelArray != null) {
202: for (int i = 0; i < channelArray.length; i++)
203: calendarReferences.add(channelArray[i]);
204: }
205: }
206:
207: // add current site
208: calendarReferences.add(M_ca.calendarReference(getSiteId(),
209: SiteService.MAIN_CONTAINER));
210: }
211: return calendarReferences;
212: }
213:
214: /*
215: * Callback class so that we can form references in a generic way. Method
216: * copied from Calendar legacy module: CalendarAction.java
217: */
218: private final class CalendarChannelReferenceMaker implements
219: MergedList.ChannelReferenceMaker {
220: public String makeReference(String siteId) {
221: return M_ca.calendarReference(siteId,
222: SiteService.MAIN_CONTAINER);
223: }
224: }
225:
226: private String getSiteId() {
227: if (siteId == null) {
228: Placement placement = M_tm.getCurrentPlacement();
229: siteId = placement.getContext();
230: }
231: return siteId;
232: }
233:
234: private CalendarEventVector getEventsFromSchedule() {
235: if (calendarEventVector == null) {
236: Calendar firstDay;
237: Calendar lastDay;
238:
239: if (viewMode.equals(MODE_WEEKVIEW)) {
240: // WEEK VIEW
241:
242: // select first day
243: firstDay = Calendar.getInstance();
244: firstDay.setTime(getViewingDate());
245: firstDay.set(Calendar.HOUR_OF_DAY, 0);
246: firstDay.set(Calendar.MINUTE, 0);
247: firstDay.set(Calendar.SECOND, 0);
248: firstDay.set(Calendar.MILLISECOND, 0);
249: int dayOfWeek = firstDay.get(Calendar.DAY_OF_WEEK);
250: // TODO Allow dynamic choice of first day of week
251: while (dayOfWeek != Calendar.SUNDAY) {
252: firstDay.add(Calendar.DAY_OF_WEEK, -1);
253: dayOfWeek = firstDay.get(Calendar.DAY_OF_WEEK);
254: }
255:
256: // select last day
257: lastDay = (Calendar) firstDay.clone();
258: lastDay.add(Calendar.DAY_OF_WEEK, 6);
259: lastDay.set(Calendar.HOUR_OF_DAY, 23);
260: lastDay.set(Calendar.MINUTE, 59);
261: lastDay.set(Calendar.SECOND, 59);
262: lastDay.set(Calendar.MILLISECOND, 999);
263: } else {
264: // MONTH VIEW
265:
266: // select first day
267: firstDay = Calendar.getInstance();
268: firstDay.setTime(getViewingDate());
269: int selYear = firstDay.get(Calendar.YEAR);
270: int selMonth = firstDay.get(Calendar.MONTH);
271: firstDay.set(Calendar.YEAR, selYear);
272: firstDay.set(Calendar.DAY_OF_MONTH, 1);
273: firstDay.set(Calendar.HOUR_OF_DAY, 0);
274: firstDay.set(Calendar.MINUTE, 0);
275: firstDay.set(Calendar.SECOND, 0);
276: firstDay.set(Calendar.MILLISECOND, 0);
277: int dayOfWeek = firstDay.get(Calendar.DAY_OF_WEEK);
278: // TODO Allow dynamic choice of first day of week
279: while (dayOfWeek != Calendar.SUNDAY) {
280: firstDay.add(Calendar.DAY_OF_WEEK, -1);
281: dayOfWeek = firstDay.get(Calendar.DAY_OF_WEEK);
282: }
283:
284: // select last day
285: lastDay = (Calendar) firstDay.clone();
286: lastDay.set(Calendar.YEAR, selYear);
287: lastDay.set(Calendar.MONTH, selMonth);
288: lastDay.set(Calendar.DAY_OF_MONTH, lastDay
289: .getActualMaximum(Calendar.DAY_OF_MONTH));
290: lastDay.set(Calendar.HOUR_OF_DAY, 23);
291: lastDay.set(Calendar.MINUTE, 59);
292: lastDay.set(Calendar.SECOND, 59);
293: lastDay.set(Calendar.MILLISECOND, 999);
294: dayOfWeek = lastDay.get(Calendar.DAY_OF_WEEK);
295: // TODO Allow dynamic choice of first day of week
296: while (dayOfWeek != Calendar.SATURDAY) {
297: lastDay.add(Calendar.DAY_OF_WEEK, 1);
298: dayOfWeek = lastDay.get(Calendar.DAY_OF_WEEK);
299: }
300: }
301:
302: Time firstTime = M_ts.newTime(firstDay.getTimeInMillis());
303: Time lastTime = M_ts.newTime(lastDay.getTimeInMillis());
304: TimeRange range = M_ts.newTimeRange(firstTime, lastTime);
305: calendarEventVector = M_ca.getEvents(
306: getCalendarReferences(), range);
307: }
308: return calendarEventVector;
309: }
310:
311: private CalendarEventVector getScheduleEventsForDay(Calendar c) {
312: CalendarEventVector cev = new CalendarEventVector();
313:
314: // find start and end of day
315: Calendar startOfDay = (Calendar) c.clone();
316: startOfDay.set(Calendar.HOUR_OF_DAY, 0);
317: startOfDay.set(Calendar.MINUTE, 0);
318: startOfDay.set(Calendar.SECOND, 0);
319: startOfDay.set(Calendar.MILLISECOND, 0);
320: Time sod = M_ts.newTime(startOfDay.getTimeInMillis());
321: Calendar endOfDay = (Calendar) c.clone();
322: endOfDay.set(Calendar.HOUR_OF_DAY, 23);
323: endOfDay.set(Calendar.MINUTE, 59);
324: endOfDay.set(Calendar.SECOND, 59);
325: endOfDay.set(Calendar.MILLISECOND, 999);
326: Time eod = M_ts.newTime(endOfDay.getTimeInMillis());
327: TimeRange range = M_ts.newTimeRange(sod, eod);
328:
329: Iterator i = getEventsFromSchedule().iterator();
330: while (i.hasNext()) {
331: CalendarEvent ce = (CalendarEvent) i.next();
332: TimeRange tr = ce.getRange();
333: if (range.contains(tr.firstTime())
334: || range.contains(tr.lastTime())) {
335: cev.add(ce);
336: }
337: }
338: return cev;
339: }
340:
341: // }
342:
343: private List getDayEvents(CalendarEventVector dayEventVector) {
344: ListIterator i = dayEventVector.listIterator();
345: List eventList = new ArrayList();
346: while (i.hasNext()) {
347: CalendarEvent e = (CalendarEvent) i.next();
348: EventSummary es = new EventSummary();
349: es.setDisplayName(e.getDisplayName());
350: es.setType(e.getType());
351: es.setCalendarRef(e.getCalendarReference());
352: es.setEventRef(e.getId());
353: es.setUrl(e.getUrl());
354: es.setAttachments(e.getAttachments());
355: eventList.add(es);
356: }
357: return eventList;
358: }
359:
360: private int getDayEventCount(CalendarEventVector dayEventVector) {
361: return dayEventVector.size();
362: }
363:
364: private String getDayPriorityCSSProperty(
365: CalendarEventVector dayEventVector) {
366: ListIterator i = dayEventVector.listIterator();
367: String highestPriorityFound = "";
368: while (i.hasNext()) {
369: CalendarEvent e = (CalendarEvent) i.next();
370: String type = e.getType();
371: if (highPriorityEvents != null
372: && highPriorityEvents.contains(type))
373: highestPriorityFound = PRIORITY_HIGH;
374: else if (mediumPriorityEvents != null
375: && mediumPriorityEvents.contains(type)
376: && !highestPriorityFound.equals(PRIORITY_HIGH))
377: highestPriorityFound = PRIORITY_MEDIUM;
378: else if (lowPriorityEvents != null
379: && lowPriorityEvents.contains(type)
380: && !highestPriorityFound.equals(PRIORITY_HIGH)
381: && !highestPriorityFound.equals(PRIORITY_MEDIUM))
382: highestPriorityFound = PRIORITY_LOW;
383:
384: if (highestPriorityFound.equals(PRIORITY_HIGH))
385: break;
386: }
387: if (highestPriorityFound.equals(""))
388: return "";
389: else if (highestPriorityFound.equals(PRIORITY_LOW))
390: return lowPrCSSProp;
391: else if (highestPriorityFound.equals(PRIORITY_MEDIUM))
392: return mediumPrCSSProp;
393: else if (highestPriorityFound.equals(PRIORITY_HIGH))
394: return highPrCSSProp;
395: return "";
396: }
397:
398: // ######################################################################################
399: // Action/ActionListener methods
400: // ######################################################################################
401: public void currDay(ActionEvent e) {
402: Calendar cal = Calendar.getInstance();
403: setViewingDate(cal.getTime());
404: // show events for today if any
405: selectedDay = getToday();
406: selectedEventRef = null;
407: updateEventList = true;
408: }
409:
410: public void prev(ActionEvent e) {
411: updateEventList = true;
412: if (viewMode.equals(MODE_WEEKVIEW)) {
413: // week view
414: prevWeek(e);
415: } else {
416: // month view
417: prevMonth(e);
418: }
419: }
420:
421: public void next(ActionEvent e) {
422: updateEventList = true;
423: if (viewMode.equals(MODE_WEEKVIEW)) {
424: // week view
425: nextWeek(e);
426: } else {
427: // month view
428: nextMonth(e);
429: }
430: }
431:
432: private void prevMonth(ActionEvent e) {
433: Calendar cal = Calendar.getInstance();
434: cal.setTime(viewingDate);
435: cal.add(Calendar.MONTH, -1);
436: setViewingDate(cal.getTime());
437: selectedDay = null;
438: selectedEventRef = null;
439: }
440:
441: private void nextMonth(ActionEvent e) {
442: Calendar cal = Calendar.getInstance();
443: cal.setTime(viewingDate);
444: cal.add(Calendar.MONTH, +1);
445: setViewingDate(cal.getTime());
446: selectedDay = null;
447: selectedEventRef = null;
448: }
449:
450: private void prevWeek(ActionEvent e) {
451: Calendar cal = Calendar.getInstance();
452: cal.setTime(viewingDate);
453: cal.add(Calendar.WEEK_OF_YEAR, -1);
454: setViewingDate(cal.getTime());
455: selectedDay = null;
456: selectedEventRef = null;
457: }
458:
459: private void nextWeek(ActionEvent e) {
460: Calendar cal = Calendar.getInstance();
461: cal.setTime(viewingDate);
462: cal.add(Calendar.WEEK_OF_YEAR, +1);
463: setViewingDate(cal.getTime());
464: selectedDay = null;
465: selectedEventRef = null;
466: }
467:
468: public void selectDate(ActionEvent e) {
469: try {
470: ExternalContext context = FacesContext.getCurrentInstance()
471: .getExternalContext();
472: Map paramMap = context.getRequestParameterMap();
473: String dateStr = (String) paramMap.get("selectedDay");
474: DateFormat df = new SimpleDateFormat(DATE_FORMAT);
475: selectedDay = df.parse(dateStr);
476: selectedEventRef = null;
477: updateEventList = true;
478: } catch (Exception ex) {
479: LOG.error("Error getting selectedDate:" + ex.toString());
480: }
481: }
482:
483: public void selectEvent(ActionEvent e) {
484: try {
485: ExternalContext context = FacesContext.getCurrentInstance()
486: .getExternalContext();
487: Map paramMap = context.getRequestParameterMap();
488: selectedCalendarRef = (String) paramMap.get("calendarRef");
489: selectedEventRef = (String) paramMap.get("eventRef");
490: selectedEvent = null;
491: updateEventList = false;
492: } catch (Exception ex) {
493: LOG
494: .error("Error getting selectedEventRef:"
495: + ex.toString());
496: }
497: }
498:
499: public void backToEventList(ActionEvent e) {
500: try {
501: selectedEventRef = null;
502: updateEventList = true;
503: } catch (Exception ex) {
504: LOG.error("Error in backToEventList:" + ex.toString());
505: }
506: }
507:
508: public String getViewMode() {
509: return viewMode;
510: }
511:
512: public void setViewMode(String viewMode) {
513: this .viewMode = viewMode;
514: }
515:
516: // ######################################################################################
517: // Generic get/set methods
518: // ######################################################################################
519: private void initializeWeeksDataStructure() {
520: weeks = new ArrayList();
521: weeks.add(week1);
522: weeks.add(week2);
523: weeks.add(week3);
524: weeks.add(week4);
525: weeks.add(week5);
526: weeks.add(week6);
527: for (int w = 0; w < 6; w++) {
528: MonthWeek week = (MonthWeek) weeks.get(w);
529: for (int d = 0; d < 7; d++) {
530: week.setDay(d, new Day());
531: }
532: weeks.set(w, week);
533: }
534: }
535:
536: public List getCalendar() {
537: if (viewMode.equals(MODE_WEEKVIEW)) {
538: // WEEK VIEW
539: return getWeek();
540: } else {
541: // MONTH VIEW
542: return getWeeks();
543: }
544: }
545:
546: private List getWeeks() {
547: if (reloadCalendarEvents()) {
548: initializeWeeksDataStructure();
549:
550: // selected month
551: Calendar c = Calendar.getInstance();
552: c.setTime(getViewingDate());
553: int selYear = c.get(Calendar.YEAR);
554: int selMonth = c.get(Calendar.MONTH);
555: c.set(Calendar.MONTH, selMonth);
556: c.set(Calendar.DAY_OF_MONTH, 1);
557: int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
558: int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
559: int currDay = 1;
560: // prev month
561: c.add(Calendar.MONTH, -1);
562: int prevMonthLastDay = c
563: .getActualMaximum(Calendar.DAY_OF_MONTH);
564:
565: // fill weeks
566: int weekNo = -1;
567: int nextMonthDay = 1;
568: while (currDay <= lastDay) {
569: MonthWeek week = null;
570: try {
571: ++weekNo;
572: week = (MonthWeek) weeks.get(weekNo);
573: } catch (IndexOutOfBoundsException e) {
574: // if previous had less weeks (4, 5)
575: if (weekNo == 4) {
576: weeks.add(week5);
577: week = week5;
578: }
579: if (weekNo == 5) {
580: weeks.add(week6);
581: week = week6;
582: }
583: }
584: for (int i = 0; i < 7; i++) {
585: Day day = week.getDay(i);
586: if ((weekNo == 0) && (i < dayOfWeek - 1)) {
587: int nDay = prevMonthLastDay - dayOfWeek + 2 + i;
588: c.set(Calendar.MONTH, selMonth - 1);
589: c.set(Calendar.DAY_OF_MONTH, nDay);
590: CalendarEventVector vector = getScheduleEventsForDay(c);
591: day = new Day(c.getTime(),
592: getDayEventCount(vector) > 0);
593: day.setOccursInOtherMonth(true);
594: day
595: .setBackgroundCSSProperty(getDayPriorityCSSProperty(vector));
596: } else if (currDay > lastDay) {
597: c.set(Calendar.MONTH, selMonth + 1);
598: c.set(Calendar.DAY_OF_MONTH, nextMonthDay++);
599: CalendarEventVector vector = getScheduleEventsForDay(c);
600: day = new Day(c.getTime(),
601: getDayEventCount(vector) > 0);
602: day.setOccursInOtherMonth(true);
603: day
604: .setBackgroundCSSProperty(getDayPriorityCSSProperty(vector));
605: } else {
606: c.set(Calendar.YEAR, selYear);
607: c.set(Calendar.MONTH, selMonth);
608: c.set(Calendar.DAY_OF_MONTH, currDay++);
609: CalendarEventVector vector = getScheduleEventsForDay(c);
610: day = new Day(c.getTime(),
611: getDayEventCount(vector) > 0);
612: day.setOccursInOtherMonth(false);
613: day
614: .setBackgroundCSSProperty(getDayPriorityCSSProperty(vector));
615: }
616: day.setToday(sameDay(c, getToday()));
617: day.setSelected(selectedDay != null
618: && sameDay(c, selectedDay));
619: week.setDay(i, day);
620:
621: }
622: weeks.set(weekNo, week);
623: }
624: // remove unused weeks (5th, 6th week of month)
625: while (((weeks.size() - 1) - weekNo) > 0) {
626: weeks.remove(weeks.size() - 1);
627: }
628: }
629: return weeks;
630: }
631:
632: private List getWeek() {
633: if (reloadCalendarEvents()) {
634: // initialize days
635: weeks = new ArrayList();
636: for (int d = 0; d < 7; d++) {
637: week1.setDay(d, new Day());
638: }
639: weeks.add(week1);
640:
641: // selected week
642: Calendar c = Calendar.getInstance();
643: c.setTime(getViewingDate());
644: int selMonth = c.get(Calendar.MONTH);
645: int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
646: //int selWeek = c.get(Calendar.WEEK_OF_YEAR);
647:
648: // select Sunday (first day of week)
649: // TODO Allow dynamic choice of first day of week
650: while (dayOfWeek != Calendar.SUNDAY) {
651: c.add(Calendar.DAY_OF_WEEK, -1);
652: dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
653: }
654:
655: for (int i = 0; i < 7; i++) {
656: Day day = (Day) week1.getDay(i);
657: boolean sameMonth = (selMonth == c.get(Calendar.MONTH));
658: boolean selected = (selectedDay != null)
659: && (sameDay(c, selectedDay));
660:
661: CalendarEventVector vector = getScheduleEventsForDay(c);
662: day = new Day(c.getTime(), getDayEventCount(vector) > 0);
663: day.setOccursInOtherMonth(!sameMonth);
664: day
665: .setBackgroundCSSProperty(getDayPriorityCSSProperty(vector));
666: day.setToday(sameDay(c, getToday()));
667: day.setSelected(selected);
668:
669: week1.setDay(i, day);
670: c.add(Calendar.DAY_OF_WEEK, +1);
671: }
672: weeks.set(0, week1);
673: }
674: return weeks;
675: }
676:
677: private boolean reloadCalendarEvents() {
678: boolean reload = (weeks == null) || (weeks.size() == 0)
679: || updateEventList || (prevViewMode != viewMode);
680: return reload;
681: }
682:
683: private boolean sameDay(Calendar date1, Date date2) {
684: date1.set(Calendar.HOUR_OF_DAY, 0);
685: date1.set(Calendar.MINUTE, 0);
686: date1.set(Calendar.SECOND, 0);
687:
688: Calendar cal = Calendar.getInstance();
689: cal.setTime(date2);
690: cal.set(Calendar.HOUR_OF_DAY, 0);
691: cal.set(Calendar.MINUTE, 0);
692: cal.set(Calendar.SECOND, 0);
693:
694: return (cal.get(Calendar.YEAR) == date1.get(Calendar.YEAR))
695: && (cal.get(Calendar.MONTH) == date1
696: .get(Calendar.MONTH))
697: && (cal.get(Calendar.DAY_OF_MONTH) == date1
698: .get(Calendar.DAY_OF_MONTH));
699: }
700:
701: public Date getToday() {
702: if (today == null) {
703: Calendar c = Calendar.getInstance();
704: today = c.getTime();
705: }
706: return today;
707: }
708:
709: public String getCaption() {
710: Calendar c = Calendar.getInstance();
711: c.setTime(getViewingDate());
712: String month = msgs.getString(months[c.get(Calendar.MONTH)]);
713: String year = c.get(Calendar.YEAR) + "";
714: return month + ", " + year;
715: }
716:
717: public boolean isViewingSelectedDay() {
718: if (selectedDay != null) {
719: Calendar t = Calendar.getInstance();
720: t.setTime(selectedDay);
721: selectedDayHasEvents = getScheduleEventsForDay(t).size() > 0;
722: }
723:
724: /*return selectedDayHasEvents && selectedDay != null && selectedEventRef == null;*/
725: return selectedDayHasEvents && selectedDay != null
726: && selectedEventRef == null;
727: }
728:
729: public boolean isViewingSelectedEvent() {
730: return selectedEventRef != null;
731: }
732:
733: public String getSelectedDayAsString() {
734: SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
735: return formatter.format(selectedDay);
736: }
737:
738: public List getSelectedDayEvents() {
739: Calendar c = Calendar.getInstance();
740: c.setTime(selectedDay);
741: return getDayEvents(getScheduleEventsForDay(c));
742: }
743:
744: public EventSummary getSelectedEvent() {
745: if (selectedEvent == null) {
746: try {
747: org.sakaiproject.calendar.api.Calendar calendar = M_ca
748: .getCalendar(selectedCalendarRef);
749: CalendarEvent event = calendar
750: .getEvent(selectedEventRef);
751: selectedEvent = new EventSummary();
752: selectedEvent.setDisplayName(event.getDisplayName());
753: selectedEvent.setDate(event.getRange());
754: selectedEvent.setType(event.getType());
755: selectedEvent.setDescription(event.getDescription());
756: selectedEvent.setLocation(event.getLocation());
757: Site site = M_ss.getSite(calendar.getContext());
758: selectedEvent.setSite(site.getTitle());
759: String eventUrl = buildEventUrl(site, event
760: .getReference());
761: selectedEvent.setUrl(eventUrl);
762: selectedEvent.setAttachments(event.getAttachments());
763: // groups
764: if (M_as.unlock("calendar.all.groups", "/site/"
765: + calendar.getContext())) {
766: Collection grps = event.getGroupObjects();
767: if (grps.size() > 0) {
768: StringBuffer sb = new StringBuffer();
769: Iterator gi = grps.iterator();
770: while (gi.hasNext()) {
771: Group g = (Group) gi.next();
772: if (sb.length() > 0)
773: sb.append(", ");
774: sb.append(g.getTitle());
775: }
776: selectedEvent.setGroups(sb.toString());
777: }
778: }
779:
780: } catch (IdUnusedException e) {
781: LOG.error("IdUnusedException: " + e.getMessage());
782: } catch (PermissionException e) {
783: e.printStackTrace();
784: LOG.error("Permission exception: " + e.getMessage());
785: }
786: }
787: return selectedEvent;
788: }
789:
790: private String buildEventUrl(Site site, String eventRef) {
791: StringBuffer url = new StringBuffer();
792: ToolConfiguration tc = site
793: .getToolForCommonId(SCHEDULE_TOOL_ID);
794: url.append(ServerConfigurationService.getPortalUrl());
795: url.append("/directtool/");
796: url.append(tc.getId());
797: url.append("?eventReference=");
798: url.append(eventRef);
799: url.append("&panel=Main&sakai_action=doDescription");
800:
801: return url.toString();
802: }
803:
804: public Map getEventImageMap() {
805: if (eventImageMap == null || eventImageMap.size() == 0) {
806: eventImageMap = new HashMap();
807: eventImageMap.put("Academic Calendar", imgLocation
808: + "academic_calendar.gif");
809: eventImageMap.put("Activity", imgLocation + "activity.gif");
810: eventImageMap.put("Cancellation", imgLocation
811: + "cancelled.gif");
812: eventImageMap.put("Class section - Discussion", imgLocation
813: + "class_dis.gif");
814: eventImageMap.put("Class section - Lab", imgLocation
815: + "class_lab.gif");
816: eventImageMap.put("Class section - Lecture", imgLocation
817: + "class_lec.gif");
818: eventImageMap.put("Class section - Small Group",
819: imgLocation + "class_sma.gif");
820: eventImageMap.put("Class session", imgLocation
821: + "class_session.gif");
822: eventImageMap.put("Computer Session", imgLocation
823: + "computersession.gif");
824: eventImageMap.put("Deadline", imgLocation + "deadline.gif");
825: eventImageMap.put("Exam", imgLocation + "exam.gif");
826: eventImageMap.put("Meeting", imgLocation + "meeting.gif");
827: eventImageMap.put("Multidisciplinary Conference",
828: imgLocation + "multi-conference.gif");
829: eventImageMap.put("Quiz", imgLocation + "quiz.gif");
830: eventImageMap.put("Special event", imgLocation
831: + "special_event.gif");
832: eventImageMap.put("Web Assignment", imgLocation
833: + "webassignment.gif");
834: }
835: return eventImageMap;
836: }
837:
838: public String getImgLocation() {
839: return this .imgLocation;
840: }
841:
842: public Date getViewingDate() {
843: if (viewingDate == null) {
844: Calendar c = Calendar.getInstance();
845: viewingDate = c.getTime();
846: }
847: return viewingDate;
848: }
849:
850: public void setViewingDate(Date selectedMonth) {
851: this.viewingDate = selectedMonth;
852: }
853:
854: }
|