001: // Copyright %G% Sun Microsystems, Inc. All Rights Reserved.
002: // "%W% %E% Sun Microsystems"
003:
004: package com.sun.portal.wireless.taglibs.cal;
005:
006: import javax.servlet.http.*;
007: import javax.servlet.jsp.*;
008: import javax.servlet.jsp.tagext.*;
009:
010: import com.iplanet.sso.SSOToken;
011: import com.iplanet.sso.SSOTokenEvent;
012:
013: import com.sun.comclient.calendar.CalendarSession;
014: import com.sun.comclient.calendar.CalendarStore;
015: import com.sun.comclient.calendar.ICalendar;
016: import com.sun.comclient.calendar.VEvent;
017: import com.sun.comclient.calendar.VTodo;
018: import com.sun.comclient.calendar.CalendarStoreException;
019:
020: import com.sun.ssoadapter.SSOAdapterFactory;
021: import com.sun.ssoadapter.SSOAdapter;
022:
023: import com.sun.portal.wireless.taglibs.base.Context;
024: import com.sun.portal.wireless.util.AppConfigConstants;
025:
026: /**
027: * CalContext - track state across requests.
028: *
029: *
030: * @author Sathya kavacheri
031: * @version 1.0
032: * @see com.sun.portal.wireless.taglibs.base.Context
033: */
034:
035: public class CalContext extends Context {
036:
037: public static final String CONTEXT_CLASS_NAME = "com.sun.portal.wireless.taglibs.cal.CalContext";
038:
039: public static final String CONTEXT_CACHE_CLASS_NAME = "com.sun.portal.wireless.taglibs.cal.CalContextCache";
040:
041: public static final String CONTEXT_TYPE = "calcontext";
042:
043: public static final String SSO_CONFIG_TYPE = "CALENDAR-TYPE";
044:
045: static final String CALSESSION = "_wps_calendar_session";
046:
047: private CalendarStore calStore = null;
048: private CalendarSession calSession = null;
049:
050: private ICalendar currentCalendar = null; // current calendar
051: private String currentCalendarID = null; // current calendar id
052:
053: private EventBean[] events; // calendar events
054: private TaskBean[] tasks; // calendar tasks
055:
056: public CalContext() {
057: super ();
058: }
059:
060: /**
061: * Initialize a context with the specified session.
062: *
063: * @param session Context belongs to this session.
064: * @exception Exception
065: */
066: public void init(HttpServletRequest request, SSOToken session,
067: SSOAdapter ssoAdapter) throws Exception {
068: super .init(request, session,
069: AppConfigConstants.CALENDAR_APP_SERVICE_NAME,
070: ssoAdapter);
071:
072: calStore = (CalendarStore) ssoAdapter.getConnection();
073: calSession = calStore.getSession();
074: calSession.setTimeZone(getTimeZone());
075:
076: try {
077: currentCalendar = calStore.openCalendar();
078: currentCalendarID = currentCalendar.getCalID();
079: } catch (CalendarStoreException cse) {
080: throw new JspException("Opening default Calendar Failed:");
081: }
082:
083: return;
084: }
085:
086: /**
087: * This creates an provider/adapter dependent Event
088: * invoked by FetcTag
089: * Can be implemented by subclasses to create a more specific EventBean
090: * @param JCAPI VEvent
091: * @return an EventBean or it's derived instance
092: */
093: public EventBean toEventBean(VEvent event) {
094: EventBean eb = new EventBean(event);
095: return eb;
096: }
097:
098: /**
099: * This creates an provider/adapter dependent Task
100: * invoked by FetcTag
101: * Can be implemented by subclasses to create a more specific TaskBean
102: * @param JCAPI VTodo
103: * @return an TaskBean or it's derived instance
104: */
105: public TaskBean toTaskBean(VTodo task) {
106: return new TaskBean(task);
107: }
108:
109: /**
110: * Returns a CalContext for this session.
111: *
112: * @param pageContext
113: * @exception Exception
114: */
115: public static CalContext getContext(PageContext pageContext)
116: throws Exception {
117:
118: return (CalContext) Context.getContext(pageContext,
119: CONTEXT_CLASS_NAME, CONTEXT_CACHE_CLASS_NAME,
120: CONTEXT_TYPE);
121: }
122:
123: /**
124: * Set the calendarID as the current Calendar
125: */
126:
127: public void setCalendar(String calendarID) throws JspException {
128: try {
129: currentCalendar = calStore.openCalendar(calendarID);
130: currentCalendarID = calendarID;
131: } catch (CalendarStoreException cse) {
132: throw new JspException("Opening Calendar Failed: "
133: + calendarID);
134: }
135: }
136:
137: /**
138: * Get the Current Calendar
139: *
140: * @return the Current Calendar; null if not available
141: */
142: public ICalendar getCalendar() {
143: return currentCalendar;
144: }
145:
146: /**
147: * Get the CalStore
148: *
149: * @return the Current Calendar; null if not available
150: */
151: public CalendarStore getCalStore() {
152: return calStore;
153: }
154:
155: /**
156: * Get the Current CalendarID
157: *
158: * @return the Current CalendarD; null if not available
159: */
160: public String getCalendarID() {
161: return currentCalendarID;
162: }
163:
164: /**
165: * Get the current event list
166: *
167: * @return the current event list; null is not available
168: */
169: public EventBean[] getEvents() {
170: return events;
171: }
172:
173: /**
174: * Get the current task list
175: * @return the current task list. null is not available
176: */
177: public TaskBean[] getTasks() {
178: return tasks;
179: }
180:
181: /**
182: * Set the current event list
183: *
184: * @return the current event list; null is not available
185: */
186: public void setEvents(EventBean[] evnts) {
187: events = evnts;
188: }
189:
190: /**
191: * Set the current task list
192: *
193: * @return the current task list; null is not available
194: */
195: public void setTasks(TaskBean[] tsks) {
196: tasks = tsks;
197: }
198:
199: /**
200: * Get the specified event from the event list
201: *
202: * @param index the index into the current event list
203: * @return the selected event or null if not found
204: */
205: public EventBean getEvent(int index) {
206: return (events != null) ? events[index] : null;
207: }
208:
209: /**
210: * Get the specified task from the task list
211: *
212: * @param index the index into the current task list
213: * @return the selected task or null if not found
214: */
215: public TaskBean getTask(int index) {
216: return (tasks != null) ? tasks[index] : null;
217: }
218:
219: /**
220: * Get the count of the current event list
221: *
222: * @return the event list count
223: */
224: public int getEventCount() {
225: return (events != null) ? events.length : 0;
226: }
227:
228: /**
229: * Get the count of the current task list
230: *
231: * @return the task list count
232: */
233: public int getTaskCount() {
234: return (tasks != null) ? tasks.length : 0;
235: }
236:
237: /**
238: * Get the size of the current event list
239: *
240: * @return the event list size
241: */
242: public int getEventSize() {
243: return getEventCount();
244: }
245:
246: /**
247: * Returns whether the events list is empty or not
248: *
249: * @return true if the event list is empty
250: * false otherwise
251: */
252: public boolean isEventsEmpty() {
253: return getEventCount() == 0;
254: }
255:
256: /**
257: * Returns whether the tasks list is empty or not
258: *
259: * @return true if the tasks list is empty
260: * false otherwise
261: */
262: public boolean isTasksEmpty() {
263: return getTaskCount() == 0;
264: }
265:
266: /**
267: * Get the iCal ServiceURL
268: *
269: * @return the service url
270: */
271: public String getServiceURL() {
272: return calStore.getServiceUrl().toString();
273: }
274:
275: /**
276: * Gets a property from the CalSession. Ex: preferredtimezone - The
277: * CalStore after the connect, retrieves user values stored in the
278: * backend service and populates them in the Properties.
279: *
280: * @param name name of the property to look for in CalSession
281: * @return value of the property
282: */
283: public String getSessionProperty(String name) {
284: return calSession.getProperty(name);
285: }
286:
287: /**
288: * Checks if user is the Owner of this calendar
289: * @return true if the user can modify it, false otherwise
290: */
291: public boolean isUserTheOwner() {
292: return true;
293: }
294:
295: /**
296: * Handle SessionChanged events from the Portal Session.
297: * Cleanup the context when the session goes invalid.
298: *
299: * @param se session change event
300: */
301: public void ssoTokenChanged(SSOTokenEvent evt) {
302: try {
303: int evtType = evt.getType();
304:
305: if (evtType != evt.SSO_TOKEN_DESTROY
306: && evtType != evt.SSO_TOKEN_IDLE_TIMEOUT
307: && evtType != evt.SSO_TOKEN_MAX_TIMEOUT) {
308: return;
309: }
310:
311: if (calStore != null) {
312: calStore.disconnect();
313: }
314:
315: } catch (Exception e) {
316: }
317:
318: super .ssoTokenChanged(evt);
319: }
320:
321: /**
322: *Read if any attributes needs to be read
323: *Lets say the user has modified time zone , we have no clue.
324: *hence read it again
325: *it is reqd in here because calSession also caches the timeZone.
326: *Hence update it
327: */
328: public void reloadUserPreferences() {
329: loadTimeZone();
330:
331: if (getSSOAdapter() != null) {
332: calStore = (CalendarStore) getSSOAdapter().getConnection();
333: }
334: if (calStore != null) {
335: calSession = calStore.getSession();
336: }
337: if (calSession != null) {
338: calSession.setTimeZone(getTimeZone());
339: }
340: }
341:
342: }
|