Search in sources :

Example 6 with ImportedToCalendar

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

the class ImportedToCalendarDAOTest method createImportedToCalendar.

@Test
public void createImportedToCalendar() {
    String toCalendarId = UUID.randomUUID().toString();
    String toType = "to-rnd-1";
    String url = "http://www.openolat.org/importedto/calendar1.ics";
    ImportedToCalendar importToCalendar = importedToCalendarDao.createImportedToCalendar(toCalendarId, toType, url, new Date());
    dbInstance.commit();
    Assert.assertNotNull(importToCalendar);
    Assert.assertNotNull(importToCalendar.getKey());
    Assert.assertNotNull(importToCalendar.getCreationDate());
    Assert.assertNotNull(importToCalendar.getLastModified());
    Assert.assertNotNull(importToCalendar.getLastUpdate());
    Assert.assertEquals(toCalendarId, importToCalendar.getToCalendarId());
    Assert.assertEquals(toType, importToCalendar.getToType());
    Assert.assertEquals(url, importToCalendar.getUrl());
}
Also used : ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 7 with ImportedToCalendar

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

the class ImportedToCalendarDAOTest method getImportedToCalendars.

@Test
public void getImportedToCalendars() {
    String toCalendarId = UUID.randomUUID().toString();
    String toType = "to-rnd-3";
    String url = "http://www.openolat.org/importedto/calendar3.ics";
    ImportedToCalendar importToCalendar = importedToCalendarDao.createImportedToCalendar(toCalendarId, toType, url, new Date());
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(importToCalendar);
    // load all calendars
    List<ImportedToCalendar> allCalendars = importedToCalendarDao.getImportedToCalendars();
    Assert.assertNotNull(allCalendars);
    Assert.assertFalse(allCalendars.isEmpty());
    Assert.assertTrue(allCalendars.contains(importToCalendar));
}
Also used : ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 8 with ImportedToCalendar

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

the class ImportToCalendarManager method importCalendarIn.

/**
 * Import an external calendar.
 *
 * @param cal
 * @param importUrl
 * @return
 */
public boolean importCalendarIn(Kalendar cal, String importUrl) {
    try (InputStream in = new URL(importUrl).openStream()) {
        Kalendar importedCal = calendarManager.buildKalendarFrom(in, cal.getType(), cal.getCalendarID());
        boolean imported = calendarManager.updateCalendar(cal, importedCal);
        if (imported) {
            List<ImportedToCalendar> importedToCalendars = importedToCalendarDao.getImportedToCalendars(cal.getCalendarID(), cal.getType(), importUrl);
            if (importedToCalendars.isEmpty()) {
                importedToCalendarDao.createImportedToCalendar(cal.getCalendarID(), cal.getType(), importUrl, new Date());
            } else {
                ImportedToCalendar importedToCalendar = importedToCalendars.get(0);
                importedToCalendar.setLastUpdate(new Date());
                importedToCalendar = importedToCalendarDao.update(importedToCalendar);
            }
        }
        return imported;
    } catch (Exception e) {
        log.error("", e);
        return false;
    }
}
Also used : ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Kalendar(org.olat.commons.calendar.model.Kalendar) InputStream(java.io.InputStream) URL(java.net.URL) Date(java.util.Date)

Example 9 with ImportedToCalendar

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

the class ImportToCalendarManager method updateCalendarIn.

/**
 * Method used by the cron job
 * @return
 */
public boolean updateCalendarIn() {
    List<ImportedToCalendar> importedToCalendars = importedToCalendarDao.getImportedToCalendars();
    log.audit("Begin to update " + importedToCalendars.size() + " calendars.");
    // make a full check only every 10 runs
    boolean check = counter.incrementAndGet() % 10 == 0;
    int count = 0;
    for (ImportedToCalendar importedToCalendar : importedToCalendars) {
        String type = importedToCalendar.getToType();
        String id = importedToCalendar.getToCalendarId();
        String importUrl = importedToCalendar.getUrl();
        if (check || check(importedToCalendar)) {
            try (InputStream in = new URL(importUrl).openStream()) {
                Kalendar cal = calendarManager.getCalendar(type, id);
                if (calendarManager.synchronizeCalendarFrom(in, importUrl, cal)) {
                    log.audit("Updated successfully calendar: " + type + " / " + id);
                } else {
                    log.audit("Failed to update calendar: " + type + " / " + id);
                }
            } catch (Exception ex) {
                log.error("Cannot synchronize calendar (" + importedToCalendar.getKey() + ") from url: " + importUrl, ex);
            }
        } else {
            log.audit("Delete imported calendar because of missing resource: " + type + " " + id + " with URL: " + importUrl);
            deleteImportedCalendars(type, id);
        }
        if (count++ % 20 == 0) {
            DBFactory.getInstance().commit();
            try {
                // sleep to don't overload the system
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                log.error("", e);
            }
        }
    }
    return false;
}
Also used : ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Kalendar(org.olat.commons.calendar.model.Kalendar) InputStream(java.io.InputStream) URL(java.net.URL)

Example 10 with ImportedToCalendar

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

the class ImportedToCalendarDAOTest method createImportedToCalendar.

@Test
public void createImportedToCalendar() {
    String toCalendarId = UUID.randomUUID().toString();
    String toType = "to-rnd-1";
    String url = "http://www.openolat.org/importedto/calendar1.ics";
    ImportedToCalendar importToCalendar = importedToCalendarDao.createImportedToCalendar(toCalendarId, toType, url, new Date());
    dbInstance.commit();
    Assert.assertNotNull(importToCalendar);
    Assert.assertNotNull(importToCalendar.getKey());
    Assert.assertNotNull(importToCalendar.getCreationDate());
    Assert.assertNotNull(importToCalendar.getLastModified());
    Assert.assertNotNull(importToCalendar.getLastUpdate());
    Assert.assertEquals(toCalendarId, importToCalendar.getToCalendarId());
    Assert.assertEquals(toType, importToCalendar.getToType());
    Assert.assertEquals(url, importToCalendar.getUrl());
}
Also used : ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ImportedToCalendar (org.olat.commons.calendar.model.ImportedToCalendar)16 Date (java.util.Date)12 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 URL (java.net.URL)4 Kalendar (org.olat.commons.calendar.model.Kalendar)4 Calendar (java.util.Calendar)2 StringTokenizer (java.util.StringTokenizer)2 ImportedCalendar (org.olat.commons.calendar.model.ImportedCalendar)2