@Override public Stream<NotificationFilter> getEnabledFilters(Collection<NotificationFilter> filters, Map<String, Boolean> filterActivations) { return filters.stream().filter( filter -> { if (filter instanceof ToggleableNotificationFilter && filterActivations.containsKey(filter.getName())) { return filterActivations.get(filter.getName()); } return true; } ); }
@Override public Stream<NotificationFilterPreference> getFilterPreferences( Collection<NotificationFilterPreference> filterPreferences, NotificationFilter filter) { return filterPreferences.stream().filter(preference -> filter.getName().equals(preference.getFilterName())); }
@Override public Collection<NotificationFilter> getAllFilters(DocumentReference user, boolean onlyEnabled) throws NotificationException { Collection<NotificationFilter> filters = getAllFilters( user.getWikiReference().getName().equals(wikiDescriptorManager.getMainWikiId())); Map<String, Boolean> filterActivations = getToggeableFilterActivations(user); Iterator<NotificationFilter> it = filters.iterator(); while (it.hasNext()) { NotificationFilter filter = it.next(); Boolean filterActivation = filterActivations.get(filter.getName()); if (onlyEnabled && filterActivation != null && !filterActivation.booleanValue()) { it.remove(); } } return filters; }
@Override public Block display(NotificationFilter filter, NotificationFilterPreference preference) throws NotificationException { try { setUpContext(scriptContextManager, filter, preference); // Try to get a template using the filter name ; if no template is found, fallback on the default one. String templateName = String.format("notification/filters/%s.vm", filter.getName().replaceAll("\\/", ".")); Template template = templateManager.getTemplate(templateName); return (template != null) ? templateManager.execute(template) : templateManager.execute("notification/filters/default.vm"); } catch (Exception e) { throw new NotificationException( String.format("Failed to display the notification filter [%s] with the filter preference [%s].", filter, preference), e); } finally { cleanUpContext(); } }
@Override public Block displayFilter(NotificationFilter filter, NotificationFilterPreference preference) throws NotificationException { /** * Try to find a {@link NotificationFilterDisplayer} that corresponds to the given filter. * If no renderer is found, fallback on the default one. */ try { List<NotificationFilterDisplayer> renderers = componentManager.getInstanceList(NotificationFilterDisplayer.class); for (NotificationFilterDisplayer renderer : renderers) { if (renderer.getSupportedFilters().contains(filter.getName())) { return renderer.display(filter, preference); } } return defaultNotificationFilterDisplayer.display(filter, preference); } catch (ComponentLookupException e) { throw new NotificationException(String.format( "Unable get a list of NotificationFilterDisplayer for filter [%s].", filter), e); } } }
@Override public Map<String, Boolean> getToggeableFilterActivations(DocumentReference user) throws NotificationException { XWikiContext context = contextProvider.get(); XWiki xwiki = context.getWiki(); final DocumentReference notificationPreferencesScopeClass = TOGGLEABLE_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference()); Map<String, Boolean> filterStatus = new HashMap<>(); try { XWikiDocument doc = xwiki.getDocument(user, context); for (NotificationFilter filter : componentManager.<NotificationFilter>getInstanceList( NotificationFilter.class)) { if (filter instanceof ToggleableNotificationFilter) { ToggleableNotificationFilter toggleableFilter = (ToggleableNotificationFilter) filter; boolean status = toggleableFilter.isEnabledByDefault(); BaseObject obj = doc.getXObject(notificationPreferencesScopeClass, FIELD_FILTER_NAME, filter.getName()); if (obj != null) { status = obj.getIntValue(FIELD_IS_ENABLED, status ? 1 : 0) != 0; } filterStatus.put(filter.getName(), status); } } } catch (Exception e) { throw new NotificationException( String.format("Failed to get the toggleable filters preferences for the user [%s].", user), e); } return filterStatus; }