001: package com.sun.portal.app.sharedtasks.util;
002:
003: import com.sun.comclient.calendar.RecurrencePattern;
004: import com.sun.portal.app.sharedtasks.faces.beans.TaskUserSessionBackingBean;
005: import com.sun.web.ui.model.Option;
006: import java.util.ResourceBundle;
007:
008: import com.sun.portal.log.common.PortalLogger;
009: import java.util.logging.Logger;
010: import java.util.logging.Level;
011:
012: /**
013: * Type-safe enumeration to model all available recurrence patterns.
014: *
015: * @author ashwin.mathew@sun.com
016: */
017: public class CalTaskRepeat {
018: private static final String RECURRENCE_COUNT = "COUNT=";
019: private static final String RECURRENCE_PREFIX = "RRULE:";
020: private static final String RECURRENCE_SEPARATOR = ";";
021: private static final int REPEAT_NO = 9;
022:
023: private String[] rStringElements;
024:
025: private String rString;
026:
027: private String displayKey;
028:
029: private static Logger logger = PortalLogger
030: .getLogger(CalTaskRepeat.class);
031:
032: private static CalTaskRepeat[] repeats = new CalTaskRepeat[REPEAT_NO];
033: private static int repeatIndex = 0;
034:
035: private CalTaskRepeat(String[] rStringElements, String displayKey) {
036: this .displayKey = displayKey;
037: this .rStringElements = rStringElements;
038:
039: StringBuffer rStringBuffer = new StringBuffer();
040: for (int i = 0; i < rStringElements.length; i++) {
041: rStringBuffer.append(rStringElements[i]).append(
042: RECURRENCE_SEPARATOR);
043: }
044: this .rString = rStringBuffer.toString();
045:
046: repeats[repeatIndex++] = this ;
047: }
048:
049: public String getDisplayKey() {
050: return displayKey;
051: }
052:
053: public String getRString() {
054: return rString;
055: }
056:
057: public String[] getRStringElements() {
058: return rStringElements;
059: }
060:
061: public static String getStringForPattern(RecurrencePattern pattern) {
062: String rPattern = null;
063: int matchSpecificity = 0;
064: String patternString = pattern.toRFC2445();
065:
066: if (logger.isLoggable(Level.FINE)) {
067: logger.fine("Getting string for recurrence pattern ["
068: + patternString + "]");
069: }
070:
071: for (int i = 0; i < REPEAT_NO; i++) {
072: boolean match = true;
073:
074: String[] rStringElements = repeats[i].getRStringElements();
075:
076: for (int j = 0; j < rStringElements.length; j++) {
077: if (logger.isLoggable(Level.FINEST)) {
078: logger.finest("Matching against element ["
079: + rStringElements[j] + "]");
080: }
081:
082: if (patternString.indexOf(rStringElements[j]) == -1) {
083: match = false;
084: break;
085: }
086: }
087:
088: if (match && rStringElements.length > matchSpecificity) {
089: rPattern = repeats[i].getRString();
090: matchSpecificity = rStringElements.length;
091: // Don't break after this, since a more specific match
092: // may be found.
093: if (logger.isLoggable(Level.FINEST)) {
094: logger.finest("Found pattern match [" + rPattern
095: + "]");
096: }
097: }
098: }
099:
100: if (logger.isLoggable(Level.FINE)) {
101: logger.fine("Matched pattern [" + rPattern + "]");
102: }
103:
104: return rPattern;
105: }
106:
107: public static RecurrencePattern makeRPattern(String rString,
108: int rCount) {
109: StringBuffer rfcPattern = new StringBuffer();
110: rfcPattern.append(RECURRENCE_PREFIX).append(rString).append(
111: RECURRENCE_COUNT).append(rCount);
112:
113: return new RecurrencePattern(rfcPattern.toString());
114: }
115:
116: public static Option[] getRecurrenceOptions() {
117: TaskUserSessionBackingBean userSession = (TaskUserSessionBackingBean) TaskBeanFactory
118: .getBean(TaskUserSessionBackingBean.BEAN_NAME);
119:
120: ResourceBundle bundle = userSession.getResourceBundle();
121:
122: Option[] recurrenceOptions = new Option[REPEAT_NO];
123:
124: for (int i = 0; i < REPEAT_NO; i++) {
125: recurrenceOptions[i] = new Option(repeats[i].getRString(),
126: bundle.getString(repeats[i].getDisplayKey()));
127: }
128:
129: return recurrenceOptions;
130: }
131:
132: /**
133: * Recurrence options.
134: */
135:
136: public static final CalTaskRepeat REPEAT_ONE_TIME = new CalTaskRepeat(
137: new String[] { "REPEAT_ONE_TIME" }, "task_repeat_onetime");
138:
139: public static final CalTaskRepeat REPEAT_HOURLY = new CalTaskRepeat(
140: new String[] { "FREQ=HOURLY" }, "task_repeat_hourly");
141:
142: public static final CalTaskRepeat REPEAT_DAILY = new CalTaskRepeat(
143: new String[] { "FREQ=DAILY" }, "task_repeat_daily");
144:
145: public static final CalTaskRepeat REPEAT_WEEKLY = new CalTaskRepeat(
146: new String[] { "FREQ=WEEKLY" }, "task_repeat_weekly");
147:
148: public static final CalTaskRepeat REPEAT_MONTHLY = new CalTaskRepeat(
149: new String[] { "FREQ=MONTHLY" }, "task_repeat_monthly");
150:
151: public static final CalTaskRepeat REPEAT_MON_WED_FRI = new CalTaskRepeat(
152: new String[] { "FREQ=WEEKLY", "BYDAY=MO,WE,FR" },
153: "task_repeat_monwedfri");
154:
155: public static final CalTaskRepeat REPEAT_TUE_THU = new CalTaskRepeat(
156: new String[] { "FREQ=WEEKLY", "BYDAY=TU,TH" },
157: "task_repeat_tuethu");
158:
159: public static final CalTaskRepeat REPEAT_SAT_SUN = new CalTaskRepeat(
160: new String[] { "FREQ=WEEKLY", "BYDAY=SA,SU" },
161: "task_repeat_satsun");
162:
163: public static final CalTaskRepeat REPEAT_MON_TUE_WED_THU_FRI = new CalTaskRepeat(
164: new String[] { "FREQ=WEEKLY", "BYDAY=MO,TU,WE,TH,FR" },
165: "task_repeat_montuewedthufri");
166:
167: }
|