1 25 package org.ofbiz.service.calendar; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.List ; 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 48 public class RecurrenceInfo { 49 50 public static final String module = RecurrenceInfo.class.getName(); 51 52 protected GenericValue info; 53 protected Date startDate; 54 protected List rRulesList; 55 protected List eRulesList; 56 protected List rDateList; 57 protected List eDateList;; 58 59 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 68 public void init() throws RecurrenceInfoException { 69 70 if (info.get("startDateTime") == null) 71 throw new RecurrenceInfoException("Recurrence startDateTime cannot be null."); 72 73 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 (startTime); 84 85 try { 87 Collection c = info.getRelated("RecurrenceRule"); 88 Iterator i = c.iterator(); 89 90 rRulesList = new ArrayList (); 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 try { 102 Collection c = info.getRelated("ExceptionRecurrenceRule"); 103 Iterator i = c.iterator(); 104 105 eRulesList = new ArrayList (); 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 rDateList = RecurrenceUtil.parseDateList(StringUtil.split(info.getString("recurrenceDateTimes"), ",")); 117 eDateList = RecurrenceUtil.parseDateList(StringUtil.split(info.getString("exceptionDateTimes"), ",")); 119 120 Collections.sort(rDateList); 122 Collections.sort(eDateList); 123 } 124 125 126 public String getID() { 127 return info.getString("recurrenceInfoId"); 128 } 129 130 131 public Date getStartDate() { 132 return this.startDate; 133 } 134 135 136 public long getStartTime() { 137 return this.startDate.getTime(); 138 } 139 140 141 public Iterator getRecurrenceRuleIterator() { 142 return rRulesList.iterator(); 143 } 144 145 146 public Iterator getRecurrenceDateIterator() { 147 return rDateList.iterator(); 148 } 149 150 151 public Iterator getExceptionRuleIterator() { 152 return eRulesList.iterator(); 153 } 154 155 156 public Iterator getExceptionDateIterator() { 157 return eDateList.iterator(); 158 } 159 160 161 public long getCurrentCount() { 162 if (info.get("recurrenceCount") != null) 163 return info.getLong("recurrenceCount").longValue(); 164 return 0; 165 } 166 167 168 public void incrementCurrentCount() throws GenericEntityException { 169 incrementCurrentCount(true); 170 } 171 172 173 public void incrementCurrentCount(boolean store) throws GenericEntityException { 174 Long count = new Long (getCurrentCount() + 1); 175 176 if (store) { 177 info.set("recurrenceCount", count); 178 info.store(); 179 } 180 } 181 182 183 public void remove() throws RecurrenceInfoException { 184 List rulesList = new ArrayList (); 185 186 rulesList.addAll(rRulesList); 187 rulesList.addAll(eRulesList); 188 Iterator 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 202 public long first() { 203 return startDate.getTime(); 204 } 206 207 208 public long last() { 209 return 0; 211 } 212 213 214 public long next() { 215 return next(RecurrenceUtil.now()); 216 } 217 218 219 public long next(long fromTime) { 220 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 if (rDateList == null && rRulesList == null) { 232 return 0; 233 } 234 235 long nextRuleTime = fromTime; 236 boolean hasNext = true; 237 238 Iterator rulesIterator = getRecurrenceRuleIterator(); 240 while (rulesIterator.hasNext()) { 241 RecurrenceRule rule = (RecurrenceRule) rulesIterator.next(); 242 while (hasNext) { 243 nextRuleTime = getNextTime(rule, nextRuleTime); 245 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 dateList, long time, long fromTime) { 261 long nextTime = time; 262 263 if (dateList != null && dateList.size() > 0) { 264 Iterator dateIterator = dateList.iterator(); 265 266 while (dateIterator.hasNext()) { 267 Date thisDate = (Date ) 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 exceptRulesIterator = getExceptionRuleIterator(); 280 281 while (exceptRulesIterator.hasNext()) { 282 RecurrenceRule except = (RecurrenceRule) exceptRulesIterator.next(); 283 284 if (except.isValid(getStartTime(), time) || eDateList.contains(new Date (time))) 285 return false; 286 } 287 return true; 288 } 289 290 public String 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 ruleId = r.primaryKey(); 309 String 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 (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 |