001: package com.sun.portal.app.sharedevents.util;
002:
003: import com.sun.comclient.calendar.CalendarSession;
004: import com.sun.comclient.calendar.CalendarStore;
005: import com.sun.comclient.calendar.CalendarStoreException;
006: import com.sun.comclient.calendar.DateTime;
007: import com.sun.comclient.calendar.StoreNotFoundException;
008: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtils;
009: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtilsFactory;
010: import com.sun.portal.app.calendarcommon.common.SharedServicesUtils;
011: import com.sun.portal.app.calendarcommon.common.SharedServicesUtilsFactory;
012: import com.sun.portal.app.calendarcommon.common.SharedServicesException;
013: import com.sun.portal.log.common.PortalLogger;
014: import java.text.DateFormat;
015: import java.text.SimpleDateFormat;
016: import java.util.Locale;
017: import java.util.Map;
018: import java.util.Properties;
019: import java.util.TimeZone;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022: import javax.faces.context.ExternalContext;
023: import javax.faces.context.FacesContext;
024: import javax.portlet.PortletContext;
025: import javax.portlet.PortletRequest;
026:
027: public class CalUserHelper {
028: private String CLASS_NAME = "CalUserHelper";
029:
030: private static Logger userHelperLogger = PortalLogger
031: .getLogger(CalUserHelper.class);
032:
033: /** Creates a new instance of CalUserHelper class
034: * Can be called only within the class.
035: * Only one instance is created per application
036: */
037: private CalUserHelper() {
038: super ();
039: }
040:
041: public static CalendarStore getCalStore(boolean checkvalidity)
042: throws SharedServicesException {
043: return getCalStore(checkvalidity, true);
044: }
045:
046: /**
047: * Returns a CalStore Object instance associated with the Portlet context.
048: *
049: * @param checkvalidity if the value is true then this method would
050: * recreate the calendar session if session is invalid.
051: * if set to false then no check is performed for the
052: * validity of the calendar session.
053: * @param createStrore flag to indicate if the creation of calendar store be enforced.
054: * if true, calendar store should be created if not present
055: *
056: * @see com.sun.comclient.calendar.CalendarStore
057: */
058: public static CalendarStore getCalStore(boolean checkvalidity,
059: boolean createStore) throws SharedServicesException {
060: CalendarStore calstore = null;
061: FacesContext context = null;
062:
063: context = FacesContext.getCurrentInstance();
064: ExternalContext extContext = context.getExternalContext();
065:
066: // Note: This code works only in portlet environment. You are testing this as
067: // web-app then don't use this code.
068:
069: // Get the CalendarStore object for the proxy community user. It contains
070: // the authenticated calendar session info. We use this to perform calendar operations
071: // on the community-calendar.
072: // The store object returned should contain a valid connection
073:
074: SharedCalendarUtils sharedCalUtils = SharedCalendarUtilsFactory
075: .getSharedCalendarUtils(context);
076: return sharedCalUtils.getProxyCalendarStore(context);
077:
078: }
079:
080: public static DateTime getToday() {
081:
082: String tzStr = CalUserHelper.getUserTimeZone();
083: TimeZone tz = TimeZone.getTimeZone(tzStr);
084:
085: DateTime dt = new DateTime(tz);
086: return dt;
087: }
088:
089: public static String getUserDateFormat() {
090: //Get the Date format computed.
091: // instead of computing it every time
092: SimpleDateFormat df = (SimpleDateFormat) getDateFormat();
093: return df.toPattern();
094: }
095:
096: /** <p>Return the DateFormat object for this Calendar.</p> */
097: private static DateFormat getDateFormat() {
098: return AppUtils.getDateFormat(getLocale());
099:
100: }
101:
102: /**
103: * <p>Convenience function to return the locale of the current context.</p>
104: */
105: private static Locale getLocale() {
106: FacesContext context = FacesContext.getCurrentInstance();
107: return context.getViewRoot().getLocale();
108: }
109:
110: public static String getUserTimeZone() {
111: String userTZ = null;
112: FacesContext context = FacesContext.getCurrentInstance();
113: ExternalContext extContext = context.getExternalContext();
114:
115: Object req = extContext.getRequest();
116: PortletRequest pReq = null;
117: if (req instanceof PortletRequest) {
118: pReq = (PortletRequest) req;
119: Map userInfo = (Map) pReq
120: .getAttribute(PortletRequest.USER_INFO);
121: if (userInfo != null) {
122: userTZ = (String) userInfo
123: .get(SharedConstants.USER_INFO_TIMEZONE);
124: if (userTZ == null) {
125: userTZ = TimeZone.getDefault().getID();
126: }
127: } else {
128: userTZ = TimeZone.getDefault().getID();
129: }
130:
131: } else {
132: userTZ = TimeZone.getDefault().getID();
133:
134: }
135:
136: return userTZ;
137: }
138:
139: public static DateTime getDateTimeFromISOString(String isoString) {
140:
141: DateTime dateTime = null;
142: if (isoString != null && isoString.length() > 0) {
143: try {
144: String tzStr = getUserTimeZone();
145: TimeZone tz = TimeZone.getTimeZone(tzStr);
146: dateTime = new DateTime(isoString, tz);
147: } catch (Exception e1) {
148: dateTime = CalUserHelper.getToday();
149: }
150: }
151:
152: return dateTime;
153:
154: }
155:
156: }
|