Search in sources :

Example 26 with Kalendar

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

the class OLATUpgrade_10_4_0 method processKalendarConfig.

private void processKalendarConfig(Identity identity, String calendarId, KalendarConfig config) {
    Kalendar cal;
    CalendarUserConfiguration userConfig;
    if (StringHelper.isLong(calendarId)) {
        // guess if it's a course or a group calendar
        if (calendarMgr.getCalendarFile(CalendarManager.TYPE_COURSE, calendarId) != null) {
            cal = new Kalendar(calendarId, CalendarManager.TYPE_COURSE);
        } else if (calendarMgr.getCalendarFile(CalendarManager.TYPE_GROUP, calendarId) != null) {
            cal = new Kalendar(calendarId, CalendarManager.TYPE_GROUP);
        } else {
            return;
        }
    } else {
        // personal calendar
        cal = new Kalendar(calendarId, CalendarManager.TYPE_USER);
    }
    userConfig = calendarUserConfigurationDao.getCalendarUserConfiguration(identity, cal.getCalendarID(), cal.getType());
    if (userConfig == null) {
        userConfig = calendarUserConfigurationDao.createCalendarUserConfiguration(cal, identity);
        userConfig.setCssClass(config.getCss());
        userConfig.setVisible(config.isVis());
        userConfig = calendarUserConfigurationDao.update(userConfig);
    }
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) CalendarUserConfiguration(org.olat.commons.calendar.model.CalendarUserConfiguration)

Example 27 with Kalendar

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

the class ICalFileCalendarManagerTest method testListEventsForPeriod.

@Test
public void testListEventsForPeriod() {
    final int numEvents = 10000;
    // maximum of 14 days duration
    final int maxEventDuratio = 1000 * 60 * 60 * 24 * 14;
    final int oneYearSec = 60 * 60 * 24 * 365;
    final int goBackNumYears = 1;
    final long kalendarStart = new Date().getTime() - (((long) goBackNumYears * oneYearSec) * 1000);
    Identity test = JunitTestHelper.createAndPersistIdentityAsUser("test");
    Kalendar kalendar = new Kalendar("test", CalendarManager.TYPE_USER);
    log.info("*** Starting test with the following configuration:");
    log.info("*** Number of events: " + numEvents);
    log.info("*** Maximum event duration (ms): " + maxEventDuratio);
    log.info("*** Generate events in between " + new Date(kalendarStart) + " and " + new Date(kalendarStart + (1000 * ((long) goBackNumYears * oneYearSec))));
    Random rand = new Random();
    long startUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    for (int i = 0; i < numEvents; i++) {
        long begin = kalendarStart + (1000 * ((long) rand.nextInt(goBackNumYears * oneYearSec)));
        String eventId = "id" + i;
        KalendarEvent event = new KalendarEvent(eventId, "test" + i, new Date(begin), rand.nextInt(maxEventDuratio));
        kalendar.addEvent(event);
    }
    long stopUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    log.info("*** SETUP: Kalendar structure uses approx. " + (stopUsed - startUsed) / 1024 + " kb memory.");
    calendarManager.persistCalendar(kalendar);
    log.info("*** Load calendar...");
    long start = System.currentTimeMillis();
    calendarManager.getPersonalCalendar(test).getKalendar();
    long stop = System.currentTimeMillis();
    log.info("Duration load: " + (stop - start) + " ms.");
    log.info("*** Find events within period...");
    start = System.currentTimeMillis();
    List<KalendarEvent> events = calendarManager.getEvents(kalendar, new Date(kalendarStart), new Date(kalendarStart + (1000 * ((long) (goBackNumYears * oneYearSec)))), true);
    stop = System.currentTimeMillis();
    log.info("Duration find: " + (stop - start) + " ms.");
    log.info("Found " + events.size() + " events out of " + kalendar.getEvents().size() + " total events.");
    assertEquals(kalendar.getEvents().size(), events.size());
    log.info("*** Save calendar...");
    start = System.currentTimeMillis();
    calendarManager.persistCalendar(kalendar);
    stop = System.currentTimeMillis();
    log.info("Duration save: " + (stop - start) + " ms.");
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) Random(java.util.Random) 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 28 with Kalendar

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

the class ICalFileCalendarManagerTest method testConcurrentAddRemoveEvent.

/**
 * Test concurrent add/delete event with two threads and code-point to control concurrency.
 */
@Test
public void testConcurrentAddRemoveEvent() {
    final String TEST_EVENT_ID_1 = "id-testConcurrentAddRemoveEvent-1";
    final String TEST_EVENT_SUBJECT_1 = "testEvent1";
    final String TEST_EVENT_ID_2 = "id-testConcurrentAddRemoveEvent-2";
    final String TEST_EVENT_SUBJECT_2 = "testEvent2";
    final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-1-");
    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
    // Generate event for update
    Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
    calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
    cal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2, null);
    assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
    assertEquals("Wrong calendar-event subject", event2.getSubject(), TEST_EVENT_SUBJECT_2);
    log.info("testConcurrentAddRemoveEvent thread2 addEvent2 DONE");
    final CountDownLatch doneSignal = new CountDownLatch(2);
    // thread 1
    Thread thread1 = new Thread() {

        public void run() {
            try {
                // 1. load calendar
                Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                // 2. add Event1 => breakpoint hit
                log.info("testConcurrentAddRemoveEvent thread1 addEvent1");
                calendarManager.addEventTo(calendar, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
                log.info("testConcurrentAddRemoveEvent thread1 addEvent1 DONE");
                // 3. check event1 exist
                calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                KalendarEvent event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
                assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
                // 4. sleep 2sec
                // 5. check event1 still exist (event2 added in meantime)
                calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
                assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
                statusList.add(Boolean.TRUE);
                log.info("testConcurrentAddRemoveEvent thread1 finished");
            } catch (Exception ex) {
                // no exception should happen
                exceptionHolder.add(ex);
            } finally {
                doneSignal.countDown();
                DBFactory.getInstance().commitAndCloseSession();
            }
        }
    };
    // thread 2
    Thread thread2 = new Thread() {

        public void run() {
            try {
                Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                // 3. add Event2 (breakpoint of thread1 blocks)
                log.info("testConcurrentAddRemoveEvent thread2 removeEvent2");
                boolean removed = calendarManager.removeEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
                assertTrue(removed);
                log.info("testConcurrentAddRemoveEvent thread1 removeEvent2 DONE");
                // 4. check event2 exist
                calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                KalendarEvent updatedEvent = calendar.getEvent(TEST_EVENT_ID_2, null);
                assertNull("Still found deleted event with id=" + TEST_EVENT_ID_2, updatedEvent);
                // 5. check event1 exist
                calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                KalendarEvent event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
                assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
                statusList.add(Boolean.TRUE);
                log.info("testConcurrentAddRemoveEvent thread2 finished");
            } catch (Exception ex) {
                // no exception should happen
                exceptionHolder.add(ex);
            } finally {
                doneSignal.countDown();
                DBFactory.getInstance().commitAndCloseSession();
            }
        }
    };
    thread1.start();
    thread2.start();
    try {
        boolean interrupt = doneSignal.await(10, TimeUnit.SECONDS);
        assertTrue("Test takes too long (more than 10s)", interrupt);
    } catch (InterruptedException e) {
        fail("" + e.getMessage());
    }
    // if not -> they are in deadlock and the db did not detect it
    for (Exception exception : exceptionHolder) {
        log.info("exception: " + exception.getMessage());
        exception.printStackTrace();
    }
    assertTrue("It throws an exception in test => see sysout", exceptionHolder.isEmpty());
    log.info("testConcurrentAddRemoveEvent finish successful");
}
Also used : KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ValidationException(net.fortuna.ical4j.model.ValidationException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Date(java.util.Date) Kalendar(org.olat.commons.calendar.model.Kalendar) Identity(org.olat.core.id.Identity) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Example 29 with Kalendar

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

the class ICalFileCalendarManagerTest method testWithinOneDay.

@Test
public void testWithinOneDay() throws IOException {
    Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-4-");
    Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
    // 1. Test Today Event
    String eventId = "short-" + UUID.randomUUID();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 14);
    Date start = calendar.getTime();
    calendar.set(Calendar.HOUR_OF_DAY, 15);
    Date end = calendar.getTime();
    KalendarEvent testEvent = new KalendarEvent(eventId, null, "Short Event", start, end);
    calendarManager.addEventTo(cal, testEvent);
    // Next days event
    String nextEventId = "long-" + UUID.randomUUID();
    calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.add(Calendar.DATE, 3);
    calendar.set(Calendar.HOUR, 8);
    Date nextStart = calendar.getTime();
    calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.add(Calendar.DATE, 6);
    calendar.set(Calendar.HOUR_OF_DAY, 18);
    Date nextEnd = calendar.getTime();
    KalendarEvent nextEvent = new KalendarEvent(nextEventId, null, "Long Event", nextStart, nextEnd);
    calendarManager.addEventTo(cal, nextEvent);
    // 2. reload and test
    emptyCalendarCache();
    KalendarEvent reloadedEvent = calendarManager.getPersonalCalendar(test).getKalendar().getEvent(eventId, null);
    Assert.assertNotNull(reloadedEvent);
    Assert.assertEquals("Short Event", reloadedEvent.getSubject());
    Assert.assertEquals(start, reloadedEvent.getBegin());
    Assert.assertEquals(end, reloadedEvent.getEnd());
    Assert.assertTrue(reloadedEvent.isToday());
    Assert.assertTrue(reloadedEvent.isWithinOneDay());
    Assert.assertFalse(reloadedEvent.isAllDayEvent());
    KalendarEvent reloadedNextEvent = calendarManager.getPersonalCalendar(test).getKalendar().getEvent(nextEventId, null);
    Assert.assertNotNull(reloadedNextEvent);
    Assert.assertEquals("Long Event", reloadedNextEvent.getSubject());
    Assert.assertEquals(nextStart, reloadedNextEvent.getBegin());
    Assert.assertEquals(nextEnd, reloadedNextEvent.getEnd());
    Assert.assertFalse(reloadedNextEvent.isToday());
    Assert.assertFalse(reloadedNextEvent.isWithinOneDay());
    Assert.assertFalse(reloadedNextEvent.isAllDayEvent());
}
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 30 with Kalendar

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

the class ICalFileCalendarManagerTest method testAddChangeRemoveEvent.

@Test
public void testAddChangeRemoveEvent() {
    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.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 12);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Date start = calendar.getTime();
    // 1 hour
    KalendarEvent testEvent = new KalendarEvent(TEST_EVENT_ID, "testEvent", start, 60 * 60 * 1000);
    calendarManager.addEventTo(cal, testEvent);
    // set manager null to force reload of calendar from file-system
    emptyCalendarCache();
    cal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent reloadedEvent = cal.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(start, reloadedEvent.getBegin());
    // calculate and check end date
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date end = calendar.getTime();
    Assert.assertEquals(end, reloadedEvent.getEnd());
    Assert.assertFalse(reloadedEvent.isAllDayEvent());
    Assert.assertTrue(reloadedEvent.isToday());
    // 2. Test Change event
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date updatedEnd = calendar.getTime();
    reloadedEvent.setSubject("testEvent changed");
    reloadedEvent.setEnd(updatedEnd);
    calendarManager.updateEventFrom(cal, reloadedEvent);
    // set manager null to force reload of calendar from file-system
    emptyCalendarCache();
    cal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID, null);
    Assert.assertNotNull("Could not found updated event", updatedEvent);
    Assert.assertEquals("Added event has wrong subject", reloadedEvent.getSubject(), updatedEvent.getSubject());
    Assert.assertEquals(start, reloadedEvent.getBegin());
    Assert.assertEquals(updatedEnd, reloadedEvent.getEnd());
    // 3. Test Remove event
    calendarManager.removeEventFrom(cal, updatedEvent);
    emptyCalendarCache();
    cal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent removedEvent = cal.getEvent(TEST_EVENT_ID, null);
    assertNull("Found removed event", removedEvent);
}
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)

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