use of org.olat.commons.calendar.model.Kalendar in project OpenOLAT by OpenOLAT.
the class OLATUpgrade_10_4_0 method processCalendarProperty.
private void processCalendarProperty(Property property) {
String calendarId;
Identity identity = property.getIdentity();
String resourceType = property.getResourceTypeName();
if (StringHelper.containsNonWhitespace(resourceType) && property.getResourceTypeId() != null) {
calendarId = property.getResourceTypeId().toString();
} else {
resourceType = CalendarManager.TYPE_USER;
calendarId = identity.getName();
}
CalendarUserConfiguration config = calendarUserConfigurationDao.getCalendarUserConfiguration(identity, calendarId, resourceType);
if (config == null) {
String token = property.getStringValue();
Kalendar mockCal = new Kalendar(calendarId, resourceType);
calendarUserConfigurationDao.createCalendarUserConfiguration(mockCal, identity, token, true, true);
}
}
use of org.olat.commons.calendar.model.Kalendar in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManagerTest method synchronizeCalendarFrom.
@Test
public void synchronizeCalendarFrom() throws ValidationException, IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("s1-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
String eventId1 = "id-not-managed-event";
// 1. Add a standard event, not managed
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 notManagedEvent = new KalendarEvent(eventId1, null, "testEvent", start, end);
calendarManager.addEventTo(cal, notManagedEvent);
// 2. Synchronize with a first calendar
Kalendar baseCalendar = calendarManager.createCalendar("user", "first-sync");
String eventIdManaged1 = "managed-event-1";
KalendarEvent managedEvent1 = new KalendarEvent(eventIdManaged1, null, "managedEvent", start, end);
baseCalendar.addEvent(managedEvent1);
String eventIdManaged2 = "managed-event-2";
KalendarEvent managedEvent2 = new KalendarEvent(eventIdManaged2, null, "managedEvent", start, end);
baseCalendar.addEvent(managedEvent2);
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
new CalendarOutputter(false).output(calendarManager.buildCalendar(baseCalendar), os1);
InputStream in1 = new ByteArrayInputStream(os1.toByteArray());
calendarManager.synchronizeCalendarFrom(in1, "http://localhost:8080/unittest", cal);
in1.close();
// 3. Synchronize with a second calendar
Kalendar resyncCalendar = calendarManager.createCalendar("user", "first-sync");
KalendarEvent managedEvent1Alt = new KalendarEvent(eventIdManaged1, null, "managedEvent resync", start, end);
resyncCalendar.addEvent(managedEvent1Alt);
String eventIdManaged3 = "managed-event-3";
KalendarEvent managedEvent3 = new KalendarEvent(eventIdManaged3, null, "managedEvent 3", start, end);
resyncCalendar.addEvent(managedEvent3);
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
new CalendarOutputter(false).output(calendarManager.buildCalendar(resyncCalendar), os2);
InputStream in2 = new ByteArrayInputStream(os2.toByteArray());
calendarManager.synchronizeCalendarFrom(in2, "http://localhost:8080/unittest", cal);
in2.close();
emptyCalendarCache();
// check
Kalendar synchedCal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent notManagedEvent1 = synchedCal.getEvent(eventId1, null);
Assert.assertNotNull(notManagedEvent1);
Assert.assertEquals("testEvent", notManagedEvent1.getSubject());
KalendarEvent event1 = synchedCal.getEvent(eventIdManaged1, null);
Assert.assertNotNull(event1);
Assert.assertEquals("managedEvent resync", event1.getSubject());
KalendarEvent event2 = synchedCal.getEvent(eventIdManaged2, null);
Assert.assertNull(event2);
KalendarEvent event3 = synchedCal.getEvent(eventIdManaged3, null);
Assert.assertNotNull(event3);
Assert.assertEquals("managedEvent 3", event3.getSubject());
}
use of org.olat.commons.calendar.model.Kalendar in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManagerTest method testConcurrentAddUpdateEvent.
/**
* Test concurrent add/update event with two threads and code-point to control concurrency.
*/
@Test
public void testConcurrentAddUpdateEvent() {
final String TEST_EVENT_ID_1 = "id-testConcurrentAddUpdateEvent-1";
final String TEST_EVENT_SUBJECT_1 = "testEvent1";
final String TEST_EVENT_ID_2 = "id-testConcurrentAddUpdateEvent-2";
final String TEST_EVENT_SUBJECT_2 = "testEvent2";
final String TEST_EVENT_SUBJECT_2_UPDATED = "testUpdatedEvent2";
final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-3-");
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("testConcurrentAddUpdateEvent thread2 addEvent2 DONE");
final CountDownLatch doneSignal = new CountDownLatch(2);
// thread 1
Thread thread1 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
log.info("testConcurrentAddUpdateEvent thread1 addEvent1");
calendarManager.addEventTo(currentCalendar, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
log.info("testConcurrentAddUpdateEvent thread1 addEvent1 DONE");
// 3. check event1 exist
currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = currentCalendar.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)
currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
event1 = currentCalendar.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("testConcurrentAddUpdateEvent 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("testConcurrentAddUpdateEvent thread2 updateEvent2");
calendarManager.updateEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
log.info("testConcurrentAddUpdateEvent thread1 updateEvent2 DONE");
// 4. check event2 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = calendar.getEvent(TEST_EVENT_ID_2, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, updatedEvent);
assertEquals("Wrong calendar-event subject", updatedEvent.getSubject(), TEST_EVENT_SUBJECT_2_UPDATED);
// 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);
// Delete Event
boolean removed = calendarManager.removeEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
assertTrue(removed);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddUpdateEvent 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("testConcurrentAddUpdateEvent finish successful");
}
use of org.olat.commons.calendar.model.Kalendar in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManagerTest method testTodayEvent.
/**
* Check a NPE
* @throws IOException
*/
@Test
public void testTodayEvent() throws IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-3-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 1. Test Today Event
String eventId = "today-" + 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, 8);
Date start = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 12);
Date end = calendar.getTime();
KalendarEvent testEvent = new KalendarEvent(eventId, null, "Today Event", start, end);
calendarManager.addEventTo(cal, testEvent);
// Next days event
String nextEventId = "next-" + 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_OF_DAY, 8);
Date nextStart = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 12);
Date nextEnd = calendar.getTime();
KalendarEvent nextEvent = new KalendarEvent(nextEventId, null, "Next 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("Today 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("Next Event", reloadedNextEvent.getSubject());
Assert.assertEquals(nextStart, reloadedNextEvent.getBegin());
Assert.assertEquals(nextEnd, reloadedNextEvent.getEnd());
Assert.assertFalse(reloadedNextEvent.isToday());
Assert.assertTrue(reloadedNextEvent.isWithinOneDay());
Assert.assertFalse(reloadedNextEvent.isAllDayEvent());
}
use of org.olat.commons.calendar.model.Kalendar in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManagerTest method updateCalendar.
@Test
public void updateCalendar() throws ValidationException, IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("u1-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
String eventIdMarker = "id-marker-event";
// 1. Add a standard event, not managed
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 markerEvent = new KalendarEvent(eventIdMarker, null, "markerEvent", start, end);
calendarManager.addEventTo(cal, markerEvent);
// 2. Update with a first calendar
Kalendar baseCalendar = calendarManager.createCalendar("user", "first-update");
String eventId1 = "event-1";
KalendarEvent event1 = new KalendarEvent(eventId1, null, "event 1", start, end);
baseCalendar.addEvent(event1);
String eventId2 = "event-2";
KalendarEvent event2 = new KalendarEvent(eventId2, null, "event 2", start, end);
baseCalendar.addEvent(event2);
calendarManager.updateCalendar(cal, baseCalendar);
// 3. Update with a second calendar
Kalendar updateCalendar = calendarManager.createCalendar("user", "first-update");
KalendarEvent event1alt = new KalendarEvent(eventId1, null, "event 1 alt", start, end);
updateCalendar.addEvent(event1alt);
String eventId3 = "event-3";
KalendarEvent event3 = new KalendarEvent(eventId3, null, "event 3", start, end);
updateCalendar.addEvent(event3);
calendarManager.updateCalendar(cal, updateCalendar);
// Check
emptyCalendarCache();
Kalendar reloadedCal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent marker = reloadedCal.getEvent(eventIdMarker, null);
Assert.assertNotNull(marker);
Assert.assertEquals("markerEvent", marker.getSubject());
KalendarEvent reloaded1 = reloadedCal.getEvent(eventId1, null);
Assert.assertNotNull(reloaded1);
Assert.assertEquals("event 1 alt", reloaded1.getSubject());
KalendarEvent reloaded2 = reloadedCal.getEvent(eventId2, null);
Assert.assertNotNull(reloaded2);
Assert.assertEquals("event 2", reloaded2.getSubject());
KalendarEvent reloaded3 = reloadedCal.getEvent(eventId3, null);
Assert.assertNotNull(reloaded3);
Assert.assertEquals("event 3", reloaded3.getSubject());
}
Aggregations