001: package com.sun.portal.app.sharedtasks.faces.beans;
002:
003: import com.sun.portal.app.sharedtasks.faces.models.TaskModel;
004: import com.sun.portal.app.sharedtasks.util.CalTask;
005: import com.sun.portal.app.sharedtasks.util.CalTaskRepeat;
006: import com.sun.portal.app.sharedtasks.util.SharedTaskException;
007: import com.sun.portal.app.sharedtasks.util.SharedConstants;
008: import com.sun.portal.app.sharedtasks.util.TaskBeanFactory;
009: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtils;
010: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtilsFactory;
011: import com.sun.portal.app.calendarcommon.common.SharedServicesUtils;
012: import com.sun.portal.app.calendarcommon.common.SharedServicesUtilsFactory;
013: import com.sun.portal.app.calendarcommon.common.SharedServicesException;
014:
015: import com.sun.portal.log.common.PortalLogger;
016: import java.util.logging.Logger;
017: import java.util.logging.Level;
018:
019: import com.sun.web.ui.model.Option;
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.ArrayList;
023: import java.util.ResourceBundle;
024: import java.util.Locale;
025: import java.util.TimeZone;
026: import java.util.Calendar;
027: import java.text.DateFormat;
028: import javax.faces.context.FacesContext;
029: import javax.faces.context.ExternalContext;
030: import javax.portlet.PortletContext;
031: import javax.portlet.PortletURL;
032:
033: import com.sun.comclient.calendar.DateTime;
034: import com.sun.comclient.calendar.CalendarComponentException;
035: import com.sun.comclient.calendar.OperationNotSupportedException;
036: import com.sun.comclient.calendar.Duration;
037:
038: /**
039: * The backing bean for the task create/edit page.
040: *
041: * @author ashwin.mathew@sun.com
042: */
043: public class TaskBackingBean {
044:
045: public static final String SAVE_SUCCESS = "saveTaskSuccess";
046: public static final String SAVE_FAILED = "saveTaskFailed";
047: public static final String SAVE_VALIDATION_FAILED = "saveTaskValidationFailed";
048: public static final String DELETE_SUCCESS = "deleteTaskSuccess";
049: public static final String DELETE_FAILED = "deleteTaskFailed";
050: public static final String CANCEL = "cancelTask";
051:
052: private static Option[] hourOptions;
053:
054: private static Option[] minOptions;
055:
056: static {
057: int hourOptionSize = SharedConstants.hoursIn24HourFormatNames.length;
058: hourOptions = new Option[hourOptionSize];
059: for (int i = 0; i < hourOptionSize; i++) {
060: Option hourOption = new Option(
061: SharedConstants.hoursIn24HourFormatValues[i],
062: SharedConstants.hoursIn24HourFormatNames[i]);
063: hourOptions[i] = hourOption;
064: }
065:
066: int minOptionSize = SharedConstants.minutesIn24HourFormatNames.length;
067: minOptions = new Option[minOptionSize];
068: for (int i = 0; i < minOptionSize; i++) {
069: Option minOption = new Option(
070: SharedConstants.minutesIn24HourFormatValues[i],
071: SharedConstants.minutesIn24HourFormatNames[i]);
072: minOptions[i] = minOption;
073: }
074: }
075:
076: private boolean isError = false;
077: private String errorMsg = null;
078: private String errorMsgDetail = null;
079:
080: private String oldRecurrencePattern;
081: private int oldRecurrenceCount;
082:
083: private TaskModel model;
084:
085: private TaskSessionBackingBean taskSession;
086: private TaskUserSessionBackingBean userSession;
087:
088: private static Logger logger = PortalLogger
089: .getLogger(TaskBackingBean.class);
090:
091: /**
092: * Creates a new instance of TaskBackingBean and retrieves a selected task
093: * if navigation is to the edit page.
094: */
095: public TaskBackingBean() {
096: taskSession = (TaskSessionBackingBean) TaskBeanFactory
097: .getBean(TaskSessionBackingBean.BEAN_NAME);
098:
099: userSession = (TaskUserSessionBackingBean) TaskBeanFactory
100: .getBean(TaskUserSessionBackingBean.BEAN_NAME);
101:
102: model = new TaskModel(userSession);
103:
104: if (taskSession.getIsEdit().equals(Boolean.TRUE)) {
105: String taskId = taskSession.getTaskId();
106: String taskRid = taskSession.getTaskRid();
107:
108: if (taskId != null) {
109: try {
110: model.retrieve(taskId, taskRid);
111: } catch (SharedTaskException e) {
112: logger.log(Level.SEVERE,
113: "Failed to retrieve task id [" + taskId
114: + "], rid [" + taskRid + "]", e);
115: ResourceBundle bundle = userSession
116: .getResourceBundle();
117: setIsError(true);
118: setErrorMsg(bundle
119: .getString(SharedConstants.KEY_RETRIEVE_FAILED));
120: }
121: }
122: }
123: }
124:
125: public String getPageTitle() {
126: ResourceBundle bundle = userSession.getResourceBundle();
127:
128: if (taskSession.getIsEdit().booleanValue()) {
129: return bundle
130: .getString(SharedConstants.KEY_PAGETITLE_EDIT_TASK);
131: } else {
132: return bundle
133: .getString(SharedConstants.KEY_PAGETITLE_NEW_TASK);
134: }
135: }
136:
137: public DateFormat getDateFormat() {
138: Locale locale = userSession.getLocale();
139: return DateFormat.getDateInstance(DateFormat.SHORT, locale);
140: }
141:
142: // Checks whether the current view is for an edit page
143: // for a recurring task
144: public Boolean getIsEditRecurring() {
145: if (taskSession.getIsEdit().booleanValue()
146: && model.getTask().getIsRecurring()) {
147: return Boolean.TRUE;
148: } else {
149: return Boolean.FALSE;
150: }
151: }
152:
153: // Checks whether current view is for a new task, or for
154: // an edit page for a recurring task
155: public Boolean getIsNewOrEditRecurring() {
156: if (!taskSession.getIsEdit().booleanValue()
157: || getIsEditRecurring().booleanValue()) {
158: return Boolean.TRUE;
159: } else {
160: return Boolean.FALSE;
161: }
162: }
163:
164: public TaskModel getModel() {
165: return model;
166: }
167:
168: public void setModel(TaskModel model) {
169: this .model = model;
170: }
171:
172: public boolean getIsError() {
173: return isError;
174: }
175:
176: public void setIsError(boolean isError) {
177: this .isError = isError;
178: }
179:
180: public String getErrorMsg() {
181: return errorMsg;
182: }
183:
184: public void setErrorMsg(String errorMsg) {
185: this .errorMsg = errorMsg;
186: }
187:
188: public String getErrorMsgDetail() {
189: return errorMsgDetail;
190: }
191:
192: public void setErrorMsgDetail(String errorMsgDetail) {
193: this .errorMsgDetail = errorMsgDetail;
194: }
195:
196: public String handleSave() {
197: clearListSessionData();
198:
199: String status = TaskBackingBean.SAVE_SUCCESS;
200:
201: try {
202: model.getTask().validateDueDate();
203: model.getTask().validateRecurrenceCount();
204:
205: model.getTask().setAttendeeIds(findAttendeeIds());
206:
207: if (taskSession.getIsEdit().booleanValue()) {
208: if (!(oldRecurrencePattern.equals(model.getTask()
209: .getRecurrencePattern()))
210: || oldRecurrenceCount != model.getTask()
211: .getRecurrenceCount()) {
212: model.modifyRecurrence();
213: }
214: model.update();
215: this .userSession.deleteTaskFromSearchDatabase(model);
216: this .userSession.addTaskToSearchDatabase(model);
217: taskSession.setIsEdit(Boolean.FALSE);
218: } else {
219: model.getTask().setOrganizerId(findOrganizerId());
220: model.save();
221: this .userSession.addTaskToSearchDatabase(model);
222: taskSession.setIsEdit(Boolean.FALSE);
223: }
224: } catch (SharedTaskException se) {
225: logger.log(Level.SEVERE, "Failed to save task", se);
226:
227: status = TaskBackingBean.SAVE_FAILED;
228:
229: ResourceBundle bundle = userSession.getResourceBundle();
230: setIsError(true);
231: setErrorMsg(bundle
232: .getString(SharedConstants.KEY_SAVE_FAILED));
233: setErrorMsgDetail(bundle.getString(se.getErrorKey()));
234: } catch (Exception e) {
235: logger.log(Level.SEVERE, "Failed to save task", e);
236:
237: status = TaskBackingBean.SAVE_FAILED;
238:
239: ResourceBundle bundle = userSession.getResourceBundle();
240: setIsError(true);
241: setErrorMsg(bundle
242: .getString(SharedConstants.KEY_SAVE_FAILED));
243: setErrorMsgDetail(bundle
244: .getString(SharedConstants.KEY_OP_FAILED_DETAIL));
245: }
246:
247: return status;
248: }
249:
250: private String findOrganizerId() throws Exception {
251: String commCalId = null;
252:
253: FacesContext context = FacesContext.getCurrentInstance();
254: ExternalContext extContext = context.getExternalContext();
255:
256: // If we are running this as a webapp, just return a hard-coded value for
257: // testing
258: /*if (!(extContext instanceof PortletContext)) // Only for testing
259: {
260: commCalId = "communitytasks";
261: return commCalId;
262: }*/
263:
264: SharedCalendarUtils sharedCalUtils = null;
265:
266: try {
267: sharedCalUtils = SharedCalendarUtilsFactory
268: .getSharedCalendarUtils(context);
269: commCalId = sharedCalUtils.getCommunityCalendarID(context);
270: } catch (Exception e1) {
271: throw e1;
272: }
273:
274: return commCalId;
275: }
276:
277: private String[] findAttendeeIds() throws Exception {
278: SharedServicesUtils sharedServices = null;
279: SharedCalendarUtils sharedCalUtils = null;
280:
281: FacesContext context = FacesContext.getCurrentInstance();
282:
283: try {
284: sharedCalUtils = SharedCalendarUtilsFactory
285: .getSharedCalendarUtils(context);
286: } catch (Exception e1) {
287: throw e1;
288: }
289:
290: try {
291: if (!sharedCalUtils.createEventsInMembersCalendar(context)) {
292: // No need to find the attendees if this flag is false
293: return new String[0];
294: }
295: } catch (Exception e) {
296: // Ignore - if an exception occurs, assume that
297: // sharedCalUtils.createEventsInMemberCalendars is true
298: }
299:
300: ArrayList attendeeCalIds = new ArrayList();
301:
302: ExternalContext extContext = context.getExternalContext();
303:
304: // If we are running this as a webapp, just return a hard-coded value for
305: // testing
306: /*if (!(extContext instanceof PortletContext))
307: {
308: return new String[] {"ashwin1", "ashwin2", "ashwin3"};
309: }*/
310:
311: Set memberIds = null;
312:
313: try {
314: sharedServices = SharedServicesUtilsFactory
315: .getSharedServicesUtils(context);
316: memberIds = sharedServices.getMemberIDs(context);
317: } catch (Exception e1) {
318: throw e1;
319: }
320:
321: if (memberIds != null && !memberIds.isEmpty()) {
322: String[] memberIdArray = (String[]) memberIds
323: .toArray(new String[0]);
324: if (memberIdArray != null && memberIdArray.length > 0) {
325: for (int i = 0; i < memberIdArray.length; i++) {
326: String calid = null;
327: try {
328: calid = sharedCalUtils.getMemberCalendarID(
329: context, memberIdArray[i]);
330: } catch (Exception e2) {
331: logger.log(Level.SEVERE,
332: "Error getting calendar id for member ["
333: + memberIdArray[i] + "]", e2);
334: throw e2;
335: }
336:
337: if (calid != null && calid.length() > 0) {
338: attendeeCalIds.add(calid);
339: }
340: }
341: }
342: }
343:
344: return (String[]) attendeeCalIds.toArray(new String[0]);
345: }
346:
347: public String handleDelete() {
348: clearListSessionData();
349:
350: String status = TaskBackingBean.DELETE_SUCCESS;
351:
352: try {
353: model.delete();
354: taskSession.setIsEdit(Boolean.FALSE);
355: this .userSession.deleteTaskFromSearchDatabase(model);
356: } catch (Exception e) {
357: logger.log(Level.SEVERE, "Failed to delete task", e);
358:
359: status = TaskBackingBean.DELETE_FAILED;
360:
361: ResourceBundle bundle = userSession.getResourceBundle();
362: setIsError(true);
363: setErrorMsg(bundle
364: .getString(SharedConstants.KEY_DELETE_FAILED));
365: setErrorMsgDetail(bundle
366: .getString(SharedConstants.KEY_OP_FAILED_DETAIL));
367: }
368:
369: return status;
370: }
371:
372: public String handleCancel() {
373: clearListSessionData();
374: taskSession.setIsEdit(Boolean.FALSE);
375: return CANCEL;
376: }
377:
378: private void clearListSessionData() {
379: TaskListSessionBackingBean listSession = (TaskListSessionBackingBean) TaskBeanFactory
380: .getBean(TaskListSessionBackingBean.BEAN_NAME);
381: listSession.setDataProvider(null);
382: }
383:
384: public Option[] getPriorityOptions() {
385: ResourceBundle bundle = userSession.getResourceBundle();
386:
387: Option[] priorityOptions = new Option[] {
388: new Option(CalTask.PRIORITY_NONE, bundle
389: .getString(SharedConstants.KEY_PRIORITY_NONE)),
390: new Option(CalTask.PRIORITY_LOW, bundle
391: .getString(SharedConstants.KEY_PRIORITY_LOW)),
392: new Option(CalTask.PRIORITY_NORMAL, bundle
393: .getString(SharedConstants.KEY_PRIORITY_NORMAL)),
394: new Option(CalTask.PRIORITY_HIGH, bundle
395: .getString(SharedConstants.KEY_PRIORITY_HIGH)) };
396:
397: return priorityOptions;
398: }
399:
400: public Option[] getStatusOptions() {
401: ResourceBundle bundle = userSession.getResourceBundle();
402:
403: Option[] statusOptions = new Option[] {
404: new Option(CalTask.STATUS_0, bundle
405: .getString(SharedConstants.KEY_STATUS_0)),
406: new Option(CalTask.STATUS_25, bundle
407: .getString(SharedConstants.KEY_STATUS_25)),
408: new Option(CalTask.STATUS_50, bundle
409: .getString(SharedConstants.KEY_STATUS_50)),
410: new Option(CalTask.STATUS_75, bundle
411: .getString(SharedConstants.KEY_STATUS_75)),
412: new Option(CalTask.STATUS_100, bundle
413: .getString(SharedConstants.KEY_STATUS_100)) };
414:
415: return statusOptions;
416: }
417:
418: public Option[] getHourOptions() {
419: return hourOptions;
420: }
421:
422: public Option[] getMinOptions() {
423: return minOptions;
424: }
425:
426: public Option[] getRecurrenceOptions() {
427: return CalTaskRepeat.getRecurrenceOptions();
428: }
429:
430: public String getOldRecurrencePattern() {
431: return model.getTask().getRecurrencePattern();
432: }
433:
434: public void setOldRecurrencePattern(String oldRecurrencePattern) {
435: this .oldRecurrencePattern = oldRecurrencePattern;
436: }
437:
438: public String getOldRecurrenceCount() {
439: return String.valueOf(model.getTask().getRecurrenceCount());
440: }
441:
442: public void setOldRecurrenceCount(String oldRecurrenceCountStr) {
443: this.oldRecurrenceCount = Integer
444: .parseInt(oldRecurrenceCountStr);
445: }
446:
447: }
|