Search in sources :

Example 1 with DateList

use of net.fortuna.ical4j.model.DateList in project bw-calendar-engine by Bedework.

the class RecurUtil method getLatestRecurrenceDate.

/**
 * Return the absolute latest end date for this event. Note that
 * exclusions may mean the actual latest date is earlier.
 *
 * @param evprops
 * @param vstart
 * @param enddt
 * @param d
 * @param maxRangeEnd
 * @return date
 * @throws CalFacadeException
 */
private static Date getLatestRecurrenceDate(final PropertyList evprops, final DtStart vstart, final DtEnd enddt, final Duration d, final Date maxRangeEnd) throws CalFacadeException {
    boolean debug = getLog().isDebugEnabled();
    try {
        Date start = vstart.getDate();
        Date until = null;
        /* Get the duration of the event to get us past the end
       */
        Dur dur;
        if (d != null) {
            dur = d.getDuration();
        } else {
            Date evend;
            if (enddt == null) {
                evend = start;
            } else {
                evend = enddt.getDate();
            }
            dur = new Dur(start, evend);
        }
        /* Make a new duration incremented a little to avoid any boundary
          conditions */
        if (dur.getWeeks() != 0) {
            dur = new Dur(dur.getWeeks() + 1);
        } else {
            dur = new Dur(dur.getDays() + 1, dur.getHours(), dur.getMinutes(), dur.getSeconds());
        }
        PropertyList rrules = evprops.getProperties(Property.RRULE);
        PropertyList rdts = evprops.getProperties(Property.RDATE);
        if ((rrules == null) && (rdts == null)) {
            // Not a recurring event
            return null;
        }
        if (rrules != null) {
            Iterator rit = rrules.iterator();
            while (rit.hasNext()) {
                RRule r = (RRule) rit.next();
                Date nextUntil = getLastDate(r.getRecur(), start, maxRangeEnd);
                if (nextUntil == null) {
                    /* We have a rule without an end date so it's infinite.
             */
                    return null;
                }
                if (debug) {
                    debugMsg("Last date for recur=" + nextUntil);
                }
                if ((until == null) || (nextUntil.after(until))) {
                    until = nextUntil;
                }
            }
            /* We have some rules - none have an end date so it's infinite.
         */
            if (until == null) {
                // infinite
                return null;
            }
        }
        if (rdts != null) {
            // Get the latest date from each
            // XXX are these sorted?
            Iterator rit = rdts.iterator();
            while (rit.hasNext()) {
                RDate r = (RDate) rit.next();
                if (Value.PERIOD.equals(r.getParameter(Parameter.VALUE))) {
                    PeriodList pl = r.getPeriods();
                    Iterator it = pl.iterator();
                    while (it.hasNext()) {
                        Period p = (Period) it.next();
                        // Not sure if a single date gives a null end
                        Date nextUntil = p.getEnd();
                        if (nextUntil == null) {
                            nextUntil = p.getStart();
                        }
                        if ((until == null) || (nextUntil.after(until))) {
                            until = nextUntil;
                        }
                    }
                } else {
                    // date or datetime
                    DateList startDates = r.getDates();
                    for (int j = 0; j < startDates.size(); j++) {
                        Date startDate = (Date) startDates.get(j);
                        Date endDate = new Date(dur.getTime(startDate));
                        if ((until == null) || (endDate.after(until))) {
                            until = endDate;
                        }
                    }
                }
            }
        }
        if (debug) {
            debugMsg("Last date before fix=" + until);
        }
        /* Now add the duration of the event to get us past the end
       */
        if (until instanceof DateTime) {
            until = new DateTime(dur.getTime(until));
            ((DateTime) until).setUtc(true);
        } else {
            until = new Date(dur.getTime(until));
        }
        if (debug) {
            debugMsg("Last date after fix=" + until);
        }
        return until;
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : Dur(net.fortuna.ical4j.model.Dur) RDate(net.fortuna.ical4j.model.property.RDate) RRule(net.fortuna.ical4j.model.property.RRule) PeriodList(net.fortuna.ical4j.model.PeriodList) Period(net.fortuna.ical4j.model.Period) RDate(net.fortuna.ical4j.model.property.RDate) Date(net.fortuna.ical4j.model.Date) DateTime(net.fortuna.ical4j.model.DateTime) BwDateTime(org.bedework.calfacade.BwDateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) PropertyList(net.fortuna.ical4j.model.PropertyList) Iterator(java.util.Iterator) DateList(net.fortuna.ical4j.model.DateList)

Example 2 with DateList

use of net.fortuna.ical4j.model.DateList in project openolat by klemens.

the class ICalFileCalendarManager method getRecurringsInPeriod.

private final DateList getRecurringsInPeriod(Date periodStart, Date periodEnd, KalendarEvent kEvent) {
    DateList recurDates = null;
    String recurrenceRule = kEvent.getRecurrenceRule();
    if (StringHelper.containsNonWhitespace(recurrenceRule)) {
        try {
            Recur recur = new Recur(recurrenceRule);
            net.fortuna.ical4j.model.Date periodStartDate = CalendarUtils.createDate(periodStart);
            net.fortuna.ical4j.model.Date periodEndDate = CalendarUtils.createDate(periodEnd);
            net.fortuna.ical4j.model.Date eventStartDate = CalendarUtils.createDate(kEvent.getBegin());
            recurDates = recur.getDates(eventStartDate, periodStartDate, periodEndDate, Value.DATE);
        } catch (ParseException e) {
            log.error("cannot restore recurrence rule: " + recurrenceRule, e);
        }
        String recurrenceExc = kEvent.getRecurrenceExc();
        if (recurrenceExc != null && !recurrenceExc.equals("")) {
            try {
                ExDate exdate = new ExDate();
                // see OLAT-5645
                if (recurrenceExc.length() > 8) {
                    exdate.setValue(recurrenceExc);
                } else {
                    exdate.getParameters().replace(Value.DATE);
                    exdate.setValue(recurrenceExc);
                }
                for (Object date : exdate.getDates()) {
                    if (recurDates.contains(date))
                        recurDates.remove(date);
                }
            } catch (ParseException e) {
                log.error("cannot restore excluded dates for this recurrence: " + recurrenceExc, e);
            }
        }
    }
    return recurDates;
}
Also used : ExDate(net.fortuna.ical4j.model.property.ExDate) ParseException(java.text.ParseException) DateList(net.fortuna.ical4j.model.DateList) Recur(net.fortuna.ical4j.model.Recur)

Example 3 with DateList

use of net.fortuna.ical4j.model.DateList in project openolat by klemens.

the class CalendarUtils method getRecurrenceExcludeDates.

/**
 * Create list with excluded dates based on the exclusion rule.
 * @param recurrenceExc
 * @return list with excluded dates
 */
public static List<Date> getRecurrenceExcludeDates(String recurrenceExc) {
    List<Date> recurExcDates = new ArrayList<>();
    if (recurrenceExc != null && !recurrenceExc.equals("")) {
        try {
            net.fortuna.ical4j.model.ParameterList pl = new net.fortuna.ical4j.model.ParameterList();
            ExDate exdate = new ExDate(pl, recurrenceExc);
            DateList dl = exdate.getDates();
            for (Object date : dl) {
                Date excDate = (Date) date;
                recurExcDates.add(excDate);
            }
        } catch (ParseException e) {
            log.error("cannot restore recurrence exceptions", e);
        }
    }
    return recurExcDates;
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) ExDate(net.fortuna.ical4j.model.property.ExDate) ParseException(java.text.ParseException) DateList(net.fortuna.ical4j.model.DateList)

Example 4 with DateList

use of net.fortuna.ical4j.model.DateList in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method getRecurringsInPeriod.

private final DateList getRecurringsInPeriod(Date periodStart, Date periodEnd, KalendarEvent kEvent) {
    DateList recurDates = null;
    String recurrenceRule = kEvent.getRecurrenceRule();
    if (StringHelper.containsNonWhitespace(recurrenceRule)) {
        try {
            Recur recur = new Recur(recurrenceRule);
            net.fortuna.ical4j.model.Date periodStartDate = CalendarUtils.createDate(periodStart);
            net.fortuna.ical4j.model.Date periodEndDate = CalendarUtils.createDate(periodEnd);
            net.fortuna.ical4j.model.Date eventStartDate = CalendarUtils.createDate(kEvent.getBegin());
            recurDates = recur.getDates(eventStartDate, periodStartDate, periodEndDate, Value.DATE);
        } catch (ParseException e) {
            log.error("cannot restore recurrence rule: " + recurrenceRule, e);
        }
        String recurrenceExc = kEvent.getRecurrenceExc();
        if (recurrenceExc != null && !recurrenceExc.equals("")) {
            try {
                ExDate exdate = new ExDate();
                // see OLAT-5645
                if (recurrenceExc.length() > 8) {
                    exdate.setValue(recurrenceExc);
                } else {
                    exdate.getParameters().replace(Value.DATE);
                    exdate.setValue(recurrenceExc);
                }
                for (Object date : exdate.getDates()) {
                    if (recurDates.contains(date))
                        recurDates.remove(date);
                }
            } catch (ParseException e) {
                log.error("cannot restore excluded dates for this recurrence: " + recurrenceExc, e);
            }
        }
    }
    return recurDates;
}
Also used : ExDate(net.fortuna.ical4j.model.property.ExDate) ParseException(java.text.ParseException) DateList(net.fortuna.ical4j.model.DateList) Recur(net.fortuna.ical4j.model.Recur)

Example 5 with DateList

use of net.fortuna.ical4j.model.DateList in project OpenOLAT by OpenOLAT.

the class CalendarUtils method getRecurrenceExcludeRule.

/**
 * Create exclusion rule based on list with dates.
 * @param dates
 * @return string with exclude rule
 */
public static String getRecurrenceExcludeRule(List<Date> dates) {
    if (dates != null && dates.size() > 0) {
        DateList dl = new DateList();
        for (Date date : dates) {
            net.fortuna.ical4j.model.Date dd = CalendarUtils.createDate(date);
            dl.add(dd);
        }
        ExDate exdate = new ExDate(dl);
        return exdate.getValue();
    }
    return null;
}
Also used : ExDate(net.fortuna.ical4j.model.property.ExDate) DateList(net.fortuna.ical4j.model.DateList) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate)

Aggregations

DateList (net.fortuna.ical4j.model.DateList)10 ExDate (net.fortuna.ical4j.model.property.ExDate)7 ParseException (java.text.ParseException)4 Date (java.util.Date)4 Date (net.fortuna.ical4j.model.Date)4 BwDateTime (org.bedework.calfacade.BwDateTime)4 DateTime (net.fortuna.ical4j.model.DateTime)3 RDate (net.fortuna.ical4j.model.property.RDate)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 Dur (net.fortuna.ical4j.model.Dur)2 Recur (net.fortuna.ical4j.model.Recur)2 TreeSet (java.util.TreeSet)1 Parameter (net.fortuna.ical4j.model.Parameter)1 Period (net.fortuna.ical4j.model.Period)1 PeriodList (net.fortuna.ical4j.model.PeriodList)1 PropertyList (net.fortuna.ical4j.model.PropertyList)1 TimeZone (net.fortuna.ical4j.model.TimeZone)1 DateListProperty (net.fortuna.ical4j.model.property.DateListProperty)1 DtStart (net.fortuna.ical4j.model.property.DtStart)1