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);
}
}
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);
}
}
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;
}
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)));
}
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();
}
Aggregations