Search in sources :

Example 1 with GreaterThanNode

use of org.xwiki.notifications.filters.expression.GreaterThanNode 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 GreaterThanNode

use of org.xwiki.notifications.filters.expression.GreaterThanNode 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 3 with GreaterThanNode

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

the class ExpressionNodeToHQLConverterTest method parseWithGreaterThanNode.

@Test
public void parseWithGreaterThanNode() {
    AbstractNode testAST = new GreaterThanNode(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 : AbstractNode(org.xwiki.notifications.filters.expression.generics.AbstractNode) StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) GreaterThanNode(org.xwiki.notifications.filters.expression.GreaterThanNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) Test(org.junit.Test)

Aggregations

GreaterThanNode (org.xwiki.notifications.filters.expression.GreaterThanNode)3 PropertyValueNode (org.xwiki.notifications.filters.expression.PropertyValueNode)3 DateValueNode (org.xwiki.notifications.filters.expression.DateValueNode)2 StringValueNode (org.xwiki.notifications.filters.expression.StringValueNode)2 AbstractOperatorNode (org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode)2 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Inject (javax.inject.Inject)1 Named (javax.inject.Named)1 Singleton (javax.inject.Singleton)1 Test (org.junit.Test)1 Component (org.xwiki.component.annotation.Component)1 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 NotificationException (org.xwiki.notifications.NotificationException)1