0001: package com.sun.portal.app.sharedevents.util;
0002:
0003: import com.sun.comclient.calendar.*;
0004: import com.sun.comclient.calendar.socs.SOCSEvent;
0005: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtils;
0006: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtilsFactory;
0007: import com.sun.portal.app.calendarcommon.common.SharedServicesUtils;
0008: import com.sun.portal.app.calendarcommon.common.SharedServicesUtilsFactory;
0009: import com.sun.portal.app.calendarcommon.common.SharedServicesException;
0010: import com.sun.portal.log.common.PortalLogger;
0011: import java.text.DateFormat;
0012: import java.text.SimpleDateFormat;
0013: import java.util.Calendar;
0014: import java.util.Locale;
0015: import java.util.Map;
0016: import java.util.ResourceBundle;
0017: import java.util.Set;
0018: import java.util.TimeZone;
0019: import java.util.logging.Level;
0020: import java.util.logging.Logger;
0021: import javax.faces.context.ExternalContext;
0022: import javax.faces.context.FacesContext;
0023: import javax.portlet.PortletURL;
0024: import javax.portlet.PortletRequest;
0025: import javax.portlet.PortletSession;
0026:
0027: import sun.text.resources.LocaleData;
0028: import com.sun.portal.search.demo.SearchDatabase;
0029:
0030: //import sun.text.resources.LocaleData;
0031:
0032: public class AppUtils {
0033: private static final String CLASS_NAME = "AppUtils";
0034:
0035: private static Logger utilsLogger = PortalLogger
0036: .getLogger(AppUtils.class);
0037:
0038: /** Creates a new instance of AppUtils class
0039: * Can be called only within the class.
0040: * Only one instance is created per application
0041: */
0042: private AppUtils() {
0043: super ();
0044: }
0045:
0046: /**
0047: * <p>Iniitalizes the date format for the current locale of this
0048: * user.</p>
0049: */
0050:
0051: public static DateFormat getDateFormat(Locale locale) {
0052: // TO DO - is there a better way to get the right date format
0053: // pattern for a locale? following code from SimpleDateFormat.java
0054: ResourceBundle r = LocaleData.getLocaleElements(locale);
0055: String[] dateTimePatterns = r
0056: .getStringArray("DateTimePatterns");
0057: String pattern = dateTimePatterns[DateFormat.SHORT + 4];
0058:
0059: /**
0060: * Parse the year, month, date order from the above pattern, which will
0061: * be composed of the year, month, and date. For an English locale, we
0062: * want to convert this pattern from "M/d/yy" to "MM/dd/yyyy") to be Y2K
0063: * compatible.
0064: */
0065: boolean yearSet = false;
0066: boolean monthSet = false;
0067: boolean daySet = false;
0068: String newPattern = "";
0069: int length = pattern.length();
0070:
0071: for (int i = 0; i < length; i++) {
0072: if (pattern.charAt(i) == 'y') {
0073: if (!yearSet) {
0074: yearSet = true;
0075: newPattern += "yyyy/";
0076: }
0077: } else if (pattern.charAt(i) == 'M') {
0078: if (!monthSet) {
0079: monthSet = true;
0080: newPattern += "MM/";
0081: }
0082: } else if (pattern.charAt(i) == 'd') {
0083: if (!daySet) {
0084: daySet = true;
0085: newPattern += "dd/";
0086: }
0087: }
0088: }
0089:
0090: // Remove the trailing /
0091: newPattern = newPattern.substring(0, newPattern.length() - 1);
0092: SimpleDateFormat dateFormat = new SimpleDateFormat(newPattern,
0093: locale);
0094: dateFormat.setLenient(false);
0095: return dateFormat;
0096:
0097: }
0098:
0099: public static DateFormat getDateFormat(String pattern, int dateStyle) {
0100: FacesContext context = FacesContext.getCurrentInstance();
0101: Locale locale = context.getExternalContext().getRequestLocale();
0102: return getDateFormat(pattern, dateStyle, locale);
0103: }
0104:
0105: public static DateFormat getDateFormat(String pattern,
0106: int dateStyle, Locale locale) {
0107:
0108: DateFormat dateFormat = null;
0109:
0110: if (pattern != null && pattern.length() > 0) {
0111:
0112: try {
0113: dateFormat = new SimpleDateFormat(pattern, locale);
0114:
0115: dateFormat.setLenient(false);
0116: } catch (Exception e) {
0117:
0118: }
0119:
0120: if (dateFormat == null) {
0121: dateFormat = DateFormat.getDateInstance(dateStyle,
0122: locale);
0123: dateFormat.setLenient(false);
0124: }
0125:
0126: }
0127:
0128: return dateFormat;
0129: }
0130:
0131: /**
0132: * Gets Displayble Date such as mm/dd/yy or dd/mm/yy or 07/30/2002
0133: * <p>
0134: * This displayable date form will be used in events display (tiled view)
0135: * in invitations view, events management/search views.
0136: * <p>
0137: */
0138: public static String getDisplayableDate(DateTime dt,
0139: String usersDateFormatPref, String usersDateDelimiterPref) {
0140: if (null == dt) {
0141: return null;
0142: }
0143:
0144: int m = dt.get(Calendar.MONTH);
0145: int d = dt.get(Calendar.DAY_OF_MONTH) - 1;
0146:
0147: String monthLabel = SharedConstants.displayableMonthLabels[m];
0148: if (monthLabel == null) {
0149: monthLabel = SharedConstants.QQ;
0150: }
0151: String dayLabel = SharedConstants.displayableDayLabels[d];
0152: if (dayLabel == null) {
0153: dayLabel = SharedConstants.QQ;
0154: }
0155:
0156: String yearLabel = getYearLabel(dt);
0157: if (yearLabel == null) {
0158: yearLabel = SharedConstants.QQQQ;
0159: }
0160:
0161: if (null == usersDateFormatPref) {
0162: usersDateFormatPref = SharedConstants.DEFAULT_DATE_FORMAT;
0163: }
0164:
0165: if (null == usersDateDelimiterPref) {
0166: usersDateDelimiterPref = SharedConstants.DEFAULT_DATE_DELIMITER;
0167: }
0168: StringBuffer DD = new StringBuffer(16);
0169:
0170: if (SharedConstants.DATE_FORMAT_D_M_Y
0171: .equalsIgnoreCase(usersDateFormatPref)) {
0172: DD.append(dayLabel);
0173: DD.append(usersDateDelimiterPref);
0174: DD.append(monthLabel);
0175: DD.append(usersDateDelimiterPref);
0176: DD.append(yearLabel);
0177: } else if (SharedConstants.DATE_FORMAT_Y_M_D
0178: .equalsIgnoreCase(usersDateFormatPref)) {
0179: DD.append(yearLabel);
0180: DD.append(usersDateDelimiterPref);
0181: DD.append(monthLabel);
0182: DD.append(usersDateDelimiterPref);
0183: DD.append(dayLabel);
0184: } else {
0185: DD.append(monthLabel);
0186: DD.append(usersDateDelimiterPref);
0187: DD.append(dayLabel);
0188: DD.append(usersDateDelimiterPref);
0189: DD.append(yearLabel);
0190: }
0191:
0192: return DD.toString();
0193: }
0194:
0195: /**
0196: * Gets Displayble Date Time such as mm/dd/yyyy hh:mm (4 hours) or
0197: * mm/dd/yyyy hh:mm (1.5 hours)
0198: * If the TimeZone is passed, the dates would be converted to that TZ
0199: * <p>
0200: * This displayable date-time form will be used in events display
0201: *
0202: * <p>
0203: */
0204: public static String getDisplayableDateTime(DateTime startdt,
0205: DateTime enddt, String usersDateFormatPref,
0206: String usersDateDelimiterPref, String usersTimeFormatPref,
0207: TimeZone tz) {
0208: StringBuffer DDT = new StringBuffer(32);
0209:
0210: String isoDt = null;
0211: DateTime tmpDt1 = null;
0212: DateTime tmpDt2 = null;
0213:
0214: // Convert dates to the TZ passed
0215: if (tz != null) {
0216: isoDt = startdt.toISO8601();
0217: try {
0218: tmpDt1 = new DateTime(isoDt, tz);
0219: } catch (Exception e1) {
0220: tmpDt1 = null;
0221: }
0222:
0223: if (tmpDt1 != null) {
0224: startdt = tmpDt1;
0225: }
0226:
0227: isoDt = enddt.toISO8601();
0228: try {
0229: tmpDt2 = new DateTime(isoDt, tz);
0230: } catch (Exception e1) {
0231: tmpDt2 = null;
0232: }
0233:
0234: if (tmpDt2 != null) {
0235: enddt = tmpDt2;
0236: }
0237:
0238: }
0239:
0240: DDT.append(getDisplayableDate(startdt, usersDateFormatPref,
0241: usersDateDelimiterPref));
0242: DDT.append(" ");
0243: DDT.append(getDisplayableTime(startdt, usersTimeFormatPref));
0244: DDT.append(" ");
0245:
0246: Duration dur = null;
0247: try {
0248: dur = new Duration(startdt, enddt);
0249: } catch (Exception e) {
0250: // _utilsLogger.warning("Couldn't find duration: Not puuting duration value in Displayble Date Time string. Reason: " + e);
0251: }
0252:
0253: if (null != dur) {
0254: DDT.append("(");
0255: DDT.append(dur.getHours());
0256: if (dur.getMinutes() > 5) {
0257: DDT.append(".");
0258: DDT.append(dur.getMinutes() / 6);
0259: }
0260: DDT.append(" ");
0261:
0262: String hoursString = null;
0263: if (null == hoursString) {
0264: hoursString = getResourceBundle()
0265: .getString("hoursText");
0266: }
0267: DDT.append(hoursString);
0268: DDT.append(")");
0269: }
0270:
0271: return DDT.toString();
0272: }
0273:
0274: /**
0275: * Returns displayble time string as in 12 hour format or 24 hour format,
0276: * for example it returns displayble time such as 16:45 or 4:45pm.
0277: * If this method fails to retrieve hours or minutes component or both
0278: * then it will returns "??" value; for example in such case it may return
0279: * displayble time such as ??:?? or ??:45 or ??:45pm or 16:?? or 4:??.
0280: * Default user's TimeFormat preference is '12' hour format in case if
0281: * 'usersTimeFormatPref' value passed is null.
0282: * <p>
0283: */
0284: public static String getDisplayableTime(DateTime dt,
0285: String usersTimeFormatPref) {
0286: String timehours = getHoursValue(dt, usersTimeFormatPref);
0287: String timeminutes = getMinutesValue(dt, usersTimeFormatPref);
0288:
0289: timehours = ((null == timehours) ? SharedConstants.QQ
0290: : timehours);
0291: timeminutes = ((null == timeminutes) ? SharedConstants.CQQ
0292: : timeminutes);
0293:
0294: return (timehours + ":" + timeminutes);
0295: }
0296:
0297: /**
0298: * Returns displayable hours string as in 12 hour format or 24 hour format.
0299: * Default user's TimeFormat preference is '12' hour format in case if
0300: * 'usersTimeFormatPref' value passed is null.
0301: * <p>
0302: * This method will return the appropriate indexed hours displayable
0303: * string usable by tiled logic's of events and todos, either from
0304: * 24'-hours-names-list or from 12'-hours-names-list based on
0305: * user's time format preference.
0306: * <p>
0307: */
0308: public static String getDisplayableHours(DateTime dt,
0309: String usersTimeFormatPref) {
0310: if (null == dt) {
0311: return null;
0312: }
0313:
0314: int h = dt.get(Calendar.HOUR_OF_DAY);
0315:
0316: if (SharedConstants.TIME_FORMAT_24.equals(usersTimeFormatPref)) {
0317: // Keep hours as same in 24 hour format
0318: //
0319: } else {
0320: // Hours conversion: 0 -> 12 am, 12 -> 12 pm,
0321: //1 to 11 --> 1 to 11 am, 13 to 23 --> 1 to 11 pm
0322: //
0323: h = (h + 12 - 1) % 12;
0324:
0325: }
0326:
0327: return Integer.toString(h);
0328: }
0329:
0330: /**
0331: * Returns displayable minutes string as in 12 hour format or 24
0332: * hour format.
0333: * Default user's TimeFormat preference is '12' hour format in case if
0334: * 'usersTimeFormatPref' value passed is null.
0335: * <p>
0336: */
0337: public static String getDisplayableMinutes(DateTime dt,
0338: String usersTimeFormatPref) {
0339: if (null == dt) {
0340: return null;
0341: }
0342:
0343: int h = dt.get(Calendar.HOUR_OF_DAY);
0344: int m = dt.get(Calendar.MINUTE)
0345: / SharedConstants.MINUTES_PRECISION;
0346:
0347: // Default user's TimeFormat is '12' hour format
0348: //
0349: if (SharedConstants.TIME_FORMAT_24.equals(usersTimeFormatPref)) {
0350: // Keep minutes as same in 24 hour format
0351: //
0352:
0353: } else {
0354: // Convert to 12 hour format
0355: //
0356: m = ((h > 11) ? (m + 12) : m);
0357:
0358: }
0359:
0360: return Integer.toString(m);
0361:
0362: }
0363:
0364: /**
0365: * Gets the context start date-time based on the given date and
0366: * view context value passed.
0367: * <p>
0368: * @param dt Date to be adjusted to context start
0369: * @param viewCtx View context to be used to adjust the date passed in 'dt'. Valid values are
0370: * SharedConstants.VIEW_CTX_DAY, SharedConstants.VIEW_CTX_WEEK, SharedConstants.VIEW_CTX_MONTH.
0371: * Default view context used is SharedConstants.VIEW_CTX_DAY in case the this value passed is null
0372: * or invalid.
0373: * @param firstDayOfWeek Used in case if viewCtx is SharedConstants.VIEW_CTX_WEEK, otherwise ignored.
0374: * Valid values are Calendar.SUNDAY, Calendar.MONDAY, .., Calendar.SATURDAY
0375: * <p>
0376: * This method clones the given date and then adjust it to the start of the
0377: * specified context.
0378: * For 'day' context it returns date-time for begining of the day,
0379: * For 'week' context it returns date-time for start date of the week
0380: * For 'month' context it returns date-time for start date of the month.
0381: * <p>
0382: * For 'day' context it resets the hour, minute, second, millisecond components to zero.
0383: * For 'week' context it resets the hour, minute, second, millisecond components to zero,
0384: * and resets DAY_OF_WEEK component to user's calendar's week-start-day preference.
0385: * For 'month' context it resets the hour, minute, second, millisecond components to zero,
0386: * and resets DAY_OF_MONTH component to 1.
0387: * <p>
0388: */
0389: public static DateTime getContextStart(DateTime dt1,
0390: String viewCtx, int firstDayOfWeek) {
0391: if (null == dt1) {
0392: return null;
0393: }
0394:
0395: DateTime dt = (DateTime) dt1.clone();
0396:
0397: // Reset the hours, minutes, seconds and millisecond components
0398: // This is applicable for all contexts such as day, week and month.
0399: //
0400: dt.setTime(0, 0, 0);
0401: dt.set(Calendar.MILLISECOND, 0);
0402:
0403: if (SharedConstants.VIEW_CTX_WEEK.equalsIgnoreCase(viewCtx)) {
0404: int dayOfWeek = dt.get(Calendar.DAY_OF_WEEK);
0405: int difference = ((7 + dayOfWeek - firstDayOfWeek) % 7);
0406: dt.add(Calendar.DAY_OF_YEAR, -difference);
0407: } else if (SharedConstants.VIEW_CTX_MONTH
0408: .equalsIgnoreCase(viewCtx)) {
0409: dt.set(Calendar.DAY_OF_MONTH, 1);
0410: }
0411:
0412: return dt;
0413: }
0414:
0415: /**
0416: * Returns the context end date-time based on the given date and
0417: * view context value passed.
0418: * <p>
0419: * @param dt Date to be adjusted to context end
0420: * @param viewCtx View context to be used to adjust the date passed in 'dt'.
0421: * Valid values are SharedConstants.VIEW_CTX_DAY, SharedConstants.VIEW_CTX_WEEK,
0422: * SharedConstants.VIEW_CTX_MONTH.
0423: * Default view context used is SharedConstants.VIEW_CTX_DAY in case the this value
0424: * passed is null or invalid.
0425: * <p>
0426: * This method clones the given date and then adjust it to the end of the
0427: * specified context.
0428: * For 'day' context it returns date-time for ending of the day,
0429: * For 'week' context it returns date-time for end date of the week,
0430: * For 'month' context it returns date-time for end date of the month.
0431: * <p>
0432: * For example, advancement will be by 1 day for day view,
0433: * 7 days for week view, one month for month view.
0434: * It substracts the 'SECOND' component by 1 so that the resultant
0435: * end date-time will fall within the boundaries of a single day or
0436: * a single week or a single month.
0437: * <p>
0438: */
0439: public static DateTime getContextEnd(DateTime dt, String viewCtx) {
0440: if (null == dt) {
0441: return null;
0442: }
0443:
0444: dt = (DateTime) dt.clone();
0445:
0446: if (SharedConstants.VIEW_CTX_DAY.equalsIgnoreCase(viewCtx)) {
0447: dt.add(Calendar.DAY_OF_YEAR, 1);
0448: dt.add(Calendar.SECOND, -1);
0449: } else if (SharedConstants.VIEW_CTX_WEEK
0450: .equalsIgnoreCase(viewCtx)) {
0451: dt.add(Calendar.DAY_OF_YEAR, 7);
0452: dt.add(Calendar.SECOND, -1);
0453: } else if (SharedConstants.VIEW_CTX_MONTH
0454: .equalsIgnoreCase(viewCtx)) {
0455: dt.add(Calendar.MONTH, 1);
0456: dt.add(Calendar.SECOND, -1);
0457: } else {
0458: dt.add(Calendar.DAY_OF_YEAR, 1);
0459: dt.add(Calendar.SECOND, -1);
0460: }
0461:
0462: return dt;
0463: }
0464:
0465: public static String getYearLabel(DateTime dt) {
0466: if (null != dt) {
0467: int y = dt.get(Calendar.YEAR) - 2005;
0468: if ((y >= 0) && (y <= 5)) {
0469: return SharedConstants.displayableYearLabels[y];
0470:
0471: } else {
0472: return Integer.toString(dt.get(Calendar.YEAR));
0473: }
0474: }
0475: return null;
0476: }
0477:
0478: /**
0479: * Returns hours value as in 12 hour format or 24 hour format.
0480: * Default user's TimeFormat preference is '12' hour format in case if
0481: * 'usersTimeFormatPref' value passed is null.
0482: * <p>
0483: */
0484: public static String getHoursValue(DateTime dt,
0485: String usersTimeFormatPref) {
0486: if (null == dt) {
0487: return null;
0488: }
0489:
0490: int h = dt.get(Calendar.HOUR_OF_DAY);
0491:
0492: if (SharedConstants.TIME_FORMAT_24.equals(usersTimeFormatPref)) {
0493: // Keep hours as same in 24 hour format
0494: //
0495: return SharedConstants.hoursIn24HourFormatValues[h];
0496:
0497: } else {
0498: // Hours conversion: 0 -> 12 am, 12 -> 12 pm,
0499: //1 to 11 --> 1 to 11 am, 13 to 23 --> 1 to 11 pm
0500: //
0501: h = (h + 12 - 1) % 12;
0502: return SharedConstants.hoursIn12HourFormatValues[h];
0503:
0504: }
0505: }
0506:
0507: /**
0508: * Returns minutes value as in 12 hour format or 24 hour format.
0509: * Default user's TimeFormat preference is '12' hour format in case if
0510: * 'usersTimeFormatPref' value passed is null.
0511: * <p>
0512: * This method will be useful for EventsModel, TasksModel,
0513: * new/edit/update event or task views.
0514: * <p>
0515: * This method will return the appropriate indexed minutes value
0516: * usable by EventsModel or TasksModel; either from 24'-minutes-values-list
0517: * or from 12'-minutes-values-list based on user's time format preference.
0518: * <p>
0519: */
0520: public static String getMinutesValue(DateTime dt,
0521: String usersTimeFormatPref) {
0522: if (null == dt) {
0523: return null;
0524: }
0525:
0526: int h = dt.get(Calendar.HOUR_OF_DAY);
0527: int m = dt.get(Calendar.MINUTE)
0528: / SharedConstants.MINUTES_PRECISION;
0529:
0530: // Default user's TimeFormat is '12' hour format
0531: //
0532: if (SharedConstants.TIME_FORMAT_24.equals(usersTimeFormatPref)) {
0533: // Keep minutes as same in 24 hour format
0534: //
0535: return SharedConstants.minutesIn24HourFormatValues[m];
0536: } else {
0537: // Convert to 12 hour format
0538: //
0539: m = ((h > 11) ? (m + 12) : m);
0540: return SharedConstants.minutesIn12HourFormatValues[m];
0541: }
0542: }
0543:
0544: /**
0545: * Converts the display date (MM/dd/YYYY, dd/MM/YYYY etc) to DateTime
0546: */
0547: public static DateTime getDateTimeFromDisplayDate(
0548: String displayDate, TimeZone tz,
0549: String userPrefDateDelimiter) {
0550:
0551: DateTime dt = null;
0552:
0553: if (displayDate == null) {
0554: return dt;
0555: }
0556:
0557: if (userPrefDateDelimiter == null) {
0558: userPrefDateDelimiter = SharedConstants.DEFAULT_DATE_DELIMITER;
0559: }
0560:
0561: if (tz == null) {
0562: tz = TimeZone.getDefault();
0563: }
0564:
0565: String month = null;
0566: String day = null;
0567: String year = null;
0568:
0569: String userDateFormat = CalUserHelper.getUserDateFormat();
0570: String[] dateInfo = null;
0571: try {
0572: dateInfo = displayDate.split(userPrefDateDelimiter);
0573: } catch (Exception e1) {
0574: utilsLogger.log(Level.WARNING, "Exception: "
0575: + e1.getMessage());
0576: return dt;
0577: }
0578:
0579: if (dateInfo == null) {
0580: return dt;
0581: }
0582: if (dateInfo.length < 3) {
0583: return dt;
0584: }
0585: if (SharedConstants.DATE_FORMAT_D_M_Y
0586: .equalsIgnoreCase(userDateFormat)) {
0587: day = dateInfo[0];
0588: month = dateInfo[1];
0589: year = dateInfo[2];
0590: } else if (SharedConstants.DATE_FORMAT_Y_M_D
0591: .equalsIgnoreCase(userDateFormat)) {
0592: day = dateInfo[2];
0593: month = dateInfo[1];
0594: year = dateInfo[0];
0595: } else {
0596: day = dateInfo[1];
0597: month = dateInfo[0];
0598: year = dateInfo[2];
0599: }
0600:
0601: int dayInt, monthInt, yearInt;
0602:
0603: try {
0604: dayInt = Integer.parseInt(day);
0605: monthInt = Integer.parseInt(month);
0606: yearInt = Integer.parseInt(year);
0607: } catch (Exception e2) {
0608: utilsLogger.log(Level.WARNING,
0609: "Exception: getDateTimeFromDisplayDate(): " + e2);
0610: return dt;
0611: }
0612:
0613: dt = new DateTime(tz);
0614: dt.setDay(dayInt);
0615: dt.setMonth(monthInt - 1);
0616: dt.setYear(yearInt);
0617:
0618: return dt;
0619:
0620: }
0621:
0622: /* Get the localized date string, using a date pattern defined in the locale resource-bundle.
0623: * If the pattern is invalid, the datestyle is used instead.
0624: *
0625: * @param dt The DateTime object corresponding to which we want the localized date string
0626: * @param pattern Display date pattern property
0627: * Pattern Syntax: http://java.sun.com/docs/books/tutorial/i18n/format/datepattern.html
0628: * @param dateStyle fall-back style if 'pattern' does not correspond to a valid pattern
0629: */
0630:
0631: public static String getLocaleDatePerPattern(DateTime dt,
0632: String pattern, int datestyle) {
0633: if (null == dt) {
0634: return null;
0635: }
0636:
0637: DateFormat dateformat = getDateFormat(pattern, datestyle);
0638: TimeZone tz = dt.getTimeZone();
0639: if (null != tz) {
0640: dateformat.setTimeZone(tz);
0641: }
0642: return dateformat.format(dt.getTime());
0643: }
0644:
0645: /*
0646: * Returns the ResourceBundle object for the current Locale that is set
0647: * for this request. This Locale is obtained from the FacesContext
0648: */
0649: public static ResourceBundle getResourceBundle() {
0650: FacesContext context = FacesContext.getCurrentInstance();
0651: Locale locale = context.getExternalContext().getRequestLocale();
0652: return getResourceBundle(locale);
0653:
0654: }
0655:
0656: /*
0657: * Returns the ResourceBundle object for the specified locale.
0658: * JVM caches per-locale bundle. So, calling this many times would
0659: * not be a big overhead.
0660: */
0661: public static ResourceBundle getResourceBundle(Locale locale) {
0662:
0663: ResourceBundle bundle = null;
0664:
0665: if (locale != null) {
0666:
0667: } else {
0668: locale = Locale.getDefault();
0669: }
0670: bundle = ResourceBundle.getBundle(
0671: SharedConstants.BUNDLE_BASE_NAME, locale);
0672:
0673: return bundle;
0674:
0675: }
0676:
0677: /*
0678: * When the App is run in the community context, this method always returns the
0679: * calendar ID of the current community-calendar.
0680: * The events that are fetched by the App. should always be from this calendar.
0681: * If the app. needs to be run in non-community mode, the method needs to return the
0682: * calendar Id of the target calendar.
0683: */
0684: public static String getTargetCalendarId() throws Exception {
0685: utilsLogger.entering(CLASS_NAME, "getTargetCalendarId()");
0686: String commCalId = null;
0687: FacesContext context = FacesContext.getCurrentInstance();
0688: ExternalContext extContext = context.getExternalContext();
0689:
0690: SharedCalendarUtils sharedCalUtils = null;
0691:
0692: try {
0693: sharedCalUtils = SharedCalendarUtilsFactory
0694: .getSharedCalendarUtils(context);
0695: commCalId = sharedCalUtils.getCommunityCalendarID(context);
0696: } catch (Exception e1) {
0697: utilsLogger.log(Level.SEVERE,
0698: "Exception in obtaining community calID: " + e1);
0699: throw e1;
0700: }
0701: utilsLogger.exiting(CLASS_NAME, "getTargetCalendarId()");
0702:
0703: return commCalId;
0704:
0705: }
0706:
0707: /*
0708: * When the App is run in the community context, the method always returns the
0709: * calendar ID of the current community-calendar.
0710: * The events that are created by the community members need to set this as Organizer
0711: * so that the calendar server creates the event appropriately (i.e event on the
0712: * community-calendar with a copy of event on all the attendees (community-memebrs)
0713: * calendar.
0714: * If the app. needs to run in a non-community mode, this has to be modified to return
0715: * the actual Organizer's calid.
0716: */
0717: public static String getOrganizerCalendarId() throws Exception {
0718: return getTargetCalendarId();
0719: }
0720:
0721: /*
0722: * When the App is run in the community context, the method always returns the
0723: * calendar IDs of the all members of the community
0724: * The events that are created by the community members need to set these as Attendees
0725: * so that the calendar server creates the event appropriately (i.e event on the
0726: * community-calendar with a copy of event on all the attendees (community-memebrs)
0727: * calendar.
0728: * If the app. needs to run in a non-community mode, this has to be modified to return
0729: * the actual attendees
0730: */
0731:
0732: public static String[] getAttendeeCalIds() throws Exception {
0733:
0734: String[] attendeeCalIds = null;
0735:
0736: FacesContext context = FacesContext.getCurrentInstance();
0737: ExternalContext extContext = context.getExternalContext();
0738:
0739: SharedServicesUtils sharedServices = null;
0740: SharedCalendarUtils sharedCalUtils = null;
0741: Set memberIds = null;
0742: try {
0743: sharedServices = SharedServicesUtilsFactory
0744: .getSharedServicesUtils(context);
0745: memberIds = sharedServices.getMemberIDs(context);
0746: } catch (Exception e1) {
0747: utilsLogger.log(Level.SEVERE,
0748: "Exception in obtaining memberIDs: " + e1);
0749: throw e1;
0750: }
0751:
0752: try {
0753: sharedCalUtils = SharedCalendarUtilsFactory
0754: .getSharedCalendarUtils(context);
0755: } catch (Exception e1) {
0756: utilsLogger.log(Level.SEVERE,
0757: "Exception in obtaining sharedCalUtils: " + e1);
0758:
0759: throw e1;
0760: }
0761:
0762: if (memberIds != null && !memberIds.isEmpty()) {
0763: attendeeCalIds = new String[memberIds.size()];
0764: Object[] objArr = memberIds.toArray();
0765: String[] memberIdArray = getStringArray(objArr);
0766: if (memberIdArray != null && memberIdArray.length > 0) {
0767: if (utilsLogger.isLoggable(Level.FINE)) {
0768: utilsLogger.fine("memberIdArray is not null");
0769: }
0770: attendeeCalIds = new String[memberIdArray.length];
0771: int calidCount = 0;
0772: for (int i = 0; i < memberIdArray.length; i++) {
0773: String calid = null;
0774: try {
0775: calid = sharedCalUtils.getMemberCalendarID(
0776: context, memberIdArray[i]);
0777: } catch (Exception e1) {
0778: utilsLogger.log(Level.SEVERE,
0779: "Exception using : getMemberCalendarID: "
0780: + e1);
0781: }
0782:
0783: if (calid != null && calid.length() > 0) {
0784: attendeeCalIds[calidCount] = calid;
0785: calidCount++;
0786: }
0787:
0788: }
0789: }
0790: }
0791:
0792: return attendeeCalIds;
0793:
0794: }
0795:
0796: public static String[] getStringArray(Object[] objVals) {
0797: if (null == objVals)
0798: return (String[]) null;
0799: else {
0800: String[] strVals = new String[objVals.length];
0801: for (int i = 0; i < objVals.length; i++)
0802: strVals[i] = (String) objVals[i];
0803: return strVals;
0804: }
0805: }
0806:
0807: /*
0808: * Returns the specified attribute's value from Session
0809: */
0810: public static Object getSessionAttribute(String attr) {
0811:
0812: ExternalContext extContext = FacesContext.getCurrentInstance()
0813: .getExternalContext();
0814: return extContext.getSessionMap().get(attr);
0815: }
0816:
0817: /*
0818: * Sets the specified attribute and its value to Session
0819: */
0820:
0821: public static void setAttributeInSession(String attr, Object value) {
0822: ExternalContext extContext = FacesContext.getCurrentInstance()
0823: .getExternalContext();
0824:
0825: if (attr != null && value != null) {
0826: extContext.getSessionMap().put(attr, value);
0827: }
0828: }
0829:
0830: /*
0831: * Removes the specified attribute and its value from Session
0832: */
0833:
0834: public static void removeAttributeFromSession(String attr) {
0835: ExternalContext extContext = FacesContext.getCurrentInstance()
0836: .getExternalContext();
0837:
0838: extContext.getSessionMap().remove(attr);
0839:
0840: }
0841:
0842: public static Object getPortletSessionAttribute(String key) {
0843: FacesContext context = FacesContext.getCurrentInstance();
0844: Object pReq = context.getExternalContext().getRequest();
0845: if (pReq instanceof PortletRequest) {
0846: PortletSession pSession = ((PortletRequest) pReq)
0847: .getPortletSession();
0848: if (pSession != null) {
0849: return pSession.getAttribute(key);
0850: }
0851: }
0852: return null;
0853: }
0854:
0855: public static void removePortletSessionAttribute(String key) {
0856: FacesContext context = FacesContext.getCurrentInstance();
0857: Object pReq = context.getExternalContext().getRequest();
0858: if (pReq instanceof PortletRequest) {
0859: PortletSession pSession = ((PortletRequest) pReq)
0860: .getPortletSession();
0861: if (pSession != null) {
0862: pSession.removeAttribute(key);
0863: }
0864: }
0865: }
0866:
0867: public static boolean hasSearchResultsInURL() {
0868: boolean result = false;
0869: Object uid = getPortletSessionAttribute(SharedConstants.SESSION_CURR_EVENT_UID);
0870: if (uid != null) {
0871: utilsLogger
0872: .fine("AppUtils.getPortletSessionAttribute(SharedConstants.SESSION_CURR_EVENT_UID)="
0873: + uid);
0874: setAttributeInSession(
0875: SharedConstants.SESSION_COMMUNITY_EVENT_SEARCH,
0876: String.valueOf(true));
0877: removePortletSessionAttribute(SharedConstants.SESSION_CURR_EVENT_UID);
0878: setAttributeInSession(
0879: SharedConstants.SESSION_CURR_EVENT_UID, uid);
0880: result = true;
0881: Object rid = AppUtils
0882: .getPortletSessionAttribute(SharedConstants.SESSION_CURR_EVENT_RID);
0883: if (rid != null) {
0884: utilsLogger
0885: .fine("AppUtils.getPortletSessionAttribute(SharedConstants.SESSION_CURR_EVENT_RID)="
0886: + rid);
0887: removePortletSessionAttribute(SharedConstants.SESSION_CURR_EVENT_RID);
0888: setAttributeInSession(
0889: SharedConstants.SESSION_CURR_EVENT_RID, rid);
0890: } else {
0891: removeAttributeFromSession(SharedConstants.SESSION_CURR_EVENT_RID);
0892: }
0893: }
0894: return result;
0895: }
0896:
0897: /*
0898: * Returns whether to use attendee information while creating/modifying events
0899: */
0900:
0901: public static boolean useAttendees() {
0902: boolean useAttendees = true;
0903: FacesContext context = FacesContext.getCurrentInstance();
0904: SharedCalendarUtils sharedCalUtils = null;
0905:
0906: try {
0907: sharedCalUtils = SharedCalendarUtilsFactory
0908: .getSharedCalendarUtils(context);
0909: useAttendees = sharedCalUtils
0910: .createEventsInMembersCalendar(context);
0911: } catch (Exception e1) {
0912: utilsLogger.log(Level.SEVERE,
0913: "Exception in obtaining sharedCalUtils: " + e1);
0914: useAttendees = true;
0915: }
0916: return useAttendees;
0917: }
0918:
0919: public static void addEventToSearchDatabase(VEvent event) {
0920: utilsLogger.fine("Enter addEvent: ev=" + event);
0921:
0922: try {
0923:
0924: if (event == null)
0925: return;
0926: utilsLogger.fine("event To be added to search Database="
0927: + event.toString());
0928:
0929: FacesContext context = FacesContext.getCurrentInstance();
0930: ExternalContext extContext = context.getExternalContext();
0931:
0932: utilsLogger.fine("extCtxt=" + extContext);
0933:
0934: String eventID = null;
0935: //String rid=null;
0936:
0937: try {
0938:
0939: DateTime stDt = event.getStartTime();
0940: DateTime endDt = event.getEndTime();
0941: String df = CalUserHelper.getUserDateFormat();
0942: String tzStr = CalUserHelper.getUserTimeZone();
0943: TimeZone tz = TimeZone.getTimeZone(tzStr);
0944: String startTime = AppUtils.getDisplayableDateTime(
0945: stDt, endDt, df, "/", "24", tz);
0946:
0947: utilsLogger.fine("startTime = " + startTime);
0948:
0949: String description = event.getDescription();
0950: String title = event.getSummary();
0951:
0952: eventID = event.getID();
0953: utilsLogger.fine("event uid = " + eventID);
0954:
0955: Map sessionMap = extContext.getSessionMap();
0956: PortletURL actionURL = (PortletURL) sessionMap
0957: .get(SharedConstants.URL_EVENT_PORTLET_ACTION);
0958: if (eventID != null) {
0959: actionURL.setParameter(
0960: SharedConstants.URL_COMM_SEARCH_EVENT_UID,
0961: eventID);
0962: }
0963:
0964: utilsLogger.fine("event.getRecurrenceRules()"
0965: + event.getRecurrenceRules());
0966: utilsLogger.fine("event.event.isRecurring()"
0967: + event.isRecurring());
0968: RecurrencePattern[] pat = event.getRecurrenceRules();
0969: // event.isRecurring() does not return valid values yet as this event is not fetched
0970:
0971: if (pat != null) {
0972: utilsLogger.fine("pat.length " + pat.length);
0973: }
0974: //Add the rid even if it is zero, so it it replaces the rid in actionURL
0975: String rid = "0";
0976: if ((pat != null) && (pat.length > 0)) {
0977: rid = String.valueOf(event.getStartTime()
0978: .toISO8601());
0979: }
0980: utilsLogger.fine("event rid = " + rid);
0981: actionURL.setParameter(
0982: SharedConstants.URL_CURR_EVENT_RID, rid);
0983:
0984: String displayURL = actionURL.toString();
0985: utilsLogger.fine("displayURL=" + displayURL);
0986:
0987: SharedServicesUtils sharedServices = SharedServicesUtilsFactory
0988: .getSharedServicesUtils(context);
0989: String memberID = sharedServices
0990: .getCurrentMemberID(context);
0991:
0992: utilsLogger.fine("mbr id = " + memberID);
0993:
0994: SharedCalendarUtils sharedCalUtils = SharedCalendarUtilsFactory
0995: .getSharedCalendarUtils(context);
0996:
0997: utilsLogger.fine("sharedCalUtils = " + sharedCalUtils);
0998: sharedCalUtils.insertItemIntoSearchDatabase(context,
0999: SharedCalendarUtils.APP_TYPE_SHAREDEVENTS,
1000: memberID, eventID, title, startTime + " -- "
1001: + description, displayURL);
1002:
1003: utilsLogger.fine("event inserted to searchDatabase");
1004:
1005: } catch (CalendarComponentException e) {
1006: utilsLogger.log(Level.WARNING, "Unable to add event "
1007: + eventID + " to search database", e);
1008: } catch (OperationNotSupportedException e) {
1009: utilsLogger.log(Level.WARNING, "Unable to add event "
1010: + eventID + " to search database", e);
1011: } catch (SharedServicesException e) {
1012: utilsLogger.log(Level.WARNING, "Unable to add event "
1013: + eventID + " to search database", e);
1014: }
1015:
1016: } catch (Exception e) {
1017: utilsLogger.log(Level.SEVERE,
1018: "Exception in addEventToSearchDatabase: "
1019: + e.getMessage(), e);
1020: }
1021:
1022: }
1023:
1024: public static void deleteEventFromSearchDatabase(VEvent event) {
1025:
1026: if (event == null)
1027: return;
1028:
1029: FacesContext context = FacesContext.getCurrentInstance();
1030: ExternalContext extContext = context.getExternalContext();
1031:
1032: String eventID = "NULL";
1033: try {
1034:
1035: eventID = event.getID();
1036: SharedCalendarUtils sharedCalUtils = SharedCalendarUtilsFactory
1037: .getSharedCalendarUtils(context);
1038: sharedCalUtils.deleteItemFromSearchDatabase(context,
1039: SharedCalendarUtils.APP_TYPE_SHAREDEVENTS, eventID);
1040:
1041: } catch (CalendarComponentException e) {
1042: utilsLogger.log(Level.WARNING, "Unable to delete event "
1043: + eventID + " from search database", e);
1044: } catch (OperationNotSupportedException e) {
1045: utilsLogger.log(Level.WARNING, "Unable to delete event "
1046: + eventID + " from search database", e);
1047: } catch (SharedServicesException e) {
1048: utilsLogger.log(Level.WARNING, "Unable to delete event "
1049: + eventID + " from search database", e);
1050: }
1051:
1052: }
1053:
1054: public static SearchDatabase getSearchDatabase() {
1055:
1056: FacesContext context = FacesContext.getCurrentInstance();
1057: ExternalContext extContext = context.getExternalContext();
1058:
1059: SearchDatabase db = null;
1060:
1061: try {
1062:
1063: SharedCalendarUtils sharedCalUtils = SharedCalendarUtilsFactory
1064: .getSharedCalendarUtils(context);
1065: db = sharedCalUtils.getSearchDatabase(context);
1066:
1067: } catch (SharedServicesException ex) {
1068: utilsLogger.log(Level.WARNING,
1069: "Unable to connect to search database");
1070: }
1071:
1072: return db;
1073:
1074: }
1075:
1076: public static Object getBean(String beanName) {
1077: FacesContext context = FacesContext.getCurrentInstance();
1078:
1079: return context.getApplication().getVariableResolver()
1080: .resolveVariable(context, beanName);
1081: }
1082:
1083: }
|