Search in sources :

Example 26 with NotificationPreference

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

the class AbstractDocumentNotificationPreferenceProvider method savePreferences.

@Override
public void savePreferences(List<NotificationPreference> preferences) throws NotificationException {
    // Create a map of preferences per target, so we can save the target's document only once for all preferences
    Map<EntityReference, List<NotificationPreference>> preferencesPerTarget = new HashMap<>();
    for (NotificationPreference preference : preferences) {
        if (preference instanceof TargetableNotificationPreference) {
            TargetableNotificationPreference targetablePreference = (TargetableNotificationPreference) preference;
            List<NotificationPreference> list = preferencesPerTarget.get(targetablePreference.getTarget());
            if (list == null) {
                list = new ArrayList<>();
                preferencesPerTarget.put(targetablePreference.getTarget(), list);
            }
            list.add(targetablePreference);
        } else {
            logger.warn("Unsupported NotificationPreference class: [{}]. This preference will not be saved.", preference.getClass().getName());
        }
    }
    for (Map.Entry<EntityReference, List<NotificationPreference>> entry : preferencesPerTarget.entrySet()) {
        savePreferences(entry.getValue(), entry.getKey());
    }
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) HashMap(java.util.HashMap) EntityReference(org.xwiki.model.reference.EntityReference) ArrayList(java.util.ArrayList) List(java.util.List) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with NotificationPreference

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

the class CachedModelBridge method getNotificationsPreferences.

@Override
public List<NotificationPreference> getNotificationsPreferences(WikiReference wikiReference) throws NotificationException {
    // We need to store the wiki reference in the cache's key
    final String specificWikiNotificationsPreferences = String.format(CACHE_KEY_PATTERN, WIKI_NOTIFICATIONS_PREFERENCES, serializer.serialize(wikiReference));
    ExecutionContext context = execution.getContext();
    if (context.hasProperty(specificWikiNotificationsPreferences)) {
        return (List<NotificationPreference>) context.getProperty(specificWikiNotificationsPreferences);
    }
    List<NotificationPreference> preferences = modelBridge.getNotificationsPreferences(wikiReference);
    context.setProperty(specificWikiNotificationsPreferences, preferences);
    return preferences;
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) ExecutionContext(org.xwiki.context.ExecutionContext) List(java.util.List)

Example 28 with NotificationPreference

use of org.xwiki.notifications.preferences.NotificationPreference 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 29 with NotificationPreference

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

the class MinorEventNotificationFilterTest method filterExpression.

@Test
public void filterExpression() throws Exception {
    NotificationPreference fakePreference = mock(NotificationPreference.class);
    DocumentReference randomUser = new DocumentReference("xwiki", "XWiki", "UserA");
    assertNull(mocker.getComponentUnderTest().filterExpression(randomUser, fakePreference));
    assertEquals("NOT ((TYPE = \"update\" AND NOT (DOCUMENT_VERSION ENDS WITH \".1\")))", mocker.getComponentUnderTest().filterExpression(randomUser, NotificationFilterType.EXCLUSIVE, NotificationFormat.ALERT).toString());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 30 with NotificationPreference

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

the class ScopeNotificationFilterTest method matchPreferenceWithCorrectPreference.

@Test
public void matchPreferenceWithCorrectPreference() throws Exception {
    NotificationPreference preference = mock(NotificationPreference.class);
    when(preference.getCategory()).thenReturn(NotificationPreferenceCategory.DEFAULT);
    when(preference.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, ""));
    assertTrue(mocker.getComponentUnderTest().matchesPreference(preference));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) Test(org.junit.Test)

Aggregations

NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)32 Test (org.junit.Test)18 DocumentReference (org.xwiki.model.reference.DocumentReference)12 HashMap (java.util.HashMap)8 List (java.util.List)8 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)8 Date (java.util.Date)7 ArrayList (java.util.ArrayList)6 WikiReference (org.xwiki.model.reference.WikiReference)5 NotificationException (org.xwiki.notifications.NotificationException)4 NotificationFormat (org.xwiki.notifications.NotificationFormat)4 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)4 TargetableNotificationPreference (org.xwiki.notifications.preferences.TargetableNotificationPreference)4 Map (java.util.Map)3 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)3 NotificationPreferenceManager (org.xwiki.notifications.preferences.NotificationPreferenceManager)3 Query (org.xwiki.query.Query)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2