Search in sources :

Example 1 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class QueryGeneratorTest method setUp.

@Before
public void setUp() throws Exception {
    queryManager = mocker.getInstance(QueryManager.class);
    notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
    serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    userPreferencesSource = mocker.getInstance(ConfigurationSource.class, "user");
    wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
    notificationFilterManager = mocker.getInstance(NotificationFilterManager.class);
    startDate = new Date(10);
    query = mock(Query.class);
    when(queryManager.createQuery(anyString(), anyString())).thenReturn(query);
    pref1StartDate = new Date(100000000);
    NotificationPreference pref1 = mock(NotificationPreference.class);
    when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
    when(pref1.getFormat()).thenReturn(NotificationFormat.ALERT);
    when(pref1.getStartDate()).thenReturn(pref1StartDate);
    when(pref1.isNotificationEnabled()).thenReturn(true);
    when(notificationPreferenceManager.getPreferences(userReference, true, NotificationFormat.ALERT)).thenReturn(Arrays.asList(pref1));
    NotificationFilterPreference fakeFilterPreference = mock(NotificationFilterPreference.class);
    when(fakeFilterPreference.isActive()).thenReturn(true);
    when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(fakeFilterPreference));
    when(userPreferencesSource.getProperty("displayHiddenDocuments", 0)).thenReturn(0);
    when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
}
Also used : NotificationFilterManager(org.xwiki.notifications.filters.NotificationFilterManager) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) ConfigurationSource(org.xwiki.configuration.ConfigurationSource) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) Query(org.xwiki.query.Query) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) QueryManager(org.xwiki.query.QueryManager) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 2 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user, NotificationFilter filter, NotificationFilterType filterType, NotificationFormat format) throws NotificationException {
    Set<NotificationFilterPreference> preferences = getFilterPreferences(user, filter);
    Iterator<NotificationFilterPreference> it = preferences.iterator();
    while (it.hasNext()) {
        NotificationFilterPreference preference = it.next();
        if (!preference.getFilterFormats().contains(format) || !preference.getFilterType().equals(filterType)) {
            it.remove();
        }
    }
    return preferences;
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference)

Example 3 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user, NotificationFilter filter) throws NotificationException {
    Set<NotificationFilterPreference> preferences = getFilterPreferences(user);
    Iterator<NotificationFilterPreference> it = preferences.iterator();
    while (it.hasNext()) {
        NotificationFilterPreference preference = it.next();
        if (!filter.getName().equals(preference.getFilterName())) {
            it.remove();
        }
    }
    return preferences;
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference)

Example 4 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference 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 5 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user, NotificationFilter filter, NotificationFilterType filterType) throws NotificationException {
    Set<NotificationFilterPreference> preferences = getFilterPreferences(user);
    Iterator<NotificationFilterPreference> it = preferences.iterator();
    while (it.hasNext()) {
        NotificationFilterPreference preference = it.next();
        if (!(filter.getName().equals(preference.getFilterName()) && preference.getFilterType().equals(filterType))) {
            it.remove();
        }
    }
    return preferences;
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference)

Aggregations

NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)29 Test (org.junit.Test)14 DocumentReference (org.xwiki.model.reference.DocumentReference)13 WatchedEntityReference (org.xwiki.notifications.filters.watch.WatchedEntityReference)8 BaseObject (com.xpn.xwiki.objects.BaseObject)4 HashSet (java.util.HashSet)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiContext (com.xpn.xwiki.XWikiContext)3 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 HashMap (java.util.HashMap)3 NotificationException (org.xwiki.notifications.NotificationException)3 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)3 Set (java.util.Set)2 Event (org.xwiki.eventstream.Event)2 SpaceReference (org.xwiki.model.reference.SpaceReference)2 WikiReference (org.xwiki.model.reference.WikiReference)2 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)2 NotificationFilterProperty (org.xwiki.notifications.filters.NotificationFilterProperty)2 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)2