Search in sources :

Example 6 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method saveFilterPreferences.

@Override
public void saveFilterPreferences(Set<NotificationFilterPreference> filterPreferences) {
    Map<String, Set<NotificationFilterPreference>> preferencesMapping = new HashMap<>();
    for (NotificationFilterPreference filterPreference : filterPreferences) {
        // Try to get the corresponding provider, if no provider can be found, discard the save of the preference
        String providerHint = filterPreference.getProviderHint();
        if (componentManager.hasComponent(NotificationFilterPreferenceProvider.class, providerHint)) {
            if (!preferencesMapping.containsKey(providerHint)) {
                preferencesMapping.put(providerHint, new HashSet<>());
            }
            preferencesMapping.get(providerHint).add(filterPreference);
        }
    }
    // Once we have created the mapping, save all the preferences using their correct providers
    for (String providerHint : preferencesMapping.keySet()) {
        try {
            NotificationFilterPreferenceProvider provider = componentManager.getInstance(NotificationFilterPreferenceProvider.class, providerHint);
            provider.saveFilterPreferences(preferencesMapping.get(providerHint));
        } catch (ComponentLookupException e) {
            logger.error("Unable to retrieve the notification filter preference provider for hint [{}]: [{}]", providerHint, e);
        } catch (NotificationException e) {
            logger.warn("Unable save the filter preferences [{}] against the provider [{}]: [{}]", preferencesMapping.get(providerHint), providerHint, ExceptionUtils.getRootCauseMessage(e));
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) NotificationFilterPreferenceProvider(org.xwiki.notifications.filters.NotificationFilterPreferenceProvider) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationException(org.xwiki.notifications.NotificationException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 7 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method fetchAllFilters.

/**
 * Fetches every filter available to the user, without taking care of whether the filter is disabled by the user
 * or not.
 *
 * @param user the user to use
 * @return a set of filters
 * @throws NotificationException if an error occurs
 */
private Set<NotificationFilter> fetchAllFilters(DocumentReference user) throws NotificationException {
    // If the user is from the main wiki, get filters from all wikis
    if (user.getWikiReference().getName().equals(wikiDescriptorManager.getMainWikiId())) {
        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 {
        // If the user is local, get filters from the current wiki only (we assume it's the wiki of the user).
        try {
            return new HashSet<>(componentManager.getInstanceList(NotificationFilter.class));
        } catch (Exception e) {
            throw new NotificationException(ERROR_MESSAGE, e);
        }
    }
}
Also used : HashMap(java.util.HashMap) NotificationException(org.xwiki.notifications.NotificationException) WikiReference(org.xwiki.model.reference.WikiReference) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NotificationException(org.xwiki.notifications.NotificationException) HashSet(java.util.HashSet)

Example 8 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultModelBridge method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
    Set<NotificationFilterPreference> preferences = new HashSet<>();
    try {
        XWikiDocument doc = xwiki.getDocument(user, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null) {
                    Map<NotificationFilterProperty, List<String>> filterPreferenceProperties = createNotificationFilterPropertiesMap(obj);
                    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()));
                    }
                    // Create the new filter preference and add it to the list of preferences
                    DefaultNotificationFilterPreference notificationFilterPreference = new DefaultNotificationFilterPreference(obj.getStringValue(FILTER_PREFERENCE_NAME));
                    notificationFilterPreference.setProviderHint("userProfile");
                    notificationFilterPreference.setFilterName(obj.getStringValue(FIELD_FILTER_NAME));
                    notificationFilterPreference.setEnabled(obj.getIntValue(FIELD_IS_ENABLED, 1) == 1);
                    notificationFilterPreference.setActive(obj.getIntValue(FIELD_IS_ACTIVE, 1) == 1);
                    notificationFilterPreference.setFilterType(filterType);
                    notificationFilterPreference.setNotificationFormats(filterFormats);
                    notificationFilterPreference.setPreferenceProperties(filterPreferenceProperties);
                    preferences.add(notificationFilterPreference);
                }
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the notification preferences scope for the user [%s].", user), e);
    }
    return preferences;
}
Also used : NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFormat(org.xwiki.notifications.NotificationFormat) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) ArrayList(java.util.ArrayList) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 9 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultModelBridge method saveFilterPreferences.

@Override
public void saveFilterPreferences(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences) throws NotificationException {
    if (user == null) {
        return;
    }
    // Convert the collection of preferences to save to a Map sorted by filter names
    Map<String, NotificationFilterPreference> toSave = filterPreferences.stream().collect(Collectors.toMap(NotificationFilterPreference::getFilterPreferenceName, Function.identity()));
    // Usual XWiki objects
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
    try {
        XWikiDocument doc = xwiki.getDocument(user, context);
        // Update existing objects if they match the filter preferences to save
        updateExistingObjects(toSave, notificationFilterPreferenceClass, doc);
        // Create objects from the remaining filter preferences to save
        createNewObjects(toSave, notificationFilterPreferenceClass, doc, context);
        // Make this change a minor edit so it's not displayed, by default, in notifications
        xwiki.saveDocument(doc, "Save notification filter preferences.", true, context);
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to save the notification preferences scope for the user [%s].", user), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException)

Example 10 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultModelBridge method deleteFilterPreference.

@Override
public void deleteFilterPreference(DocumentReference user, String filterPreferenceName) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
    boolean shouldSave = false;
    try {
        XWikiDocument doc = xwiki.getDocument(user, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null && StringUtils.equals(filterPreferenceName, obj.getStringValue(FILTER_PREFERENCE_NAME))) {
                    doc.removeXObject(obj);
                    shouldSave = true;
                }
            }
        }
        if (shouldSave) {
            xwiki.saveDocument(doc, String.format("Remove filter preference [%s].", filterPreferenceName), context);
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to delete filters [%s] for user [%s].", filterPreferenceName, user), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

NotificationException (org.xwiki.notifications.NotificationException)24 DocumentReference (org.xwiki.model.reference.DocumentReference)14 XWikiContext (com.xpn.xwiki.XWikiContext)10 XWikiException (com.xpn.xwiki.XWikiException)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 ArrayList (java.util.ArrayList)6 List (java.util.List)4 NotificationFormat (org.xwiki.notifications.NotificationFormat)4 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)4 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)3 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 Map (java.util.Map)2 CompositeEvent (org.xwiki.notifications.CompositeEvent)2