Search in sources :

Example 81 with Kalendar

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

the class ImportCalendarManager method reloadCalendarFromUrl.

/**
 * Reload calendar from url and store calendar file locally.
 * @param importUrl
 * @param calType
 * @param calId
 */
private void reloadCalendarFromUrl(String importUrl, String calType, String calId, KalendarEventFilter filter) {
    try (InputStream in = new URL(importUrl).openStream()) {
        String targetId = "-" + CalendarManager.TYPE_USER + "-" + calId;
        Kalendar kalendar = calendarManager.buildKalendarFrom(in, calType, calId);
        Collection<KalendarEvent> events = kalendar.getEvents();
        for (KalendarEvent event : events) {
            if (!filter.test(event) || event.getID().contains(targetId)) {
                kalendar.removeEvent(event);
            }
        }
        calendarManager.persistCalendar(kalendar);
    } catch (Exception e) {
        log.error("Could not reload calendar from url=" + importUrl, e);
    }
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) InputStream(java.io.InputStream) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) URL(java.net.URL) IOException(java.io.IOException)

Example 82 with Kalendar

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

the class ImportToCalendarManager method importCalendarIn.

/**
 * Append the stream of events to the specified calendar.
 *
 * @param calenderWrapper The target calendar.
 * @param in An iCal stream.
 * @return true if successfully imported
 */
public boolean importCalendarIn(KalendarRenderWrapper calenderWrapper, InputStream in) {
    try {
        Kalendar cal = calenderWrapper.getKalendar();
        Kalendar importedCal = calendarManager.buildKalendarFrom(in, cal.getType(), cal.getCalendarID());
        return calendarManager.updateCalendar(cal, importedCal);
    } catch (Exception e) {
        log.error("", e);
        return false;
    }
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar)

Example 83 with Kalendar

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

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

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

the class CalendarEntryDetailsController method doSave.

private void doSave(UserRequest ureq) {
    // ok, save edited entry
    kalendarEvent = eventForm.getUpdatedKalendarEvent();
    if (isNew) {
        boolean doneSuccessfully = true;
        // this is a new event, add event to calendar
        String calendarID = eventForm.getChoosenKalendarID();
        for (Iterator<KalendarRenderWrapper> iter = availableCalendars.iterator(); iter.hasNext(); ) {
            KalendarRenderWrapper calendarWrapper = iter.next();
            if (!calendarWrapper.getKalendar().getCalendarID().equals(calendarID)) {
                continue;
            }
            Kalendar cal = calendarWrapper.getKalendar();
            boolean result = calendarManager.addEventTo(cal, kalendarEvent);
            if (result == false) {
                // if one failed => done not successfully
                doneSuccessfully = false;
            }
        }
        reportSaveStatus(ureq, doneSuccessfully);
    } else if (kalendarEvent instanceof KalendarRecurEvent && !StringHelper.containsNonWhitespace(kalendarEvent.getRecurrenceID())) {
        updateCtr = new ConfirmUpdateController(ureq, getWindowControl(), (KalendarRecurEvent) kalendarEvent);
        listenTo(updateCtr);
        String title = translate("cal.edit.update");
        cmc = new CloseableModalController(getWindowControl(), translate("close"), updateCtr.getInitialComponent(), true, title);
        listenTo(cmc);
        cmc.activate();
    } else {
        // this is an existing event, so we get the previousely assigned calendar from the event
        Kalendar cal = kalendarEvent.getCalendar();
        boolean doneSuccessfully = calendarManager.updateEventFrom(cal, kalendarEvent);
        reportSaveStatus(ureq, doneSuccessfully);
    }
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) KalendarRecurEvent(org.olat.commons.calendar.model.KalendarRecurEvent) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) KalendarRenderWrapper(org.olat.commons.calendar.ui.components.KalendarRenderWrapper)

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