Search in sources :

Example 6 with RecurrenceId

use of net.fortuna.ical4j.model.property.RecurrenceId in project OpenOLAT by OpenOLAT.

the class CalendarImportTest method testImportRecurringCal.

@Test
public void testImportRecurringCal() throws IOException, ParserException {
    InputStream in = CalendarImportTest.class.getResourceAsStream("RecurringEvent.ics");
    CalendarBuilder builder = new CalendarBuilder();
    Calendar calendar = builder.build(in);
    assertNotNull(calendar);
    VEvent rootEvent = null;
    VEvent exceptionEvent = null;
    for (Iterator<?> iter = calendar.getComponents().iterator(); iter.hasNext(); ) {
        Object comp = iter.next();
        if (comp instanceof VEvent) {
            VEvent vevent = (VEvent) comp;
            if (vevent.getRecurrenceId() == null) {
                rootEvent = vevent;
            } else {
                exceptionEvent = vevent;
            }
        }
    }
    assertNotNull(rootEvent);
    assertNotNull(exceptionEvent);
    java.util.Date startDate = CalendarUtils.getDate(2016, java.util.Calendar.OCTOBER, 10);
    DateTime start = new DateTime(startDate);
    java.util.Date endDate = CalendarUtils.getDate(2016, java.util.Calendar.NOVEMBER, 10);
    DateTime end = new DateTime(endDate);
    Period period = new Period(start, end);
    PeriodList pList = rootEvent.calculateRecurrenceSet(period);
    for (Object obj : pList) {
        Period p = (Period) obj;
        System.out.println("Period: " + p.getStart());
    }
    RecurrenceId recurrenceId = exceptionEvent.getRecurrenceId();
    Date recurrenceDate = recurrenceId.getDate();
    System.out.println("Recurrence: " + recurrenceDate);
    exceptionEvent.getSequence();
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) InputStream(java.io.InputStream) Date(java.util.Date) Calendar(net.fortuna.ical4j.model.Calendar) Period(net.fortuna.ical4j.model.Period) PeriodList(net.fortuna.ical4j.model.PeriodList) DateTime(net.fortuna.ical4j.model.DateTime) Date(java.util.Date) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) Test(org.junit.Test)

Example 7 with RecurrenceId

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

the class ICalFileCalendarManager method getEvents.

@Override
public List<KalendarEvent> getEvents(Kalendar calendar, Date from, Date to, boolean privateEventsVisible) {
    List<KalendarEvent> allEvents = calendar.getEvents();
    List<KalendarEvent> events = new ArrayList<>(128);
    Map<String, List<KalendarRecurEvent>> idToRecurringEvents = new HashMap<>();
    // first pass, ignore events with recurrenceId
    for (KalendarEvent event : allEvents) {
        if (!privateEventsVisible && event.getClassification() == KalendarEvent.CLASS_PRIVATE) {
            continue;
        }
        if (StringHelper.containsNonWhitespace(event.getRecurrenceID())) {
            continue;
        }
        if (StringHelper.containsNonWhitespace(event.getRecurrenceRule())) {
            List<KalendarRecurEvent> recurringEvents = getRecurringEventsInPeriod(event, from, to, tz);
            if (recurringEvents.size() > 0) {
                idToRecurringEvents.put(event.getID(), recurringEvents);
                for (KalendarRecurEvent recurEvent : recurringEvents) {
                    events.add(recurEvent);
                }
            }
        } else if (isInRange(from, to, event)) {
            events.add(event);
        }
    }
    // process events with recurrenceId
    for (KalendarEvent event : allEvents) {
        if (!StringHelper.containsNonWhitespace(event.getRecurrenceID())) {
            continue;
        }
        String id = event.getID();
        if (idToRecurringEvents.containsKey(id)) {
            VEvent vEvent = getVEvent(event);
            RecurrenceId recurrenceId = vEvent.getRecurrenceId();
            net.fortuna.ical4j.model.Date startDate = recurrenceId.getDate();
            if (startDate instanceof net.fortuna.ical4j.model.DateTime) {
                List<KalendarRecurEvent> recurringEvents = idToRecurringEvents.get(id);
                for (KalendarRecurEvent recurEvent : recurringEvents) {
                    Date beginDate = recurEvent.getBegin();
                    if (beginDate.equals(startDate)) {
                        recurEvent.setRecurrenceEvent(event);
                    }
                }
            } else {
                List<KalendarRecurEvent> recurringEvents = idToRecurringEvents.get(id);
                for (KalendarRecurEvent recurEvent : recurringEvents) {
                    Date beginDate = recurEvent.getBegin();
                    net.fortuna.ical4j.model.Date occDate = CalendarUtils.createDate(beginDate);
                    if (occDate.equals(startDate)) {
                        recurEvent.setRecurrenceEvent(event);
                    }
                }
            }
        }
    }
    return events;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) DateTime(net.fortuna.ical4j.model.DateTime) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarRecurEvent(org.olat.commons.calendar.model.KalendarRecurEvent) PropertyList(net.fortuna.ical4j.model.PropertyList) List(java.util.List) DateList(net.fortuna.ical4j.model.DateList) ArrayList(java.util.ArrayList) PeriodList(net.fortuna.ical4j.model.PeriodList) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId)

Example 8 with RecurrenceId

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

the class ICalFileCalendarManager method createKalendarEventRecurringOccurence.

@Override
public KalendarEvent createKalendarEventRecurringOccurence(KalendarRecurEvent recurEvent) {
    KalendarEvent rootEvent = recurEvent.getCalendar().getEvent(recurEvent.getID(), null);
    VEvent vEvent = getVEvent(recurEvent);
    PropertyList vEventProperties = vEvent.getProperties();
    for (Iterator<?> objIt = vEventProperties.iterator(); objIt.hasNext(); ) {
        Object property = objIt.next();
        if (property instanceof RRule || property instanceof ExDate) {
            objIt.remove();
        }
    }
    try {
        Kalendar calendar = recurEvent.getCalendar();
        Date startDate = recurEvent.getOccurenceDate();
        String startString = CalendarUtils.formatRecurrenceDate(startDate, rootEvent.isAllDayEvent());
        RecurrenceId recurId;
        if (rootEvent.isAllDayEvent()) {
            recurId = new RecurrenceId(tz);
            recurId.setDate(CalendarUtils.createDate(startDate));
        } else {
            recurId = new RecurrenceId(startString, tz);
        }
        vEventProperties.add(recurId);
        KalendarEvent kEvent = getKalendarEvent(vEvent);
        kEvent.setKalendar(calendar);
        return kEvent;
    } catch (ParseException e) {
        log.error("", e);
        return null;
    }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) PropertyList(net.fortuna.ical4j.model.PropertyList) RRule(net.fortuna.ical4j.model.property.RRule) Kalendar(org.olat.commons.calendar.model.Kalendar) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) ParseException(java.text.ParseException) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate)

Example 9 with RecurrenceId

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

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 10 with RecurrenceId

use of net.fortuna.ical4j.model.property.RecurrenceId in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method createKalendarEventRecurringOccurence.

@Override
public KalendarEvent createKalendarEventRecurringOccurence(KalendarRecurEvent recurEvent) {
    KalendarEvent rootEvent = recurEvent.getCalendar().getEvent(recurEvent.getID(), null);
    VEvent vEvent = getVEvent(recurEvent);
    PropertyList vEventProperties = vEvent.getProperties();
    for (Iterator<?> objIt = vEventProperties.iterator(); objIt.hasNext(); ) {
        Object property = objIt.next();
        if (property instanceof RRule || property instanceof ExDate) {
            objIt.remove();
        }
    }
    try {
        Kalendar calendar = recurEvent.getCalendar();
        Date startDate = recurEvent.getOccurenceDate();
        String startString = CalendarUtils.formatRecurrenceDate(startDate, rootEvent.isAllDayEvent());
        RecurrenceId recurId;
        if (rootEvent.isAllDayEvent()) {
            recurId = new RecurrenceId(tz);
            recurId.setDate(CalendarUtils.createDate(startDate));
        } else {
            recurId = new RecurrenceId(startString, tz);
        }
        vEventProperties.add(recurId);
        KalendarEvent kEvent = getKalendarEvent(vEvent);
        kEvent.setKalendar(calendar);
        return kEvent;
    } catch (ParseException e) {
        log.error("", e);
        return null;
    }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) PropertyList(net.fortuna.ical4j.model.PropertyList) RRule(net.fortuna.ical4j.model.property.RRule) Kalendar(org.olat.commons.calendar.model.Kalendar) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) ParseException(java.text.ParseException) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate)

Aggregations

RecurrenceId (net.fortuna.ical4j.model.property.RecurrenceId)13 Date (java.util.Date)12 ExDate (net.fortuna.ical4j.model.property.ExDate)10 PropertyList (net.fortuna.ical4j.model.PropertyList)9 VEvent (net.fortuna.ical4j.model.component.VEvent)9 KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)8 DateTime (net.fortuna.ical4j.model.DateTime)7 ParseException (java.text.ParseException)6 ArrayList (java.util.ArrayList)6 Calendar (net.fortuna.ical4j.model.Calendar)5 PeriodList (net.fortuna.ical4j.model.PeriodList)5 Contact (net.fortuna.ical4j.model.property.Contact)5 Created (net.fortuna.ical4j.model.property.Created)5 Description (net.fortuna.ical4j.model.property.Description)5 LastModified (net.fortuna.ical4j.model.property.LastModified)5 Location (net.fortuna.ical4j.model.property.Location)5 Uid (net.fortuna.ical4j.model.property.Uid)5 RRule (net.fortuna.ical4j.model.property.RRule)4 XProperty (net.fortuna.ical4j.model.property.XProperty)4 Kalendar (org.olat.commons.calendar.model.Kalendar)4