Search in sources :

Example 6 with QueryCriteria

use of org.jbpm.query.jpa.data.QueryCriteria in project jbpm by kiegroup.

the class JPAAuditLogService method createDeleteQuery.

private static String createDeleteQuery(String queryBase, QueryWhere queryWhere, Map<String, Object> queryParams, boolean skipMetaParams, String subQuery) {
    // setup
    StringBuilder queryBuilder = new StringBuilder(queryBase);
    QueryAndParameterAppender queryAppender = new QueryAndParameterAppender(queryBuilder, queryParams);
    boolean addLastCriteria = false;
    List<Object[]> varValCriteriaList = new ArrayList<Object[]>();
    List<QueryCriteria> queryWhereCriteriaList = queryWhere.getCriteria();
    // 3. apply normal query parameters
    checkVarValCriteria(queryWhere, varValCriteriaList);
    // last criteria
    Iterator<QueryCriteria> iter = queryWhereCriteriaList.iterator();
    while (iter.hasNext()) {
        QueryCriteria criteria = iter.next();
        if (criteria.getListId().equals(LAST_VARIABLE_LIST)) {
            addLastCriteria = true;
            iter.remove();
        }
    }
    for (QueryCriteria criteria : queryWhere.getCriteria()) {
        String listId = criteria.getListId();
        switch(criteria.getType()) {
            case NORMAL:
                queryAppender.addQueryParameters(criteria.getParameters(), listId, criteriaFieldClasses.get(listId), criteriaFields.get(listId), criteria.isUnion());
                break;
            case RANGE:
                queryAppender.addRangeQueryParameters(criteria.getParameters(), listId, criteriaFieldClasses.get(listId), criteriaFields.get(listId), criteria.isUnion());
                break;
            case REGEXP:
                List<String> stringParams = castToStringList(criteria.getParameters());
                queryAppender.addRegexQueryParameters(stringParams, listId, criteriaFields.get(listId), criteria.isUnion());
                break;
            default:
                throw new IllegalArgumentException("Unknown criteria type in delete query builder: " + criteria.getType().toString());
        }
    }
    while (queryAppender.getParenthesesNesting() > 0) {
        queryAppender.closeParentheses();
    }
    // 6. Add special criteria
    boolean addWhereClause = !queryAppender.hasBeenUsed();
    if (!varValCriteriaList.isEmpty()) {
        addVarValCriteria(addWhereClause, queryAppender, "l", varValCriteriaList);
        addWhereClause = false;
    }
    if (addLastCriteria) {
        addLastInstanceCriteria(queryAppender);
    }
    if (subQuery != null && !subQuery.isEmpty()) {
        queryAppender.addToQueryBuilder(subQuery, false);
    }
    // 8. return query
    return queryBuilder.toString();
}
Also used : QueryCriteria(org.jbpm.query.jpa.data.QueryCriteria) ArrayList(java.util.ArrayList) QueryAndParameterAppender(org.jbpm.query.jpa.impl.QueryAndParameterAppender)

Example 7 with QueryCriteria

use of org.jbpm.query.jpa.data.QueryCriteria in project jbpm by kiegroup.

the class QueryCriteriaUtil method createPredicateFromIntersectingCriteriaList.

/**
 * This method is necessary because the AND operator in SQL has precedence over the OR operator.
 * </p>
 * That means that intersecting criteria should always be grouped together (and processed first, basically), which is essentially
 * what this method does.
 *
 * @param query The {@link CriteriaQuery} that is being built
 * @param intersectingCriteriaList The list of intersecting (ANDed) {@link QueryCriteria}
 * @param builder The {@link CriteriaBuilder} builder instance
 * @param queryType The (persistent {@link Entity}) {@link Class} that we are querying on
 * @return A {@link Predicate} created on the basis of the given {@link List} of {@link QueryCriteria}
 */
private <R, T> Predicate createPredicateFromIntersectingCriteriaList(CriteriaQuery<R> query, CriteriaBuilder builder, Class<T> queryType, List<QueryCriteria> intersectingCriteriaList, QueryWhere queryWhere) {
    combineIntersectingRangeCriteria(intersectingCriteriaList);
    assert intersectingCriteriaList.size() > 0 : "Empty list of currently intersecting criteria!";
    Predicate[] intersectingPredicates = new Predicate[intersectingCriteriaList.size()];
    int i = 0;
    for (QueryCriteria intersectingCriteria : intersectingCriteriaList) {
        Predicate predicate = createPredicateFromSingleOrGroupCriteria(query, builder, queryType, intersectingCriteria, queryWhere);
        assert predicate != null : "Null predicate when evaluating individual intersecting criteria [" + intersectingCriteria.toString() + "]";
        intersectingPredicates[i++] = predicate;
    }
    Predicate predicate;
    if (intersectingPredicates.length > 1) {
        predicate = builder.and(intersectingPredicates);
    } else {
        predicate = intersectingPredicates[0];
    }
    return predicate;
}
Also used : QueryCriteria(org.jbpm.query.jpa.data.QueryCriteria) Predicate(javax.persistence.criteria.Predicate)

Example 8 with QueryCriteria

use of org.jbpm.query.jpa.data.QueryCriteria in project jbpm by kiegroup.

the class QueryCriteriaUtil method createPredicateFromCriteriaList.

/**
 * This method is contains the setup steps for creating and assembling {@link Predicate} instances
 * from the information in a {@link List} of {@link QueryCriteria} instances.
 * </p>
 * The steps taken when assembling a {@link Predicate} are the following:
 * <ol>
 * <li>Separate the given {@link List} of {@link QueryCriteria} into an intersection and disjunction (union) list.</li>
 * <li>Combine separate "range" {@link QueryCriteria} that apply to the same listId</li>
 * <li>Call the {@link #createPredicateFromCriteriaList(CriteriaQuery, List, CriteriaBuilder, Class, boolean)}
 * method on disjunction criteria list and on the intersection criteria list</li>
 * <li>Take the result of the previous step and appropriately combine the returned {@link Predicate} instances into a
 * final {@link Predicate} instance that is then returned.</li>
 * </ol>
 * @param query The {@link CriteriaQuery} instance that we're assembling {@link Predicate} instances for
 * @param inputCriteriaList The list of {@link QueryCriteria} instances that will be processed
 * @param builder A {@link CriteriaBuilder} instance to help us build {@link Predicate} instances
 * @param resultType The {@link Class} (type) of the result, given so that later methods can use it
 * @return A {@link Predicate} instance based on the given {@link QueryCriteria} list
 */
private <R, T> Predicate createPredicateFromCriteriaList(CriteriaQuery<R> query, CriteriaBuilder builder, Class<T> resultType, List<QueryCriteria> inputCriteriaList, QueryWhere queryWhere) {
    Predicate queryPredicate = null;
    if (inputCriteriaList.size() > 1) {
        List<Predicate> predicateList = new LinkedList<Predicate>();
        QueryCriteria previousCriteria = null;
        QueryCriteria firstCriteria = null;
        List<QueryCriteria> currentIntersectingCriteriaList = new LinkedList<QueryCriteria>();
        int i = 0;
        for (QueryCriteria criteria : inputCriteriaList) {
            assert i++ != 0 || criteria.isFirst() : "First criteria is not flagged as first!";
            if (criteria.isFirst()) {
                firstCriteria = previousCriteria = criteria;
                continue;
            } else if (firstCriteria != null) {
                if (criteria.isUnion()) {
                    Predicate predicate = createPredicateFromSingleOrGroupCriteria(query, builder, resultType, previousCriteria, queryWhere);
                    predicateList.add(predicate);
                } else {
                    currentIntersectingCriteriaList.add(firstCriteria);
                }
                firstCriteria = null;
            }
            if (criteria.isUnion()) {
                // - create a predicate from the AND criteria
                if (previousCriteria != null && !previousCriteria.isUnion() && !currentIntersectingCriteriaList.isEmpty()) {
                    Predicate predicate = createPredicateFromIntersectingCriteriaList(query, builder, resultType, currentIntersectingCriteriaList, queryWhere);
                    assert predicate != null : "Null predicate when evaluating intersecting criteria [" + criteria.toString() + "]";
                    predicateList.add(predicate);
                    // - new (empty) current intersecting criteria list
                    currentIntersectingCriteriaList = new LinkedList<QueryCriteria>();
                }
                // Process the current union criteria
                Predicate predicate = createPredicateFromSingleOrGroupCriteria(query, builder, resultType, criteria, queryWhere);
                assert predicate != null : "Null predicate when evaluating union criteria [" + criteria.toString() + "]";
                predicateList.add(predicate);
            } else {
                currentIntersectingCriteriaList.add(criteria);
            }
            previousCriteria = criteria;
        }
        if (!currentIntersectingCriteriaList.isEmpty()) {
            Predicate predicate = createPredicateFromIntersectingCriteriaList(query, builder, resultType, currentIntersectingCriteriaList, queryWhere);
            predicateList.add(predicate);
        }
        assert !predicateList.isEmpty() : "The predicate list should not (can not?) be empty here!";
        if (predicateList.size() == 1) {
            queryPredicate = predicateList.get(0);
        } else {
            Predicate[] predicates = predicateList.toArray(new Predicate[predicateList.size()]);
            queryPredicate = builder.or(predicates);
        }
    } else if (inputCriteriaList.size() == 1) {
        QueryCriteria singleCriteria = inputCriteriaList.get(0);
        queryPredicate = createPredicateFromSingleOrGroupCriteria(query, builder, resultType, singleCriteria, queryWhere);
    }
    return queryPredicate;
}
Also used : QueryCriteria(org.jbpm.query.jpa.data.QueryCriteria) LinkedList(java.util.LinkedList) Predicate(javax.persistence.criteria.Predicate)

Example 9 with QueryCriteria

use of org.jbpm.query.jpa.data.QueryCriteria in project jbpm by kiegroup.

the class TaskSummaryQueryCriteriaUtil method addUserRolesLimitCriteria.

/**
 * Adds an (intersecting) {@link QueryCriteria} that limits the results to results that the user is allowed to see
 *
 * @param queryWhere The {@link QueryWhere} instance that defines the query criteria
 * @param userId The user id
 * @param groupIds The user's group ids
 */
private void addUserRolesLimitCriteria(QueryWhere queryWhere, String userId, List<String> groupIds) {
    List<QueryCriteria> newBaseCriteriaList = new ArrayList<QueryCriteria>(2);
    // user role limiting criteria
    QueryCriteria userRolesLimitingCriteria = new QueryCriteria(QueryParameterIdentifiers.TASK_USER_ROLES_LIMIT_LIST, false, QueryCriteriaType.NORMAL, 2);
    userRolesLimitingCriteria.setFirst(true);
    userRolesLimitingCriteria.getValues().add(userId);
    userRolesLimitingCriteria.getValues().add(groupIds);
    newBaseCriteriaList.add(userRolesLimitingCriteria);
    // original criteria list in a new group
    if (!queryWhere.getCriteria().isEmpty()) {
        QueryCriteria originalBaseCriteriaGroup = new QueryCriteria(false);
        originalBaseCriteriaGroup.setCriteria(queryWhere.getCriteria());
        newBaseCriteriaList.add(originalBaseCriteriaGroup);
    }
    queryWhere.setCriteria(newBaseCriteriaList);
}
Also used : QueryCriteria(org.jbpm.query.jpa.data.QueryCriteria) ArrayList(java.util.ArrayList)

Aggregations

QueryCriteria (org.jbpm.query.jpa.data.QueryCriteria)9 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 Predicate (javax.persistence.criteria.Predicate)3 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 QueryAndParameterAppender (org.jbpm.query.jpa.impl.QueryAndParameterAppender)1 TaskVariableImpl (org.jbpm.services.task.audit.impl.model.TaskVariableImpl)1 TaskImpl (org.jbpm.services.task.impl.model.TaskImpl)1