Search in sources :

Example 1 with NotificationFilterProperty

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

the class DefaultModelBridge method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
    Set<NotificationFilterPreference> preferences = new HashSet<>();
    try {
        XWikiDocument doc = xwiki.getDocument(user, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null) {
                    Map<NotificationFilterProperty, List<String>> filterPreferenceProperties = createNotificationFilterPropertiesMap(obj);
                    NotificationFilterType filterType = NotificationFilterType.valueOf(obj.getStringValue(FIELD_FILTER_TYPE).toUpperCase());
                    Set<NotificationFormat> filterFormats = new HashSet<>();
                    for (String format : (List<String>) obj.getListValue(FIELD_FILTER_FORMATS)) {
                        filterFormats.add(NotificationFormat.valueOf(format.toUpperCase()));
                    }
                    // Create the new filter preference and add it to the list of preferences
                    DefaultNotificationFilterPreference notificationFilterPreference = new DefaultNotificationFilterPreference(obj.getStringValue(FILTER_PREFERENCE_NAME));
                    notificationFilterPreference.setProviderHint("userProfile");
                    notificationFilterPreference.setFilterName(obj.getStringValue(FIELD_FILTER_NAME));
                    notificationFilterPreference.setEnabled(obj.getIntValue(FIELD_IS_ENABLED, 1) == 1);
                    notificationFilterPreference.setActive(obj.getIntValue(FIELD_IS_ACTIVE, 1) == 1);
                    notificationFilterPreference.setFilterType(filterType);
                    notificationFilterPreference.setNotificationFormats(filterFormats);
                    notificationFilterPreference.setPreferenceProperties(filterPreferenceProperties);
                    preferences.add(notificationFilterPreference);
                }
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the notification preferences scope for the user [%s].", user), e);
    }
    return preferences;
}
Also used : NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFormat(org.xwiki.notifications.NotificationFormat) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) ArrayList(java.util.ArrayList) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 2 with NotificationFilterProperty

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

the class WatchedLocationReference method createFilterPreference.

private DefaultNotificationFilterPreference createFilterPreference() {
    DefaultNotificationFilterPreference filterPreference = new DefaultNotificationFilterPreference(Long.toString(new Date().getTime()));
    // Fields
    filterPreference.setEnabled(true);
    filterPreference.setFilterType(NotificationFilterType.INCLUSIVE);
    filterPreference.setFilterName(ScopeNotificationFilter.FILTER_NAME);
    filterPreference.setNotificationFormats(ALL_NOTIFICATION_FORMATS);
    filterPreference.setProviderHint(UserProfileNotificationPreferenceProvider.NAME);
    filterPreference.setActive(false);
    // Properties
    Map<NotificationFilterProperty, List<String>> preferenceProperties = new HashMap<>();
    filterPreference.setPreferenceProperties(preferenceProperties);
    preferenceProperties.put(NotificationFilterProperty.EVENT_TYPE, Collections.emptyList());
    // Scope value
    List<String> value = Collections.singletonList(serializedReference);
    switch(entityReference.getType()) {
        case WIKI:
            preferenceProperties.put(NotificationFilterProperty.WIKI, value);
            break;
        case SPACE:
            preferenceProperties.put(NotificationFilterProperty.SPACE, value);
            break;
        case DOCUMENT:
            preferenceProperties.put(NotificationFilterProperty.PAGE, value);
            break;
        default:
            break;
    }
    return filterPreference;
}
Also used : DefaultNotificationFilterPreference(org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference) NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) HashMap(java.util.HashMap) List(java.util.List) Date(java.util.Date)

Example 3 with NotificationFilterProperty

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

the class WatchedUserReference method createInclusiveFilterPreference.

@Override
public NotificationFilterPreference createInclusiveFilterPreference() {
    DefaultNotificationFilterPreference filterPreference = new DefaultNotificationFilterPreference(Long.toString(new Date().getTime()));
    filterPreference.setEnabled(true);
    filterPreference.setFilterType(NotificationFilterType.INCLUSIVE);
    filterPreference.setFilterName(EventUserFilter.FILTER_NAME);
    filterPreference.setNotificationFormats(Sets.newHashSet(NotificationFormat.values()));
    filterPreference.setProviderHint(UserProfileNotificationPreferenceProvider.NAME);
    filterPreference.setActive(true);
    // Properties
    Map<NotificationFilterProperty, List<String>> preferenceProperties = new HashMap<>();
    filterPreference.setPreferenceProperties(preferenceProperties);
    preferenceProperties.put(NotificationFilterProperty.EVENT_TYPE, Collections.emptyList());
    preferenceProperties.put(NotificationFilterProperty.USER, Collections.singletonList(userId));
    return filterPreference;
}
Also used : DefaultNotificationFilterPreference(org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference) NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) HashMap(java.util.HashMap) List(java.util.List) Date(java.util.Date)

Example 4 with NotificationFilterProperty

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

the class DefaultNotificationFilterPreferenceTest method defaultNotificationFilterPreference.

@Test
public void defaultNotificationFilterPreference() throws Exception {
    String filterPreferenceName = "fp1";
    String filterName = "f1";
    boolean isEnabled = true;
    boolean isActive = true;
    NotificationFilterType filterType = NotificationFilterType.INCLUSIVE;
    Set<NotificationFormat> notificationFormats = Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL);
    Map<NotificationFilterProperty, List<String>> propertiesMap = new HashMap<>();
    propertiesMap.put(NotificationFilterProperty.APPLICATION, Arrays.asList("a1", "a2"));
    DefaultNotificationFilterPreference preference = new DefaultNotificationFilterPreference(filterPreferenceName);
    preference.setFilterName(filterName);
    preference.setEnabled(isEnabled);
    preference.setActive(isActive);
    preference.setFilterType(filterType);
    preference.setNotificationFormats(notificationFormats);
    preference.setPreferenceProperties(propertiesMap);
    assertEquals(filterPreferenceName, preference.getFilterPreferenceName());
    assertEquals(filterName, preference.getFilterName());
    assertEquals(isEnabled, preference.isEnabled());
    assertEquals(isActive, preference.isActive());
    assertEquals(filterType, preference.getFilterType());
    assertEquals(notificationFormats, preference.getFilterFormats());
    assertEquals(propertiesMap.get(NotificationFilterProperty.APPLICATION), preference.getProperties(NotificationFilterProperty.APPLICATION));
}
Also used : NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) HashMap(java.util.HashMap) NotificationFormat(org.xwiki.notifications.NotificationFormat) List(java.util.List) Test(org.junit.Test)

Example 5 with NotificationFilterProperty

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

the class ScopeNotificationFilterTest method mockNotificationFilterPreference.

private NotificationFilterPreference mockNotificationFilterPreference(String entityStringValue, EntityReference resultReference, NotificationFilterType filterType, String eventName) {
    NotificationFilterProperty property;
    if (resultReference.getType().equals(EntityType.DOCUMENT)) {
        property = NotificationFilterProperty.PAGE;
    } else if (resultReference.getType().equals(EntityType.SPACE)) {
        property = NotificationFilterProperty.SPACE;
    } else {
        property = NotificationFilterProperty.WIKI;
    }
    NotificationFilterPreference preference = mock(NotificationFilterPreference.class);
    when(preference.getProperties(property)).thenReturn(Collections.singletonList(entityStringValue));
    when(preference.getFilterName()).thenReturn(ScopeNotificationFilter.FILTER_NAME);
    when(preference.getProperties(eq(NotificationFilterProperty.EVENT_TYPE))).thenReturn(eventName != null ? Arrays.asList(eventName) : Collections.emptyList());
    when(preference.getFilterType()).thenReturn(filterType);
    when(preference.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(preference.isEnabled()).thenReturn(true);
    when(resolver.resolve(entityStringValue, resultReference.getType())).thenReturn(resultReference);
    when(serializer.serialize(eq(resultReference))).thenReturn(entityStringValue);
    return preference;
}
Also used : NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference)

Aggregations

NotificationFilterProperty (org.xwiki.notifications.filters.NotificationFilterProperty)7 List (java.util.List)6 HashMap (java.util.HashMap)5 DefaultNotificationFilterPreference (org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference)4 Date (java.util.Date)3 NotificationFormat (org.xwiki.notifications.NotificationFormat)2 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)2 NotificationFilterType (org.xwiki.notifications.filters.NotificationFilterType)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 NotificationException (org.xwiki.notifications.NotificationException)1