Example usage for com.liferay.portal.kernel.cal TZSRecurrence setFrequency

List of usage examples for com.liferay.portal.kernel.cal TZSRecurrence setFrequency

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.cal TZSRecurrence setFrequency.

Prototype

public void setFrequency(int freq) 

Source Link

Document

Method setFrequency

Usage

From source file:com.liferay.portlet.calendar.service.impl.CalEventLocalServiceImpl.java

License:Open Source License

protected TZSRecurrence toRecurrence(RRule rRule, Calendar startDate) {
    Recur recur = rRule.getRecur();//from  w  w w  .  j  a v a  2s  .co m

    Calendar recStartCal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

    recStartCal.setTime(startDate.getTime());

    TZSRecurrence recurrence = new TZSRecurrence(recStartCal,
            new com.liferay.portal.kernel.cal.Duration(1, 0, 0, 0));

    recurrence.setWeekStart(Calendar.SUNDAY);

    if (recur.getInterval() > 1) {
        recurrence.setInterval(recur.getInterval());
    }

    Calendar until = Calendar.getInstance(TimeZoneUtil.getTimeZone(StringPool.UTC));

    String frequency = recur.getFrequency();

    if (recur.getUntil() != null) {
        until.setTime(recur.getUntil());

        recurrence.setUntil(until);
    } else if (rRule.getValue().indexOf("COUNT") >= 0) {
        until.setTimeInMillis(startDate.getTimeInMillis());

        int addField = 0;

        if (Recur.DAILY.equals(frequency)) {
            addField = Calendar.DAY_OF_YEAR;
        } else if (Recur.WEEKLY.equals(frequency)) {
            addField = Calendar.WEEK_OF_YEAR;
        } else if (Recur.MONTHLY.equals(frequency)) {
            addField = Calendar.MONTH;
        } else if (Recur.YEARLY.equals(frequency)) {
            addField = Calendar.YEAR;
        }

        int addAmount = recurrence.getInterval() * recur.getCount();

        until.add(addField, addAmount);
        until.add(Calendar.DAY_OF_YEAR, -1);

        recurrence.setUntil(until);
    }

    if (Recur.DAILY.equals(frequency)) {
        recurrence.setFrequency(Recurrence.DAILY);

        List<DayAndPosition> dayPosList = new ArrayList<DayAndPosition>();

        Iterator<WeekDay> itr = recur.getDayList().iterator();

        while (itr.hasNext()) {
            WeekDay weekDay = itr.next();

            dayPosList.add(new DayAndPosition(toCalendarWeekDay(weekDay), 0));
        }

        if (!dayPosList.isEmpty()) {
            recurrence.setByDay(dayPosList.toArray(new DayAndPosition[dayPosList.size()]));
        }
    } else if (Recur.WEEKLY.equals(frequency)) {
        recurrence.setFrequency(Recurrence.WEEKLY);

        List<DayAndPosition> dayPosList = new ArrayList<DayAndPosition>();

        Iterator<WeekDay> itr = recur.getDayList().iterator();

        while (itr.hasNext()) {
            WeekDay weekDay = itr.next();

            dayPosList.add(new DayAndPosition(toCalendarWeekDay(weekDay), 0));
        }

        if (!dayPosList.isEmpty()) {
            recurrence.setByDay(dayPosList.toArray(new DayAndPosition[dayPosList.size()]));
        }
    } else if (Recur.MONTHLY.equals(frequency)) {
        recurrence.setFrequency(Recurrence.MONTHLY);

        Iterator<Integer> monthDayListItr = recur.getMonthDayList().iterator();

        if (monthDayListItr.hasNext()) {
            Integer monthDay = monthDayListItr.next();

            recurrence.setByMonthDay(new int[] { monthDay.intValue() });
        }

        Iterator<WeekDay> dayListItr = recur.getDayList().iterator();

        if (dayListItr.hasNext()) {
            WeekDay weekDay = dayListItr.next();

            DayAndPosition[] dayPos = { new DayAndPosition(toCalendarWeekDay(weekDay), weekDay.getOffset()) };

            recurrence.setByDay(dayPos);
        }
    } else if (Recur.YEARLY.equals(frequency)) {
        recurrence.setFrequency(Recurrence.YEARLY);
    }

    return recurrence;
}