Search in sources :

Example 1 with PropertyValueNode

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

the class ExpressionNodeToHQLConverterTest method parseWithInNode.

@Test
public void parseWithInNode() {
    AbstractNode testAST = new InNode(new PropertyValueNode(EventProperty.PAGE), Arrays.asList(new StringValueNode(TEST_VALUE_1), new StringValueNode(TEST_VALUE_2)));
    ExpressionNodeToHQLConverter.HQLQuery result = parser.parse(testAST);
    assertEquals(String.format("event.page IN (:%s, :%s)", TEST_VALUE_1_IDENTIFIER, TEST_VALUE_2_IDENTIFIER), result.getQuery());
    assertEquals(TEST_VALUE_1, result.getQueryParameters().get(TEST_VALUE_1_IDENTIFIER));
    assertEquals(TEST_VALUE_2, result.getQueryParameters().get(TEST_VALUE_2_IDENTIFIER));
}
Also used : InNode(org.xwiki.notifications.filters.expression.InNode) AbstractNode(org.xwiki.notifications.filters.expression.generics.AbstractNode) StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) Test(org.junit.Test)

Example 2 with PropertyValueNode

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

the class ExpressionNodeToHQLConverterTest method parseWithOrderBy.

@Test
public void parseWithOrderBy() {
    AbstractNode testAST = new OrderByNode(new EqualsNode(new PropertyValueNode(EventProperty.SPACE), new PropertyValueNode(EventProperty.PAGE)), new PropertyValueNode(EventProperty.DATE), OrderByNode.Order.ASC);
    ExpressionNodeToHQLConverter.HQLQuery result = parser.parse(testAST);
    assertEquals("event.space = event.page ORDER BY event.date ASC", result.getQuery());
}
Also used : AbstractNode(org.xwiki.notifications.filters.expression.generics.AbstractNode) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) NotEqualsNode(org.xwiki.notifications.filters.expression.NotEqualsNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) Test(org.junit.Test)

Example 3 with PropertyValueNode

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

the class QueryGeneratorTest method generateQueryWithFilters.

@Test
public void generateQueryWithFilters() throws Exception {
    // Mocks
    NotificationFilter notificationFilter1 = mock(NotificationFilter.class);
    NotificationFilter notificationFilter2 = mock(NotificationFilter.class);
    when(notificationFilterManager.getFilters(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(Sets.newSet(notificationFilter1, notificationFilter2));
    when(notificationFilter1.filterExpression(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(new AndNode(new EqualsNode(new PropertyValueNode(EventProperty.PAGE), new StringValueNode("someValue1")), new EqualsNode(new StringValueNode("1"), new StringValueNode("1"))));
    when(notificationFilter2.filterExpression(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(new AndNode(new EqualsNode(new PropertyValueNode(EventProperty.TYPE), new StringValueNode("someValue2")), new EqualsNode(new StringValueNode("2"), new StringValueNode("2"))));
    when(notificationFilter1.matchesPreference(any(NotificationPreference.class))).thenReturn(true);
    when(notificationFilter2.matchesPreference(any(NotificationPreference.class))).thenReturn(true);
    // Test
    ExpressionNode node = mocker.getComponentUnderTest().generateQueryExpression(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, null, startDate, Arrays.asList("event1", "event2"));
    assertEquals("(((DATE >= \"Thu Jan 01 01:00:00 CET 1970\" " + "AND (((TYPE = \"create\" AND DATE >= \"Fri Jan 02 04:46:40 CET 1970\") " + "AND (PAGE = \"someValue1\" AND \"1\" = \"1\")) " + "AND (TYPE = \"someValue2\" AND \"2\" = \"2\"))) " + "AND NOT (ID IN (\"event1\", \"event2\"))) " + "AND HIDDEN <> true) " + "ORDER BY DATE DESC", node.toString());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) AndNode(org.xwiki.notifications.filters.expression.AndNode) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with PropertyValueNode

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

the class QueryGenerator method generateQueryExpression.

/**
 * Generate the query.
 *
 * @param user user interested in the notifications
 * @param format only match notifications enabled for that format
 * @param endDate do not return events happened after this date
 * @param startDate do not return events happened before this date. Note that since 9.7RC1, this start date is
 * completely optional, {@link NotificationPreference#getStartDate()} should be used for more granular control on
 * notifications
 * @param blackList list of ids of blacklisted events to not return (to not get already known events again)
 * @return the query to execute
 *
 * @throws NotificationException if error happens
 * @throws QueryException if error happens
 *
 * @since 9.8RC1
 */
public ExpressionNode generateQueryExpression(DocumentReference user, NotificationFormat format, Date endDate, Date startDate, List<String> blackList) throws NotificationException, QueryException {
    // First: get the active preferences of the given user
    List<NotificationPreference> preferences = notificationPreferenceManager.getPreferences(user, true, format);
    // Ensure that we have at least one filter preference that is active
    if (preferences.isEmpty() && notificationFilterManager.getFilterPreferences(user).stream().noneMatch(NotificationFilterPreference::isActive)) {
        return null;
    }
    AbstractOperatorNode topNode = null;
    // Condition 1: (maybe) events have happened after the given start date
    if (startDate != null) {
        topNode = new GreaterThanNode(new PropertyValueNode(EventProperty.DATE), new DateValueNode(startDate));
    }
    // Condition 2: handle other preferences
    AbstractOperatorNode preferencesNode = handleEventPreferences(user, preferences);
    // Condition 3: handle exclusive global notification filters
    AbstractOperatorNode globalExclusiveFiltersNode = handleExclusiveGlobalFilters(user, format);
    if (globalExclusiveFiltersNode != null) {
        if (preferencesNode == null) {
            preferencesNode = globalExclusiveFiltersNode;
        } else {
            preferencesNode = preferencesNode.and(globalExclusiveFiltersNode);
        }
    }
    // Condition 4: handle inclusive global notification filters
    AbstractOperatorNode globalInclusiveFiltersNode = handleInclusiveGlobalFilters(user, format);
    if (globalInclusiveFiltersNode != null) {
        if (preferencesNode == null) {
            preferencesNode = globalInclusiveFiltersNode;
        } else {
            preferencesNode = preferencesNode.or(globalInclusiveFiltersNode);
        }
    }
    // Mix all these conditions
    if (preferencesNode != null) {
        if (topNode != null) {
            topNode = topNode.and(preferencesNode);
        } else {
            topNode = preferencesNode;
        }
    }
    // Other basic filters
    topNode = handleBlackList(blackList, topNode);
    topNode = handleEndDate(endDate, topNode);
    topNode = handleHiddenEvents(topNode);
    topNode = handleWiki(user, topNode);
    topNode = handleOrder(topNode);
    return topNode;
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) AbstractOperatorNode(org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode) DateValueNode(org.xwiki.notifications.filters.expression.DateValueNode) GreaterThanNode(org.xwiki.notifications.filters.expression.GreaterThanNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode)

Example 5 with PropertyValueNode

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

the class ExpressionNodeToHQLConverterTest method parseWithLesserThanNode.

@Test
public void parseWithLesserThanNode() {
    AbstractNode testAST = new LesserThanNode(new PropertyValueNode(EventProperty.DATE), new StringValueNode(TEST_VALUE_1));
    ExpressionNodeToHQLConverter.HQLQuery result = parser.parse(testAST);
    assertEquals(String.format("event.date <= :%s", TEST_VALUE_1_IDENTIFIER), result.getQuery());
    assertEquals(TEST_VALUE_1, result.getQueryParameters().get(TEST_VALUE_1_IDENTIFIER));
}
Also used : LesserThanNode(org.xwiki.notifications.filters.expression.LesserThanNode) AbstractNode(org.xwiki.notifications.filters.expression.generics.AbstractNode) StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) Test(org.junit.Test)

Aggregations

PropertyValueNode (org.xwiki.notifications.filters.expression.PropertyValueNode)6 Test (org.junit.Test)5 StringValueNode (org.xwiki.notifications.filters.expression.StringValueNode)4 AbstractNode (org.xwiki.notifications.filters.expression.generics.AbstractNode)4 EqualsNode (org.xwiki.notifications.filters.expression.EqualsNode)2 GreaterThanNode (org.xwiki.notifications.filters.expression.GreaterThanNode)2 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)2 DocumentReference (org.xwiki.model.reference.DocumentReference)1 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)1 AndNode (org.xwiki.notifications.filters.expression.AndNode)1 DateValueNode (org.xwiki.notifications.filters.expression.DateValueNode)1 ExpressionNode (org.xwiki.notifications.filters.expression.ExpressionNode)1 InNode (org.xwiki.notifications.filters.expression.InNode)1 LesserThanNode (org.xwiki.notifications.filters.expression.LesserThanNode)1 NotEqualsNode (org.xwiki.notifications.filters.expression.NotEqualsNode)1 AbstractOperatorNode (org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode)1