use of org.xwiki.notifications.filters.expression.ExpressionNode 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;
}
use of org.xwiki.notifications.filters.expression.ExpressionNode 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);
}
use of org.xwiki.notifications.filters.expression.ExpressionNode in project xwiki-platform by xwiki.
the class QueryGeneratorTest method generateQueryWithNoRelevantFilters.
@Test
public void generateQueryWithNoRelevantFilters() throws Exception {
// Mocks
NotificationFilter notificationFilter1 = mock(NotificationFilter.class);
when(notificationFilterManager.getFilters(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(Collections.singleton(notificationFilter1));
when(notificationFilter1.filterExpression(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(new EmptyNode());
when(notificationFilter1.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 NOT (ID IN (\"event1\", \"event2\"))) " + "AND HIDDEN <> true) " + "ORDER BY DATE DESC", node.toString());
}
use of org.xwiki.notifications.filters.expression.ExpressionNode in project xwiki-platform by xwiki.
the class QueryGeneratorTest method generateQueryWithUntilDateAndBlackList.
@Test
public void generateQueryWithUntilDateAndBlackList() throws Exception {
Date untilDate = new Date(1000000000000L);
// Test
ExpressionNode node = mocker.getComponentUnderTest().generateQueryExpression(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, untilDate, null, Arrays.asList("event1", "event2"));
// Verify
assertEquals("((((TYPE = \"create\" " + "AND DATE >= \"Fri Jan 02 04:46:40 CET 1970\") " + "AND NOT (ID IN (\"event1\", \"event2\"))) " + "AND DATE <= \"Sun Sep 09 03:46:40 CEST 2001\") " + "AND HIDDEN <> true) " + "ORDER BY DATE DESC", node.toString());
}
use of org.xwiki.notifications.filters.expression.ExpressionNode in project xwiki-platform by xwiki.
the class QueryGeneratorTest method generateQueryWhenHiddenDocsAreEnabled.
@Test
public void generateQueryWhenHiddenDocsAreEnabled() throws Exception {
// Mock
when(userPreferencesSource.getProperty("displayHiddenDocuments", 0)).thenReturn(1);
// Test
ExpressionNode node = mocker.getComponentUnderTest().generateQueryExpression(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, null, startDate, null);
// Verify
assertEquals("(DATE >= \"Thu Jan 01 01:00:00 CET 1970\" " + "AND (TYPE = \"create\" AND DATE >= \"Fri Jan 02 04:46:40 CET 1970\")) " + "ORDER BY DATE DESC", node.toString());
// Test 2
mocker.getComponentUnderTest().generateQuery(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, null, startDate, null);
verify(queryManager).createQuery("where (" + "event.date >= :date_688218ea2b05763819a1e155109e4bf1e8921dd72e8b43d4c89c89133d4a5357) " + "AND ((event.type = :value_fa8847b0c33183273f5945508b31c3208a9e4ece58ca47233a05628d8dba3799) " + "AND (event.date >= :date_25db83d7521312b07fa98ca0023df696d1b94ee4fb7c49578c807f5aeb634f7a)) " + "ORDER BY event.date DESC", Query.HQL);
verify(query).bindValue(eq("date_688218ea2b05763819a1e155109e4bf1e8921dd72e8b43d4c89c89133d4a5357"), eq(startDate));
verify(query).bindValue(eq("value_fa8847b0c33183273f5945508b31c3208a9e4ece58ca47233a05628d8dba3799"), eq("create"));
verify(query).bindValue("date_25db83d7521312b07fa98ca0023df696d1b94ee4fb7c49578c807f5aeb634f7a", pref1StartDate);
}
Aggregations