}
int count = 0;
while (!startDate.getTime().after(end.getTime())) {
Event recurrentEvent = copyEvent(baseEvent);
recurrentEvent.setRecurrenceDatesToIgnore("");
if(count>0 && UtilMethods.isSet(baseEvent.getDisconnectedFrom())){
recurrentEvent.setDisconnectedFrom("");
}
switch(baseEvent.getOccursEnum()) {
case DAILY:
//build the start time/date
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, startTime.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, startTime.get(Calendar.SECOND));
recurrentEvent.setStartDate(cal.getTime());
//build end date/time
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, endTime.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, endTime.get(Calendar.SECOND));
recurrentEvent.setEndDate(cal.getTime());
//if this event is after, die
if (cal.getTime().after(end.getTime()))
break;
GregorianCalendar calToIgnore = new GregorianCalendar();
calToIgnore.setTime(recurrentEvent.getStartDate());
calToIgnore.set(Calendar.SECOND, 0);
calToIgnore.set(Calendar.MILLISECOND, 0);
if(!datesToIgnore.contains(String.valueOf(calToIgnore.getTime().getTime()))){
GregorianCalendar firstOccurence = new GregorianCalendar();
firstOccurence.setTime(baseEvent.getStartDate());
long numberOfDays = (long)( (calToIgnore.getTime().getTime() - firstOccurence.getTime().getTime()) / (1000 * 60 * 60 * 24));
if(numberOfDays%interval==0){
recurrentEvent.setStartDate(calToIgnore.getTime());
//http://jira.dotmarketing.net/browse/DOTCMS-6303
recurrentEvent.setIdentifier(RecurrenceUtil.getRecurrentEventIdentifier(recurrentEvent));
events.add(recurrentEvent);
count++;
}
}
//add to start date
startDate.add(Calendar.DAY_OF_MONTH, 1);
break;
case WEEKLY:
if (baseEvent.getRecurrenceDaysOfWeek() == null) {
break;
}
for (int j = 1; j < 8; j++) {
int x = startDate.get(Calendar.DAY_OF_WEEK);
if (baseEvent.getRecurrenceDaysOfWeek().contains(String.valueOf(x))) {
recurrentEvent = copyEvent(baseEvent);
recurrentEvent.setRecurrenceDatesToIgnore("");
if(count>0 && UtilMethods.isSet(baseEvent.getDisconnectedFrom())){
recurrentEvent.setDisconnectedFrom("");
}
//build the start time/date
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, startTime.get(Calendar.MINUTE));
recurrentEvent.setStartDate(cal.getTime());
//build end date/time
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, endTime.get(Calendar.MINUTE));
recurrentEvent.setEndDate(cal.getTime());
//if this event is after, die
if (cal.getTime().after(end.getTime()))
break;
calToIgnore = new GregorianCalendar();
calToIgnore.setTime(recurrentEvent.getStartDate());
calToIgnore.set(Calendar.SECOND, 0);
calToIgnore.set(Calendar.MILLISECOND, 0);
if(!datesToIgnore.contains(String.valueOf(calToIgnore.getTime().getTime()))){
GregorianCalendar baseCal = new GregorianCalendar();
baseCal.setTime(baseEvent.getStartDate());
GregorianCalendar firstOccurence = calculateFirstOccurence(baseEvent);
GregorianCalendar c1 = baseCal;
GregorianCalendar c2 = new GregorianCalendar();
c2.setTime(recurrentEvent.getStartDate());
long numberOfWeeks = 0;
for( long i=1; ; i++ ) {
c1.add( Calendar.WEEK_OF_YEAR, 1 );
if( c1.after(c2) ) {
numberOfWeeks = i-1;
break;
}
}
if(baseCal.get(Calendar.WEEK_OF_YEAR)<firstOccurence.get(Calendar.WEEK_OF_YEAR)){
c1 = firstOccurence;
c2 = new GregorianCalendar();
c2.setTime(recurrentEvent.getStartDate());;
for( long i=1; ; i++ ) {
c1.add( Calendar.WEEK_OF_YEAR, 1 );
if( c1.after(c2) ) {
numberOfWeeks = i-1;
break;
}
}
}
if((numberOfWeeks%(interval)==0)){
recurrentEvent.setStartDate(calToIgnore.getTime());
//http://jira.dotmarketing.net/browse/DOTCMS-6303
recurrentEvent.setIdentifier(RecurrenceUtil.getRecurrentEventIdentifier(recurrentEvent));
events.add(recurrentEvent);
count++;
}
}
}
startDate.add(Calendar.DAY_OF_MONTH, 1);
}
break;
case MONTHLY:
while (true) {
recurrentEvent = copyEvent(baseEvent);
recurrentEvent.setRecurrenceDatesToIgnore("");
if(count>0 && UtilMethods.isSet(baseEvent.getDisconnectedFrom())){
recurrentEvent.setDisconnectedFrom("");
}
boolean isInRange = false;
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, startTime.get(Calendar.MINUTE));
int weekOfMonth = baseEvent.getRecurrenceWeekOfMonth();
int dayOfWeek = baseEvent.getRecurrenceDayOfWeek();
if(UtilMethods.isSet(baseEvent.getRecurrenceDayOfMonth()) && (baseEvent.getRecurrenceDayOfMonth() > 0)){
GregorianCalendar baseCal = new GregorianCalendar();
baseCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
baseCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
GregorianCalendar baseCal2 = new GregorianCalendar();
baseCal2.set(Calendar.MONTH, cal.get(Calendar.MONTH));
baseCal2.set(Calendar.YEAR, cal.get(Calendar.YEAR));
int dayOfMonth = baseEvent.getRecurrenceDayOfMonth();
baseCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
while(baseCal.get(Calendar.MONTH) != baseCal2.get(Calendar.MONTH)){
baseCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
dayOfMonth--;
}
weekOfMonth = baseCal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
dayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK);
}
GregorianCalendar auxCal = new GregorianCalendar();
auxCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
auxCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
auxCal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
if(weekOfMonth==1){
auxCal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
}else{
if(weekOfMonth<=4){
auxCal.set(Calendar.DAY_OF_WEEK_IN_MONTH, weekOfMonth);
}else{
auxCal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
}
}
recurrentEvent.setStartDate(cal.getTime());
if((cal.get(Calendar.WEEK_OF_MONTH)==auxCal.get(Calendar.WEEK_OF_MONTH)) && (cal.get(Calendar.DAY_OF_WEEK)==auxCal.get(Calendar.DAY_OF_WEEK))){
GregorianCalendar baseCal = new GregorianCalendar();
baseCal.setTime(baseEvent.getStartDate());
GregorianCalendar firstOccurence = calculateFirstOccurence(baseEvent);
int numberOfMonths =((baseCal.get(Calendar.YEAR) - auxCal.get(Calendar.YEAR)) * 12) + (baseCal.get(Calendar.MONTH) - auxCal.get(Calendar.MONTH));
if(baseCal.get(Calendar.MONTH)<firstOccurence.get(Calendar.MONTH)){
numberOfMonths =((firstOccurence.get(Calendar.YEAR) - auxCal.get(Calendar.YEAR)) * 12) + (firstOccurence.get(Calendar.MONTH) - auxCal.get(Calendar.MONTH));
}
if(numberOfMonths==0 || (numberOfMonths%interval==0)){
isInRange = true;
}
}
//build end date/time
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, endTime.get(Calendar.MINUTE));
recurrentEvent.setEndDate(cal.getTime());
//if this event is after, die
if (cal.getTime().after(end.getTime()))
break;
if(isInRange){
calToIgnore = new GregorianCalendar();
calToIgnore.setTime(recurrentEvent.getStartDate());
calToIgnore.set(Calendar.SECOND, 0);
calToIgnore.set(Calendar.MILLISECOND, 0);
if(!datesToIgnore.contains(String.valueOf(calToIgnore.getTime().getTime()))){
recurrentEvent.setStartDate(calToIgnore.getTime());
//http://jira.dotmarketing.net/browse/DOTCMS-6303
recurrentEvent.setIdentifier(RecurrenceUtil.getRecurrentEventIdentifier(recurrentEvent));
events.add(recurrentEvent);
count++;
}
}
startDate.add(Calendar.DAY_OF_MONTH, 1);
}
//add to start date
startDate.add(Calendar.MONTH, interval);
break;
case ANNUALLY:
while (true) {
recurrentEvent = copyEvent(baseEvent);
recurrentEvent.setRecurrenceDatesToIgnore("");
if(count>0 && UtilMethods.isSet(baseEvent.getDisconnectedFrom())){
recurrentEvent.setDisconnectedFrom("");
}
boolean isInRange = false;
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, startTime.get(Calendar.MINUTE));
int weekOfMonth = baseEvent.getRecurrenceWeekOfMonth();
int dayOfWeek = baseEvent.getRecurrenceDayOfWeek();
int monthOfYear = baseEvent.getRecurrenceMonthOfYear()>0?baseEvent.getRecurrenceMonthOfYear()-1:0;
if(UtilMethods.isSet(baseEvent.getRecurrenceDayOfMonth()) && (baseEvent.getRecurrenceDayOfMonth() > 0)){
GregorianCalendar baseCal = new GregorianCalendar();
baseCal.set(Calendar.MONTH, monthOfYear);
baseCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
GregorianCalendar baseCal2 = new GregorianCalendar();
baseCal2.set(Calendar.MONTH, monthOfYear);
baseCal2.set(Calendar.YEAR, cal.get(Calendar.YEAR));
int dayOfMonth = baseEvent.getRecurrenceDayOfMonth();
baseCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
while(baseCal.get(Calendar.MONTH) != baseCal2.get(Calendar.MONTH)){
baseCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
dayOfMonth--;
}
weekOfMonth = baseCal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
dayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK);
}
GregorianCalendar auxCal = new GregorianCalendar();
auxCal.set(Calendar.MONTH, monthOfYear);
auxCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
auxCal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
if(weekOfMonth==1){
auxCal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
}else{
if(weekOfMonth<=4){
auxCal.set(Calendar.DAY_OF_WEEK_IN_MONTH, weekOfMonth);
}else{
auxCal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
}
}
recurrentEvent.setStartDate(cal.getTime());
if((cal.get(Calendar.MONTH)==auxCal.get(Calendar.MONTH)) && (cal.get(Calendar.WEEK_OF_MONTH)==auxCal.get(Calendar.WEEK_OF_MONTH)) && (cal.get(Calendar.DAY_OF_WEEK)==auxCal.get(Calendar.DAY_OF_WEEK))){
GregorianCalendar baseCal = new GregorianCalendar();
baseCal.setTime(baseEvent.getStartDate());
GregorianCalendar firstOccurence = calculateFirstOccurence(baseEvent);
int numberOfYears =(baseCal.get(Calendar.YEAR) - auxCal.get(Calendar.YEAR));
if(baseCal.get(Calendar.YEAR)<firstOccurence.get(Calendar.YEAR)){
numberOfYears =(firstOccurence.get(Calendar.YEAR) - auxCal.get(Calendar.YEAR));
}
if(numberOfYears==0 || (numberOfYears%interval==0)){
isInRange = true;
}
}
//build end date/time
cal = new GregorianCalendar();
cal.setTime(startDate.getTime());
cal.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, endTime.get(Calendar.MINUTE));
recurrentEvent.setEndDate(cal.getTime());
//if this event is after, die
if (cal.getTime().after(end.getTime()))
break;
if(isInRange){
calToIgnore = new GregorianCalendar();
calToIgnore.setTime(recurrentEvent.getStartDate());
calToIgnore.set(Calendar.SECOND, 0);
calToIgnore.set(Calendar.MILLISECOND, 0);
if(!datesToIgnore.contains(String.valueOf(calToIgnore.getTime().getTime()))){
recurrentEvent.setStartDate(calToIgnore.getTime());
//http://jira.dotmarketing.net/browse/DOTCMS-6303
recurrentEvent.setIdentifier(RecurrenceUtil.getRecurrentEventIdentifier(recurrentEvent));
events.add(recurrentEvent);
count++;
}
}
startDate.add(Calendar.DAY_OF_MONTH, 1);