Search in sources :

Example 11 with CalDavEvent

use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.

the class CalDavLoaderImpl method getEvents.

@Override
public List<CalDavEvent> getEvents(final CalDavQuery query) {
    log.trace("quering events for filter: {}", query);
    final ArrayList<CalDavEvent> eventList = new ArrayList<CalDavEvent>();
    if (query.getCalendarIds() != null) {
        for (String calendarId : query.getCalendarIds()) {
            final CalendarRuntime eventRuntime = EventStorage.getInstance().getEventCache().get(calendarId);
            if (eventRuntime == null) {
                log.debug("calendar id {} not found", calendarId);
                continue;
            }
            for (EventContainer eventContainer : eventRuntime.getEventMap().values()) {
                for (CalDavEvent calDavEvent : eventContainer.getEventList()) {
                    if (query.getFrom() != null) {
                        if (calDavEvent.getEnd().isBefore(query.getFrom())) {
                            continue;
                        }
                    }
                    if (query.getTo() != null) {
                        if (calDavEvent.getStart().isAfter(query.getTo())) {
                            continue;
                        }
                    }
                    if (query.getFilterName() != null) {
                        if (!calDavEvent.getName().matches(query.getFilterName())) {
                            continue;
                        }
                    }
                    if (query.getFilterCategory() != null) {
                        log.trace("processing filter category");
                        if (calDavEvent.getCategoryList() == null) {
                            log.trace("not found event category for event {}", calDavEvent.getId());
                            continue;
                        } else {
                            log.trace("processing event category");
                            boolean eventCategoriesMatchFilterCategories = false;
                            if (query.getFilterCategoryMatchesAny()) {
                                log.trace("filter-category-any encountered");
                                int filterCategoriesIndex = 0;
                                List<String> filterCategories = query.getFilterCategory();
                                List<String> eventCategories = calDavEvent.getCategoryList();
                                log.trace("comparing filter '{}' to event categories '{}' from event {}", filterCategories, eventCategories, calDavEvent.getId());
                                // browse filter categories, which are not null
                                while (eventCategoriesMatchFilterCategories == false && filterCategoriesIndex < filterCategories.size()) {
                                    int eventCategoriesIndex = 0;
                                    // browse event categories, which can be null
                                    while (eventCategoriesMatchFilterCategories == false && eventCategoriesIndex < eventCategories.size()) {
                                        if (eventCategories.get(eventCategoriesIndex).equalsIgnoreCase(filterCategories.get(filterCategoriesIndex))) {
                                            log.debug("filter category {} matches event category {}", filterCategories.get(filterCategoriesIndex), eventCategories.get(eventCategoriesIndex));
                                            eventCategoriesMatchFilterCategories = true;
                                        }
                                        eventCategoriesIndex++;
                                    }
                                    filterCategoriesIndex++;
                                }
                            } else {
                                log.trace("filter-category encountered");
                                eventCategoriesMatchFilterCategories = calDavEvent.getCategoryList().containsAll(query.getFilterCategory());
                            }
                            if (!eventCategoriesMatchFilterCategories) {
                                continue;
                            }
                        }
                    } else {
                        log.trace("not found any filter category");
                    }
                    eventList.add(calDavEvent);
                }
            }
        }
    }
    if (query.getSort() != null) {
        Collections.sort(eventList, new Comparator<CalDavEvent>() {

            @Override
            public int compare(CalDavEvent arg0, CalDavEvent arg1) {
                if (query.getSort().equals(CalDavQuery.Sort.ASCENDING)) {
                    return (arg0.getStart().compareTo(arg1.getStart()));
                } else if (query.getSort().equals(CalDavQuery.Sort.DESCENDING)) {
                    return (arg1.getStart().compareTo(arg0.getStart()));
                } else {
                    return 0;
                }
            }
        });
    }
    log.debug("return event list for {} with {} entries", query, eventList.size());
    return eventList;
}
Also used : EventContainer(org.openhab.io.caldav.internal.EventStorage.EventContainer) CalDavEvent(org.openhab.io.caldav.CalDavEvent) ArrayList(java.util.ArrayList) CalendarRuntime(org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)

Example 12 with CalDavEvent

use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.

the class EventJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    try {
        final String config = context.getJobDetail().getJobDataMap().getString(KEY_CONFIG);
        final String eventId = context.getJobDetail().getJobDataMap().getString(KEY_EVENT);
        final int recIndex = context.getJobDetail().getJobDataMap().getInt(KEY_REC_INDEX);
        final EventTrigger eventTrigger = EventTrigger.valueOf(context.getJobDetail().getJobDataMap().getString(KEY_EVENT_TRIGGER));
        CalendarRuntime calendarRuntime = EventStorage.getInstance().getEventCache().get(config);
        if (calendarRuntime == null) {
            throw new JobExecutionException("cannot get runtime for config: " + config, false);
        }
        EventContainer eventContainer = calendarRuntime.getEventMap().get(eventId);
        if (eventContainer == null) {
            throw new JobExecutionException("cannot get event-container for config: " + config + " and eventId: " + eventId, false);
        }
        if (eventContainer.getEventList().size() <= recIndex) {
            throw new JobExecutionException("cannot get recurence-event for config: " + config + " and eventId: " + eventId + " and occurence: " + recIndex, false);
        }
        CalDavEvent event = eventContainer.getEventList().get(recIndex);
        log.info("event {} for: {}", eventTrigger, event.getShortName());
        for (EventNotifier notifier : CalDavLoaderImpl.instance.getEventListenerList()) {
            try {
                if (eventTrigger == EventTrigger.BEGIN) {
                    notifier.eventBegins(event);
                } else if (eventTrigger == EventTrigger.END) {
                    notifier.eventEnds(event);
                } else {
                    throw new IllegalStateException("not implemented event trigger: " + eventTrigger);
                }
            } catch (Exception e) {
                log.error("error while invoking listener", e);
            }
        }
        if (eventTrigger == EventTrigger.END) {
            // if event is ended, remove it from the map
            calendarRuntime.getEventMap().remove(eventContainer.getEventId());
        }
    } catch (Exception e) {
        log.error("error executing event job", e);
        throw new JobExecutionException("error executing event job", e, false);
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) EventContainer(org.openhab.io.caldav.internal.EventStorage.EventContainer) CalDavEvent(org.openhab.io.caldav.CalDavEvent) EventNotifier(org.openhab.io.caldav.EventNotifier) CalendarRuntime(org.openhab.io.caldav.internal.EventStorage.CalendarRuntime) JobExecutionException(org.quartz.JobExecutionException)

Example 13 with CalDavEvent

use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.

the class CalDavBinding method handleForEventPreview.

private synchronized void handleForEventPreview() {
    CalDavBindingProvider provider = getCalDavBindingProvider();
    if (provider == null) {
        logger.error("cannot find any provider");
        return;
    }
    List<CalDavEvent> events = calDavLoader.getEvents(new CalDavQuery(this.readCalendars, DateTime.now()));
    for (String configItemName : provider.getItemNames()) {
        final CalDavNextEventConfig config = provider.getConfig(configItemName);
        this.updateItemState(config, events);
    }
}
Also used : CalDavEvent(org.openhab.io.caldav.CalDavEvent) CalDavBindingProvider(org.openhab.binding.caldav_command.CalDavBindingProvider) CalDavQuery(org.openhab.io.caldav.CalDavQuery)

Example 14 with CalDavEvent

use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.

the class CalDavBinding method updateItemsForEvent.

private void updateItemsForEvent() {
    CalDavBindingProvider bindingProvider = null;
    for (CalDavBindingProvider bindingProvider_ : this.providers) {
        bindingProvider = bindingProvider_;
    }
    if (bindingProvider == null) {
        logger.error("no binding provider found");
        return;
    }
    Map<Integer, List<CalDavEvent>> eventCache = new HashMap<Integer, List<CalDavEvent>>();
    for (String item : bindingProvider.getItemNames()) {
        CalDavConfig config = bindingProvider.getConfig(item);
        List<CalDavEvent> events = eventCache.get(config.getUniqueEventListKey());
        if (events == null) {
            CalDavQuery query = getQueryForConfig(config);
            events = this.calDavLoader.getEvents(query);
            eventCache.put(config.getUniqueEventListKey(), events);
        }
        this.updateItem(item, config, events);
    }
}
Also used : HashMap(java.util.HashMap) CalDavEvent(org.openhab.io.caldav.CalDavEvent) CalDavBindingProvider(org.openhab.binding.caldav_personal.CalDavBindingProvider) ArrayList(java.util.ArrayList) List(java.util.List) CalDavQuery(org.openhab.io.caldav.CalDavQuery)

Aggregations

CalDavEvent (org.openhab.io.caldav.CalDavEvent)14 ArrayList (java.util.ArrayList)6 CalendarRuntime (org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)6 EventContainer (org.openhab.io.caldav.internal.EventStorage.EventContainer)6 CalDavQuery (org.openhab.io.caldav.CalDavQuery)5 EventNotifier (org.openhab.io.caldav.EventNotifier)4 IOException (java.io.IOException)3 State (org.openhab.core.types.State)3 EventUtils (org.openhab.io.caldav.EventUtils)3 JobExecutionException (org.quartz.JobExecutionException)3 SchedulerException (org.quartz.SchedulerException)3 SardineException (com.github.sardine.impl.SardineException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ParserException (net.fortuna.ical4j.data.ParserException)2 PeriodList (net.fortuna.ical4j.model.PeriodList)2 DateTime (org.joda.time.DateTime)2 CalDavBindingProvider (org.openhab.binding.caldav_command.CalDavBindingProvider)2 Item (org.openhab.core.items.Item)2 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)2