Search in sources :

Example 26 with CalendarBuilder

use of net.fortuna.ical4j.data.CalendarBuilder in project openolat by klemens.

the class ICalFileCalendarManager method buildKalendarFrom.

@Override
public Kalendar buildKalendarFrom(InputStream in, String calType, String calId) {
    Kalendar kalendar = null;
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
        CalendarBuilder builder = new CalendarBuilder();
        Calendar calendar = builder.build(reader);
        kalendar = createKalendar(calType, calId, calendar);
    } catch (Exception e) {
        throw new OLATRuntimeException("Error parsing calendar file.", e);
    }
    return kalendar;
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) InputStreamReader(java.io.InputStreamReader) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Calendar(net.fortuna.ical4j.model.Calendar) BufferedReader(java.io.BufferedReader) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 27 with CalendarBuilder

use of net.fortuna.ical4j.data.CalendarBuilder in project openolat by klemens.

the class ICalFileCalendarManager method synchronizeCalendarFrom.

@Override
public boolean synchronizeCalendarFrom(InputStream in, String source, Kalendar targetCalendar) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
        Calendar inCalendar = new CalendarBuilder().build(reader);
        Kalendar inTmpKalendar = createKalendar("TEMP", UUID.randomUUID().toString(), inCalendar);
        String targetId = "-" + targetCalendar.getType() + "-" + targetCalendar.getCalendarID() + "-";
        OLATResourceable calOres = getOresHelperFor(targetCalendar);
        Boolean updatedSuccessful = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(calOres, new SyncerCallback<Boolean>() {

            @Override
            public Boolean execute() {
                // remove event in target calendar which doesn't exist in stream
                Collection<KalendarEvent> currentEvents = targetCalendar.getEvents();
                for (KalendarEvent currentEvent : currentEvents) {
                    if (currentEvent.getExternalSource() != null && source.equals(currentEvent.getExternalSource())) {
                        String eventId = currentEvent.getID();
                        String recurrenceId = currentEvent.getRecurrenceID();
                        if (inTmpKalendar.getEvent(eventId, recurrenceId) == null) {
                            targetCalendar.removeEvent(currentEvent);
                        } else if (eventId.contains(targetId)) {
                            // don't import myself;
                            targetCalendar.removeEvent(currentEvent);
                        }
                    }
                }
                Collection<KalendarEvent> inEvents = inTmpKalendar.getEvents();
                for (KalendarEvent inEvent : inEvents) {
                    if (inEvent.getID().contains(targetId)) {
                        continue;
                    }
                    inEvent.setManagedFlags(new CalendarManagedFlag[] { CalendarManagedFlag.all });
                    inEvent.setExternalSource(source);
                    KalendarEvent currentEvent = targetCalendar.getEvent(inEvent.getID(), inEvent.getRecurrenceID());
                    if (currentEvent == null) {
                        targetCalendar.addEvent(inEvent);
                    } else {
                        // need perhaps more refined synchronization per event
                        targetCalendar.addEvent(inEvent);
                    }
                }
                boolean successfullyPersist = persistCalendar(targetCalendar);
                // inform all controller about calendar change for reload
                CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(targetCalendar), OresHelper.lookupType(CalendarManager.class));
                return new Boolean(successfullyPersist);
            }
        });
        return updatedSuccessful.booleanValue();
    } catch (Exception e) {
        log.error("", e);
        return false;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) OLATResourceable(org.olat.core.id.OLATResourceable) Calendar(net.fortuna.ical4j.model.Calendar) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Kalendar(org.olat.commons.calendar.model.Kalendar) BufferedReader(java.io.BufferedReader) CalendarManagedFlag(org.olat.commons.calendar.CalendarManagedFlag) Collection(java.util.Collection) CalendarGUIModifiedEvent(org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)

Example 28 with CalendarBuilder

use of net.fortuna.ical4j.data.CalendarBuilder in project openolat by klemens.

the class ICalServlet method getOutlookVTimeZone.

/**
 * Load the VTimeZone for Outlook. ical4j use a static map to reuse the TimeZone objects, we need to load
 * and save our specialized TimeZone in a separate map.
 */
private VTimeZone getOutlookVTimeZone(final String id) throws IOException, ParserException {
    return outlookVTimeZones.computeIfAbsent(id, (timeZoneId) -> {
        try {
            URL resource = ResourceLoader.getResource("zoneinfo-outlook/" + id + ".ics");
            CalendarBuilder builder = new CalendarBuilder();
            Calendar calendar = builder.build(resource.openStream());
            VTimeZone vTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE);
            return vTimeZone;
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    });
}
Also used : CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) Calendar(net.fortuna.ical4j.model.Calendar) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) URL(java.net.URL) ValidationException(net.fortuna.ical4j.model.ValidationException) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) ParserException(net.fortuna.ical4j.data.ParserException) IOException(java.io.IOException)

Aggregations

CalendarBuilder (net.fortuna.ical4j.data.CalendarBuilder)28 Calendar (net.fortuna.ical4j.model.Calendar)25 Test (org.junit.Test)15 InputStream (java.io.InputStream)14 URISyntaxException (java.net.URISyntaxException)7 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)6 BufferedReader (java.io.BufferedReader)5 IOException (java.io.IOException)5 InputStreamReader (java.io.InputStreamReader)5 ParserException (net.fortuna.ical4j.data.ParserException)5 FileNotFoundException (java.io.FileNotFoundException)4 ParseException (java.text.ParseException)4 Property (net.fortuna.ical4j.model.Property)4 VEvent (net.fortuna.ical4j.model.component.VEvent)4 Kalendar (org.olat.commons.calendar.model.Kalendar)4 Date (java.util.Date)3 Component (net.fortuna.ical4j.model.Component)3 DateTime (net.fortuna.ical4j.model.DateTime)3 Period (net.fortuna.ical4j.model.Period)3 PeriodList (net.fortuna.ical4j.model.PeriodList)3