Example usage for javax.persistence.criteria CriteriaBuilder equal

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

Introduction

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

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

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

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

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

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Operator> root,
        OperatorQueryCommand command) {//from   w ww  .j a  v a  2 s . 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.web.data.repositories.jpa.specifications.JpaClusterSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param commandId The id of the command that is registered with this cluster
 * @param statuses  The status of the cluster
 * @return The specification//from w  w w . j a  v  a  2  s . c o m
 */
public static Specification<ClusterEntity> findClustersForCommand(final String commandId,
        @Nullable final Set<ClusterStatus> statuses) {
    return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        final Join<ClusterEntity, CommandEntity> commands = root.join(ClusterEntity_.commands);

        predicates.add(cb.equal(commands.get(CommandEntity_.uniqueId), commandId));

        if (statuses != null && !statuses.isEmpty()) {
            //Could optimize this as we know size could use native array
            predicates.add(
                    cb.or(statuses.stream().map(status -> cb.equal(root.get(ClusterEntity_.status), status))
                            .toArray(Predicate[]::new)));
        }

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

From source file:com.netflix.genie.core.jpa.specifications.JpaCommandSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param applicationId The id of the application that is registered with these commands
 * @param statuses      The status of the commands
 * @return The specification/* w ww. j av a2  s .  c  om*/
 */
public static Specification<CommandEntity> findCommandsForApplication(final String applicationId,
        final Set<CommandStatus> statuses) {
    return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        final Join<CommandEntity, ApplicationEntity> application = root.join(CommandEntity_.applications);

        predicates.add(cb.equal(application.get(ApplicationEntity_.id), applicationId));

        if (statuses != null && !statuses.isEmpty()) {
            final List<Predicate> orPredicates = statuses.stream()
                    .map(status -> cb.equal(root.get(CommandEntity_.status), status))
                    .collect(Collectors.toList());
            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.web.data.repositories.jpa.specifications.JpaCommandSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param applicationId The id of the application that is registered with these commands
 * @param statuses      The status of the commands
 * @return The specification//from w  w  w .jav  a2s  . c  o m
 */
public static Specification<CommandEntity> findCommandsForApplication(final String applicationId,
        @Nullable final Set<CommandStatus> statuses) {
    return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        final Join<CommandEntity, ApplicationEntity> application = root.join(CommandEntity_.applications);

        predicates.add(cb.equal(application.get(ApplicationEntity_.uniqueId), applicationId));

        if (statuses != null && !statuses.isEmpty()) {
            predicates.add(
                    cb.or(statuses.stream().map(status -> cb.equal(root.get(CommandEntity_.status), status))
                            .toArray(Predicate[]::new)));
        }

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

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

/**
 * Get all the clusters given the specified parameters.
 *
 * @param applicationId The id of the application that is registered with these commands
 * @param statuses The status of the commands
 * @return The specification/*from ww w. j a  v  a 2s  .co  m*/
 */
public static Specification<Command> findCommandsForApplication(final String applicationId,
        final Set<CommandStatus> statuses) {
    return new Specification<Command>() {
        @Override
        public Predicate toPredicate(final Root<Command> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            final Join<Command, Application> application = root.join(Command_.application);

            predicates.add(cb.equal(application.get(Application_.id), applicationId));

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

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

From source file:com.netflix.genie.core.jpa.specifications.JpaClusterSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param commandId The id of the command that is registered with this cluster
 * @param statuses  The status of the cluster
 * @return The specification/*from   w  ww  .ja  va  2 s. c o m*/
 */
public static Specification<ClusterEntity> findClustersForCommand(final String commandId,
        final Set<ClusterStatus> statuses) {
    return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        final Join<ClusterEntity, CommandEntity> commands = root.join(ClusterEntity_.commands);

        predicates.add(cb.equal(commands.get(CommandEntity_.id), commandId));

        if (statuses != null && !statuses.isEmpty()) {
            //Could optimize this as we know size could use native array
            final List<Predicate> orPredicates = statuses.stream()
                    .map(status -> cb.equal(root.get(ClusterEntity_.status), status))
                    .collect(Collectors.toList());
            predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
        }

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

From source file:com.goodhuddle.huddle.repository.MemberSpecification.java

public static Specification<Member> search(final Huddle huddle, final SearchMembersRequest request) {
    return new Specification<Member>() {
        @Override//www  .j a va2s.  c  o m
        public Predicate toPredicate(Root<Member> member, CriteriaQuery<?> query, CriteriaBuilder builder) {

            Predicate conjunction = builder.conjunction();

            // huddle
            conjunction.getExpressions().add(builder.equal(member.get("huddle"), huddle));

            // keywords

            if (StringUtils.isNotBlank(request.getKeywords())) {
                String[] terms = StringUtils.split(request.getKeywords());
                for (String keyword : terms) {
                    if (keyword != null && keyword.length() > 0) {

                        String matchTerm = "%" + keyword.toLowerCase() + "%";
                        conjunction.getExpressions().add(builder.or(
                                builder.like(builder.lower(member.<String>get("username")), matchTerm),
                                builder.like(builder.lower(member.<String>get("email")), matchTerm),
                                builder.like(builder.lower(member.<String>get("firstName")), matchTerm),
                                builder.like(builder.lower(member.<String>get("lastName")), matchTerm)));
                    }
                }
            }

            // security groups

            if (CollectionUtils.isNotEmpty(request.getSecurityGroupIds())) {

                Join<Object, Object> securityGroup = member.join("securityGroup",
                        request.isIncludeNoAccess() ? JoinType.LEFT : JoinType.INNER);

                Predicate disjunction = builder.disjunction();
                for (Long id : request.getSecurityGroupIds()) {
                    disjunction.getExpressions().add(builder.equal(securityGroup.get("id"), id));
                }
                if (request.isIncludeNoAccess()) {
                    disjunction.getExpressions().add(builder.isNull(securityGroup.get("id")));
                }
                conjunction.getExpressions().add(disjunction);

            } else if (request.isIncludeNoAccess()) {

                conjunction.getExpressions().add(builder.isNull(member.get("securityGroup")));

            }

            // tags

            MemberTagFilter tagFilter = request.getTags();
            if (tagFilter != null) {

                if (tagFilter.getIncluded() != null) {

                    if (CollectionUtils.isNotEmpty(tagFilter.getIncluded().getTagIds())) {

                        MemberTagFilter.TagSet included = request.getTags().getIncluded();
                        MatchType matchType = included.getMatchType();
                        Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.conjunction()
                                : builder.disjunction();

                        for (Long tagId : included.getTagIds()) {
                            Subquery<Member> sq = query.subquery(Member.class);
                            Root<Member> subMember = sq.from(Member.class);
                            Join<Member, Tag> tag = subMember.join("tags");
                            sq.select(subMember).where(builder.equal(tag.get("id"), tagId));
                            tagPredicate.getExpressions().add(builder.in(member).value(sq));
                        }

                        conjunction.getExpressions().add(tagPredicate);
                    }
                }

                if (tagFilter.getExcluded() != null) {

                    if (CollectionUtils.isNotEmpty(tagFilter.getExcluded().getTagIds())) {

                        MemberTagFilter.TagSet excluded = request.getTags().getExcluded();
                        MatchType matchType = excluded.getMatchType();
                        Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.disjunction()
                                : builder.conjunction();

                        for (Long tagId : excluded.getTagIds()) {
                            Subquery<Member> sq = query.subquery(Member.class);
                            Root<Member> subMember = sq.from(Member.class);
                            Join<Member, Tag> tag = subMember.join("tags");
                            sq.select(subMember).where(builder.equal(tag.get("id"), tagId));
                            tagPredicate.getExpressions().add(builder.in(member).value(sq).not());
                        }

                        conjunction.getExpressions().add(tagPredicate);
                    }
                }

            }

            return conjunction;
        }
    };
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate notRedacted(Root<Annotation> root, CriteriaBuilder cb) {
    return cb.equal(root.get(REDACTED), false);
}

From source file:org.unidle.feature.step.StepDefs.java

public static Specification<UserConnection> hasService(final String service) {
    return new Specification<UserConnection>() {
        @Override//from ww  w .  j  ava2 s  .  com
        public Predicate toPredicate(final Root<UserConnection> root, final CriteriaQuery<?> criteriaQuery,
                final CriteriaBuilder criteriaBuilder) {
            return criteriaBuilder.equal(root.get(providerId), service);
        }
    };
}