Search in sources :

Example 1 with NotificationPreferenceProvider

use of org.xwiki.notifications.preferences.NotificationPreferenceProvider in project xwiki-platform by xwiki.

the class DefaultNotificationPreferenceManager method savePreferences.

@Override
public void savePreferences(List<NotificationPreference> preferences) throws NotificationException {
    // We construct a map where each key is a provider hint and each value is a list of associated providers
    // this allows calling each provider only once
    Map<String, List<NotificationPreference>> preferencesMapping = new HashMap<>();
    for (NotificationPreference notificationPreference : preferences) {
        // Try to get the corresponding provider, if no provider can be found, discard the save of the preference
        String providerHint = notificationPreference.getProviderHint();
        if (componentManager.hasComponent(NotificationPreferenceProvider.class, providerHint)) {
            if (!preferencesMapping.containsKey(providerHint)) {
                preferencesMapping.put(providerHint, new ArrayList<>());
            }
            preferencesMapping.get(providerHint).add(notificationPreference);
        }
    }
    // Once we have created the mapping, save all the preferences using their correct providers
    for (String providerHint : preferencesMapping.keySet()) {
        try {
            NotificationPreferenceProvider provider = componentManager.getInstance(NotificationPreferenceProvider.class, providerHint);
            provider.savePreferences(preferencesMapping.get(providerHint));
        } catch (ComponentLookupException e) {
            logger.error("Unable to retrieve the notification preference provide for hint {}: {}", providerHint, e);
        }
    }
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NotificationPreferenceProvider(org.xwiki.notifications.preferences.NotificationPreferenceProvider)

Example 2 with NotificationPreferenceProvider

use of org.xwiki.notifications.preferences.NotificationPreferenceProvider in project xwiki-platform by xwiki.

the class DefaultNotificationPreferenceManager method getProviders.

private List<NotificationPreferenceProvider> getProviders() throws NotificationException {
    try {
        // Get every registered notification provider
        List<NotificationPreferenceProvider> providers = componentManager.getInstanceList(NotificationPreferenceProvider.class);
        // We handle conflicts between similar preferences by sorting the providers by order. Since
        // notificationPreferences is a set, only the first occurrence of a preference is stored.
        Collections.sort(providers, (o1, o2) -> o2.getProviderPriority() - o1.getProviderPriority());
        return providers;
    } catch (ComponentLookupException e) {
        throw new NotificationException("Unable to fetch the notifications preferences providers from the component manager", e);
    }
}
Also used : NotificationException(org.xwiki.notifications.NotificationException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NotificationPreferenceProvider(org.xwiki.notifications.preferences.NotificationPreferenceProvider)

Aggregations

ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 NotificationPreferenceProvider (org.xwiki.notifications.preferences.NotificationPreferenceProvider)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 NotificationException (org.xwiki.notifications.NotificationException)1 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)1