Calendar calcStartDate = GregorianCalendar.getInstance();
calcStartDate.setTime(event.getStartDate());
int step = event.getRecurrenceCycle();
EventCalendarVO eventCalendar = getEventCalendar(myCalendar, event);
// handle days
if(EventVO.RecurrenceUnit.DAY.equals(event.getRecurrenceCycleUnit()))
{
if(EventVO.RecurrenceType.NUMBER_OF_TIMES.equals(event.getRecurrenceType()))
{
for(int i=1; i<event.getRecurrenceNumberOfTimes(); i++)
{
calcStartDate.add(Calendar.DAY_OF_MONTH, step);
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
}
}
else
{
Date recurrenceEndDate = endDate;
if(EventVO.RecurrenceType.UNTIL_DATE.equals(event.getRecurrenceType()))
recurrenceEndDate = event.getRecurrenceUntilDate();
boolean searching = true;
while(searching)
{
calcStartDate.add(Calendar.DAY_OF_MONTH, step);
if(calcStartDate.getTime().before(recurrenceEndDate))
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
else
searching = false;
}
}
}
else if(EventVO.RecurrenceUnit.WEEK.equals(event.getRecurrenceCycleUnit()))
{
if(log.isDebugEnabled())
log.debug("recurrence unit is "+EventVO.RecurrenceUnit.WEEK);
int[] day = new int[event.getRecurrenceInWeek().size()];
int[] dayAdd = new int[event.getRecurrenceInWeek().size()];
int week = DateUtility.getCalendar(event.getStartDate(), myCalendar.getFirstDayOfWeek().getDay()).get(Calendar.WEEK_OF_YEAR);
Calendar calcDateWeek = GregorianCalendar.getInstance();
calcDateWeek.setTime(event.getStartDate());
calcDateWeek.add(Calendar.DAY_OF_WEEK, 1);
// check days between recurrences (max 7 day in sum (-> week))
int pos = 0;
for(int i=0; i<7; i++)
{
RecurrenceInWeek inWeek = RecurrenceInWeek.getForDate(calcDateWeek.getTime());
dayAdd[pos]++;
day[pos] = inWeek.getCalendarValue();
if(event.getRecurrenceInWeek().contains(inWeek)) {
pos++; // go to next position
}
if(pos >= dayAdd.length) {
pos = 0; // go to zero position
}
calcDateWeek.roll(Calendar.DAY_OF_WEEK, 1);
}
if(log.isDebugEnabled())
{
StringBuilder builder = new StringBuilder();
for(int i=0; i<day.length; i++) {
if(i > 0)
builder.append(", ");
builder.append("[day:"+day[i]+"-add:"+dayAdd[i]+"]");
}
log.debug("day adds: size="+dayAdd.length+" / "+builder.toString());
}
if(EventVO.RecurrenceType.NUMBER_OF_TIMES.equals(event.getRecurrenceType()))
{
int run = 0;
for(int i=1; i<event.getRecurrenceNumberOfTimes(); i++)
{
if(run < dayAdd.length)
{
calcStartDate.add(Calendar.DAY_OF_WEEK, dayAdd[run]);
run++;
}
if(step > 1) {
int newWeek = DateUtility.getCalendar(calcStartDate.getTime(), myCalendar.getFirstDayOfWeek().getDay()).get(Calendar.WEEK_OF_YEAR);
if(newWeek != week)
calcStartDate.add(Calendar.DAY_OF_WEEK, 7*(step-1));
week = DateUtility.getCalendar(calcStartDate.getTime(), myCalendar.getFirstDayOfWeek().getDay()).get(Calendar.WEEK_OF_YEAR);
}
if(run >= dayAdd.length) {
run = 0;
}
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
}
}
else
{
Date recurrenceEndDate = endDate;
if(EventVO.RecurrenceType.UNTIL_DATE.equals(event.getRecurrenceType()))
recurrenceEndDate = event.getRecurrenceUntilDate();
int run = 0;
boolean searching = true;
while(searching)
{
if(run < dayAdd.length)
{
calcStartDate.add(Calendar.DAY_OF_WEEK, dayAdd[run]);
run++;
}
if(step > 1) {
int newWeek = DateUtility.getCalendar(calcStartDate.getTime(), myCalendar.getFirstDayOfWeek().getDay()).get(Calendar.WEEK_OF_YEAR);
if(newWeek != week)
calcStartDate.add(Calendar.DAY_OF_WEEK, 7*(step-1));
week = DateUtility.getCalendar(calcStartDate.getTime(), myCalendar.getFirstDayOfWeek().getDay()).get(Calendar.WEEK_OF_YEAR);
}
if(run >= dayAdd.length) {
run = 0;
}
if(calcStartDate.getTime().before(recurrenceEndDate))
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
else
searching = false;
}
}
}
else if(EventVO.RecurrenceUnit.MONTH.equals(event.getRecurrenceCycleUnit()))
{
if(EventVO.RecurrenceType.NUMBER_OF_TIMES.equals(event.getRecurrenceType()))
{
for(int i=1; i<event.getRecurrenceNumberOfTimes(); i++)
{
if(EventVO.RecurrenceInMonth.DAY_OF_MONTH.equals(event.getRecurrenceInMonth()))
calcStartDate.add(Calendar.MONTH, step);
else
{
int dayOfWeek = calcStartDate.get(Calendar.DAY_OF_WEEK);
int dayOfWeekInMonth = calcStartDate.get(Calendar.DAY_OF_WEEK_IN_MONTH);
calcStartDate.add(Calendar.MONTH, step);
calcStartDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calcStartDate.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth);
}
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
}
}
else
{
Date recurrenceEndDate = endDate;
if(EventVO.RecurrenceType.UNTIL_DATE.equals(event.getRecurrenceType()))
recurrenceEndDate = event.getRecurrenceUntilDate();
boolean searching = true;
while(searching)
{
if(EventVO.RecurrenceInMonth.DAY_OF_MONTH.equals(event.getRecurrenceInMonth()))
calcStartDate.add(Calendar.MONTH, step);
else
{
int dayOfWeek = calcStartDate.get(Calendar.DAY_OF_WEEK);
int dayOfWeekInMonth = calcStartDate.get(Calendar.DAY_OF_WEEK_IN_MONTH);
calcStartDate.add(Calendar.MONTH, step);
calcStartDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calcStartDate.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth);
}
if(calcStartDate.getTime().before(recurrenceEndDate))
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
else
searching = false;
}
}
}
else if(EventVO.RecurrenceUnit.YEAR.equals(event.getRecurrenceCycleUnit()))
{
if(EventVO.RecurrenceType.NUMBER_OF_TIMES.equals(event.getRecurrenceType()))
{
for(int i=1; i<event.getRecurrenceNumberOfTimes(); i++)
{
calcStartDate.add(Calendar.YEAR, step);
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
}
}
else
{
Date recurrenceEndDate = endDate;
if(EventVO.RecurrenceType.UNTIL_DATE.equals(event.getRecurrenceType()))
recurrenceEndDate = event.getRecurrenceUntilDate();
boolean searching = true;
while(searching)
{
calcStartDate.add(Calendar.YEAR, step);
if(calcStartDate.getTime().before(recurrenceEndDate))
list.add(new EventBean(calcStartDate.getTime(), duration, list.size()+1, event, readable, eventCalendar.getParticipiantType(), eventCalendar.getInvitationStatus()));
else
searching = false;
}
}