001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.model;
042:
043: import java.io.Serializable;
044: import java.util.ArrayList;
045: import java.util.Date;
046: import java.util.Calendar;
047: import java.util.Iterator;
048: import com.sun.rave.web.ui.model.scheduler.RepeatInterval;
049: import com.sun.rave.web.ui.model.scheduler.RepeatUnit;
050:
051: /**
052: *
053: * @author avk
054: */
055: public class ScheduledEvent implements Serializable {
056:
057: /**
058: * The start time, as a java.util.Date
059: */
060: private Date startTime = null;
061:
062: /**
063: * The end time, as a java.util.Date
064: */
065: private Date endTime = null;
066:
067: private ArrayList dateList = null;
068:
069: /**
070: * Whether the event is repeating or not
071: */
072: private boolean repeatingEvent = false;
073:
074: private final static boolean DEBUG = false;
075:
076: /**
077: * Retrieves the start time, as a java.util.Date
078: * @return The start time, as a java.util.Date
079: */
080: public Date getStartTime() {
081:
082: return this .startTime;
083: }
084:
085: /**
086: * Sets the start time
087: * @param startTime The start time, as a java.util.Date
088: */
089: public void setStartTime(Date startTime) {
090: dateList = null;
091: this .startTime = startTime;
092: }
093:
094: /**
095: * The end time, as a java.util.Date
096: * @return The end time, as a java.util.Date
097: */
098: public Date getEndTime() {
099:
100: return this .endTime;
101: }
102:
103: /**
104: * Setter for The end time, as a java.util.Date
105: * @param endTimeThe end time, as a java.util.Date
106: */
107: public void setEndTime(Date endTime) {
108:
109: this .endTime = endTime;
110: dateList = null;
111: }
112:
113: public String toString() {
114: StringBuffer buffer = new StringBuffer(128);
115: buffer.append(this .getClass().getName());
116: buffer.append(": Start time: ");
117: buffer.append(startTime.toString());
118: if (endTime != null) {
119: buffer.append("\tEnd time: ");
120: buffer.append(endTime.toString());
121: buffer.append(" ");
122: } else {
123: buffer.append("\tNo end time. ");
124: }
125: if (isRepeatingEvent()) {
126: buffer.append("\tThis is a repeating event. ");
127: buffer.append("\t Repeat frequency (Calendar.field): ");
128: buffer.append(String.valueOf(frequency));
129: if (duration != null) {
130: buffer.append("\tLimited duration of repeats.");
131: buffer.append("\tDuration is ");
132: buffer.append(String.valueOf(duration));
133: buffer.append(" of unit (in Calendar.field) ");
134: buffer.append(String.valueOf(durationUnit));
135: }
136: } else {
137: buffer.append("\tThis is not a repeating event. ");
138: }
139: return buffer.toString();
140: }
141:
142: /**
143: * If true, indicates that this is a repeating event
144: * @return true it this is a repeating event, false otherwise
145: */
146: public boolean isRepeatingEvent() {
147:
148: return this .repeatingEvent;
149: }
150:
151: /**
152: * Invoke this method with the value true to indicate that the event
153: * is repeating, false if it is not repeating
154: * @param repeatingEvent whether the event is repeating
155: */
156: public void setRepeatingEvent(boolean repeatingEvent) {
157:
158: this .repeatingEvent = repeatingEvent;
159: }
160:
161: /**
162: * Holds value of property frequency.
163: */
164: private RepeatInterval frequency = null;
165:
166: /**
167: * <p>Get the repeat frequency. The value must be the Integer value
168: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
169: * <code>java.util.Calendar</code> for details. </p>
170: * <p>To specify that the event repeats weekely... </p>
171: * @return Value of property frequency.
172: */
173: public RepeatInterval getRepeatInterval() {
174:
175: return this .frequency;
176: }
177:
178: /**
179: * <p>Setter for the repeat frequency. The new value must be the
180: * Integer value
181: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
182: * <code>java.util.Calendar</code> for details. </p>
183: * <p>To specify that the event repeats weekly... </p>
184: * @param frequency New value of property frequency.
185: */
186: public void setRepeatInterval(RepeatInterval frequency) {
187:
188: this .frequency = frequency;
189: dateList = null;
190: }
191:
192: /**
193: * * <p>The repeat frequency. The value must be the Integer value
194: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
195: * <code>java.util.Calendar</code> for details. </p>
196: * <p>To specify that the event repeats for three months... </p>
197: */
198: private RepeatUnit durationUnit = null;
199:
200: /**
201: * <p>Get the unit (hours, weeks, days, etc) for the duration interval
202: * of the event. The value must be the Integer value
203: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
204: * <code>java.util.Calendar</code> for details. </p>
205: * <p>To specify that the event repeats for three months... </p>
206: * @return Value of property durationUnit.
207: */
208: public RepeatUnit getDurationUnit() {
209:
210: return this .durationUnit;
211: }
212:
213: /**
214: * <p>Set the unit (hours, weeks, days, etc) for the duration interval
215: * of the event. The value must be the Integer value
216: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
217: * <code>java.util.Calendar</code> for details. </p>
218: *
219: * <p>To specify that the event repeats for three months... </p>
220: * @param durationUnit New value of property durationUnit.
221: */
222: public void setDurationUnit(RepeatUnit durationUnit) {
223:
224: this .durationUnit = durationUnit;
225: dateList = null;
226: }
227:
228: /**
229: * Holds value of property duration.
230: */
231: private Integer duration = null;
232:
233: /**
234: * <p>Get the number of units (see DurationUnit) for the duration interval
235: * of the event. The value must be the Integer value
236: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
237: * <code>java.util.Calendar</code> for details. </p>
238: * <p>To specify that the event repeats for three months... </p>
239: * @return Value of property duration.
240: */
241: public Integer getDuration() {
242:
243: return this .duration;
244: }
245:
246: /**
247: * <p>Set the number of units (see DurationUnit) for the duration interval
248: * of the event. The value must be the Integer value
249: * of a calendar field identifier (Calendar.HOUR_OF_DAY, etc). See
250: * <code>java.util.Calendar</code> for details. </p>
251: * <p>To specify that the event repeats weekely... </p>
252: * @param duration New value of property duration.
253: */
254: public void setDuration(Integer duration) {
255:
256: this .duration = duration;
257: dateList = null;
258: }
259:
260: public boolean equals(Object object) {
261: if (object == null) {
262: return false;
263: }
264: if ((object instanceof ScheduledEvent)) {
265: return false;
266: }
267: ScheduledEvent event = (ScheduledEvent) object;
268: if (event.getStartTime() == getStartTime()
269: && event.getEndTime() == getEndTime()
270: && event.getDuration() == getDuration()
271: && event.getDurationUnit() == getDurationUnit()
272: && event.getRepeatInterval() == event
273: .getRepeatInterval()) {
274: return true;
275: }
276: return false;
277: }
278:
279: /** Returns an iterator of dates which mark the start of scheduled event.
280: * If no time has been set, an empty iterator is returned.
281: * If a time has been set and the event is not repeating, an iterator
282: * with a single date corresponding to the start time is returned
283: * provided it is before the date specified in untilDate.
284: * If the event is repeating, all start times before untilDate are
285: * returned.
286: * @return a java.util.Iterator whose items are java.util.Calendar
287: */
288: public Iterator getDates(Calendar untilDate) {
289: return getDates(null, untilDate);
290: }
291:
292: public Iterator getDates(Calendar fromDate, Calendar untilDate) {
293:
294: if (dateList != null) {
295: return dateList.iterator();
296: }
297:
298: dateList = new ArrayList();
299: Date date = getStartTime();
300:
301: if (DEBUG) {
302: if (date != null) {
303: log("First event on " + date.toString()); //NOI18N
304: } else {
305: log("No events scheduled"); //NOI18N
306: }
307: }
308:
309: Date from = null;
310: if (fromDate != null) {
311: if (DEBUG)
312: log("Start date is " + fromDate.getTime().toString());//NOI18N
313: from = fromDate.getTime();
314: } else if (DEBUG) {
315: log("No start date");
316: }
317:
318: if (DEBUG)
319: log("End date is " + untilDate.getTime().toString());
320:
321: if (date != null && date.before(untilDate.getTime())) {
322:
323: Calendar startDate = (Calendar) (untilDate.clone());
324: startDate.setTime(date);
325:
326: dateList.add(startDate.clone());
327: if (DEBUG)
328: log("Added date " + date.toString());
329:
330: if (isRepeatingEvent()) {
331:
332: int interval = getRepeatInterval().getCalendarField()
333: .intValue();
334: if (interval > -1) {
335:
336: if (DEBUG)
337: log("Repeating event");
338:
339: Calendar endCalendar = (Calendar) (untilDate
340: .clone());
341:
342: Integer duration = getDuration();
343: RepeatUnit repeatUnit = getDurationUnit();
344: Integer durationUnit = null;
345: if (repeatUnit != null) {
346: durationUnit = repeatUnit.getCalendarField();
347: }
348: if (duration != null && durationUnit != null) {
349: int durationValue = duration.intValue();
350: durationValue--;
351:
352: int durationField = durationUnit.intValue();
353: if (durationValue > 0) {
354: endCalendar = (Calendar) (startDate.clone());
355: endCalendar.add(durationField,
356: durationValue);
357: endCalendar.getTime();
358: }
359: }
360:
361: Date end = endCalendar.getTime();
362: if (DEBUG)
363: log("Using end date " + end.toString());
364:
365: Date current = startDate.getTime();
366:
367: while (current.before(end)) {
368:
369: startDate.add(interval, 1);
370: current = startDate.getTime();
371: if (from != null) {
372: if (current.after(from)) {
373: dateList.add(startDate.clone());
374: if (DEBUG)
375: log("Added date "
376: + current.toString());
377: }
378: } else {
379: dateList.add(startDate.clone());
380: if (DEBUG)
381: log("Added date " + current.toString());
382: }
383: }
384: }
385: }
386: }
387: return dateList.iterator();
388: }
389:
390: private void log(String s) {
391: System.out.println(this .getClass().getName() + "::" + s);
392: }
393:
394: }
|