Search in sources :

Example 1 with WatchListEventMatcher

use of org.xwiki.watchlist.internal.WatchListEventMatcher in project xwiki-platform by xwiki.

the class WatchListJob method executeJob.

/**
 * Method called from the scheduler.
 *
 * @param jobContext Context of the request
 * @throws JobExecutionException if the job execution fails.
 */
@Override
public void executeJob(JobExecutionContext jobContext) throws JobExecutionException {
    try {
        // Don't go further if the application is disabled
        if (!isWatchListEnabled()) {
            return;
        }
        init(jobContext);
        if (this.watchListJobObject == null) {
            return;
        }
        Collection<String> subscribers = getSubscribers();
        // Stop here if nobody is interested.
        if (!hasSubscribers()) {
            return;
        }
        // Determine what happened since the last execution for everybody.
        Date previousFireTime = getPreviousFireTime();
        WatchListEventMatcher eventMatcher = Utils.getComponent(WatchListEventMatcher.class);
        List<WatchListEvent> events = eventMatcher.getEventsSince(previousFireTime);
        setPreviousFireTime();
        // Stop here if nothing happened in the meantime.
        if (events.size() == 0) {
            return;
        }
        // Notify all the interested subscribers of the events that occurred.
        // When processing the events, a subscriber will only be notified of events that interest him.
        Map<String, Object> notificationData = new HashMap<>();
        notificationData.put(DefaultWatchListNotifier.PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime);
        String mailTemplate = this.watchListJobObject.getStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD);
        notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, mailTemplate);
        // Send the notification for processing.
        this.watchlist.getNotifier().sendNotification(subscribers, events, notificationData);
    } catch (Exception e) {
        // We're in a job, we don't throw exceptions
        LOGGER.error("Exception while running job", e);
    }
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) HashMap(java.util.HashMap) BaseObject(com.xpn.xwiki.objects.BaseObject) WatchListEventMatcher(org.xwiki.watchlist.internal.WatchListEventMatcher) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) JobExecutionException(org.quartz.JobExecutionException)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 JobExecutionException (org.quartz.JobExecutionException)1 WatchListEventMatcher (org.xwiki.watchlist.internal.WatchListEventMatcher)1 WatchListEvent (org.xwiki.watchlist.internal.api.WatchListEvent)1