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/PrefsBean.java $
003: * $Id: PrefsBean.java 27605 2007-03-22 12:23:08Z nuno@ufp.pt $
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.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import javax.faces.application.FacesMessage;
034: import javax.faces.application.FacesMessage.Severity;
035: import javax.faces.context.FacesContext;
036: import javax.faces.el.ValueBinding;
037: import javax.faces.model.SelectItem;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.sakaiproject.component.api.ServerConfigurationService;
042: import org.sakaiproject.component.cover.ComponentManager;
043: import org.sakaiproject.entity.api.ResourceProperties;
044: import org.sakaiproject.entity.api.ResourcePropertiesEdit;
045: import org.sakaiproject.exception.IdUnusedException;
046: import org.sakaiproject.tool.api.SessionManager;
047: import org.sakaiproject.user.api.Preferences;
048: import org.sakaiproject.user.api.PreferencesEdit;
049: import org.sakaiproject.user.api.PreferencesService;
050: import org.sakaiproject.util.ResourceLoader;
051:
052: public class PrefsBean {
053:
054: /** Preferences properties */
055: public static String PREFS_KEY = "sakai:calendar:calendar-summary";
056: public static String PREFS_LAST_MODIFIED = "lastModified";
057: public static String PREFS_VIEW_MODE = "viewMode";
058: public static String PREFS_HIGHPRIORITY_COLOR = "highPriorityColor";
059: public static String PREFS_MEDIUMPRIORITY_COLOR = "mediumPriorityColor";
060: public static String PREFS_LOWPRIORITY_COLOR = "lowPriorityColor";
061: public static String PREFS_HIGHPRIORITY_EVENTS = "highPriorityEvents";
062: public static String PREFS_MEDIUMPRIORITY_EVENTS = "mediumPriorityEvents";
063: public static String PREFS_LOWPRIORITY_EVENTS = "lowPriorityEvents";
064:
065: /** sakai.properties default values */
066: public static String SAKPROP_BASE = "calendarSummary.";
067:
068: /** Our log (commons). */
069: private static Log LOG = LogFactory.getLog(PrefsBean.class);
070:
071: /** Resource bundle */
072: private transient ResourceLoader msgs = new ResourceLoader(
073: "org.sakaiproject.tool.summarycalendar.bundle.Messages");
074:
075: /** Bean members */
076: private List viewModes = null;
077: private String selectedViewMode = null;
078: private String selectedHighPrColor = null;
079: private String selectedMediumPrColor = null;
080: private String selectedLowPrColor = null;
081: private static List eventTypes = null;
082: private Collection highPriorityEvents = null;
083: private Collection mediumPriorityEvents = null;
084: private Collection lowPriorityEvents = null;
085:
086: /** Private members */
087: private String message = null;
088: private Severity messageSeverity = null;
089: private Map priorityColorsMap = null;
090: private Map priorityEventsMap = null;
091:
092: /** Sakai Services */
093: private static transient PreferencesService M_ps = (PreferencesService) ComponentManager
094: .get(PreferencesService.class.getName());
095: private static transient SessionManager M_sm = (SessionManager) ComponentManager
096: .get(SessionManager.class.getName());
097: private static transient ServerConfigurationService M_cfg = (ServerConfigurationService) ComponentManager
098: .get(ServerConfigurationService.class.getName());
099:
100: static {
101: eventTypes = new ArrayList();
102: eventTypes.add("Academic Calendar");
103: eventTypes.add("Activity");
104: eventTypes.add("Cancellation");
105: eventTypes.add("Class section - Discussion");
106: eventTypes.add("Class section - Lab");
107: eventTypes.add("Class section - Lecture");
108: eventTypes.add("Class section - Small Group");
109: eventTypes.add("Class session");
110: eventTypes.add("Computer Session");
111: eventTypes.add("Deadline");
112: eventTypes.add("Exam");
113: eventTypes.add("Meeting");
114: eventTypes.add("Multidisciplinary Conference");
115: eventTypes.add("Quiz");
116: eventTypes.add("Special event");
117: eventTypes.add("Web Assignment");
118: }
119:
120: // ######################################################################################
121: // Main methods
122: // ######################################################################################
123: public PrefsBean() {
124: }
125:
126: // ######################################################################################
127: // Action/ActionListener methods
128: // ######################################################################################
129: public boolean isMessageToBeDisplayed() {
130: if (message != null) {
131: FacesContext fc = FacesContext.getCurrentInstance();
132: fc.addMessage("msg", new FacesMessage(messageSeverity,
133: message, null));
134: message = null;
135: return true;
136: }
137: return false;
138: }
139:
140: public String update() {
141: try {
142: // read from FacesContext
143: setSelectedViewMode(getValueFromFacesContext("prefsForm:selectViewMode"));
144: setSelectedHighPriorityColor(getValueFromFacesContext("prefsForm:highPriorityColor"));
145: setSelectedMediumPriorityColor(getValueFromFacesContext("prefsForm:mediumPriorityColor"));
146: setSelectedLowPriorityColor(getValueFromFacesContext("prefsForm:lowPriorityColor"));
147: setSelectedHighPriorityEvents(getValuesFromFacesContext("prefsForm:highPriorityEvents"));
148: setSelectedMediumPriorityEvents(getValuesFromFacesContext("prefsForm:mediumPriorityEvents"));
149: setSelectedLowPriorityEvents(getValuesFromFacesContext("prefsForm:lowPriorityEvents"));
150:
151: // update User Preferences
152: setPreferenceString(PREFS_VIEW_MODE, selectedViewMode);
153: setPreferenceString(PREFS_HIGHPRIORITY_COLOR,
154: selectedHighPrColor);
155: setPreferenceString(PREFS_MEDIUMPRIORITY_COLOR,
156: selectedMediumPrColor);
157: setPreferenceString(PREFS_LOWPRIORITY_COLOR,
158: selectedLowPrColor);
159: clearPreferenceList(PREFS_HIGHPRIORITY_EVENTS);
160: clearPreferenceList(PREFS_MEDIUMPRIORITY_EVENTS);
161: clearPreferenceList(PREFS_LOWPRIORITY_EVENTS);
162: setPreferenceList(PREFS_HIGHPRIORITY_EVENTS,
163: highPriorityEvents);
164: setPreferenceList(PREFS_MEDIUMPRIORITY_EVENTS,
165: mediumPriorityEvents);
166: setPreferenceList(PREFS_LOWPRIORITY_EVENTS,
167: lowPriorityEvents);
168: setPreferenceString(PREFS_LAST_MODIFIED, Long
169: .toString(System.currentTimeMillis()));
170:
171: priorityColorsMap = null;
172: priorityEventsMap = null;
173: } catch (Exception e) {
174: // error occurred
175: message = msgs.getString("prefs_not_updated");
176: messageSeverity = FacesMessage.SEVERITY_FATAL;
177: LOG.error("Calendar Summary: " + message, e);
178: return "prefs";
179: }
180:
181: // all ok
182: return "calendar";
183: }
184:
185: public String cancel() {
186: message = null;
187: priorityColorsMap = null;
188: priorityEventsMap = null;
189: return "calendar";
190: }
191:
192: // ######################################################################################
193: // Generic get/set methods
194: // ######################################################################################
195: private void readPriorityColorsMap() {
196: // priority colors (CSS properties)
197: priorityColorsMap = getPreferencePriorityColors();
198: selectedHighPrColor = (String) priorityColorsMap
199: .get(PREFS_HIGHPRIORITY_COLOR);
200: selectedMediumPrColor = (String) priorityColorsMap
201: .get(PREFS_MEDIUMPRIORITY_COLOR);
202: selectedLowPrColor = (String) priorityColorsMap
203: .get(PREFS_LOWPRIORITY_COLOR);
204: }
205:
206: private void readPriorityEventsMap() {
207: // priority events
208: priorityEventsMap = getPreferencePriorityEvents();
209: highPriorityEvents = (List) priorityEventsMap
210: .get(PREFS_HIGHPRIORITY_EVENTS);
211: mediumPriorityEvents = (List) priorityEventsMap
212: .get(PREFS_MEDIUMPRIORITY_EVENTS);
213: lowPriorityEvents = (List) priorityEventsMap
214: .get(PREFS_LOWPRIORITY_EVENTS);
215: }
216:
217: public String getSelectedViewMode() {
218: selectedViewMode = getPreferenceViewMode();
219: return selectedViewMode;
220: }
221:
222: public void setSelectedViewMode(String selectedViewMode) {
223: this .selectedViewMode = selectedViewMode;
224: }
225:
226: public List getViewModes() {
227: viewModes = new ArrayList();
228: viewModes.add(new SelectItem(CalendarBean.MODE_MONTHVIEW, msgs
229: .getString("month_view")));
230: viewModes.add(new SelectItem(CalendarBean.MODE_WEEKVIEW, msgs
231: .getString("week_view")));
232: return viewModes;
233: }
234:
235: public String getSelectedHighPriorityColor() {
236: if (priorityColorsMap == null)
237: readPriorityColorsMap();
238: return selectedHighPrColor;
239: }
240:
241: public void setSelectedHighPriorityColor(String color) {
242: this .selectedHighPrColor = color;
243: }
244:
245: public String getSelectedMediumPriorityColor() {
246: if (priorityColorsMap == null)
247: readPriorityColorsMap();
248: return selectedMediumPrColor;
249: }
250:
251: public void setSelectedMediumPriorityColor(String color) {
252: this .selectedMediumPrColor = color;
253: }
254:
255: public String getSelectedLowPriorityColor() {
256: if (priorityColorsMap == null)
257: readPriorityColorsMap();
258: return selectedLowPrColor;
259: }
260:
261: public void setSelectedLowPriorityColor(String color) {
262: this .selectedLowPrColor = color;
263: }
264:
265: public List getHighPriorityEvents() {
266: if (priorityEventsMap == null)
267: readPriorityEventsMap();
268: return listOfStringsToListOfSelectItem(highPriorityEvents);
269: }
270:
271: public void setHighPriorityEvents(List events) {
272: this .highPriorityEvents = events;
273: }
274:
275: public List getSelectedHighPriorityEvents() {
276: return new ArrayList();
277: }
278:
279: public void setSelectedHighPriorityEvents(Collection events) {
280: this .highPriorityEvents = events;
281: }
282:
283: public void setSelectedHighPriorityEvents(List events) {
284: this .highPriorityEvents = events;
285: }
286:
287: public List getMediumPriorityEvents() {
288: if (priorityEventsMap == null)
289: readPriorityEventsMap();
290: return listOfStringsToListOfSelectItem(mediumPriorityEvents);
291: }
292:
293: public void setMediumPriorityEvents(List events) {
294: this .mediumPriorityEvents = events;
295: }
296:
297: public List getSelectedMediumPriorityEvents() {
298: return new ArrayList();
299: }
300:
301: public void setSelectedMediumPriorityEvents(Collection events) {
302: this .mediumPriorityEvents = events;
303: }
304:
305: public void setSelectedMediumPriorityEvents(List events) {
306: this .mediumPriorityEvents = events;
307: }
308:
309: public List getLowPriorityEvents() {
310: if (priorityEventsMap == null)
311: readPriorityEventsMap();
312: return listOfStringsToListOfSelectItem(lowPriorityEvents);
313: }
314:
315: public void setLowPriorityEvents(List events) {
316: this .lowPriorityEvents = events;
317: }
318:
319: public List getSelectedLowPriorityEvents() {
320: return new ArrayList();
321: }
322:
323: public void setSelectedLowPriorityEvents(Collection events) {
324: this .lowPriorityEvents = events;
325: }
326:
327: public void setSelectedLowPriorityEvents(List events) {
328: this .lowPriorityEvents = events;
329: }
330:
331: // ######################################################################################
332: // Preferences methods
333: // ######################################################################################
334: public static long getPreferenceLastModified() {
335: Long lastModified = 0l;
336: String value = getPreferenceString(PREFS_LAST_MODIFIED);
337:
338: if (value != null) {
339: try {
340: lastModified = Long.parseLong(value);
341: } catch (NumberFormatException e) {
342: lastModified = 0l;
343: }
344: }
345:
346: return lastModified;
347: }
348:
349: public static String getPreferenceViewMode() {
350: String value = getPreferenceString(PREFS_VIEW_MODE);
351:
352: // preferences not set, read from sakai.properties
353: if (value == null) {
354: value = getDefaultStringFromSakaiProperties(PREFS_VIEW_MODE);
355: }
356:
357: // sakai.properties default not set, using 'month'
358: if (value == null) {
359: return CalendarBean.MODE_MONTHVIEW;
360: } else
361: return value;
362: }
363:
364: public static Map getPreferencePriorityColors() {
365: HashMap map = new HashMap();
366: String h = getPreferenceString(PREFS_HIGHPRIORITY_COLOR);
367: String m = getPreferenceString(PREFS_MEDIUMPRIORITY_COLOR);
368: String l = getPreferenceString(PREFS_LOWPRIORITY_COLOR);
369:
370: // preferences not set, read from sakai.properties
371: if (h == null && m == null && l == null) {
372: h = getDefaultStringFromSakaiProperties(PREFS_HIGHPRIORITY_COLOR);
373: m = getDefaultStringFromSakaiProperties(PREFS_MEDIUMPRIORITY_COLOR);
374: l = getDefaultStringFromSakaiProperties(PREFS_LOWPRIORITY_COLOR);
375: }
376:
377: map.put(PREFS_HIGHPRIORITY_COLOR, h);
378: map.put(PREFS_MEDIUMPRIORITY_COLOR, m);
379: map.put(PREFS_LOWPRIORITY_COLOR, l);
380: return map;
381: }
382:
383: public static Map getPreferencePriorityEvents() {
384: HashMap map = new HashMap();
385: List h = getPreferenceList(PREFS_HIGHPRIORITY_EVENTS);
386: List m = getPreferenceList(PREFS_MEDIUMPRIORITY_EVENTS);
387: List l = getPreferenceList(PREFS_LOWPRIORITY_EVENTS);
388:
389: // preferences not set, read from sakai.properties
390: if (h == null && m == null && l == null) {
391: h = getDefaultListFromSakaiProperties(PREFS_HIGHPRIORITY_EVENTS);
392: m = getDefaultListFromSakaiProperties(PREFS_MEDIUMPRIORITY_EVENTS);
393: l = getDefaultListFromSakaiProperties(PREFS_LOWPRIORITY_EVENTS);
394: }
395:
396: if (h == null)
397: h = new ArrayList();
398: if (m == null)
399: m = new ArrayList();
400: if (l == null)
401: l = new ArrayList();
402:
403: // make sure all available events are listed
404: // no pass-by-reference in java, must use a work-around...
405: List temp = new ArrayList();
406: temp.addAll(eventTypes);
407: PairList lists = new PairList(h, temp);
408: lists = validateEventList(lists);
409: h = lists.dataList;
410:
411: lists.dataList = m;
412: lists = validateEventList(lists);
413: m = lists.dataList;
414:
415: lists.dataList = l;
416: lists = validateEventList(lists);
417: l = lists.dataList;
418:
419: // add all non-specified events to low priority list
420: l.addAll(lists.tempList);
421:
422: // sort lists
423: Collections.sort(h);
424: Collections.sort(m);
425: Collections.sort(l);
426:
427: map.put(PREFS_HIGHPRIORITY_EVENTS, h);
428: map.put(PREFS_MEDIUMPRIORITY_EVENTS, m);
429: map.put(PREFS_LOWPRIORITY_EVENTS, l);
430: return map;
431: }
432:
433: /**
434: * Get the current user preference value. First attempt Preferences, then defaults from sakai.properties.
435: * @param name The property name.
436: * @return The preference value or null if not set.
437: */
438: private static String getPreferenceString(String name) {
439: Preferences prefs = M_ps.getPreferences(M_sm
440: .getCurrentSessionUserId());
441: ResourceProperties rp = prefs.getProperties(PREFS_KEY);
442: String value = rp.getProperty(name);
443: return value;
444: }
445:
446: /**
447: * Get the current user preference list value. First attempt Preferences, then defaults from sakai.properties.
448: * @param name The property name.
449: * @return The preference list value or null if not set.
450: */
451: private static List getPreferenceList(String name) {
452: Preferences prefs = M_ps.getPreferences(M_sm
453: .getCurrentSessionUserId());
454: ResourceProperties rp = prefs.getProperties(PREFS_KEY);
455: List l = rp.getPropertyList(name);
456: return l;
457: }
458:
459: private static void setPreferenceString(String name, String value)
460: throws Exception {
461: PreferencesEdit prefsEdit = null;
462: String userId = M_sm.getCurrentSessionUserId();
463: try {
464: prefsEdit = M_ps.edit(userId);
465: } catch (IdUnusedException e) {
466: prefsEdit = M_ps.add(userId);
467: }
468: try {
469: ResourcePropertiesEdit props = prefsEdit
470: .getPropertiesEdit(PREFS_KEY);
471:
472: if (value == null) {
473: props.removeProperty(name);
474: } else {
475: props.addProperty(name, value.toString());
476: }
477: } catch (Exception e) {
478: if (prefsEdit != null)
479: M_ps.cancel(prefsEdit);
480: throw e;
481: }
482: M_ps.commit(prefsEdit);
483: }
484:
485: private static void setPreferenceList(String name, Collection values)
486: throws Exception {
487: PreferencesEdit prefsEdit = null;
488: String userId = M_sm.getCurrentSessionUserId();
489: try {
490: prefsEdit = M_ps.edit(userId);
491: } catch (IdUnusedException e) {
492: prefsEdit = M_ps.add(userId);
493: }
494: try {
495: ResourcePropertiesEdit props = prefsEdit
496: .getPropertiesEdit(PREFS_KEY);
497:
498: if (values == null) {
499: props.removeProperty(name);
500: } else {
501: List existing = props.getPropertyList(name);
502: Iterator it = values.iterator();
503: while (it.hasNext()) {
504: String value = (String) it.next();
505: if (existing == null || !existing.contains(value))
506: props.addPropertyToList(name, value.toString());
507: }
508: }
509: } catch (Exception e) {
510: if (prefsEdit != null)
511: M_ps.cancel(prefsEdit);
512: M_ps.cancel(prefsEdit);
513: throw e;
514: }
515: M_ps.commit(prefsEdit);
516: }
517:
518: private static void clearPreferenceList(String name)
519: throws Exception {
520: PreferencesEdit prefsEdit = null;
521: try {
522: prefsEdit = M_ps.edit(M_sm.getCurrentSessionUserId());
523: ResourcePropertiesEdit props = prefsEdit
524: .getPropertiesEdit(PREFS_KEY);
525:
526: props.removeProperty(name);
527: } catch (Exception e) {
528: M_ps.cancel(prefsEdit);
529: throw e;
530: }
531: M_ps.commit(prefsEdit);
532: }
533:
534: private static String getDefaultStringFromSakaiProperties(
535: String name) {
536: String value = M_cfg.getString(SAKPROP_BASE + name);
537: return value;
538: }
539:
540: private static List getDefaultListFromSakaiProperties(String name) {
541: List l = new ArrayList();
542: String[] valuesStr = M_cfg.getStrings(SAKPROP_BASE + name);
543: if (valuesStr == null)
544: return l;
545: else {
546: for (int i = 0; i < valuesStr.length; i++) {
547: l.add(valuesStr[i]);
548: }
549: }
550: return l;
551: }
552:
553: // ######################################################################################
554: // Util methods
555: // ######################################################################################
556: protected Set getValuesFromFacesContext(String componentId) {
557: Set values = new HashSet();
558: String[] str = (String[]) FacesContext.getCurrentInstance()
559: .getExternalContext().getRequestParameterValuesMap()
560: .get(componentId);
561: if (str != null) {
562: for (int i = 0; i < str.length; i++) {
563: values.add(str[i]);
564: }
565: }
566: return values;
567: }
568:
569: protected String getValueFromFacesContext(String componentId) {
570: String[] str = (String[]) FacesContext.getCurrentInstance()
571: .getExternalContext().getRequestParameterValuesMap()
572: .get(componentId);
573: return str[0];
574: }
575:
576: private List listOfStringsToListOfSelectItem(Collection l) {
577: List losi = new ArrayList();
578: if (l == null)
579: return losi;
580: Iterator los = l.iterator();
581: while (los.hasNext()) {
582: String s = (String) los.next();
583: losi.add(new SelectItem(s));
584: }
585: return losi;
586: }
587:
588: /**
589: * Foreach 'list' entry A, remove it from 'temp'. If A doesn't exist in 'temp', remove it from 'list'.
590: * @param list A event priority List.
591: * @param temp A List with all events.
592: */
593: private static PairList validateEventList(PairList lists) {
594: List temp = lists.tempList;
595: List list = lists.dataList;
596: if (list == null) {
597: return lists;
598: }
599: List toRemoveFromList = new ArrayList();
600: Iterator iL = list.iterator();
601: while (iL.hasNext()) {
602: Object e = iL.next();
603: if (temp.contains(e))
604: temp.remove(e);
605: else
606: toRemoveFromList.add(e);
607: }
608:
609: Iterator iR = toRemoveFromList.iterator();
610: while (iR.hasNext()) {
611: Object e = iR.next();
612: list.remove(e);
613: }
614: lists.dataList = list;
615: lists.tempList = temp;
616: return lists;
617: }
618: }
619:
620: /** Pair of lists */
621: class PairList {
622: public List dataList;
623: public List tempList;
624:
625: public PairList(List dataList, List tempList) {
626: this.dataList = dataList;
627: this.tempList = tempList;
628: }
629: }
|