Search in sources :

Example 1 with NotificationPreference

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

the class QueryGenerator method handleEventPreferences.

/**
 * For each notification preference of the given user, add a constraint on the events to
 * - have one of the notification types that have been subscribed by the user;
 * - have a date superior to the start date corresponding to this type;
 * - match the custom defined user filters.
 *
 * @param user the current user
 * @param preferences a list of the user preferences
 * @return a list of maps that contains query parameters
 * @throws NotificationException if an error occurred
 */
private AbstractOperatorNode handleEventPreferences(DocumentReference user, List<NotificationPreference> preferences) throws NotificationException {
    AbstractOperatorNode preferencesNode = null;
    // Filter the notification preferences that are not bound to a specific EVENT_TYPE
    Iterator<NotificationPreference> it = preferences.stream().filter(pref -> pref.getProperties().containsKey(NotificationPreferenceProperty.EVENT_TYPE)).iterator();
    while (it.hasNext()) {
        NotificationPreference preference = it.next();
        AbstractOperatorNode preferenceTypeNode = new AndNode(new EqualsNode(value(EventProperty.TYPE), value((String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE))), new GreaterThanNode(value(EventProperty.DATE), value(preference.getStartDate())));
        // Get the notification filters that can be applied to the current preference
        Collection<NotificationFilter> filters = notificationFilterManager.getFilters(user, preference);
        for (NotificationFilter filter : filters) {
            ExpressionNode node = filter.filterExpression(user, preference);
            if (node != null && node instanceof AbstractOperatorNode) {
                preferenceTypeNode = preferenceTypeNode.and((AbstractOperatorNode) node);
            }
        }
        if (preferencesNode == null) {
            preferencesNode = preferenceTypeNode;
        } else {
            preferencesNode = preferencesNode.or(preferenceTypeNode);
        }
    }
    return preferencesNode;
}
Also used : StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) BooleanValueNode(org.xwiki.notifications.filters.expression.BooleanValueNode) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) Date(java.util.Date) Component(org.xwiki.component.annotation.Component) NotificationFilterManager(org.xwiki.notifications.filters.NotificationFilterManager) GreaterThanNode(org.xwiki.notifications.filters.expression.GreaterThanNode) NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFormat(org.xwiki.notifications.NotificationFormat) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) Inject(javax.inject.Inject) Map(java.util.Map) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) Named(javax.inject.Named) NotificationException(org.xwiki.notifications.NotificationException) AbstractOperatorNode(org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) InNode(org.xwiki.notifications.filters.expression.InNode) Iterator(java.util.Iterator) EventProperty(org.xwiki.notifications.filters.expression.EventProperty) NotNode(org.xwiki.notifications.filters.expression.NotNode) Collection(java.util.Collection) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) LesserThanNode(org.xwiki.notifications.filters.expression.LesserThanNode) AndNode(org.xwiki.notifications.filters.expression.AndNode) QueryManager(org.xwiki.query.QueryManager) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) ExpressionBuilder.value(org.xwiki.notifications.filters.expression.generics.ExpressionBuilder.value) List(java.util.List) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) AbstractValueNode(org.xwiki.notifications.filters.expression.generics.AbstractValueNode) DocumentReference(org.xwiki.model.reference.DocumentReference) NotEqualsNode(org.xwiki.notifications.filters.expression.NotEqualsNode) ConfigurationSource(org.xwiki.configuration.ConfigurationSource) DateValueNode(org.xwiki.notifications.filters.expression.DateValueNode) EntityReferenceNode(org.xwiki.notifications.filters.expression.EntityReferenceNode) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) AbstractOperatorNode(org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) AndNode(org.xwiki.notifications.filters.expression.AndNode) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) NotEqualsNode(org.xwiki.notifications.filters.expression.NotEqualsNode) GreaterThanNode(org.xwiki.notifications.filters.expression.GreaterThanNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter)

Example 2 with NotificationPreference

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

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

the class NotificationPreferenceScriptServiceTest method isEventTypeEnabledForWiki.

@Test
public void isEventTypeEnabledForWiki() throws Exception {
    WikiReference wiki = new WikiReference("whatever");
    when(notificationPreferenceManager.getAllPreferences(wiki)).thenReturn(Collections.emptyList());
    assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT, wiki.getName()));
    NotificationPreference pref1 = mock(NotificationPreference.class);
    NotificationPreference pref2 = mock(NotificationPreference.class);
    when(pref1.getFormat()).thenReturn(NotificationFormat.EMAIL);
    Map<NotificationPreferenceProperty, Object> properties1 = new HashMap<>();
    properties1.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
    when(pref1.getProperties()).thenReturn(properties1);
    when(pref2.getFormat()).thenReturn(NotificationFormat.ALERT);
    Map<NotificationPreferenceProperty, Object> properties2 = new HashMap<>();
    properties2.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
    when(pref2.getProperties()).thenReturn(properties2);
    when(pref2.isNotificationEnabled()).thenReturn(true);
    when(notificationPreferenceManager.getAllPreferences(wiki)).thenReturn(Arrays.asList(pref1, pref2));
    assertTrue(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT, wiki.getName()));
    assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.EMAIL, wiki.getName()));
}
Also used : AbstractNotificationPreference(org.xwiki.notifications.preferences.internal.AbstractNotificationPreference) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Example 4 with NotificationPreference

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

the class DefaultModelBridge method getNotificationPreferences.

private List<NotificationPreference> getNotificationPreferences(DocumentReference document, String providerHint) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationPreferencesClass = NOTIFICATION_PREFERENCE_CLASS.setWikiReference(document.getWikiReference());
    List<NotificationPreference> preferences = new ArrayList<>();
    try {
        XWikiDocument doc = xwiki.getDocument(document, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationPreferencesClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null) {
                    String objFormat = obj.getStringValue(FORMAT_FIELD);
                    Date objStartDate = obj.getDateValue(START_DATE_FIELD);
                    Map<NotificationPreferenceProperty, Object> properties = extractNotificationPreferenceProperties(obj);
                    notificationPreferenceBuilder.prepare();
                    notificationPreferenceBuilder.setProperties(properties);
                    notificationPreferenceBuilder.setStartDate((objStartDate != null) ? objStartDate : doc.getCreationDate());
                    notificationPreferenceBuilder.setFormat(StringUtils.isNotBlank(objFormat) ? NotificationFormat.valueOf(objFormat.toUpperCase()) : NotificationFormat.ALERT);
                    notificationPreferenceBuilder.setTarget(document);
                    notificationPreferenceBuilder.setProviderHint(providerHint);
                    notificationPreferenceBuilder.setEnabled(obj.getIntValue(NOTIFICATION_ENABLED_FIELD, 0) != 0);
                    notificationPreferenceBuilder.setCategory(NotificationPreferenceCategory.DEFAULT);
                    preferences.add(notificationPreferenceBuilder.build());
                }
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the notification preferences from the document [%s].", document), e);
    }
    return preferences;
}
Also used : ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with NotificationPreference

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

the class DefaultNotificationPreferenceManagerTest method getNotificationPreferences.

@Test
public void getNotificationPreferences() throws Exception {
    DocumentReference user = new DocumentReference("xwiki", "test", "user");
    mockPreferenceProviders(user);
    List<NotificationPreference> preferences = mocker.getComponentUnderTest().getAllPreferences(user);
    assertEquals(4, preferences.size());
    assertTrue(preferences.contains(mockPreference11));
    assertTrue(preferences.contains(mockPreference12));
    assertTrue(preferences.contains(mockPreference21));
    assertTrue(preferences.contains(mockPreference22));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) DocumentReference(org.xwiki.model.reference.DocumentReference) 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