001: package com.sun.portal.app.sharedtasks.faces.models;
002:
003: import com.sun.comclient.calendar.CalendarComponent;
004: import com.sun.comclient.calendar.CalendarException;
005: import com.sun.comclient.calendar.DateTime;
006: import com.sun.comclient.calendar.ICalendar;
007: import com.sun.comclient.calendar.OperationNotSupportedException;
008: import com.sun.comclient.calendar.VTodo;
009: import com.sun.comclient.calendar.socs.SOCSCalendar;
010:
011: import com.sun.portal.app.sharedtasks.faces.beans.TaskListSessionBackingBean;
012: import com.sun.portal.app.sharedtasks.faces.beans.TaskUserSessionBackingBean;
013: import com.sun.portal.app.sharedtasks.util.CalTask;
014: import com.sun.portal.app.sharedtasks.util.CalTaskListItem;
015: import com.sun.portal.app.sharedtasks.util.SharedConstants;
016: import com.sun.portal.app.sharedtasks.util.SharedTaskException;
017:
018: import java.util.ArrayList;
019:
020: import com.sun.portal.log.common.PortalLogger;
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023:
024: /**
025: * Model class representing a list of tasks, with capabilities
026: * to search tasks, filter tasks by set criteria and delete groups
027: * of tasks.
028: *
029: * @author ashwin.mathew@sun.com
030: */
031: public class TaskListModel {
032:
033: private TaskListSessionBackingBean listSession;
034:
035: private TaskUserSessionBackingBean userSession;
036:
037: private static Logger logger = PortalLogger
038: .getLogger(TaskListModel.class);
039:
040: private static final String SEARCH_PREFIX = "ALL=";
041:
042: public TaskListModel(TaskListSessionBackingBean listSession,
043: TaskUserSessionBackingBean userSession) {
044: this .listSession = listSession;
045: this .userSession = userSession;
046: }
047:
048: public CalTaskListItem[] retrieve() throws SharedTaskException {
049: if (logger.isLoggable(Level.FINEST)) {
050: logger.finest("Retrieving task list, search string ["
051: + listSession.getSearchString()
052: + "], view context ["
053: + listSession.getViewContext()
054: + "], view context date ["
055: + listSession.getViewDate() + "], status filter ["
056: + listSession.getStatusFilter() + "]");
057: }
058:
059: ArrayList results = new ArrayList();
060:
061: try {
062: ICalendar cal = userSession.getCalendar();
063:
064: CalendarComponent[] tasks = null;
065:
066: if (listSession.getSearchString() != null) {
067: SOCSCalendar socsCal = (SOCSCalendar) cal;
068: tasks = socsCal.searchTodos(SEARCH_PREFIX
069: + listSession.getSearchString(), 0);
070: } else if (listSession.getViewContext() == null
071: // The condition below is a temporary fix. There are some cases
072: // where the view context arrives as an empty string, if
073: // it is not the default, and the filter has been applied
074: // such that an empty list is returned.
075: || listSession.getViewContext().length() == 0
076: || listSession.getViewContext().equals(
077: SharedConstants.VIEW_CTX_DAY)) {
078: tasks = cal.fetchComponents(listSession
079: .getViewDateTime(), ICalendar.VTODO);
080: } else if (listSession.getViewContext().equals(
081: SharedConstants.VIEW_CTX_WEEK)
082: || listSession.getViewContext().equals(
083: SharedConstants.VIEW_CTX_MONTH)) {
084: DateTime contextStartDateTime = listSession
085: .getContextStartDateTime();
086: DateTime contextEndDateTime = listSession
087: .getContextEndDateTime();
088:
089: tasks = cal.fetchComponents(contextStartDateTime,
090: contextEndDateTime, ICalendar.VTODO);
091: }
092:
093: for (int i = 0; i < tasks.length; i++) {
094: VTodo task = (VTodo) tasks[i];
095:
096: // If the status filter is set to view incomplete tasks and
097: // the current task is complete OR if the status filter is set
098: // to view complete tasks and the current task is incomplete,
099: // continue.
100: // If it's a full text search, ignore the filter.
101: if (listSession.getSearchString() == null
102: && listSession.getStatusFilter() != null
103: && ((listSession
104: .getStatusFilter()
105: .equals(
106: SharedConstants.FILTER_INCOMPLETE_TASKS) && task
107: .getPercent() == 100) || (listSession
108: .getStatusFilter()
109: .equals(
110: SharedConstants.FILTER_COMPLETED_TASKS) && task
111: .getPercent() < 100))) {
112: continue;
113: }
114:
115: CalTask calTask = new CalTask();
116: calTask.setTimeZone(userSession.getTimeZone());
117: calTask.load(task);
118:
119: CalTaskListItem item = new CalTaskListItem(calTask);
120:
121: results.add(item);
122: }
123: } catch (CalendarException cex) {
124: logger.log(Level.SEVERE, "Error retrieving tasks", cex);
125: throw new SharedTaskException(
126: SharedTaskException.CALENDAR_ERROR, cex);
127: } catch (OperationNotSupportedException onex) {
128: logger.log(Level.SEVERE, "Error retrieving tasks", onex);
129: throw new SharedTaskException(
130: SharedTaskException.CALENDAR_ERROR, onex);
131: } catch (Exception ex) {
132: logger.log(Level.SEVERE, "Error retrieving tasks", ex);
133: throw new SharedTaskException(
134: SharedTaskException.APPLICATION_ERROR, ex);
135: }
136:
137: return (CalTaskListItem[]) results
138: .toArray(new CalTaskListItem[0]);
139: }
140:
141: }
|