001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: *
004: */
005:
006: package com.sun.ssoadapter.calendar.pim;
007:
008: import java.util.*;
009: import com.sun.comclient.calendar.*;
010:
011: import com.aligo.pim.interfaces.*;
012: import com.aligo.pim.exceptions.*;
013: import com.aligo.pim.*;
014:
015: /**
016: *
017: */
018: public class APimCalendar extends ICalendar {
019:
020: private PimContainer pimContainer = null;
021:
022: PimCalendar getCalendarFolder() throws PimException {
023: return (PimCalendar) pimContainer
024: .getFolder(PimFolderType.CALENDAR);
025: }
026:
027: PimTask getTaskFolder() throws PimException {
028: return (PimTask) pimContainer.getFolder(PimFolderType.TASK);
029: }
030:
031: PimInvitation getInvitationFolder() throws PimException {
032: try {
033: return (PimInvitation) pimContainer
034: .getFolder(PimFolderType.INVITATION);
035: } catch (PimException ex) {
036: ex.printStackTrace();
037: throw ex;
038: }
039:
040: }
041:
042: PimAppointmentItems getAppointmentItems() throws PimException {
043: return getCalendarFolder().getAppointmentItems();
044: }
045:
046: PimTaskItems getTaskItems() throws PimException {
047: PimTask taskFolder = getTaskFolder();
048: if (taskFolder != null) {
049: return getTaskFolder().getTaskItems();
050: }
051: return null;
052: }
053:
054: PimInvitationItems getInvitationItems() throws PimException {
055: return getInvitationFolder().getInvitationItems();
056: }
057:
058: /**
059: * Calendar constructor.
060: * <br><br>
061: *
062: * @param store The CalStore this calendar belongs to
063: */
064: public APimCalendar(CalendarStore store, String calId)
065: throws CalendarException {
066: super (store, calId);
067:
068: APimCalStore eStore = (APimCalStore) store;
069: pimContainer = eStore.getPimContainer();
070:
071: // TBD, probably should do a user ID check against calId, since I
072: // see no way to open any other calendar besides the users.
073:
074: }
075:
076: public VEvent createEvent() throws OperationNotSupportedException {
077: try {
078: return new APimEvent(this , null, true, null);
079: } catch (Exception e) {
080: e.printStackTrace();
081: throw new OperationNotSupportedException(e.getMessage());
082: }
083: }
084:
085: public VTodo createTodo() throws OperationNotSupportedException {
086: try {
087:
088: return new APimTask(this , null, true);
089: } catch (Exception e) {
090: e.printStackTrace();
091: throw new OperationNotSupportedException(e.getMessage());
092: }
093: }
094:
095: public String getDisplayName()
096: throws OperationNotSupportedException {
097: throw new OperationNotSupportedException(
098: "Method getDisplayName() not supported");
099: }
100:
101: public VTodo[] fetchTodos(String uid, String rid, String modifier)
102: throws OperationNotSupportedException {
103: throw new OperationNotSupportedException(
104: "Method fetchTodos(String, String, String) not supported");
105:
106: }
107:
108: public VEvent[] fetchEvents(String uid, String rid, String modifier)
109: throws OperationNotSupportedException {
110: throw new OperationNotSupportedException(
111: "Method fetchEvents(String, String, String) not supported");
112:
113: }
114:
115: /**
116: * Fetch the events between the specified dates.
117: * <br><br>
118: *
119: * @param start Start date
120: * @param end End date
121: *
122: * @exception CalendarException if fetchEvents fails for whatever reason
123: */
124: public CalendarComponent[] fetchComponents(DateTime start,
125: DateTime end, int types) throws CalendarException {
126: VEvent[] events = null;
127: VTodo[] tasks = null;
128: VFreeBusy[] freeBusy = null;
129: VEvent[] invitedEvents = null;
130:
131: try {
132:
133: if ((types & VEVENT) != 0) {
134: PimAppointmentItems appItems = getAppointmentItems();
135: PimAppointmentItemFilter appFilter = appItems
136: .getAppointmentItemFilter();
137: appFilter.setStartTime(start.getTime());
138: appFilter.setEndTime(end.getTime());
139:
140: events = toEvents(appItems);
141:
142: if (types == VEVENT) {
143: return events;
144: }
145: }
146:
147: if ((types & VTODO) != 0) {
148: PimTaskItems taskItems = getTaskItems();
149: if (taskItems != null) {
150: PimTaskItemFilter taskFilter = taskItems
151: .getTaskItemFilter();
152: taskFilter.setStartDate(start.getTime());
153: taskFilter.setDueDate(end.getTime());
154:
155: tasks = toTasks(taskItems);
156: }
157:
158: if (types == VTODO) {
159: return tasks;
160: }
161: }
162:
163: if ((types & VFREEBUSY) != 0) {
164: PimAddressEntryItem addrEntry = pimContainer
165: .getCurrentUser();
166: PimBusyStatusType busyStatus = addrEntry
167: .getBusyStatusType(start.getTime(), end
168: .getTime(), 60);
169:
170: freeBusy = toFreeBusy(busyStatus, start, end);
171:
172: }
173: //Once bug#4946227 is fixed un comment the following
174: //PimInvitationItems invItems = getInvitationItems();
175: //do the filter stuff here
176:
177: //invitedEvents = getInvitedEvents(invItems);
178:
179: int len = 0;
180: if (events != null)
181: len += events.length;
182: if (tasks != null)
183: len += tasks.length;
184: if (freeBusy != null)
185: len += freeBusy.length;
186: if (invitedEvents != null)
187: len += invitedEvents.length;
188:
189: CalendarComponent[] cc = new CalendarComponent[len];
190:
191: int i = 0;
192: if (events != null) {
193: for (i = 0; i < events.length; i++) {
194: cc[i] = events[i];
195: }
196: }
197:
198: if (tasks != null) {
199: for (int j = 0; j < tasks.length; j++) {
200: cc[i] = tasks[j];
201: i++;
202: }
203: }
204:
205: if (freeBusy != null) {
206: for (int k = 0; k < freeBusy.length; k++) {
207: cc[i] = freeBusy[k];
208: i++;
209: }
210: }
211: if (invitedEvents != null) {
212: for (int l = 0; l < invitedEvents.length; l++) {
213: cc[i] = invitedEvents[l];
214: i++;
215: }
216: }
217:
218: return cc;
219: } catch (Exception e) {
220: e.printStackTrace();
221: throw new CalendarException(e.getMessage());
222: }
223: }
224:
225: public CalendarComponent[] fetchComponents(String uid, String rid,
226: String modifier) throws OperationNotSupportedException {
227: throw new OperationNotSupportedException(
228: "Method fetchComponents(String, String, String) not supported");
229: }
230:
231: /**
232: * Store the Event.
233: * <br><br>
234: *
235: * @exception CalendarException if add fails for whatever reason
236: */
237: public void addEvent(VEvent event, boolean notify)
238: throws CalendarException {
239: try {
240: APimEvent pimEvent = (APimEvent) event;
241: pimEvent.update();
242: } catch (Exception e) {
243: e.printStackTrace();
244: throw new CalendarException(e.getMessage());
245: }
246: }
247:
248: public void addTodo(VTodo task, boolean notify)
249: throws CalendarException {
250: try {
251: APimTask pimTask = (APimTask) task;
252: pimTask.update();
253: } catch (Exception e) {
254: e.printStackTrace();
255: throw new CalendarException(e.getMessage());
256: }
257: }
258:
259: /**
260: * Store the event with a specified modifier.
261: * <br><br>
262: *
263: * @param event The modifed event
264: * @param modifer Event modifier, see RecurrenceModifier
265: * @exception CalendarException if modify fails for whatever reason
266: */
267: public void modifyEvent(VEvent event, String modifier,
268: boolean notify) throws CalendarException {
269: try {
270: APimEvent pimEvent = (APimEvent) event;
271: pimEvent.update();
272: } catch (Exception e) {
273: e.printStackTrace();
274: throw new CalendarException(e.getMessage());
275: }
276: }
277:
278: public void modifyTodo(VTodo task, String modifier, boolean notify)
279: throws CalendarException {
280: try {
281: APimTask pimTask = (APimTask) task;
282: pimTask.update();
283: } catch (Exception e) {
284: e.printStackTrace();
285: throw new CalendarException(e.getMessage());
286: }
287: }
288:
289: /**
290: * Delete the Specified Event.
291: * <br><br>
292: *
293: * @param event The modifed event
294: * @param modifer Event modifier, see RecurrenceModifier
295: * @exception CalendarException if delete fails for whatever reason
296: */
297: public void deleteEvent(VEvent event, String modifier,
298: boolean notify) throws CalendarException {
299: try {
300: APimEvent pimEvent = (APimEvent) event;
301: pimEvent.delete();
302: } catch (Exception e) {
303: e.printStackTrace();
304: throw new CalendarException(e.getMessage());
305: }
306: }
307:
308: public void deleteTodo(VTodo task, String modifier, boolean notify)
309: throws CalendarException {
310: try {
311: APimTask pimTask = (APimTask) task;
312: pimTask.delete();
313: } catch (Exception e) {
314: e.printStackTrace();
315: throw new CalendarException(e.getMessage());
316: }
317: }
318:
319: public Properties getCalProps()
320: throws OperationNotSupportedException {
321: throw new OperationNotSupportedException(
322: "Method getCalProps() not supported");
323: }
324:
325: public void setCalProps(Properties props)
326: throws OperationNotSupportedException {
327: throw new OperationNotSupportedException(
328: "Method getCalProps() not supported");
329: }
330:
331: private VEvent[] toEvents(PimAppointmentItems pItems)
332: throws PimException {
333: if (pItems == null) {
334: return null;
335: }
336:
337: Vector tmp = new Vector();
338: PimAppointmentItem pItem = pItems.getFirstAppointmentItem();
339: while (pItem != null) {
340: tmp.addElement(new APimEvent(this , pItem));
341: pItem = pItems.getNextAppointmentItem();
342: }
343: return (VEvent[]) tmp.toArray(new APimEvent[tmp.size()]);
344: }
345:
346: private VTodo[] toTasks(PimTaskItems tItems) throws PimException {
347: if (tItems == null) {
348: return null;
349: }
350:
351: Vector tmp = new Vector();
352: PimTaskItem tItem = tItems.getFirstTaskItem();
353: while (tItem != null) {
354: tmp.addElement(new APimTask(this , tItem));
355: tItem = tItems.getNextTaskItem();
356: }
357: return (VTodo[]) tmp.toArray(new APimTask[tmp.size()]);
358: }
359:
360: private VFreeBusy[] toFreeBusy(PimBusyStatusType busyStatus,
361: DateTime start, DateTime end) {
362: if (busyStatus == null) {
363: return null;
364: }
365: FreeBusy[] freeBusyList = new FreeBusy[1];
366: freeBusyList[0] = new FreeBusy(busyStatus.toString(), start,
367: end);
368:
369: APimFreeBusy[] result = new APimFreeBusy[1];
370: result[0] = new APimFreeBusy(false, freeBusyList);
371: return result;
372: }
373:
374: public boolean isUserTheOwner(String id) {
375: return true;
376: }
377:
378: public String toString() {
379: return "APimCalendar: " + calId;
380: }
381:
382: public String toRFC2445() {
383: return null;
384: }
385:
386: public void setTimeZone(TimeZone tz) {
387: CalendarStore cs = getStore();
388: CalendarSession session = cs.getSession();
389: session.setTimeZone(tz);
390: }
391:
392: public TimeZone getTimeZone() {
393: CalendarStore cs = getStore();
394: CalendarSession session = cs.getSession();
395: return session.getTimeZone();
396: }
397:
398: private VEvent[] getInvitedEvents(PimInvitationItems invItems)
399: throws PimException {
400: if (invItems == null) {
401: return null;
402: }
403: Vector tmp = new Vector();
404: PimMeetingItem meet = invItems.getFirstInvitationItem();
405: if (meet != null) {
406: PimAppointmentItem pItem = meet
407: .getAssociatedAppointmentItem();
408: tmp.addElement(new APimEvent(this , pItem, false, meet));
409: meet = invItems.getNextInvitationItem();
410: }
411: return (VEvent[]) tmp.toArray(new APimEvent[tmp.size()]);
412: }
413:
414: }
|