/** * @return the formats handled by this filter */ default List<NotificationFormat> getFormats() { return Arrays.asList(NotificationFormat.values()); }
@Override public void setStartDateForUser(DocumentReference user, Date startDate) throws NotificationException { try { List<NotificationFilterPreferenceProvider> providers = componentManager.getInstanceList(NotificationFilterPreferenceProvider.class); for (NotificationFilterPreferenceProvider provider : providers) { provider.setStartDateForUser(user, startDate); } } catch (ComponentLookupException e) { throw new NotificationException(String.format("Unable to set the starting date for filter preferences" + " with user [%s].", user)); } } }
@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); } } }
private void handleObject(List<NotificationFilterPreference> preferencesToConvert, BaseObject obj) { DefaultNotificationFilterPreference preference = new DefaultNotificationFilterPreference(); NotificationFilterType filterType = NotificationFilterType.valueOf( obj.getStringValue(FIELD_FILTER_TYPE).toUpperCase()); Set<NotificationFormat> filterFormats = new HashSet<>(); for (String format : (List<String>) obj.getListValue(FIELD_FILTER_FORMATS)) { filterFormats.add(NotificationFormat.valueOf(format.toUpperCase())); } preference.setProviderHint("userProfile"); preference.setFilterName(obj.getStringValue(FIELD_FILTER_NAME)); preference.setEnabled(obj.getIntValue(FIELD_IS_ENABLED, 1) == 1); preference.setActive(obj.getIntValue(FIELD_IS_ACTIVE, 1) == 1); preference.setFilterType(filterType); preference.setNotificationFormats(filterFormats); preference.setStartingDate(obj.getDateValue(FIELD_STARTING_DATE)); handleProperties(preferencesToConvert, obj, preference); }
List<NotificationFilterPreference> preferencesToSave = convertXObjectsToPreferences(doc, notificationFilterPreferenceClass); Set<NotificationFilterPreference> preferencesInTheNewStore = modelBridge.getFilterPreferences(user); if (!modelBridge.getFilterPreferences(user).isEmpty() && preferencesInTheNewStore.size() == preferencesToSave.size()) { logger.info("It seems the notification filter preferences of user [{}] has already been migrated," modelBridge.saveFilterPreferences(user, preferencesToSave); throw new NotificationException( String.format("Failed to migrate the notification preferences for the user [%s].", user), e);
@Override public Block display(NotificationFilter filter, NotificationFilterPreference preference) throws NotificationException { try { setUpContext(scriptContextManager, filter, preference); // If we have no template defined, fallback on the default displayer if (filterTemplate == null) { return ((NotificationFilterDisplayer) componentManager.getInstance(NotificationFilterDisplayer.class)) .display(filter, preference); } return templateManager.execute(filterTemplate); } catch (Exception e) { throw new NotificationException( String.format("Unable to display the notification filter template for the filters [%s].", componentHint), e); } finally { cleanUpContext(); } }
@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 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; }
baseObject.getListValue(WikiNotificationFilterDisplayerDocumentInitializer.SUPPORTED_FILTERS)); componentHint = generateComponentHint(); if (!StringUtils.isBlank(xObjectTemplate)) { filterTemplate = templateManager.createStringTemplate( xObjectTemplate, this.getAuthorReference()); } else { filterTemplate = null; throw new NotificationException( String.format("Unable to construct a new WikiNotificationFilterDisplayer from" + " the base object [%s]", baseObject), e);
/** * Ensure that the given author has the administrative rights in the current context. * * @param documentReference the working entity * @param authorReference the author that should have its rights checked * @throws NotificationException if the author rights are not sufficient */ private void checkRights(DocumentReference documentReference, DocumentReference authorReference) throws NotificationException { if (!this.authorizationManager.hasAccess(Right.ADMIN, authorReference, documentReference.getWikiReference())) { throw new NotificationException( "Registering custom Notification Filter Displayers requires wiki administration rights."); } } }
DefaultNotificationFilterPreference copy = new DefaultNotificationFilterPreference(preference); copy.setOwner(serializedUser); session.saveOrUpdate(copy); hibernateStore.endTransaction(context, false); throw new NotificationException("Failed to save the notification filter preferences.", e); } finally { if (!currentWiki.equals(context.getWikiId())) {
@Override public Collection<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException { Set<NotificationFilterPreference> filterPreferences = new HashSet<>(); try { List<NotificationFilterPreferenceProvider> providers = componentManager.getInstanceList(NotificationFilterPreferenceProvider.class); for (NotificationFilterPreferenceProvider provider : providers) { filterPreferences.addAll(provider.getFilterPreferences(user)); } return filterPreferences; } catch (ComponentLookupException e) { throw new NotificationException(String.format("Unable to fetch a list of notification preference " + "providers with user [%s].", user)); } }
@Override public List<CompositeEventStatus> getCompositeEventStatuses(List<CompositeEvent> compositeEvents, String entityId) throws Exception { // Creating a list of all events to avoid multiple calls to getEventStatuses() and so multiple calls to the // database. List<Event> allEvents = new ArrayList<>(); // But maintain a mapping between eventId and their composite event status Map<String, CompositeEventStatus> map = new HashMap<>(); for (CompositeEvent compositeEvent : compositeEvents) { CompositeEventStatus compositeEventStatus = new CompositeEventStatus(compositeEvent); for (Event event : compositeEvent.getEvents()) { map.put(event.getId(), compositeEventStatus); } allEvents.addAll(compositeEvent.getEvents()); } // Put the event statuses into the composite events statuses for (EventStatus eventStatus : getEventStatuses(allEvents, entityId)) { map.get(eventStatus.getEvent().getId()).add(eventStatus); } List<CompositeEventStatus> results = new ArrayList<>(); // Keep the same order than inputs for (CompositeEvent event : compositeEvents) { results.add(map.get(event.getEvents().get(0).getId())); } return results; }
preference.setProviderHint("userProfile"); throw new NotificationException(String.format( "Error while loading the notification filter preferences of the user [%s].", serializedUser), e);
/** * Add an event to the composite event. * @param event the event to add * @param similarity the similarity between the event to add and the events of the composite events * @throws NotificationException if the addition is illegal (lower similarity for example) */ public void add(Event event, int similarity) throws NotificationException { if (similarity < similarityBetweenEvents) { throw new NotificationException("Invalid addition of an event inside a CompositeEvent"); } similarityBetweenEvents = similarity; events.add(event); // Ensure the events are always sorted by date (more recent firsts) Collections.sort(events, (e1, e2) -> e2.getDate().compareTo(e1.getDate())); }
@Override public Collection<NotificationFilter> getAllFilters(boolean allWikis) throws NotificationException { if (allWikis) { String currentWikiId = wikiDescriptorManager.getCurrentWikiId(); Map<String, NotificationFilter> filters = new HashMap<>(); try { for (String wikiId : wikiDescriptorManager.getAllIds()) { modelContext.setCurrentEntityReference(new WikiReference(wikiId)); filters.putAll(componentManager.getInstanceMap(NotificationFilter.class)); } } catch (Exception e) { throw new NotificationException(ERROR_MESSAGE, e); } finally { modelContext.setCurrentEntityReference(new WikiReference(currentWikiId)); } return new HashSet<>(filters.values()); } else { // Get filters from the current wiki only try { return new HashSet<>(componentManager.getInstanceList(NotificationFilter.class)); } catch (Exception e) { throw new NotificationException(ERROR_MESSAGE, e); } } }