Search in sources :

Example 71 with Kalendar

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

the class ICalFileCalendarManagerTest method testAddChangeEvent_v2.

@Test
public void testAddChangeEvent_v2() {
    Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-1-");
    String TEST_EVENT_ID = "id-testAddEvent";
    Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
    // 1. Test Add Event
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    Date start = calendar.getTime();
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date end = calendar.getTime();
    KalendarEvent testEvent = new KalendarEvent(TEST_EVENT_ID, null, "testEvent", start, end);
    calendarManager.addEventTo(cal, testEvent);
    // empty the cache
    emptyCalendarCache();
    Kalendar reloadedCal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent reloadedEvent = reloadedCal.getEvent(TEST_EVENT_ID, null);
    Assert.assertNotNull("Could not found added event", reloadedEvent);
    Assert.assertEquals("Added event has wrong subject", testEvent.getSubject(), reloadedEvent.getSubject());
    Assert.assertEquals(reloadedEvent.getBegin(), start);
    Assert.assertEquals(reloadedEvent.getEnd(), end);
    // 2. Test Change event
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date updatedEnd = calendar.getTime();
    calendar.add(Calendar.HOUR_OF_DAY, -4);
    Date updatedStart = calendar.getTime();
    reloadedEvent.setSubject("testEvent changed");
    reloadedEvent.setBegin(updatedStart);
    reloadedEvent.setEnd(updatedEnd);
    calendarManager.updateEventFrom(cal, reloadedEvent);
    // empty the cache
    emptyCalendarCache();
    Kalendar updatedCal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent updatedEvent = updatedCal.getEvent(TEST_EVENT_ID, null);
    Assert.assertNotNull("Could not found updated event", updatedEvent);
    Assert.assertEquals("Added event has wrong subject", "testEvent changed", updatedEvent.getSubject());
    Assert.assertEquals(updatedStart, updatedEvent.getBegin());
    Assert.assertEquals(updatedEnd, updatedEvent.getEnd());
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) Calendar(java.util.Calendar) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Identity(org.olat.core.id.Identity) Date(java.util.Date) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Example 72 with Kalendar

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

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 73 with Kalendar

use of org.olat.commons.calendar.model.Kalendar 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)

Example 74 with Kalendar

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

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 75 with Kalendar

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

the class ICalFileCalendarManager method getCalendar.

@Override
public Kalendar getCalendar(final String type, final String calendarID) {
    String key = getKeyFor(type, calendarID);
    Kalendar cal = calendarCache.get(key);
    if (cal == null) {
        cal = getCalendarFromCache(type, calendarID);
    }
    return cal;
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar)

Aggregations

Kalendar (org.olat.commons.calendar.model.Kalendar)126 KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)68 Identity (org.olat.core.id.Identity)40 Date (java.util.Date)38 Test (org.junit.Test)26 CalendarManager (org.olat.commons.calendar.CalendarManager)24 KalendarRenderWrapper (org.olat.commons.calendar.ui.components.KalendarRenderWrapper)22 ArrayList (java.util.ArrayList)20 CalendarImportTest (org.olat.commons.calendar.CalendarImportTest)20 OLATResourceable (org.olat.core.id.OLATResourceable)20 CalendarGUIModifiedEvent (org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)18 Calendar (java.util.Calendar)14 CalendarUserConfiguration (org.olat.commons.calendar.model.CalendarUserConfiguration)12 URISyntaxException (java.net.URISyntaxException)10 ParseException (java.text.ParseException)10 ExDate (net.fortuna.ical4j.model.property.ExDate)10 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 ICourse (org.olat.course.ICourse)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8