Search in sources :

Example 21 with CalendarGUIModifiedEvent

use of org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent in project openolat by klemens.

the class ICalFileCalendarManager method updateEventsFrom.

@Override
public boolean updateEventsFrom(Kalendar cal, List<KalendarEvent> kalendarEvents) {
    final OLATResourceable calOres = getOresHelperFor(cal);
    Boolean updatedSuccessful = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(calOres, new SyncerCallback<Boolean>() {

        @Override
        public Boolean execute() {
            Kalendar loadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
            for (KalendarEvent kalendarEvent : kalendarEvents) {
                // remove old event
                loadedCal.removeEvent(kalendarEvent);
                // add changed event
                loadedCal.addEvent(kalendarEvent);
            }
            boolean successfullyPersist = persistCalendar(loadedCal);
            // inform all controller about calendar change for reload
            CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(cal), OresHelper.lookupType(CalendarManager.class));
            return successfullyPersist;
        }
    });
    return updatedSuccessful.booleanValue();
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) OLATResourceable(org.olat.core.id.OLATResourceable) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) CalendarGUIModifiedEvent(org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)

Example 22 with CalendarGUIModifiedEvent

use of org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent 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 23 with CalendarGUIModifiedEvent

use of org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent in project openolat by klemens.

the class ICalFileCalendarManager method removeFutureOfEvent.

@Override
public boolean removeFutureOfEvent(Kalendar cal, KalendarRecurEvent kalendarEvent) {
    OLATResourceable calOres = getOresHelperFor(cal);
    Boolean removeSuccessful = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(calOres, new SyncerCallback<Boolean>() {

        @Override
        public Boolean execute() {
            boolean successfullyPersist = false;
            try {
                String uid = kalendarEvent.getID();
                Date occurenceDate = kalendarEvent.getOccurenceDate();
                Kalendar loadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
                KalendarEvent rootEvent = loadedCal.getEvent(kalendarEvent.getID(), null);
                String rRule = rootEvent.getRecurrenceRule();
                Recur recur = new Recur(rRule);
                recur.setUntil(CalendarUtils.createDate(occurenceDate));
                RRule rrule = new RRule(recur);
                rootEvent.setRecurrenceRule(rrule.getValue());
                for (KalendarEvent kEvent : loadedCal.getEvents()) {
                    if (uid.equals(kEvent.getID()) && StringHelper.containsNonWhitespace(kEvent.getRecurrenceID()) && occurenceDate.before(kEvent.getBegin())) {
                        loadedCal.removeEvent(kEvent);
                    }
                }
                successfullyPersist = persistCalendar(loadedCal);
            } catch (ParseException e) {
                log.error("", e);
            }
            return new Boolean(successfullyPersist);
        }
    });
    // inform all controller about calendar change for reload
    CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(cal), OresHelper.lookupType(CalendarManager.class));
    return removeSuccessful.booleanValue();
}
Also used : RRule(net.fortuna.ical4j.model.property.RRule) OLATResourceable(org.olat.core.id.OLATResourceable) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) CalendarManager(org.olat.commons.calendar.CalendarManager) Kalendar(org.olat.commons.calendar.model.Kalendar) ParseException(java.text.ParseException) CalendarGUIModifiedEvent(org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent) Recur(net.fortuna.ical4j.model.Recur)

Example 24 with CalendarGUIModifiedEvent

use of org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent in project openolat by klemens.

the class ICalFileCalendarManager method removeEventFrom.

/**
 * @see org.olat.commons.calendar.CalendarManager#removeEventFrom(org.olat.commons.calendar.model.Kalendar, org.olat.commons.calendar.model.KalendarEvent)
 */
@Override
public boolean removeEventFrom(final Kalendar cal, final KalendarEvent kalendarEvent) {
    OLATResourceable calOres = getOresHelperFor(cal);
    Boolean removeSuccessful = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(calOres, new SyncerCallback<Boolean>() {

        @Override
        public Boolean execute() {
            String uid = kalendarEvent.getID();
            String recurrenceId = kalendarEvent.getRecurrenceID();
            Kalendar loadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
            if (StringHelper.containsNonWhitespace(recurrenceId)) {
                loadedCal.removeEvent(kalendarEvent);
                KalendarEvent rootEvent = loadedCal.getEvent(kalendarEvent.getID(), null);
                if (rootEvent != null && kalendarEvent instanceof KalendarRecurEvent) {
                    Date recurrenceDate = ((KalendarRecurEvent) kalendarEvent).getOccurenceDate();
                    rootEvent.addRecurrenceExc(recurrenceDate);
                }
            } else {
                for (KalendarEvent kEvent : loadedCal.getEvents()) {
                    if (uid.equals(kEvent.getID())) {
                        loadedCal.removeEvent(kEvent);
                    }
                }
            }
            boolean successfullyPersist = persistCalendar(loadedCal);
            return new Boolean(successfullyPersist);
        }
    });
    // inform all controller about calendar change for reload
    CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(cal), OresHelper.lookupType(CalendarManager.class));
    return removeSuccessful.booleanValue();
}
Also used : CalendarManager(org.olat.commons.calendar.CalendarManager) Kalendar(org.olat.commons.calendar.model.Kalendar) OLATResourceable(org.olat.core.id.OLATResourceable) KalendarRecurEvent(org.olat.commons.calendar.model.KalendarRecurEvent) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) CalendarGUIModifiedEvent(org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)

Example 25 with CalendarGUIModifiedEvent

use of org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent in project openolat by klemens.

the class ICalFileCalendarManager method updateCalendar.

@Override
public boolean updateCalendar(final Kalendar cal, final Kalendar importedCal) {
    OLATResourceable calOres = getOresHelperFor(cal);
    Boolean updatedSuccessful = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(calOres, new SyncerCallback<Boolean>() {

        @Override
        public Boolean execute() {
            Map<KalendarEventKey, KalendarEvent> uidToEvent = new HashMap<>();
            for (KalendarEvent event : cal.getEvents()) {
                if (StringHelper.containsNonWhitespace(event.getID())) {
                    uidToEvent.put(new KalendarEventKey(event), event);
                }
            }
            Kalendar loadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
            for (KalendarEvent importedEvent : importedCal.getEvents()) {
                KalendarEventKey uid = new KalendarEventKey(importedEvent);
                if (uidToEvent.containsKey(uid)) {
                    // remove old event
                    loadedCal.removeEvent(importedEvent);
                    // add changed event
                    loadedCal.addEvent(importedEvent);
                } else {
                    loadedCal.addEvent(importedEvent);
                }
            }
            boolean successfullyPersist = persistCalendar(cal);
            // inform all controller about calendar change for reload
            CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(cal), OresHelper.lookupType(CalendarManager.class));
            return new Boolean(successfullyPersist);
        }
    });
    return updatedSuccessful.booleanValue();
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) OLATResourceable(org.olat.core.id.OLATResourceable) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) KalendarEventKey(org.olat.commons.calendar.model.KalendarEventKey) Map(java.util.Map) HashMap(java.util.HashMap) CalendarGUIModifiedEvent(org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)

Aggregations

CalendarGUIModifiedEvent (org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)26 Kalendar (org.olat.commons.calendar.model.Kalendar)18 KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)18 OLATResourceable (org.olat.core.id.OLATResourceable)18 CalendarManager (org.olat.commons.calendar.CalendarManager)14 Date (java.util.Date)8 ExDate (net.fortuna.ical4j.model.property.ExDate)8 ParseException (java.text.ParseException)6 KalendarRenderWrapper (org.olat.commons.calendar.ui.components.KalendarRenderWrapper)4 BufferedReader (java.io.BufferedReader)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStreamReader (java.io.InputStreamReader)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CalendarBuilder (net.fortuna.ical4j.data.CalendarBuilder)2 Calendar (net.fortuna.ical4j.model.Calendar)2 Recur (net.fortuna.ical4j.model.Recur)2