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.jsp.*;
007: import javax.servlet.jsp.tagext.*;
008:
009: import com.sun.comclient.calendar.RecurrencePattern;
010: import com.sun.comclient.calendar.DateTime;
011: import com.sun.portal.wireless.taglibs.base.BeanTag;
012: import com.sun.portal.wireless.taglibs.base.Util;
013: import com.sun.portal.wireless.taglibs.util.Catalog;
014: import com.sun.portal.log.common.PortalLogger;
015:
016: import java.text.SimpleDateFormat;
017: import java.util.Date;
018: import java.util.Calendar;
019: import java.util.TimeZone;
020: import java.util.logging.Logger;
021: import java.util.logging.Level;
022:
023: /**
024: * RecurrenceTag - tag that represents an JCAPI Recurrence bean
025: *
026: * @version 1.0
027: * @see com.sun.portal.calendar.RecurrencePattern
028: */
029:
030: public class RecurrenceTag extends BeanTag {
031:
032: /**
033: * Find the bean
034: *
035: * BeanTag.findBean() searches for a collection if no
036: * "name" is specified. There is no such thing as a Recurrence
037: * collection, so we must override this method, and NOT search
038: * for a collection.
039: *
040: * @return the found bean
041: */
042:
043: private Object frequency = new Integer(RecurrencePattern.WEEKLY);
044: private int interval = 1;
045: private String repeatOn = "";
046: private Object repeatUntil = new Integer(-1); // -1 indicates ForEver
047:
048: private static Catalog resBundle;
049: private static String PARSE_DATE_FORMAT = "parsedatefmt";
050: private static String DEFAULT_DATE_FORMAT = "MMddyyyy";
051: private String catalog;
052:
053: // Create a logger for this class
054: private static Logger debugLogger = PortalLogger
055: .getLogger(RecurrenceTag.class);
056:
057: /*
058: * The default constructor
059: */
060: public RecurrenceTag() {
061: super ();
062: }
063:
064: /*
065: * Set the Catalog.
066: */
067: public void setCatalog(String catalog) {
068: this .catalog = catalog;
069: resBundle = (Catalog) pageContext.getAttribute(catalog);
070: }
071:
072: /**
073: * Gets the Bean if name is specfied else creates one
074: * @return the JCAPI RecurrencePattern object
075: */
076: public Object findBean() throws Exception {
077: Object result;
078:
079: if (name != null) {
080: return pageContext.getAttribute(name);
081: }
082: result = newBean();
083: if (id != null) {
084: pageContext.setAttribute(id, result);
085: }
086: return result;
087: }
088:
089: /**
090: * Create a new Recurrence bean object
091: * valid values are
092: * frequency, interval , repeatUntil, repatOn
093: * Integer , int , Integer , String
094: * String , int , Integer , String
095: * Integer , int , DateTime , String
096: * @return the Recurrence object
097: */
098: public Object newBean() throws Exception {
099: RecurrencePattern result = null;
100:
101: if (repeatUntil instanceof Integer) {
102: if (frequency instanceof Integer) {
103: result = new RecurrencePattern(((Integer) frequency)
104: .intValue(), interval, ((Integer) repeatUntil)
105: .intValue());
106: } else if (frequency instanceof String) {
107: result = new RecurrencePattern((String) frequency,
108: interval, ((Integer) repeatUntil).intValue());
109: }
110: } else if ((repeatUntil instanceof DateTime)
111: && (frequency instanceof Integer)) {
112: result = new RecurrencePattern(((Integer) frequency)
113: .intValue(), interval, (DateTime) repeatUntil);
114: }
115: if (result == null) {
116: throw new Exception(
117: "Not able to create a RecurrencePattern");
118: }
119: setRecRepeatOn(result, frequency, repeatOn);
120:
121: return result;
122: }
123:
124: /**
125: * Set the frequency into the context Recurrence list
126: *
127: * @param frequency frequency of Recurrence to use
128: */
129: public void setFrequency(String freq) throws Exception {
130: try {
131: this .frequency = getRecFrequency(freq);
132: } catch (Exception ex) {
133: debugLogger.log(Level.INFO, "PSMA_CSPWTC0001", ex);
134: throw ex;
135: }
136: }
137:
138: /**
139: * Set the interval
140: *
141: * @param interval interval of Recurrence to use
142: */
143: public void setInterval(String theInterval) throws Exception {
144: try {
145: if (theInterval != null) {
146: interval = Integer.parseInt(theInterval);
147: }
148: } catch (Exception ex) {
149: debugLogger.log(Level.FINE, "PSMA_CSPWTC0010",
150: new Object[] { theInterval, ex.getMessage() });
151: }
152: }
153:
154: /**
155: * Set the repeaton
156: *
157: * @param repeatOn repeatOn of Recurrence to use
158: */
159: public void setRepeatOn(String repeatOn) throws Exception {
160: if (repeatOn != null) {
161: this .repeatOn = repeatOn;
162: }
163: //setRecRepeatOn(bean,frequencey,repeatOn);
164: }
165:
166: /**
167: * Set the repeatUntil
168: *
169: * @param repeatUntil repeatUntil of Recurrence to use
170: */
171: public void setRepeatUntil(String until) throws Exception {
172: if (until != null) {
173: CalContext context = CalContext.getContext(pageContext);
174: TimeZone tz = context.getTimeZone();
175: try {
176: this .repeatUntil = getRecRepeatUntil(until, tz);
177: pageContext.setAttribute("validRepeatUntil", "true");
178: } catch (Exception e1) {
179: // Failed to construct repeatUntil information
180: // This could be used by JSPs to show appropriate info
181: pageContext.setAttribute("validRepeatUntil", "false");
182: }
183: }
184: }
185:
186: private static Object getRecRepeatUntil(String until, TimeZone tz)
187: throws Exception {
188: Object result = null;
189: if (until.length() >= 6) {
190: try {
191: String parseDateFmt = resBundle.getMsg(
192: PARSE_DATE_FORMAT, DEFAULT_DATE_FORMAT);
193: SimpleDateFormat dateFormat = new SimpleDateFormat(
194: parseDateFmt);
195: dateFormat.setTimeZone(tz);
196: Date date = dateFormat.parse(until);
197: DateTime tempResult = new DateTime(tz);
198: tempResult.setTime(date);
199: tempResult.set(Calendar.HOUR, 23);
200: tempResult.set(Calendar.MINUTE, 59);
201: tempResult.set(Calendar.SECOND, 59);
202: result = tempResult;
203: } catch (NumberFormatException ex) {
204: throw new Exception("Unable to parse " + until);
205: }
206: } else // if ( repeatOn.length() < 4 ) it is too long
207: {
208: try {
209: result = new Integer(until);
210: } catch (NumberFormatException ex) {
211: throw new Exception("Not able to parse " + until);
212: }
213: }
214: return result;
215: }
216:
217: private static Object getRecFrequency(String param)
218: throws Exception {
219: Object result = null;
220: try {
221: result = new Integer(param);
222:
223: } catch (NumberFormatException ex) {
224: if (param.equals("DAILY")) {
225: result = new Integer(RecurrencePattern.DAILY);
226: } else if (param.equals("WEEKLY")) {
227: result = new Integer(RecurrencePattern.WEEKLY);
228: } else if (param.equals("MONTHLY")) {
229: result = new Integer(RecurrencePattern.MONTHLY);
230: } else if (param.equals("YEARLY")) {
231: result = new Integer(RecurrencePattern.YEARLY);
232: }
233: }
234: if (result == null) {
235: throw new Exception("Incorrect frequency=" + param);
236: }
237: return result;
238: }
239:
240: private static void setRecRepeatOn(RecurrencePattern recPat,
241: Object freq, String repOn) throws Exception {
242: if ((repOn == null) || (repOn.trim().equals(""))) {
243: return;
244: }
245:
246: repOn = repOn.replace(';', ',');
247:
248: if (freq == null) {
249: throw new Exception("frequency is Null in setRepeatOn");
250: }
251: String freqSt = freq.toString();
252: //In GUI the Daily+repeatOn is actually Weekly+repeatOn
253: //Hence not doing for Daily
254:
255: if (freqSt.equals("DAILY")
256: || freqSt.equals(String
257: .valueOf(RecurrencePattern.DAILY))
258: || freqSt.equals("WEEKLY")
259: || freqSt.equals(String
260: .valueOf(RecurrencePattern.WEEKLY))) {
261: if (!repOn.trim().equals("0")) {
262: recPat.setByDay(repOn);
263: }
264: } else if (freqSt.equals("MONTHLY")
265: || freqSt.equals(String
266: .valueOf(RecurrencePattern.MONTHLY))) {
267: recPat.setByMonthDay(repOn);
268: } else if (freqSt.equals("YEARLY")
269: || freqSt.equals(String
270: .valueOf(RecurrencePattern.YEARLY))) {
271: //only dayOfYear for now in mmdd format
272: //TBD: WeekdayOfYear
273:
274: if ((repOn.length() >= 2) && (repOn.length() <= 4)) {
275: try {
276: String dd = repOn.substring(repOn.length() - 2);
277: String mm = repOn.substring(0, repOn.length() - 2);
278: Integer.parseInt(dd);
279: Integer.parseInt(mm);
280: recPat.setByMonthDay(dd);
281: recPat.setByMonth(mm);
282: } catch (Exception ex) {
283: throw new Exception(" Date format error of mmdd "
284: + repOn);
285: }
286: } else
287: throw new Exception(" Date format size error of mmdd "
288: + repOn);
289: //otherwise do nt how to handle it care about it
290: }
291: }
292:
293: }
|