Search in sources :

Example 1 with CalendarComponent

use of net.fortuna.ical4j.model.component.CalendarComponent in project openhab1-addons by openhab.

the class EventReloaderJob method loadEvents.

public void loadEvents(String filename, org.joda.time.DateTime lastResourceChangeFS, final InputStream inputStream, final CalDavConfig config, final List<String> oldEventIds, boolean readFromFile) throws IOException, ParserException {
    CalendarBuilder builder = new CalendarBuilder();
    InputStreamReader is = new InputStreamReader(inputStream, config.getCharset());
    BufferedReader in = new BufferedReader(is, 50);
    final UnfoldingReader uin = new UnfoldingReader(in, 50, true);
    Calendar calendar = builder.build(uin);
    uin.close();
    // log.trace("calendar: {}", calendar);
    EventContainer eventContainer = new EventContainer(config.getKey());
    eventContainer.setFilename(filename);
    eventContainer.setLastChanged(lastResourceChangeFS);
    org.joda.time.DateTime loadFrom = org.joda.time.DateTime.now().minusMinutes(config.getHistoricLoadMinutes());
    org.joda.time.DateTime loadTo = org.joda.time.DateTime.now().plusMinutes(config.getPreloadMinutes());
    final ComponentList<CalendarComponent> vEventComponents = calendar.getComponents(Component.VEVENT);
    if (vEventComponents.size() == 0) {
        log.debug("could not find a VEVENT from calendar build, based on file {}", filename);
        // no events inside
        if (!readFromFile) {
            Util.storeToDisk(config.getKey(), filename, calendar);
        }
        return;
    }
    org.joda.time.DateTime lastModifedVEventOverAll = null;
    for (CalendarComponent comp : vEventComponents) {
        VEvent vEvent = (VEvent) comp;
        log.trace("loading event: " + vEvent.getUid().getValue() + ":" + vEvent.getSummary().getValue());
        // fallback, because 'LastModified' in VEvent is optional
        org.joda.time.DateTime lastModifedVEvent = lastResourceChangeFS;
        if (vEvent.getLastModified() != null) {
            lastModifedVEvent = new org.joda.time.DateTime(vEvent.getLastModified().getDateTime());
            log.trace("overriding lastmodified from file FS ({}) with event's last-modified property ({})", lastResourceChangeFS, lastModifedVEvent);
        }
        if (!config.isLastModifiedFileTimeStampValid()) {
            if (lastModifedVEventOverAll == null || lastModifedVEvent.isAfter(lastModifedVEventOverAll)) {
                lastModifedVEventOverAll = lastModifedVEvent;
            }
            if (eventContainer != null && !lastModifedVEvent.isBefore(eventContainer.getLastChanged())) {
                // to be created
                if (eventContainer.getCalculatedUntil() != null && vEventComponents.size() == 1 && eventContainer.getCalculatedUntil().isAfter(org.joda.time.DateTime.now().plusMinutes(config.getReloadMinutes()))) {
                    // the event is calculated as long as the next reload
                    // interval can handle this
                    log.trace("skipping resource processing {}, not changed", filename);
                    continue;
                }
                if (eventContainer.isHistoricEvent()) {
                    // no more upcoming events, do nothing
                    log.trace("skipping resource processing {}, not changed", filename);
                    continue;
                }
            }
        }
        Period period = new Period(new DateTime(loadFrom.toDate()), new DateTime(loadTo.toDate()));
        PeriodList periods = vEvent.calculateRecurrenceSet(period);
        periods = periods.normalise();
        String eventId = vEvent.getUid().getValue();
        final String eventName = vEvent.getSummary().getValue();
        // no more upcoming events
        if (periods.size() > 0) {
            if (vEvent.getConsumedTime(new net.fortuna.ical4j.model.Date(), new net.fortuna.ical4j.model.Date(org.joda.time.DateTime.now().plusYears(10).getMillis())).size() == 0) {
                log.trace("event will never be occur (historic): {}", eventName);
                eventContainer.setHistoricEvent(true);
            }
        }
        // expecting this is for every vEvent inside a calendar equals
        eventContainer.setEventId(eventId);
        eventContainer.setCalculatedUntil(loadTo);
        for (Period p : periods) {
            org.joda.time.DateTime start = getDateTime("start", p.getStart(), p.getRangeStart());
            org.joda.time.DateTime end = getDateTime("end", p.getEnd(), p.getRangeEnd());
            CalDavEvent event = new CalDavEvent(eventName, vEvent.getUid().getValue(), config.getKey(), start, end);
            event.setLastChanged(lastModifedVEvent);
            if (vEvent.getLocation() != null) {
                event.setLocation(vEvent.getLocation().getValue());
            }
            if (vEvent.getDescription() != null) {
                event.setContent(vEvent.getDescription().getValue());
            }
            event.getCategoryList().addAll(readCategory(vEvent));
            event.setFilename(filename);
            log.trace("adding event: " + event.getShortName());
            eventContainer.getEventList().add(event);
        }
    }
    if (lastModifedVEventOverAll != null && !config.isLastModifiedFileTimeStampValid()) {
        eventContainer.setLastChanged(lastModifedVEventOverAll);
        log.debug("changing eventcontainer last modified to {}", lastModifedVEventOverAll);
    }
    // if (!eventContainer.getEventList().isEmpty()) {
    CalDavLoaderImpl.instance.addEventToMap(eventContainer, true);
    if (!readFromFile) {
        Util.storeToDisk(config.getKey(), filename, calendar);
    }
// }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) InputStreamReader(java.io.InputStreamReader) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) EventContainer(org.openhab.io.caldav.internal.EventStorage.EventContainer) CalendarComponent(net.fortuna.ical4j.model.component.CalendarComponent) UnfoldingReader(net.fortuna.ical4j.data.UnfoldingReader) Calendar(net.fortuna.ical4j.model.Calendar) Period(net.fortuna.ical4j.model.Period) PeriodList(net.fortuna.ical4j.model.PeriodList) DateTime(net.fortuna.ical4j.model.DateTime) LocalDateTime(org.joda.time.LocalDateTime) CalDavEvent(org.openhab.io.caldav.CalDavEvent) BufferedReader(java.io.BufferedReader) CalendarRuntime(org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 CalendarBuilder (net.fortuna.ical4j.data.CalendarBuilder)1 UnfoldingReader (net.fortuna.ical4j.data.UnfoldingReader)1 Calendar (net.fortuna.ical4j.model.Calendar)1 DateTime (net.fortuna.ical4j.model.DateTime)1 Period (net.fortuna.ical4j.model.Period)1 PeriodList (net.fortuna.ical4j.model.PeriodList)1 CalendarComponent (net.fortuna.ical4j.model.component.CalendarComponent)1 VEvent (net.fortuna.ical4j.model.component.VEvent)1 LocalDateTime (org.joda.time.LocalDateTime)1 CalDavEvent (org.openhab.io.caldav.CalDavEvent)1 CalendarRuntime (org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)1 EventContainer (org.openhab.io.caldav.internal.EventStorage.EventContainer)1