Example usage for javax.persistence.criteria Join get

List of usage examples for javax.persistence.criteria Join get

Introduction

In this page you can find the example usage for javax.persistence.criteria Join get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

From source file:org.wallride.repository.CategorySpecifications.java

public static Specification<Category> hasArticle(String language) {
    return (root, query, cb) -> {
        query.distinct(true);// ww w. j a  v  a  2s.c o m

        Subquery<Long> subquery = query.subquery(Long.class);
        Root<Article> a = subquery.from(Article.class);
        Join<Article, Category> c = a.join(Article_.categories, JoinType.INNER);
        subquery.select(c.get(Category_.id)).where(cb.equal(a.get(Article_.status), Article.Status.PUBLISHED));

        List<Predicate> predicates = new ArrayList<>();
        predicates.add(root.get(Category_.id).in(subquery));
        predicates.add(cb.equal(root.get(Category_.language), language));
        return cb.and(predicates.toArray(new Predicate[0]));
    };
}

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

public static Specification<BlogPost> search(final Huddle huddle, final SearchBlogPostRequest request) {
    return new Specification<BlogPost>() {
        @Override//w  w w .j a v  a 2s.com
        public Predicate toPredicate(Root<BlogPost> blogPost, CriteriaQuery<?> query, CriteriaBuilder builder) {

            Predicate conjunction = builder.conjunction();
            conjunction.getExpressions().add(builder.equal(blogPost.get("huddle"), huddle));

            if (StringUtils.isNotBlank(request.getPhrase())) {
                String phrase = "%" + request.getPhrase().toLowerCase() + "%";
                conjunction.getExpressions()
                        .add(builder.like(builder.lower(blogPost.<String>get("title")), phrase));
            }

            if (CollectionUtils.isNotEmpty(request.getBlogIds())) {
                Join<Object, Object> blog = blogPost.join("blog");
                conjunction.getExpressions().add(builder.in(blog.get("id")).value(request.getBlogIds()));
            }

            if (!request.isIncludeUnpublished()) {
                conjunction.getExpressions()
                        .add(builder.lessThan((Expression) blogPost.get("publishedOn"), new DateTime()));
            }

            return conjunction;
        }
    };
}

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//w  w w.  j a va2  s.  c  om
 */
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/*from ww w . j  a  v  a 2 s  .  com*/
 */
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  a 2  s . 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/*w w 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/*  w w  w .ja v a  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.netflix.genie.server.repository.jpa.ClusterSpecs.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  ww  w .  j ava 2s.co  m*/
 */
public static Specification<Cluster> findClustersForCommand(final String commandId,
        final Set<ClusterStatus> statuses) {
    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);

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

            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.ClusterSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param clusterCriteria The cluster criteria
 * @param commandCriteria The command Criteria
 * @return The specification//  www. j  a  va2s.c  om
 */
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.goodhuddle.huddle.repository.MemberSpecification.java

public static Specification<Member> search(final Huddle huddle, final SearchMembersRequest request) {
    return new Specification<Member>() {
        @Override/*from  ww  w .  ja  v  a2s .com*/
        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;
        }
    };
}