Example usage for javax.persistence.criteria CriteriaBuilder isMember

List of usage examples for javax.persistence.criteria CriteriaBuilder isMember

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder isMember.

Prototype

<E, C extends Collection<E>> Predicate isMember(E elem, Expression<C> collection);

Source Link

Document

Create a predicate that tests whether an element is a member of a collection.

Usage

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Operator> root,
        OperatorQueryCommand command) {/*from  w  w  w.  j  a v  a 2s .c  o m*/
    List<Predicate> ps = Lists.newArrayListWithCapacity(3);
    ps.add(cb.equal(root.get(Operator_.deleteFlag), command.isDeleteFlag()));
    if (command.getCustomer() != null) {
        ps.add(cb.isMember(command.getCustomer(), root.get(Operator_.customers)));
    }
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(Operator_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.netflix.genie.server.repository.jpa.ClusterSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param clusterCriteria The cluster criteria
 * @param commandCriteria The command Criteria
 * @return The specification//from ww w  . j  a  v a 2  s.co  m
 */
public static Specification<Cluster> findByClusterAndCommandCriteria(final ClusterCriteria clusterCriteria,
        final Set<String> commandCriteria) {
    return new Specification<Cluster>() {
        @Override
        public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            final Join<Cluster, Command> commands = root.join(Cluster_.commands);

            cq.distinct(true);

            predicates.add(cb.equal(commands.get(Command_.status), CommandStatus.ACTIVE));
            predicates.add(cb.equal(root.get(Cluster_.status), ClusterStatus.UP));

            if (commandCriteria != null) {
                for (final String tag : commandCriteria) {
                    predicates.add(cb.isMember(tag, commands.get(Command_.tags)));

                }
            }

            if (clusterCriteria != null) {
                for (final String tag : clusterCriteria.getTags()) {
                    predicates.add(cb.isMember(tag, root.get(Cluster_.tags)));
                }
            }

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Task> root,
        TaskQueryCommand command) {/*from   w  w w.  j  a va2 s.c om*/
    Predicate p1 = cb.equal(root.get(Task_.charger), command.getOperator());
    ListJoin<Task, Operator> joinFollowers = root.join(Task_.followers, JoinType.LEFT);
    Predicate p2 = cb.equal(joinFollowers.get(Operator_.id), command.getOperator().getId());
    ListJoin<Task, Operator> joinExecutors = root.join(Task_.executors, JoinType.LEFT);
    Predicate p3 = cb.equal(joinExecutors.get(Operator_.id), command.getOperator().getId());
    Predicate p = cb.or(p1, p2, p3);
    if (command.getExecutor() != null) {
        p = cb.and(p, cb.equal(p, cb.isMember(command.getExecutor(), root.get(Task_.executors))));
    }
    if (command.getCustomer() != null) {
        p = cb.and(p, cb.equal(root.get(Task_.customer), command.getCustomer()));
    }
    if (!isBlank(command.getContent())) {
        p = cb.and(p, cb.like(root.get(Task_.content), command.getContentQuery()));
    }
    Collection<TaskType> TaskTypes = command.getTypes();
    if (TaskTypes != null && !TaskTypes.isEmpty()) {
        p = cb.and(p, root.get(Task_.type).in(TaskTypes));
    }
    Collection<TaskStatus> statuses = command.getStatuses();
    if (statuses != null && !statuses.isEmpty()) {
        p = cb.and(p, root.get(Task_.status).in(statuses));
    }
    if (command.getCreateDate() != null) {
        Date createDateStart = LocalDate.fromDateFields(command.getCreateDate()).toDate();
        Date createDateEnd = LocalDate.fromDateFields(command.getCreateDate()).plusDays(1).toDate();
        p = cb.and(p, cb.between(root.get(Task_.logInfo).get(LogInfo_.createDateTime), createDateStart,
                createDateEnd));
        // TODO timestamp date convert
        //p = cb.and(p, cb.equal(root.get(Task_.logInfo).get(LogInfo_.createDateTime).as(java.sql.Date.class), command.getCreateDate()));
    }
    cq.where(p);
}

From source file:com.netflix.genie.server.repository.jpa.ClusterSpecs.java

/**
 * Generate a specification given the parameters.
 *
 * @param name          The name of the cluster to find
 * @param statuses      The statuses of the clusters to find
 * @param tags          The tags of the clusters to find
 * @param minUpdateTime The minimum updated time of the clusters to find
 * @param maxUpdateTime The maximum updated time of the clusters to find
 * @return The specification/*from   w  w w.  j a v  a2s . c o  m*/
 */
public static Specification<Cluster> find(final String name, final Set<ClusterStatus> statuses,
        final Set<String> tags, final Long minUpdateTime, final Long maxUpdateTime) {
    return new Specification<Cluster>() {
        @Override
        public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(name)) {
                predicates.add(cb.like(root.get(Cluster_.name), name));
            }
            if (minUpdateTime != null) {
                predicates.add(cb.greaterThanOrEqualTo(root.get(Cluster_.updated), new Date(minUpdateTime)));
            }
            if (maxUpdateTime != null) {
                predicates.add(cb.lessThan(root.get(Cluster_.updated), new Date(maxUpdateTime)));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Cluster_.tags)));
                    }
                }
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final ClusterStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Cluster_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

From source file:com.netflix.genie.server.repository.jpa.ApplicationSpecs.java

/**
 * Get a specification using the specified parameters.
 *
 * @param name     The name of the application
 * @param userName The name of the user who created the application
 * @param statuses The status of the application
 * @param tags     The set of tags to search the command for
 * @return A specification object used for querying
 *//*from   ww  w  .j  a  v  a  2  s.  c o m*/
public static Specification<Application> find(final String name, final String userName,
        final Set<ApplicationStatus> statuses, final Set<String> tags) {
    return new Specification<Application>() {
        @Override
        public Predicate toPredicate(final Root<Application> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(name)) {
                predicates.add(cb.equal(root.get(Application_.name), name));
            }
            if (StringUtils.isNotBlank(userName)) {
                predicates.add(cb.equal(root.get(Application_.user), userName));
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final ApplicationStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Application_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Application_.tags)));
                    }
                }
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

From source file:com.netflix.genie.server.repository.jpa.CommandSpecs.java

/**
 * Get a specification using the specified parameters.
 *
 * @param name     The name of the command
 * @param userName The name of the user who created the command
 * @param statuses The status of the command
 * @param tags     The set of tags to search the command for
 * @return A specification object used for querying
 *//*ww  w .  ja v  a  2s.co  m*/
public static Specification<Command> find(final String name, final String userName,
        final Set<CommandStatus> statuses, final Set<String> tags) {
    return new Specification<Command>() {
        @Override
        public Predicate toPredicate(final Root<Command> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(name)) {
                predicates.add(cb.equal(root.get(Command_.name), name));
            }
            if (StringUtils.isNotBlank(userName)) {
                predicates.add(cb.equal(root.get(Command_.user), userName));
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final CommandStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Command_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Command_.tags)));
                    }
                }
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

From source file:com.netflix.genie.server.repository.jpa.JobSpecs.java

/**
 * Find jobs based on the parameters./*from w  w  w  .ja  va  2  s  .  com*/
 *
 * @param id          The job id
 * @param jobName     The job name
 * @param userName    The user who created the job
 * @param statuses    The job statuses
 * @param tags        The tags for the jobs to find
 * @param clusterName The cluster name
 * @param clusterId   The cluster id
 * @param commandName The command name
 * @param commandId   The command id
 * @return The specification
 */
public static Specification<Job> find(final String id, final String jobName, final String userName,
        final Set<JobStatus> statuses, final Set<String> tags, final String clusterName, final String clusterId,
        final String commandName, final String commandId) {
    return new Specification<Job>() {
        @Override
        public Predicate toPredicate(final Root<Job> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(id)) {
                predicates.add(cb.like(root.get(Job_.id), id));
            }
            if (StringUtils.isNotBlank(jobName)) {
                predicates.add(cb.like(root.get(Job_.name), jobName));
            }
            if (StringUtils.isNotBlank(userName)) {
                predicates.add(cb.equal(root.get(Job_.user), userName));
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final JobStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Job_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Job_.tags)));
                    }
                }
            }
            if (StringUtils.isNotBlank(clusterName)) {
                predicates.add(cb.equal(root.get(Job_.executionClusterName), clusterName));
            }
            if (StringUtils.isNotBlank(clusterId)) {
                predicates.add(cb.equal(root.get(Job_.executionClusterId), clusterId));
            }
            if (StringUtils.isNotBlank(commandName)) {
                predicates.add(cb.equal(root.get(Job_.commandName), commandName));
            }
            if (StringUtils.isNotBlank(commandId)) {
                predicates.add(cb.equal(root.get(Job_.commandId), commandId));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a JPA2 predicate based on a Filter
 * /*from   ww w  .  j a va  2s. c  o m*/
 * @param filter
 *            the filter
 * @param builder
 *            the criteria builder
 * @param root
 *            the entity root
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Predicate createPredicate(Filter filter, CriteriaBuilder builder, Root<?> root) {
    if (filter == null) {
        return null;
    }

    if (filter instanceof And) {
        return createAndPredicate(builder, root, filter);
    } else if (filter instanceof Or) {
        return createOrPredicate(builder, root, filter);
    } else if (filter instanceof Not) {
        Not not = (Not) filter;
        return builder.not(createPredicate(not.getFilter(), builder, root));
    } else if (filter instanceof Between) {
        Between between = (Between) filter;
        Expression property = getPropertyPath(root, between.getPropertyId());
        return builder.between(property, (Comparable) between.getStartValue(),
                (Comparable) between.getEndValue());
    } else if (filter instanceof Compare) {
        return createComparePredicate(builder, root, filter);
    } else if (filter instanceof IsNull) {
        IsNull isNull = (IsNull) filter;
        return builder.isNull(getPropertyPath(root, isNull.getPropertyId()));
    } else if (filter instanceof Like) {
        return createLikePredicate(builder, root, filter);
    } else if (filter instanceof Contains) {
        Contains contains = (Contains) filter;
        return builder.isMember(contains.getValue(),
                (Expression) getPropertyPath(root, contains.getPropertyId()));
    } else if (filter instanceof In) {
        In in = (In) filter;
        if (in.getValues() != null && !in.getValues().isEmpty()) {
            Expression exp = getPropertyPath(root, in.getPropertyId());
            return exp.in(in.getValues());
        } else {
            Expression exp = getPropertyPath(root, in.getPropertyId());
            return exp.in(Lists.newArrayList(-1));
        }
    } else if (filter instanceof Modulo) {
        return createModuloPredicate(builder, root, filter);
    }

    throw new UnsupportedOperationException("Filter: " + filter.getClass().getName() + " not recognized");
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.emailParameter = this.createParameterExpression(String.class, "email");

    this.getUsersByPrimaryEmailQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() {
                @Override/*ww  w .  jav  a  2 s. co  m*/
                public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb
                            .createQuery(ConferenceUserImpl.class);
                    final Root<ConferenceUserImpl> definitionRoot = criteriaQuery
                            .from(ConferenceUserImpl.class);
                    criteriaQuery.select(definitionRoot);
                    criteriaQuery
                            .where(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter));

                    return criteriaQuery;
                }
            });

    this.getUsersByAnyEmailQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() {
                @Override
                public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb
                            .createQuery(ConferenceUserImpl.class);
                    final Root<ConferenceUserImpl> definitionRoot = criteriaQuery
                            .from(ConferenceUserImpl.class);
                    criteriaQuery.select(definitionRoot);
                    criteriaQuery.where(
                            cb.or(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter),
                                    cb.isMember(emailParameter,
                                            definitionRoot.get(ConferenceUserImpl_.additionalEmails))));

                    return criteriaQuery;
                }
            });
}