KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > calendar > RecurrenceInfo


1 /*
2  * $Id: RecurrenceInfo.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.calendar;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.StringUtil;
36 import org.ofbiz.base.util.UtilMisc;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.ofbiz.entity.GenericValue;
40
41 /**
42  * Recurrence Info Object
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class RecurrenceInfo {
49     
50     public static final String JavaDoc module = RecurrenceInfo.class.getName();
51
52     protected GenericValue info;
53     protected Date JavaDoc startDate;
54     protected List JavaDoc rRulesList;
55     protected List JavaDoc eRulesList;
56     protected List JavaDoc rDateList;
57     protected List JavaDoc eDateList;;
58
59     /** Creates new RecurrenceInfo */
60     public RecurrenceInfo(GenericValue info) throws RecurrenceInfoException {
61         this.info = info;
62         if (!info.getEntityName().equals("RecurrenceInfo"))
63             throw new RecurrenceInfoException("Invalid RecurrenceInfo Value object.");
64         init();
65     }
66
67     /** Initializes the rules for this RecurrenceInfo object. */
68     public void init() throws RecurrenceInfoException {
69
70         if (info.get("startDateTime") == null)
71             throw new RecurrenceInfoException("Recurrence startDateTime cannot be null.");
72
73         // Get start date
74
long startTime = info.getTimestamp("startDateTime").getTime();
75
76         if (startTime > 0) {
77             int nanos = info.getTimestamp("startDateTime").getNanos();
78
79             startTime += (nanos / 1000000);
80         } else {
81             throw new RecurrenceInfoException("Recurrence startDateTime must have a value.");
82         }
83         startDate = new Date JavaDoc(startTime);
84
85         // Get the recurrence rules objects
86
try {
87             Collection JavaDoc c = info.getRelated("RecurrenceRule");
88             Iterator JavaDoc i = c.iterator();
89
90             rRulesList = new ArrayList JavaDoc();
91             while (i.hasNext()) {
92                 rRulesList.add(new RecurrenceRule((GenericValue) i.next()));
93             }
94         } catch (GenericEntityException gee) {
95             rRulesList = null;
96         } catch (RecurrenceRuleException rre) {
97             throw new RecurrenceInfoException("Illegal rule format.", rre);
98         }
99
100         // Get the exception rules objects
101
try {
102             Collection JavaDoc c = info.getRelated("ExceptionRecurrenceRule");
103             Iterator JavaDoc i = c.iterator();
104
105             eRulesList = new ArrayList JavaDoc();
106             while (i.hasNext()) {
107                 eRulesList.add(new RecurrenceRule((GenericValue) i.next()));
108             }
109         } catch (GenericEntityException gee) {
110             eRulesList = null;
111         } catch (RecurrenceRuleException rre) {
112             throw new RecurrenceInfoException("Illegal rule format", rre);
113         }
114
115         // Get the recurrence date list
116
rDateList = RecurrenceUtil.parseDateList(StringUtil.split(info.getString("recurrenceDateTimes"), ","));
117         // Get the exception date list
118
eDateList = RecurrenceUtil.parseDateList(StringUtil.split(info.getString("exceptionDateTimes"), ","));
119
120         // Sort the lists.
121
Collections.sort(rDateList);
122         Collections.sort(eDateList);
123     }
124
125     /** Returns the primary key for this value object */
126     public String JavaDoc getID() {
127         return info.getString("recurrenceInfoId");
128     }
129
130     /** Returns the startDate Date object. */
131     public Date JavaDoc getStartDate() {
132         return this.startDate;
133     }
134
135     /** Returns the long value of the startDate. */
136     public long getStartTime() {
137         return this.startDate.getTime();
138     }
139
140     /** Returns a recurrence rule iterator */
141     public Iterator JavaDoc getRecurrenceRuleIterator() {
142         return rRulesList.iterator();
143     }
144
145     /** Returns a sorted recurrence date iterator */
146     public Iterator JavaDoc getRecurrenceDateIterator() {
147         return rDateList.iterator();
148     }
149
150     /** Returns a exception recurrence iterator */
151     public Iterator JavaDoc getExceptionRuleIterator() {
152         return eRulesList.iterator();
153     }
154
155     /** Returns a sorted exception date iterator */
156     public Iterator JavaDoc getExceptionDateIterator() {
157         return eDateList.iterator();
158     }
159
160     /** Returns the current count of this recurrence. */
161     public long getCurrentCount() {
162         if (info.get("recurrenceCount") != null)
163             return info.getLong("recurrenceCount").longValue();
164         return 0;
165     }
166
167     /** Increments the current count of this recurrence and updates the record. */
168     public void incrementCurrentCount() throws GenericEntityException {
169         incrementCurrentCount(true);
170     }
171
172     /** Increments the current count of this recurrence. */
173     public void incrementCurrentCount(boolean store) throws GenericEntityException {
174         Long JavaDoc count = new Long JavaDoc(getCurrentCount() + 1);
175
176         if (store) {
177             info.set("recurrenceCount", count);
178             info.store();
179         }
180     }
181
182     /** Removes the recurrence from persistant store. */
183     public void remove() throws RecurrenceInfoException {
184         List JavaDoc rulesList = new ArrayList JavaDoc();
185
186         rulesList.addAll(rRulesList);
187         rulesList.addAll(eRulesList);
188         Iterator JavaDoc i = rulesList.iterator();
189
190         try {
191             while (i.hasNext())
192                 ((RecurrenceRule) i.next()).remove();
193             info.remove();
194         } catch (RecurrenceRuleException rre) {
195             throw new RecurrenceInfoException(rre.getMessage(), rre);
196         } catch (GenericEntityException gee) {
197             throw new RecurrenceInfoException(gee.getMessage(), gee);
198         }
199     }
200
201     /** Returns the first recurrence. */
202     public long first() {
203         return startDate.getTime();
204         // First recurrence is always the start time
205
}
206
207     /** Returns the estimated last recurrence. */
208     public long last() {
209         // TODO: find the last recurrence.
210
return 0;
211     }
212
213     /** Returns the next recurrence from now. */
214     public long next() {
215         return next(RecurrenceUtil.now());
216     }
217
218     /** Returns the next recurrence from the specified time. */
219     public long next(long fromTime) {
220         // Check for the first recurrence (StartTime is always the first recurrence)
221
if (getCurrentCount() == 0 || fromTime == 0 || fromTime == startDate.getTime()) {
222             return first();
223         }
224             
225         if (Debug.verboseOn()) {
226             Debug.logVerbose("Date List Size: " + (rDateList == null ? 0 : rDateList.size()), module);
227             Debug.logVerbose("Rule List Size: " + (rRulesList == null ? 0 : rRulesList.size()), module);
228         }
229
230         // Check the rules and date list
231
if (rDateList == null && rRulesList == null) {
232             return 0;
233         }
234
235         long nextRuleTime = fromTime;
236         boolean hasNext = true;
237
238         // Get the next recurrence from the rule(s).
239
Iterator JavaDoc rulesIterator = getRecurrenceRuleIterator();
240         while (rulesIterator.hasNext()) {
241             RecurrenceRule rule = (RecurrenceRule) rulesIterator.next();
242             while (hasNext) {
243                 // Gets the next recurrence time from the rule.
244
nextRuleTime = getNextTime(rule, nextRuleTime);
245                 // Tests the next recurrence against the rules.
246
if (nextRuleTime == 0 || isValid(nextRuleTime)) {
247                     hasNext = false;
248                 }
249             }
250         }
251         return nextRuleTime;
252     }
253
254     private long getNextTime(RecurrenceRule rule, long fromTime) {
255         long nextTime = rule.next(getStartTime(), fromTime, getCurrentCount());
256         if (Debug.verboseOn()) Debug.logVerbose("Next Time Before Date Check: " + nextTime, module);
257         return checkDateList(rDateList, nextTime, fromTime);
258     }
259
260     private long checkDateList(List JavaDoc dateList, long time, long fromTime) {
261         long nextTime = time;
262
263         if (dateList != null && dateList.size() > 0) {
264             Iterator JavaDoc dateIterator = dateList.iterator();
265
266             while (dateIterator.hasNext()) {
267                 Date JavaDoc thisDate = (Date JavaDoc) dateIterator.next();
268
269                 if (nextTime > 0 && thisDate.getTime() < nextTime && thisDate.getTime() > fromTime)
270                     nextTime = thisDate.getTime();
271                 else if (nextTime == 0 && thisDate.getTime() > fromTime)
272                     nextTime = thisDate.getTime();
273             }
274         }
275         return nextTime;
276     }
277
278     private boolean isValid(long time) {
279         Iterator JavaDoc exceptRulesIterator = getExceptionRuleIterator();
280
281         while (exceptRulesIterator.hasNext()) {
282             RecurrenceRule except = (RecurrenceRule) exceptRulesIterator.next();
283
284             if (except.isValid(getStartTime(), time) || eDateList.contains(new Date JavaDoc(time)))
285                 return false;
286         }
287         return true;
288     }
289
290     public String JavaDoc primaryKey() {
291         return info.getString("recurrenceInfoId");
292     }
293     
294     public static RecurrenceInfo makeInfo(GenericDelegator delegator, long startTime, int frequency,
295             int interval, int count) throws RecurrenceInfoException {
296         return makeInfo(delegator, startTime, frequency, interval, count, 0);
297     }
298
299     public static RecurrenceInfo makeInfo(GenericDelegator delegator, long startTime, int frequency,
300             int interval, long endTime) throws RecurrenceInfoException {
301         return makeInfo(delegator, startTime, frequency, interval, -1, endTime);
302     }
303
304     public static RecurrenceInfo makeInfo(GenericDelegator delegator, long startTime, int frequency,
305             int interval, int count, long endTime) throws RecurrenceInfoException {
306         try {
307             RecurrenceRule r = RecurrenceRule.makeRule(delegator, frequency, interval, count, endTime);
308             String JavaDoc ruleId = r.primaryKey();
309             String JavaDoc infoId = delegator.getNextSeqId("RecurrenceInfo").toString();
310             GenericValue value = delegator.makeValue("RecurrenceInfo", UtilMisc.toMap("recurrenceInfoId", infoId));
311
312             value.set("recurrenceRuleId", ruleId);
313             value.set("startDateTime", new java.sql.Timestamp JavaDoc(startTime));
314             delegator.create(value);
315             RecurrenceInfo newInfo = new RecurrenceInfo(value);
316
317             return newInfo;
318         } catch (RecurrenceRuleException re) {
319             throw new RecurrenceInfoException(re.getMessage(), re);
320         } catch (GenericEntityException ee) {
321             throw new RecurrenceInfoException(ee.getMessage(), ee);
322         } catch (RecurrenceInfoException rie) {
323             throw rie;
324         }
325     }
326 }
327
Popular Tags