/** * Finds the next occurrence for monthly recurrence. * * @param startDate the start date of the previous calendar item. * @param endDate the end date of the previous calendar item. * @param lastDay if <tt>true</tt> we are interested in last day of the month * @return the next item */ public CalendarItemTimerTask nextMonth(Date startDate, Date endDate, boolean lastDay) { long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime(); Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal = incrementMonths(cal, lastDay, period); Date currentDate = new Date(); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { Calendar cal2 = Calendar.getInstance(); cal2.setTime(currentDate); int years = cal2.get(Calendar.YEAR) - cal.get(Calendar.YEAR); int months = (years * 12) + (cal2.get(Calendar.MONTH) - cal.get(Calendar.MONTH)); int monthsToAdd = months; monthsToAdd -= months % period; cal = incrementMonths(cal, lastDay, monthsToAdd); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { cal = incrementMonths(cal, lastDay, period); } } Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); while (deletedInstances.contains(cal.getTime())) { cal = incrementMonths(cal, lastDay, period); cal2 = incrementMonths(cal2, lastDay, period); } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (dateOutOfRange(endDate)) { return null; } boolean executeNow = false; if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); }
/** * Finds the occurrence of the events in the next months * * @param startDate the start date if the calendar item * @param dayOfWeekInMonth the number of week days occurrences * @return the date of the next occurrence */ private Date getMonthNStartDate(Date startDate, int dayOfWeekInMonth) { Calendar cal = Calendar.getInstance(); cal.setTime(startDate); if (dayOfWeekInMonth == -1) { Date result = null; cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth); for (int day : allowedDaysOfWeek) { cal.set(Calendar.DAY_OF_WEEK, day); if (result == null || result.before(cal.getTime())) result = cal.getTime(); } return result; } else while (dayOfWeekInMonth > 0) { int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if (allowedDaysOfWeek.contains(dayOfWeek)) dayOfWeekInMonth--; if (dayOfWeekInMonth > 0) cal.add(Calendar.DAY_OF_MONTH, 1); } return cal.getTime(); }
/** * Finds the next occurrence for monthly Nth recurrence. * * @param startDate the start date of the previous calendar item. * @param endDate the end date of the previous calendar item. * @return the next item */ public CalendarItemTimerTask nextMonthN(Date startDate, Date endDate) { int dayOfWeekInMonth = (patternSpecific2 == 5 ? -1 : patternSpecific2); long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime(); Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, period); cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth)); Date currentDate = new Date(); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { Calendar cal2 = Calendar.getInstance(); cal2.setTime(currentDate); int years = cal2.get(Calendar.YEAR) - cal.get(Calendar.YEAR); int months = (years * 12) + (cal2.get(Calendar.MONTH) - cal.get(Calendar.MONTH)); int monthsToAdd = months; monthsToAdd -= months % period; cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, monthsToAdd); cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth)); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, monthsToAdd); cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth)); } } Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); while (deletedInstances.contains(cal.getTime())) { cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, period); startDate = null; for (int dayOfWeek : allowedDaysOfWeek) { cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth); if ((cal.after(startDate) && dayOfWeekInMonth == -1) || (cal.before(startDate) && dayOfWeekInMonth != -1) || startDate == null) { startDate = cal.getTime(); cal2.set(Calendar.YEAR, cal.get(Calendar.YEAR)); cal2.set(Calendar.MONTH, cal.get(Calendar.MONTH)); cal2.set(Calendar.DATE, cal.get(Calendar.DATE)); } } } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (dateOutOfRange(endDate)) return null; boolean executeNow = false; if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); }
/** * Calculates and creates the next calendar item. * * @param previousStartDate the start date of the previous occurrence. * @param previousEndDate the end date of the previous occurrence. * @return the new calendar item or null if there are no more calendar items from that recurrent * series. */ public CalendarItemTimerTask next(Date previousStartDate, Date previousEndDate) { if (dateOutOfRange(new Date())) { return null; } Date startDate = previousStartDate; Date endDate = null; boolean executeNow = false; long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime(); switch (patternType) { case Day: { startDate = new Date(startDate.getTime() + period * 60000); endDate = new Date(previousEndDate.getTime() + period * 60000); Date currentDate = new Date(); if (endDate.before(currentDate)) { long offset = currentDate.getTime() - endDate.getTime(); offset -= offset % (period * 60000); if (endDate.getTime() + offset < currentDate.getTime()) { offset += period * 60000; } startDate = new Date(startDate.getTime() + offset); } Calendar cal = Calendar.getInstance(); cal.setTime(startDate); Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); while (deletedInstances.contains(cal.getTime())) { cal.add(Calendar.MINUTE, period); cal2.add(Calendar.MINUTE, period); } if (dateOutOfRange(cal.getTime())) { return null; } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); } case Week: { Calendar cal = Calendar.getInstance(); /** The enum for the firstDow field is the same as Calendar day of week enum + 1 day */ cal.setFirstDayOfWeek(firstDow + 1); cal.setTime(startDate); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); int index = allowedDaysOfWeek.indexOf(dayOfWeek); if (++index < allowedDaysOfWeek.size()) { cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index)); startDate = cal.getTime(); endDate = new Date(startDate.getTime() + duration); } else { cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(0)); cal.add(Calendar.WEEK_OF_YEAR, period); startDate = cal.getTime(); endDate = new Date(startDate.getTime() + duration); } Date currentDate = new Date(); if (endDate.before(currentDate)) { cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(0)); endDate = new Date(cal.getTimeInMillis() + duration); long offset = (currentDate.getTime() - endDate.getTime()); // 1 week = 604800000 is milliseconds offset -= offset % (period * 604800000); if (endDate.getTime() + offset < currentDate.getTime()) { cal.add(Calendar.WEEK_OF_YEAR, (int) (offset / (period * 604800000))); int i = 1; while (((cal.getTimeInMillis() + duration) < (currentDate.getTime()))) { if (i == allowedDaysOfWeek.size()) { cal.add(Calendar.WEEK_OF_YEAR, period); i = 0; } cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(i)); i++; } startDate = cal.getTime(); } else { startDate = new Date(cal.getTimeInMillis() + offset); } } cal.setTime(startDate); Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); index = allowedDaysOfWeek.indexOf(dayOfWeek) + 1; while (deletedInstances.contains(cal.getTime())) { if (index >= allowedDaysOfWeek.size()) { index = 0; cal.add(Calendar.WEEK_OF_YEAR, period); cal2.add(Calendar.WEEK_OF_YEAR, period); } cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index)); cal2.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index)); index++; } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (dateOutOfRange(endDate)) return null; if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); } case Month: case MonthEnd: case HjMonth: case HjMonthEnd: { return nextMonth(startDate, endDate, false); } case MonthNth: case HjMonthNth: { if (patternSpecific1 == 0x7f && patternSpecific2 == 0x05) { return nextMonth(startDate, endDate, true); } return nextMonthN(startDate, endDate); } } return null; }
/** * Parses the binary data that describes the recurrent pattern. * * @param data the binary data. * @param sourceTask the calendar item. */ public RecurringPattern(byte[] data, CalendarItemTimerTask sourceTask) { this.sourceTask = sourceTask; dataBuffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); int offset = 4; recurFrequency = dataBuffer.getShort(offset); offset += 2; patternType = PatternType.getFromShort(dataBuffer.getShort(offset)); offset += 2; calendarType = dataBuffer.getShort(offset); offset += 2; firstDateTime = dataBuffer.getInt(offset); offset += 4; period = dataBuffer.getInt(offset); offset += 4; slidingFlag = dataBuffer.getInt(offset); offset += 4; switch (patternType) { case Week: case Month: case MonthEnd: case HjMonth: case HjMonthEnd: patternSpecific1 = dataBuffer.getInt(offset); patternSpecific2 = 0; offset += 4; if (patternType == PatternType.Week) { for (int day = firstDow; day < firstDow + 7; day++) { if ((patternSpecific1 & (weekOfDayMask[day % 7])) != 0) { allowedDaysOfWeek.add((day % 7) + 1); } } } break; case MonthNth: case HjMonthNth: patternSpecific1 = dataBuffer.getInt(offset); patternSpecific2 = dataBuffer.getInt(offset + 4); if (patternSpecific1 == 0x7f && patternSpecific2 != 0x5) { patternType = PatternType.Month; } for (int day = 0; day < 7; day++) { if ((patternSpecific1 & (weekOfDayMask[day])) != 0) { allowedDaysOfWeek.add((day) + 1); } } offset += 8; break; default: break; } // endType endType = dataBuffer.getInt(offset); offset += 4; occurenceCount = dataBuffer.getInt(offset); offset += 4; firstDow = dataBuffer.getInt(offset); offset += 4; deletedInstanceCount = dataBuffer.getInt(offset); offset += 4; // deleted instances for (int i = 0; i < deletedInstanceCount; i++) { deletedInstances.add(windowsTimeToDateObject(dataBuffer.getInt(offset))); offset += 4; } modifiedInstanceCount = dataBuffer.getInt(offset); offset += 4; // modified instances modifiedInstances = new int[modifiedInstanceCount]; for (int i = 0; i < modifiedInstanceCount; i++) { modifiedInstances[i] = dataBuffer.getInt(offset); offset += 4; } startDate = dataBuffer.getInt(offset); offset += 4; endDate = dataBuffer.getInt(offset); offset += 4; offset += 16; short exceptionCount = dataBuffer.getShort(offset); offset += 2; exceptionInfo = new ArrayList<ExceptionInfo>(exceptionCount); for (int i = 0; i < exceptionCount; i++) { ExceptionInfo tmpExceptionInfo = new ExceptionInfo(offset); exceptionInfo.add(tmpExceptionInfo); offset += tmpExceptionInfo.sizeInBytes(); CalendarService.BusyStatusEnum status = tmpExceptionInfo.getBusyStatus(); Date startTime = tmpExceptionInfo.getStartDate(); Date endTime = tmpExceptionInfo.getEndDate(); if (status == CalendarService.BusyStatusEnum.FREE || startTime == null || endTime == null) continue; Date currentTime = new Date(); if (endTime.before(currentTime) || endTime.equals(currentTime)) return; boolean executeNow = false; if (startTime.before(currentTime) || startTime.equals(currentTime)) executeNow = true; CalendarItemTimerTask task = new CalendarItemTimerTask( status, startTime, endTime, sourceTask.getId(), executeNow, this); task.scheduleTasks(); } }