001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.kvem.midp.pim;
028:
029: import java.util.Date;
030: import java.util.Enumeration;
031: import java.util.Vector;
032: import javax.microedition.pim.Event;
033: import javax.microedition.pim.EventList;
034: import javax.microedition.pim.PIM;
035: import javax.microedition.pim.PIMException;
036: import javax.microedition.pim.RepeatRule;
037:
038: /**
039: * Class EventListImpl implements methods of PIM interface EventList.
040: *
041: */
042: class EventListImpl extends AbstractPIMList implements EventList {
043: /**
044: * Constructs an Event list.
045: * @param name label for the Event List
046: * @param mode read or write access
047: * @param handle handle of the list
048: */
049: EventListImpl(String name, int mode, Object handle) {
050: super (PIM.EVENT_LIST, name, mode, handle);
051: }
052:
053: /**
054: * Creates a new event instance.
055: * @return Event handler
056: */
057: public Event createEvent() {
058: return new EventImpl(this );
059: }
060:
061: /**
062: * Gets supported repeat rule fields.
063: * @param frequency repeat rule frequency
064: * @return array of repeat rule fields
065: */
066: public int[] getSupportedRepeatRuleFields(int frequency) {
067: switch (frequency) {
068: case RepeatRule.DAILY:
069: return new int[] { RepeatRule.COUNT, RepeatRule.INTERVAL,
070: RepeatRule.END };
071: case RepeatRule.WEEKLY:
072: return new int[] { RepeatRule.COUNT, RepeatRule.INTERVAL,
073: RepeatRule.END, RepeatRule.DAY_IN_WEEK };
074: case RepeatRule.MONTHLY:
075: return new int[] { RepeatRule.COUNT, RepeatRule.INTERVAL,
076: RepeatRule.END, RepeatRule.DAY_IN_WEEK,
077: RepeatRule.WEEK_IN_MONTH, RepeatRule.DAY_IN_MONTH };
078: case RepeatRule.YEARLY:
079: return new int[] { RepeatRule.COUNT, RepeatRule.INTERVAL,
080: RepeatRule.END, RepeatRule.DAY_IN_WEEK,
081: RepeatRule.WEEK_IN_MONTH, RepeatRule.DAY_IN_MONTH,
082: RepeatRule.MONTH_IN_YEAR, RepeatRule.DAY_IN_YEAR };
083: default:
084: throw new IllegalArgumentException(
085: "Unsupported frequency: " + frequency);
086: }
087: }
088:
089: /**
090: * Creates new event from item template.
091: * @param item template with initial data
092: * @return Event implementation handler
093: */
094: public Event importEvent(Event item) {
095: return new EventImpl(this , item);
096: }
097:
098: /**
099: * Gets enumeration of Event items.
100: * @param searchType search rule
101: * @param startDate beginning of range
102: * @param endDate end of range
103: * @param initialEventOnly first event only
104: * @return Enumeration of matching events
105: */
106: public Enumeration items(int searchType, long startDate,
107: long endDate, boolean initialEventOnly) throws PIMException {
108:
109: switch (searchType) {
110: case ENDING:
111: case OCCURRING:
112: case STARTING:
113: break;
114: default:
115: throw new IllegalArgumentException("Invalid search type: "
116: + searchType);
117: }
118: if (startDate > endDate) {
119: throw new IllegalArgumentException(
120: "Start date must be earlier than end date");
121: }
122: Vector selectedItems = new Vector();
123: Vector itemKeys = new Vector();
124: for (Enumeration e = items(); e.hasMoreElements();) {
125: Event event = (Event) e.nextElement();
126: long eventStart = 0l;
127: long eventEnd = 0l;
128: // a START or END field may have at most one value
129: if (event.countValues(Event.START) != 0) {
130: eventStart = event.getDate(Event.START, 0);
131: if (event.countValues(Event.END) != 0) {
132: eventEnd = event.getDate(Event.END, 0);
133: } else {
134: // see specification of Event.END field: if
135: // END is not specified but START is, the event only
136: // occurs at the START time.
137: eventEnd = eventStart;
138: }
139: } else if (event.countValues(Event.END) != 0) {
140: // see specification of Event.START field: if
141: // START is not specified but END is, the event only
142: // occurs at the END time.
143: eventEnd = event.getDate(Event.END, 0);
144: eventStart = eventEnd;
145: } else {
146: // no start or end date
147: continue;
148: }
149: long duration = Math.max(0, eventEnd - eventStart);
150: RepeatRule repeatRule = event.getRepeat();
151: boolean includeItem = false;
152: if (repeatRule != null) {
153: // check all occurrences of the event
154: long timeSlot = eventEnd - eventStart;
155: Enumeration dates = repeatRule.dates(eventStart, Math
156: .max(startDate - duration, 0), endDate);
157: while (dates.hasMoreElements()) {
158: Date date = (Date) dates.nextElement();
159: eventStart = date.getTime();
160: eventEnd = eventStart + timeSlot;
161: if (eventStart > endDate) {
162: // no point continuing
163: break;
164: }
165: includeItem = checkRange(searchType, startDate,
166: endDate, eventStart, eventEnd);
167: if (includeItem) {
168: break;
169: }
170: if (initialEventOnly) {
171: break;
172: }
173: }
174: } else {
175: // check the base occurrence
176: includeItem = checkRange(searchType, startDate,
177: endDate, eventStart, eventEnd);
178: }
179: if (includeItem) {
180: KeySortUtility.store(itemKeys, selectedItems,
181: eventStart, event);
182: }
183: }
184: return selectedItems.elements();
185: }
186:
187: /**
188: * Verifies search range.
189: * @param searchType search mode
190: * @param startDate beginning of range
191: * @param endDate end of range
192: * @param eventStart event start date
193: * @param eventEnd event end date
194: * @return <code>true</code> if event is within range
195: */
196: private boolean checkRange(int searchType, long startDate,
197: long endDate, long eventStart, long eventEnd) {
198: switch (searchType) {
199: case EventList.STARTING:
200: return (eventStart >= startDate && eventStart <= endDate);
201: case EventList.ENDING:
202: return (eventEnd >= startDate && eventEnd <= endDate);
203: case EventList.OCCURRING:
204: return (eventStart <= endDate && eventEnd >= startDate);
205: default:
206: return false;
207: }
208: }
209:
210: /**
211: * Removes event.
212: * @param item to remove
213: * @throws PIMException if item not found
214: */
215: public void removeEvent(Event item) throws PIMException {
216: removeItem(item);
217: }
218:
219: }
|