Search in sources :

Example 21 with NotificationFilterPreference

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

the class DefaultModelBridge method updateExistingObjects.

/**
 * Update existing XObjects with the values contained in the map of filter preferences.
 *
 * @param toSave map of filter preferences to save, sorted by name
 * @param notificationFilterPreferenceClass class of the XObjects
 * @param doc the document on which the objects are located
 */
private void updateExistingObjects(Map<String, NotificationFilterPreference> toSave, DocumentReference notificationFilterPreferenceClass, XWikiDocument doc) {
    // Get existing objects
    List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
    if (preferencesObj == null) {
        return;
    }
    for (BaseObject obj : preferencesObj) {
        if (obj == null) {
            continue;
        }
        // Get the filter preference corresponding to this base object
        NotificationFilterPreference correspondingPref = toSave.get(obj.getStringValue(FILTER_PREFERENCE_NAME));
        if (correspondingPref == null) {
            // If there is none, pass to the next base object
            continue;
        }
        // Update the existing base object with the values of the filter preferences
        convertFilterPreferenceToBaseObject(correspondingPref, obj);
        // Remove this preference from the map of preferences to save, so that remaining preferences
        // will be stored in new objects
        toSave.remove(correspondingPref);
    }
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 22 with NotificationFilterPreference

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

the class DefaultModelBridge method createNewObjects.

/**
 * Create new XObjects to save the filter preferences present in the given collection.
 *
 * @param toSave map of filter preferences to save, sorted by name
 * @param notificationFilterPreferenceClass class of the XObjects
 * @param doc the document on which the objects are located
 * @param context XWiki Context
 *
 * @throws XWikiException if error happens
 */
private void createNewObjects(Map<String, NotificationFilterPreference> toSave, DocumentReference notificationFilterPreferenceClass, XWikiDocument doc, XWikiContext context) throws XWikiException {
    for (NotificationFilterPreference filterPreference : toSave.values()) {
        int objNumber = doc.createXObject(notificationFilterPreferenceClass, context);
        BaseObject obj = doc.getXObject(notificationFilterPreferenceClass, objNumber);
        convertFilterPreferenceToBaseObject(filterPreference, obj);
    }
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 23 with NotificationFilterPreference

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

the class WatchlistBridgeProvider method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException {
    if (!configuration.isEnabled()) {
        return Collections.emptySet();
    }
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    Set<NotificationFilterPreference> results = new HashSet<>();
    try {
        XWikiDocument document = xwiki.getDocument(user, context);
        List<BaseObject> objects = document.getXObjects(CLASS_REFERENCE.appendParent(user.getWikiReference()));
        if (objects == null) {
            return Collections.emptySet();
        }
        for (BaseObject obj : objects) {
            if (obj == null) {
                continue;
            }
            getValues(obj, FIELD_WIKIS, NotificationFilterProperty.WIKI, results);
            getValues(obj, FIELD_SPACES, NotificationFilterProperty.SPACE, results);
            getValues(obj, FIELD_DOCUMENTS, NotificationFilterProperty.PAGE, results);
        }
    } catch (XWikiException e) {
        logger.error("Failed to read the preferences of the watchlist for the user {}.", user, e);
    }
    return results;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultNotificationFilterPreference(org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) HashSet(java.util.HashSet) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 24 with NotificationFilterPreference

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

the class DefaultWatchedEntitiesManagerTest method watchWhenNoFilterMatch.

@Test
public void watchWhenNoFilterMatch() throws Exception {
    // Mocks
    WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Filters
    NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
    when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
    when(watchedEntityReference.isWatched(user)).thenReturn(false);
    NotificationFilterPreference createdPref = mock(NotificationFilterPreference.class);
    when(watchedEntityReference.createInclusiveFilterPreference()).thenReturn(createdPref);
    // Test
    mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
    // Checks
    verify(notificationFilterManager).saveFilterPreferences(eq(Sets.newSet(createdPref)));
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 25 with NotificationFilterPreference

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

the class DefaultWatchedEntitiesManagerTest method watchWhen2OppositeFilters.

@Test
public void watchWhen2OppositeFilters() throws Exception {
    // Mocks
    WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Filters
    NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
    when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
    when(pref1.getFilterType()).thenReturn(NotificationFilterType.INCLUSIVE);
    when(pref1.isEnabled()).thenReturn(true);
    when(pref1.getFilterPreferenceName()).thenReturn("pref1");
    NotificationFilterPreference pref2 = mock(NotificationFilterPreference.class);
    when(pref2.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref2.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(watchedEntityReference.matchExactly(pref2)).thenReturn(true);
    when(pref2.getFilterType()).thenReturn(NotificationFilterType.EXCLUSIVE);
    when(pref2.isEnabled()).thenReturn(true);
    when(pref2.getFilterPreferenceName()).thenReturn("pref2");
    when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1, pref2));
    when(watchedEntityReference.isWatched(user)).thenReturn(false, true);
    // Test
    mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
    // Checks
    verify(notificationFilterManager).deleteFilterPreference("pref2");
    verify(watchedEntityReference, never()).createInclusiveFilterPreference();
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

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