Search in sources :

Example 41 with DocumentReference

use of org.xwiki.model.reference.DocumentReference 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 42 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsUC3.

@Test
public void getEventsUC3() throws Exception {
    // Facts:
    // * Alice updates the page "Bike"
    // * Bob comments the page "Bike"
    // Expected:
    // * Alice has updated the page "Bike"
    // * Bob has commented the page "Bike"
    // Comment: same as UC2 but we make sure we don't lose the event concerning Alice
    // Note: the UC4 described in https://jira.xwiki.org/browse/XWIKI-14114 is actually similar to that one
    // because we don't care of the event' user in our tests.
    // Mocks
    Event event1 = createMockedEvent();
    Event event2 = createMockedEvent();
    Event event3 = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(event1.getDocument()).thenReturn(doc);
    when(event2.getDocument()).thenReturn(doc);
    when(event3.getDocument()).thenReturn(doc);
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(event1.getType()).thenReturn("update");
    when(event2.getType()).thenReturn("addComment");
    when(event3.getType()).thenReturn("update");
    when(event1.getGroupId()).thenReturn("g1");
    when(event2.getGroupId()).thenReturn("g2");
    when(event3.getGroupId()).thenReturn("g2");
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2, event3));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 5);
    // Verify
    assertEquals(2, results.size());
    assertEquals(event1, results.get(0).getEvents().get(0));
    assertEquals(event2, results.get(1).getEvents().get(0));
    assertEquals(event3, results.get(1).getEvents().get(1));
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 43 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsUC1.

@Test
public void getEventsUC1() throws Exception {
    // Facts:
    // * Alice updates the page "Bike"
    // * Bob updates the page "Bike"
    // Expected:
    // * Alice and Bob have updated the page "Bike"
    // Comment:
    // Note: the 2 events have been combined
    // Mocks
    Event eventAlice = createMockedEvent();
    Event eventBob = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(eventAlice.getDocument()).thenReturn(doc);
    when(eventBob.getDocument()).thenReturn(doc);
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(eventAlice.getType()).thenReturn("update");
    when(eventBob.getType()).thenReturn("update");
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventAlice, eventBob));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 2);
    // Verify
    assertEquals(1, results.size());
    assertEquals(eventAlice, results.get(0).getEvents().get(0));
    assertEquals(eventBob, results.get(0).getEvents().get(1));
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 44 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsXWIKI14454.

@Test
public void getEventsXWIKI14454() throws Exception {
    // Facts:
    // * Then Bob updates the page "Bike"
    // * Then Bob updates the page "Bike" again
    // * Then bob add a comment to the "Bike" page
    // Expected:
    // * Bob has commented the page "Bike"
    // * Bob has updated the page "Bike"
    // Mocks
    Event eventUpdate1 = createMockedEvent();
    Event eventUpdate2 = createMockedEvent();
    Event eventAddComment = createMockedEvent();
    Event eventAddCommentUpdate = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(eventUpdate1.getDocument()).thenReturn(doc);
    when(eventUpdate1.toString()).thenReturn("update1");
    when(eventUpdate2.getDocument()).thenReturn(doc);
    when(eventUpdate2.toString()).thenReturn("update2");
    when(eventAddComment.getDocument()).thenReturn(doc);
    when(eventAddComment.toString()).thenReturn("addComment");
    when(eventAddCommentUpdate.getDocument()).thenReturn(doc);
    when(eventAddCommentUpdate.toString()).thenReturn("updateComment");
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(eventUpdate1.getType()).thenReturn("update");
    when(eventUpdate2.getType()).thenReturn("update");
    when(eventAddComment.getType()).thenReturn("addComment");
    when(eventAddCommentUpdate.getType()).thenReturn("update");
    when(eventUpdate1.getGroupId()).thenReturn("g1");
    when(eventUpdate2.getGroupId()).thenReturn("g2");
    when(eventAddComment.getGroupId()).thenReturn("g3");
    when(eventAddCommentUpdate.getGroupId()).thenReturn("g3");
    // They comes with inverse chronological order because of the query
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventAddComment, eventAddCommentUpdate, eventUpdate2, eventUpdate1));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 5);
    // Verify
    assertEquals(2, results.size());
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 45 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class QueryGeneratorTest method generateQueryWithUntilDate.

@Test
public void generateQueryWithUntilDate() throws Exception {
    Date untilDate = new Date(1000000000000L);
    // Test
    ExpressionNode node = mocker.getComponentUnderTest().generateQueryExpression(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, untilDate, startDate, Collections.emptyList());
    // Verify
    assertEquals("(((DATE >= \"Thu Jan 01 01:00:00 CET 1970\" " + "AND (TYPE = \"create\" AND DATE >= \"Fri Jan 02 04:46:40 CET 1970\")) " + "AND DATE <= \"Sun Sep 09 03:46:40 CEST 2001\") AND HIDDEN <> true) " + "ORDER BY DATE DESC", node.toString());
    // Test 2
    mocker.getComponentUnderTest().generateQuery(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, untilDate, startDate, Collections.emptyList());
    verify(queryManager).createQuery("where (((" + "event.date >= :date_688218ea2b05763819a1e155109e4bf1e8921dd72e8b43d4c89c89133d4a5357) " + "AND ((event.type = :value_fa8847b0c33183273f5945508b31c3208a9e4ece58ca47233a05628d8dba3799) " + "AND (event.date >= :date_25db83d7521312b07fa98ca0023df696d1b94ee4fb7c49578c807f5aeb634f7a))) " + "AND (event.date <= :date_582ce8e50c9ad1782bdd021604912ed119e6ab2ff58a094f23b3be0ce6105306)) " + "AND (event.hidden <> true) " + "ORDER BY event.date DESC", Query.HQL);
    verify(query).bindValue("date_688218ea2b05763819a1e155109e4bf1e8921dd72e8b43d4c89c89133d4a5357", startDate);
    verify(query).bindValue("date_25db83d7521312b07fa98ca0023df696d1b94ee4fb7c49578c807f5aeb634f7a", pref1StartDate);
    verify(query).bindValue("date_582ce8e50c9ad1782bdd021604912ed119e6ab2ff58a094f23b3be0ce6105306", untilDate);
}
Also used : ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

DocumentReference (org.xwiki.model.reference.DocumentReference)1324 Test (org.junit.Test)711 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)482 BaseObject (com.xpn.xwiki.objects.BaseObject)250 XWikiContext (com.xpn.xwiki.XWikiContext)186 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)157 ArrayList (java.util.ArrayList)128 WikiReference (org.xwiki.model.reference.WikiReference)127 XWikiException (com.xpn.xwiki.XWikiException)121 EntityReference (org.xwiki.model.reference.EntityReference)113 SpaceReference (org.xwiki.model.reference.SpaceReference)96 XWiki (com.xpn.xwiki.XWiki)82 HashMap (java.util.HashMap)54 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)52 Expectations (org.jmock.Expectations)50 Before (org.junit.Before)50 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)46 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)45 AttachmentReference (org.xwiki.model.reference.AttachmentReference)44 Date (java.util.Date)42