Search in sources :

Example 11 with ImportedToCalendar

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

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 12 with ImportedToCalendar

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

the class OLATUpgrade_10_4_0 method processImportedCalendarsTo.

private void processImportedCalendarsTo(Property property) {
    // the calendar are imported in an existent calendar
    // urls are | separated
    String calendarId = property.getName();
    String type = property.getResourceTypeName();
    if ("user".equals(type)) {
        // don't convert this, they have there own synchronization mechanism
        return;
    }
    String importUrls = property.getTextValue();
    if (StringHelper.containsNonWhitespace(importUrls) && StringHelper.containsNonWhitespace(calendarId) && StringHelper.containsNonWhitespace(type)) {
        for (StringTokenizer tokenizer = new StringTokenizer(importUrls, "|"); tokenizer.hasMoreTokens(); ) {
            String importUrl = tokenizer.nextToken();
            List<ImportedToCalendar> currentCalendars = importedToCalendarDao.getImportedToCalendars(calendarId, type, importUrl);
            if (currentCalendars.isEmpty()) {
                Date importDate;
                if (property.getLongValue() != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(property.getLongValue().longValue());
                    importDate = calendar.getTime();
                } else {
                    importDate = new Date();
                }
                importedToCalendarDao.createImportedToCalendar(calendarId, type, importUrl, importDate);
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) ImportedCalendar(org.olat.commons.calendar.model.ImportedCalendar) Calendar(java.util.Calendar) ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Date(java.util.Date)

Example 13 with ImportedToCalendar

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

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 14 with ImportedToCalendar

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

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 15 with ImportedToCalendar

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

the class ImportedToCalendarDAO method createImportedToCalendar.

public ImportedToCalendar createImportedToCalendar(String toCalendarId, String toType, String url, Date lastUpdate) {
    ImportedToCalendar calendar = new ImportedToCalendar();
    calendar.setCreationDate(new Date());
    calendar.setLastModified(calendar.getCreationDate());
    calendar.setLastUpdate(lastUpdate);
    calendar.setToCalendarId(toCalendarId);
    calendar.setToType(toType);
    calendar.setUrl(url);
    dbInstance.getCurrentEntityManager().persist(calendar);
    return calendar;
}
Also used : ImportedToCalendar(org.olat.commons.calendar.model.ImportedToCalendar) Date(java.util.Date)

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