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