Search in sources :

Example 66 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method updateEventAlreadyInSync.

/**
 * @see org.olat.commons.calendar.CalendarManager#updateEventFrom(org.olat.commons.calendar.model.Kalendar, org.olat.commons.calendar.model.KalendarEvent)
 */
@Override
public boolean updateEventAlreadyInSync(final Kalendar cal, final KalendarEvent kalendarEvent) {
    OLATResourceable calOres = getOresHelperFor(cal);
    CoordinatorManager.getInstance().getCoordinator().getSyncer().assertAlreadyDoInSyncFor(calOres);
    Kalendar reloadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
    if (StringHelper.containsNonWhitespace(kalendarEvent.getRecurrenceRule())) {
        Date oldBegin = kalendarEvent.getImmutableBegin();
        Date oldEnd = kalendarEvent.getImmutableEnd();
        KalendarEvent originalEvent = reloadedCal.getEvent(kalendarEvent.getID(), null);
        Date newBegin = kalendarEvent.getBegin();
        Date newEnd = kalendarEvent.getEnd();
        long beginDiff = newBegin.getTime() - oldBegin.getTime();
        long endDiff = newEnd.getTime() - oldEnd.getTime();
        java.util.Calendar cl = java.util.Calendar.getInstance();
        cl.setTime(originalEvent.getBegin());
        cl.add(java.util.Calendar.MILLISECOND, (int) beginDiff);
        kalendarEvent.setBegin(cl.getTime());
        cl.setTime(originalEvent.getEnd());
        cl.add(java.util.Calendar.MILLISECOND, (int) endDiff);
        kalendarEvent.setEnd(cl.getTime());
        List<KalendarEvent> exEvents = new ArrayList<>();
        List<KalendarEvent> allEvents = reloadedCal.getEvents();
        for (KalendarEvent event : allEvents) {
            if (event.getID().equals(kalendarEvent.getID()) && StringHelper.containsNonWhitespace(event.getRecurrenceID())) {
                exEvents.add(event);
            }
        }
        if (exEvents.size() > 0) {
            for (KalendarEvent exEvent : exEvents) {
                try {
                    reloadedCal.removeEvent(exEvent);
                    String recurrenceId = exEvent.getRecurrenceID();
                    RecurrenceId recurId = new RecurrenceId(recurrenceId, tz);
                    Date currentRecurrence = recurId.getDate();
                    java.util.Calendar calc = java.util.Calendar.getInstance();
                    calc.clear();
                    calc.setTime(currentRecurrence);
                    if (beginDiff > 0) {
                        calc.add(java.util.Calendar.MILLISECOND, (int) beginDiff);
                    }
                    Date newRecurrenceDate = calc.getTime();
                    boolean allDay = kalendarEvent.isAllDayEvent();
                    RecurrenceId newRecurId;
                    if (allDay) {
                        newRecurId = new RecurrenceId(tz);
                        newRecurId.setDate(CalendarUtils.createDate(newRecurrenceDate));
                    } else {
                        String startString = CalendarUtils.formatRecurrenceDate(newRecurrenceDate, false);
                        newRecurId = new RecurrenceId(startString, tz);
                    }
                    exEvent.setRecurrenceID(newRecurId.getValue());
                    reloadedCal.addEvent(exEvent);
                } catch (ParseException e) {
                    log.error("", e);
                }
            }
        }
    }
    // remove old event
    reloadedCal.removeEvent(kalendarEvent);
    kalendarEvent.resetImmutableDates();
    // add changed event
    reloadedCal.addEvent(kalendarEvent);
    boolean successfullyPersist = persistCalendar(reloadedCal);
    // inform all controller about calendar change for reload
    CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(cal), OresHelper.lookupType(CalendarManager.class));
    return successfullyPersist;
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) 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) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) ParseException(java.text.ParseException) CalendarGUIModifiedEvent(org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)

Example 67 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method removeOccurenceOfEvent.

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

        @Override
        public Boolean execute() {
            String uid = kalendarEvent.getID();
            Date occurenceDate = kalendarEvent.getBegin();
            Kalendar loadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
            KalendarEvent rootEvent = loadedCal.getEvent(kalendarEvent.getID(), null);
            rootEvent.addRecurrenceExc(kalendarEvent.getBegin());
            for (KalendarEvent kEvent : loadedCal.getEvents()) {
                if (uid.equals(kEvent.getID()) && kEvent.getOccurenceDate() != null && occurenceDate.equals(kEvent.getOccurenceDate())) {
                    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) 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 68 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

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)

Example 69 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method createKalendar.

protected Kalendar createKalendar(String type, String calendarID, Calendar calendar) {
    Kalendar cal = new Kalendar(calendarID, type);
    for (Iterator<?> iter = calendar.getComponents().iterator(); iter.hasNext(); ) {
        Object comp = iter.next();
        if (comp instanceof VEvent) {
            VEvent vevent = (VEvent) comp;
            KalendarEvent calEvent = getKalendarEvent(vevent);
            cal.addEvent(calEvent);
        } else if (comp instanceof VTimeZone) {
            log.info("createKalendar: VTimeZone Component is not supported and will not be added to calender");
            log.debug("createKalendar: VTimeZone=" + comp);
        } else {
            log.warn("createKalendar: unknown Component=" + comp);
        }
    }
    return cal;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) Kalendar(org.olat.commons.calendar.model.Kalendar) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent)

Example 70 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.

the class CalendarTest method testPutPersonalCalendarEvents.

@Test
public void testPutPersonalCalendarEvents() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id2.getName(), "A6B7C8"));
    URI calUri = UriBuilder.fromUri(getContextURI()).path("users").path(id2.getKey().toString()).path("calendars").build();
    HttpGet calMethod = conn.createGet(calUri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(calMethod);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<CalendarVO> vos = parseArray(response);
    assertNotNull(vos);
    assertTrue(2 <= vos.size());
    CalendarVO calendar = getUserCalendar(vos);
    // create an event
    EventVO event = new EventVO();
    Calendar cal = Calendar.getInstance();
    event.setBegin(cal.getTime());
    cal.add(Calendar.HOUR_OF_DAY, 1);
    event.setEnd(cal.getTime());
    String subject = UUID.randomUUID().toString();
    event.setSubject(subject);
    URI eventUri = UriBuilder.fromUri(getContextURI()).path("users").path(id2.getKey().toString()).path("calendars").path(calendar.getId()).path("event").build();
    HttpPut putEventMethod = conn.createPut(eventUri, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(putEventMethod, event);
    HttpResponse putEventResponse = conn.execute(putEventMethod);
    assertEquals(200, putEventResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(putEventResponse.getEntity());
    // check if the event is saved
    KalendarRenderWrapper calendarWrapper = calendarManager.getPersonalCalendar(id2);
    Collection<KalendarEvent> savedEvents = calendarWrapper.getKalendar().getEvents();
    boolean found = false;
    for (KalendarEvent savedEvent : savedEvents) {
        if (subject.equals(savedEvent.getSubject())) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    conn.shutdown();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) Calendar(java.util.Calendar) CalendarVO(org.olat.commons.calendar.restapi.CalendarVO) HttpResponse(org.apache.http.HttpResponse) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) KalendarRenderWrapper(org.olat.commons.calendar.ui.components.KalendarRenderWrapper) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) EventVO(org.olat.commons.calendar.restapi.EventVO) Test(org.junit.Test)

Aggregations

KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)172 Date (java.util.Date)80 Kalendar (org.olat.commons.calendar.model.Kalendar)68 Test (org.junit.Test)48 KalendarRenderWrapper (org.olat.commons.calendar.ui.components.KalendarRenderWrapper)46 Identity (org.olat.core.id.Identity)46 ArrayList (java.util.ArrayList)42 Calendar (java.util.Calendar)40 CalendarImportTest (org.olat.commons.calendar.CalendarImportTest)34 CalendarManager (org.olat.commons.calendar.CalendarManager)32 OLATResourceable (org.olat.core.id.OLATResourceable)20 CalendarGUIModifiedEvent (org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)18 File (java.io.File)16 ExDate (net.fortuna.ical4j.model.property.ExDate)16 ICourse (org.olat.course.ICourse)16 URI (java.net.URI)14 HttpResponse (org.apache.http.HttpResponse)14 URL (java.net.URL)12 KalendarRecurEvent (org.olat.commons.calendar.model.KalendarRecurEvent)12 EventVO (org.olat.commons.calendar.restapi.EventVO)12