Search in sources :

Example 1 with TimeRangeCalendar

use of org.openhab.io.gcal.internal.util.TimeRangeCalendar in project openhab1-addons by openhab.

the class GCalEventDownloader method processEntries.

/**
     * <p>
     * Iterates through <code>entries</code>, extracts the event content and
     * creates quartz calendars, jobs and corresponding triggers for each event.
     * </p>
     * <p>
     * The following steps are done at event processing:
     * <ul>
     * <li>find events with empty content</li>
     * <li>create a {@link TimeRangeCalendar} for each event (unique by title) and add a TimeRange for each {@link When}
     * </li>
     * <li>add each {@link TimeRangeCalendar} to the {@link Scheduler}</li>
     * <li>find events with content</li>
     * <li>add a Job with the corresponding Triggers for each event</li>
     * </ul>
     *
     * @param entries the GCalendar events to create quart jobs for.
     * @throws SchedulerException if there is an internal Scheduler error.
     */
private void processEntries(List<Event> entries) throws SchedulerException {
    Map<String, TimeRangeCalendar> calendarCache = new HashMap<String, TimeRangeCalendar>();
    // the scheduler
    for (Event event : entries) {
        String eventContent = event.getDescription();
        String eventTitle = event.getSummary();
        if (StringUtils.isBlank(eventContent)) {
            logger.debug("found event '{}' with no content, add this event to the excluded TimeRangesCalendar - this event could be referenced by the modifiedBy clause", eventTitle);
            if (!calendarCache.containsKey(eventTitle)) {
                calendarCache.put(eventTitle, new TimeRangeCalendar());
            }
            TimeRangeCalendar timeRangeCalendar = calendarCache.get(eventTitle);
            timeRangeCalendar.addTimeRange(new LongRange(event.getStart().getDateTime().getValue(), event.getEnd().getDateTime().getValue()));
        }
    }
    // the calendars has to be added first, to schedule Triggers successfully
    for (Entry<String, TimeRangeCalendar> entry : calendarCache.entrySet()) {
        scheduler.addCalendar(entry.getKey(), entry.getValue(), true, true);
    }
    // now we process all events with content
    for (Event event : entries) {
        String eventContent = event.getDescription();
        String eventTitle = event.getSummary();
        if (StringUtils.isNotBlank(eventContent)) {
            CalendarEventContent cec = parseEventContent(eventContent, (eventTitle != null) && eventTitle.startsWith("[PresenceSimulation]"));
            String modifiedByEvent = null;
            if (calendarCache.containsKey(cec.modifiedByEvent)) {
                modifiedByEvent = cec.modifiedByEvent;
            }
            JobDetail startJob = createJob(cec.startCommands, event, true);
            boolean triggersCreated = createTriggerAndSchedule(startJob, event, modifiedByEvent, true);
            if (triggersCreated) {
                logger.debug("created new startJob '{}' with details '{}'", eventTitle, createJobInfo(event, startJob));
            }
            // do only create end-jobs if there are end-commands ...
            if (StringUtils.isNotBlank(cec.endCommands)) {
                JobDetail endJob = createJob(cec.endCommands, event, false);
                triggersCreated = createTriggerAndSchedule(endJob, event, modifiedByEvent, false);
                if (triggersCreated) {
                    logger.debug("created new endJob '{}' with details '{}'", eventTitle, createJobInfo(event, endJob));
                }
            }
        }
    }
}
Also used : TimeRangeCalendar(org.openhab.io.gcal.internal.util.TimeRangeCalendar) JobDetail(org.quartz.JobDetail) LongRange(org.apache.commons.lang.math.LongRange) HashMap(java.util.HashMap) Event(com.google.api.services.calendar.model.Event)

Aggregations

Event (com.google.api.services.calendar.model.Event)1 HashMap (java.util.HashMap)1 LongRange (org.apache.commons.lang.math.LongRange)1 TimeRangeCalendar (org.openhab.io.gcal.internal.util.TimeRangeCalendar)1 JobDetail (org.quartz.JobDetail)1