Search in sources :

Example 16 with Recur

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

the class ICalFileCalendarManager method getVEvent.

private VEvent getVEvent(KalendarEvent kEvent) {
    VEvent vEvent = new VEvent();
    if (!kEvent.isAllDayEvent()) {
        // regular VEvent
        DateTime dtBegin = new DateTime(kEvent.getBegin());
        if (tz != null) {
            dtBegin.setTimeZone(tz);
        }
        Date kEventEnd = kEvent.getEnd();
        if (kEventEnd == null) {
            vEvent = new VEvent(dtBegin, kEvent.getSubject());
        } else {
            DateTime dtEnd = new DateTime(kEventEnd);
            if (tz != null) {
                dtEnd.setTimeZone(tz);
            }
            vEvent = new VEvent(dtBegin, dtEnd, kEvent.getSubject());
        }
    } else {
        // AllDay VEvent
        net.fortuna.ical4j.model.Date dtBegin = CalendarUtils.createDate(kEvent.getBegin());
        // adjust end date: ICal end dates for all day events are on the next day
        Date adjustedEndDate = new Date(kEvent.getEnd().getTime() + (1000 * 60 * 60 * 24));
        net.fortuna.ical4j.model.Date dtEnd = CalendarUtils.createDate(adjustedEndDate);
        vEvent = new VEvent(dtBegin, dtEnd, kEvent.getSubject());
    }
    if (kEvent.getCreated() > 0) {
        Created created = new Created(new DateTime(kEvent.getCreated()));
        vEvent.getProperties().add(created);
    }
    if ((kEvent.getCreatedBy() != null) && !kEvent.getCreatedBy().trim().isEmpty()) {
        Contact contact = new Contact();
        contact.setValue(kEvent.getCreatedBy());
        vEvent.getProperties().add(contact);
    }
    if (kEvent.getLastModified() > 0) {
        LastModified lastMod = new LastModified(new DateTime(kEvent.getLastModified()));
        vEvent.getProperties().add(lastMod);
    }
    // Uid
    PropertyList vEventProperties = vEvent.getProperties();
    vEventProperties.add(new Uid(kEvent.getID()));
    // clazz
    switch(kEvent.getClassification()) {
        case KalendarEvent.CLASS_PRIVATE:
            vEventProperties.add(ICAL_CLASS_PRIVATE);
            break;
        case KalendarEvent.CLASS_PUBLIC:
            vEventProperties.add(ICAL_CLASS_PUBLIC);
            break;
        case KalendarEvent.CLASS_X_FREEBUSY:
            vEventProperties.add(ICAL_CLASS_X_FREEBUSY);
            break;
        default:
            vEventProperties.add(ICAL_CLASS_PRIVATE);
            break;
    }
    // location
    if (kEvent.getLocation() != null) {
        vEventProperties.add(new Location(kEvent.getLocation()));
    }
    if (kEvent.getDescription() != null) {
        vEventProperties.add(new Description(kEvent.getDescription()));
    }
    // event links
    Url urlOnce = null;
    List<KalendarEventLink> kalendarEventLinks = kEvent.getKalendarEventLinks();
    if ((kalendarEventLinks != null) && !kalendarEventLinks.isEmpty()) {
        for (Iterator<KalendarEventLink> iter = kalendarEventLinks.iterator(); iter.hasNext(); ) {
            KalendarEventLink link = iter.next();
            StringBuilder linkEncoded = new StringBuilder(200);
            linkEncoded.append(link.getProvider());
            linkEncoded.append("§");
            linkEncoded.append(link.getId());
            linkEncoded.append("§");
            linkEncoded.append(link.getDisplayName());
            linkEncoded.append("§");
            linkEncoded.append(link.getURI());
            linkEncoded.append("§");
            linkEncoded.append(link.getIconCssClass());
            XProperty linkProperty = new XProperty(ICAL_X_OLAT_LINK, linkEncoded.toString());
            vEventProperties.add(linkProperty);
            if (urlOnce == null) {
                try {
                    Url url = new Url();
                    url.setValue(link.getURI());
                    urlOnce = url;
                } catch (URISyntaxException e) {
                    log.error("Invalid URL:" + link.getURI());
                }
            }
        }
    }
    if (urlOnce != null) {
        vEventProperties.add(urlOnce);
    }
    if (kEvent.getComment() != null) {
        vEventProperties.add(new XProperty(ICAL_X_OLAT_COMMENT, kEvent.getComment()));
    }
    if (kEvent.getNumParticipants() != null) {
        vEventProperties.add(new XProperty(ICAL_X_OLAT_NUMPARTICIPANTS, Integer.toString(kEvent.getNumParticipants())));
    }
    if (kEvent.getParticipants() != null) {
        StringBuilder strBuf = new StringBuilder();
        String[] participants = kEvent.getParticipants();
        for (String participant : participants) {
            strBuf.append(participant);
            strBuf.append("§");
        }
        vEventProperties.add(new XProperty(ICAL_X_OLAT_PARTICIPANTS, strBuf.toString()));
    }
    if (kEvent.getSourceNodeId() != null) {
        vEventProperties.add(new XProperty(ICAL_X_OLAT_SOURCENODEID, kEvent.getSourceNodeId()));
    }
    if (kEvent.getManagedFlags() != null) {
        String val = CalendarManagedFlag.toString(kEvent.getManagedFlags());
        vEventProperties.add(new XProperty(ICAL_X_OLAT_MANAGED, val));
    }
    if (StringHelper.containsNonWhitespace(kEvent.getExternalId())) {
        vEventProperties.add(new XProperty(ICAL_X_OLAT_EXTERNAL_ID, kEvent.getExternalId()));
    }
    if (StringHelper.containsNonWhitespace(kEvent.getExternalSource())) {
        vEventProperties.add(new XProperty(ICAL_X_OLAT_EXTERNAL_SOURCE, kEvent.getExternalSource()));
    }
    String recurenceId = kEvent.getRecurrenceID();
    if (StringHelper.containsNonWhitespace(recurenceId)) {
        try {
            RecurrenceId recurId = new RecurrenceId(tz);
            // VALUE=DATE recurrence id need to be specially saved
            if (recurenceId.length() < 9) {
                recurId = new RecurrenceId(tz);
                recurId.setDate(CalendarUtils.createDate(new net.fortuna.ical4j.model.Date(recurenceId)));
            } else {
                recurId = new RecurrenceId(recurenceId, tz);
            }
            vEventProperties.add(recurId);
        } catch (ParseException e) {
            log.error("cannot create recurrence ID: " + recurenceId, e);
        }
    }
    // recurrence
    String recurrence = kEvent.getRecurrenceRule();
    if (recurrence != null && !recurrence.equals("")) {
        try {
            Recur recur = new Recur(recurrence);
            RRule rrule = new RRule(recur);
            vEventProperties.add(rrule);
        } catch (ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }
    // recurrence exclusions
    String recurrenceExc = kEvent.getRecurrenceExc();
    if (recurrenceExc != null && !recurrenceExc.equals("")) {
        ExDate exdate = new ExDate();
        try {
            exdate.setValue(recurrenceExc);
            vEventProperties.add(exdate);
        } catch (ParseException e) {
            log.error("", e);
        }
    }
    return vEvent;
}
Also used : Description(net.fortuna.ical4j.model.property.Description) RRule(net.fortuna.ical4j.model.property.RRule) KalendarEventLink(org.olat.commons.calendar.model.KalendarEventLink) URISyntaxException(java.net.URISyntaxException) DateTime(net.fortuna.ical4j.model.DateTime) Url(net.fortuna.ical4j.model.property.Url) Created(net.fortuna.ical4j.model.property.Created) LastModified(net.fortuna.ical4j.model.property.LastModified) ExDate(net.fortuna.ical4j.model.property.ExDate) VEvent(net.fortuna.ical4j.model.component.VEvent) XProperty(net.fortuna.ical4j.model.property.XProperty) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) Contact(net.fortuna.ical4j.model.property.Contact) Uid(net.fortuna.ical4j.model.property.Uid) PropertyList(net.fortuna.ical4j.model.PropertyList) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) ParseException(java.text.ParseException) Location(net.fortuna.ical4j.model.property.Location) Recur(net.fortuna.ical4j.model.Recur)

Example 17 with Recur

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

the class ICalFileCalendarManager method getRecurrenceEndDate.

/**
 * @param rule
 * @return date of recurrence end
 */
@Override
public Date getRecurrenceEndDate(String rule) {
    if (rule != null) {
        try {
            TimeZone ltz = calendarModule.getDefaultTimeZone();
            Recur recur = new Recur(rule);
            Date dUntil = recur.getUntil();
            DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
            if (dtUntil != null) {
                if (ltz != null) {
                    dtUntil.setTimeZone(ltz);
                }
                return dtUntil;
            }
        } catch (ParseException e) {
            log.error("cannot restore recurrence rule", e);
        }
    }
    return null;
}
Also used : TimeZone(net.fortuna.ical4j.model.TimeZone) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) ParseException(java.text.ParseException) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) DateTime(net.fortuna.ical4j.model.DateTime) Recur(net.fortuna.ical4j.model.Recur)

Example 18 with Recur

use of net.fortuna.ical4j.model.Recur in project zm-mailbox by Zimbra.

the class RecurrenceDefinition method icalRecurrenceProperty.

/**
 * @param isAllDay
 * @param isFloating
 * @return The main recurrence property as ICAL - typically an RRULE but
 *         could theoretically be an X-MICROSOFT-RRULE
 * @throws ServiceException
 */
public Property icalRecurrenceProperty(boolean isAllDay, boolean isFloating) throws ServiceException {
    RRule theRule = null;
    if (this.calScale.isSolarCalendar() == false) {
        throw TNEFtoIcalendarServiceException.NON_SOLAR_CALENDAR();
    }
    // iCal4j Recur is a bit basic when it come to building things
    // up from components, for instance, there currently isn't a way
    // to set any BYDAY= value other than in the constructor from a String
    StringBuffer recurrenceRule = new StringBuffer("FREQ=");
    // According to RFC, only absolutely NEED WKST of have a
    // weekly recurrence with interval greater than 1 OR using
    // BYWEEKNO - however, does no harm to always include it.
    String weekStartDay = firstDayOfWeek.toString();
    boolean hasBYDAY = false;
    boolean isYearly = false;
    int interval = 1;
    switch(patternType) {
        case DAY:
            recurrenceRule.append(Recur.DAILY);
            interval = Long.valueOf(period).intValue() / 1440;
            break;
        case WEEK:
            recurrenceRule.append(Recur.WEEKLY);
            interval = Long.valueOf(period).intValue();
            hasBYDAY = true;
            break;
        case MONTH:
            interval = Long.valueOf(period).intValue();
            if ((interval % 12) == 0) {
                isYearly = true;
                recurrenceRule.append(Recur.YEARLY);
                interval = (interval / 12);
            } else {
                recurrenceRule.append(Recur.MONTHLY);
            }
            if (dayOfMonth != 0) {
                recurrenceRule.append(";BYMONTHDAY=");
                if (dayOfMonth == 31) {
                    recurrenceRule.append("-1");
                } else {
                    recurrenceRule.append(dayOfMonth);
                }
            }
            if (isYearly) {
                java.util.TimeZone javaTZ = null;
                if (tzDef != null) {
                    javaTZ = tzDef.getTimeZone();
                } else {
                    javaTZ = TimeZone.getTimeZone(TimeZones.UTC_ID);
                }
                Date bymonthDate = IcalUtil.localMinsSince1601toDate(firstDateTime, tzDef);
                Calendar bymonthCal = new GregorianCalendar(javaTZ);
                bymonthCal.setTimeInMillis(bymonthDate.getTime());
                String MONTH_ONLY_PATTERN = "MM";
                DateFormat monthOnlyFormat = new SimpleDateFormat(MONTH_ONLY_PATTERN);
                monthOnlyFormat.setCalendar(bymonthCal);
                recurrenceRule.append(";BYMONTH=");
                recurrenceRule.append(monthOnlyFormat.format(bymonthDate));
            }
            break;
        case MONTH_NTH:
            interval = Long.valueOf(period).intValue();
            if ((interval % 12) == 0) {
                isYearly = true;
                recurrenceRule.append(Recur.YEARLY);
                interval = (interval / 12);
            } else {
                recurrenceRule.append(Recur.MONTHLY);
            }
            hasBYDAY = true;
            recurrenceRule.append(";BYSETPOS=");
            if (weekDayOccurrenceNumber == 5) {
                recurrenceRule.append(-1);
            } else {
                recurrenceRule.append(weekDayOccurrenceNumber);
            }
            if (isYearly) {
                java.util.TimeZone javaTZ = null;
                if (tzDef != null) {
                    javaTZ = tzDef.getTimeZone();
                } else {
                    javaTZ = TimeZone.getTimeZone(TimeZones.UTC_ID);
                }
                Date bymonthDate = IcalUtil.localMinsSince1601toDate(firstDateTime, tzDef);
                Calendar bymonthCal = new GregorianCalendar(javaTZ);
                bymonthCal.setTimeInMillis(bymonthDate.getTime());
                String MONTH_ONLY_PATTERN = "MM";
                DateFormat monthOnlyFormat = new SimpleDateFormat(MONTH_ONLY_PATTERN);
                monthOnlyFormat.setCalendar(bymonthCal);
                recurrenceRule.append(";BYMONTH=");
                recurrenceRule.append(monthOnlyFormat.format(bymonthDate));
            }
            break;
        case MONTH_END:
        case HJ_MONTH:
        case HJ_MONTH_END:
        case HJ_MONTH_NTH:
            throw TNEFtoIcalendarServiceException.UNSUPPORTED_RECURRENCE_TYPE(patternType.name());
        default:
            throw TNEFtoIcalendarServiceException.UNSUPPORTED_RECURRENCE_TYPE(patternType.name());
    }
    if (recurrenceRule.length() > 5) /* length of "FREQ=" */
    {
        if (endType.equals(EndType.END_AFTER_N_OCCURRENCES)) {
            recurrenceRule.append(";COUNT=");
            recurrenceRule.append(occurrenceCount);
        } else if (endType.equals(EndType.END_BY_DATE)) {
            // MS-OXCICAL :
            // set to (EndDate + startTimeOffset), converted from the
            // time zone specified by PidLidTimeZoneStruct to the UTC time zone
            // From RFC 5545 :
            // The UNTIL rule part defines a DATE or DATE-TIME value that bounds
            // the recurrence rule in an inclusive manner.  If the value
            // specified by UNTIL is synchronized with the specified recurrence,
            // this DATE or DATE-TIME becomes the last instance of the
            // recurrence.  The value of the UNTIL rule part MUST have the same
            // value type as the "DTSTART" property.  Furthermore, if the
            // "DTSTART" property is specified as a date with local time, then
            // the UNTIL rule part MUST also be specified as a date with local
            // time [Gren - i.e. when DTSTART is a floating time].
            // If the "DTSTART" property is specified as a date with UTC
            // time or a date with local time and time zone reference, then the
            // UNTIL rule part MUST be specified as a date with UTC time.
            long minsSince1601 = this.endMinsSince1601 + this.startTimeOffset;
            DateTime untilDateTime = IcalUtil.localMinsSince1601toDate(minsSince1601, tzDef);
            recurrenceRule.append(";UNTIL=");
            java.util.TimeZone untilTZ = null;
            if (this.tzDef != null) {
                untilTZ = this.tzDef.getTimeZone();
            }
            if (isFloating) {
                // Use localtime.
                recurrenceRule.append(IcalUtil.iCalDateTimeValue(untilDateTime, untilTZ, isAllDay));
            } else {
                if (isAllDay) {
                    recurrenceRule.append(IcalUtil.iCalDateTimeValue(untilDateTime, untilTZ, isAllDay));
                } else {
                    // MUST be UTC time
                    recurrenceRule.append(IcalUtil.icalUtcTime(minsSince1601, tzDef));
                }
            }
        }
        if (hasBYDAY) {
            WeekDayList weekDayList = new WeekDayList();
            dayOfWeekMask = getDayOfWeekMask();
            for (DayOfWeek dow : dayOfWeekMask) {
                weekDayList.add(dow.iCal4JWeekDay());
            }
            if (!weekDayList.isEmpty()) {
                recurrenceRule.append(";BYDAY=");
                recurrenceRule.append(weekDayList);
            }
        }
        if (interval != 1) {
            recurrenceRule.append(";INTERVAL=");
            recurrenceRule.append(interval);
        }
        if (weekStartDay != null) {
            recurrenceRule.append(";WKST=");
            recurrenceRule.append(weekStartDay);
        }
        Recur recur;
        try {
            recur = new Recur(recurrenceRule.toString());
        } catch (ParseException ex) {
            throw TNEFtoIcalendarServiceException.RRULE_PARSING_PROBLEM(ex);
        }
        theRule = new RRule(recur);
    }
    return theRule;
}
Also used : RRule(net.fortuna.ical4j.model.property.RRule) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WeekDayList(net.fortuna.ical4j.model.WeekDayList) Date(net.fortuna.ical4j.model.Date) DateTime(net.fortuna.ical4j.model.DateTime) TimeZone(net.fortuna.ical4j.model.TimeZone) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Recur(net.fortuna.ical4j.model.Recur)

Example 19 with Recur

use of net.fortuna.ical4j.model.Recur 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 20 with Recur

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

the class ICalFileCalendarManager method getRecurrenceRule.

/**
 * Build iCalendar-compliant recurrence rule
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
@Override
public String getRecurrenceRule(String recurrence, Date recurrenceEnd) {
    if (recurrence != null) {
        // recurrence available
        // create recurrence rule
        StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(KalendarEvent.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(KalendarEvent.DAILY).append(";").append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(KalendarEvent.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(KalendarEvent.WEEKLY).append(";").append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            java.util.Calendar recurEndCal = java.util.Calendar.getInstance();
            recurEndCal.setTimeZone(tz);
            recurEndCal.setTime(recurrenceEnd);
            recurEndCal = CalendarUtils.getEndOfDay(recurEndCal);
            long recTime = recurEndCal.getTimeInMillis() - tz.getOffset(recurEndCal.getTimeInMillis());
            DateTime recurEndDT = new DateTime(recTime);
            if (tz != null) {
                recurEndDT.setTimeZone(tz);
            }
            sb.append(";").append(KalendarEvent.UNTIL).append("=").append(recurEndDT.toString());
        }
        try {
            Recur recur = new Recur(sb.toString());
            RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }
    return null;
}
Also used : RRule(net.fortuna.ical4j.model.property.RRule) ParseException(java.text.ParseException) DateTime(net.fortuna.ical4j.model.DateTime) Recur(net.fortuna.ical4j.model.Recur)

Aggregations

Recur (net.fortuna.ical4j.model.Recur)25 ParseException (java.text.ParseException)13 ExDate (net.fortuna.ical4j.model.property.ExDate)9 RRule (net.fortuna.ical4j.model.property.RRule)9 DateTime (net.fortuna.ical4j.model.DateTime)8 Date (java.util.Date)6 NumberList (net.fortuna.ical4j.model.NumberList)4 WeekDay (net.fortuna.ical4j.model.WeekDay)4 WeekDayList (net.fortuna.ical4j.model.WeekDayList)4 Date (net.fortuna.ical4j.model.Date)3 TimeZone (net.fortuna.ical4j.model.TimeZone)3 VEvent (net.fortuna.ical4j.model.component.VEvent)3 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)3 URISyntaxException (java.net.URISyntaxException)2 PropertyList (net.fortuna.ical4j.model.PropertyList)2 Contact (net.fortuna.ical4j.model.property.Contact)2 Created (net.fortuna.ical4j.model.property.Created)2 Description (net.fortuna.ical4j.model.property.Description)2 LastModified (net.fortuna.ical4j.model.property.LastModified)2 Location (net.fortuna.ical4j.model.property.Location)2