Example usage for javax.persistence.criteria Root join

List of usage examples for javax.persistence.criteria Root join

Introduction

In this page you can find the example usage for javax.persistence.criteria Root join.

Prototype

<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create an inner join to the specified single-valued attribute.

Usage

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  a va 2 s . c  o m*/
 */
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//w w w .  j  a  v a2  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.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/*www . ja  va2 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.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//  ww  w.  ja  v a 2 s  .c  o 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 ww . j a  va  2  s  .  com*/
 */
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.core.jpa.specifications.JpaClusterSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param clusterCriteria The cluster criteria
 * @param commandCriteria The command Criteria
 * @return The specification/*ww w . j a v  a2 s  .  c om*/
 */
public static Specification<ClusterEntity> findByClusterAndCommandCriteria(
        final ClusterCriteria clusterCriteria, final Set<String> commandCriteria) {
    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);

        cq.distinct(true);

        predicates.add(cb.equal(root.get(ClusterEntity_.status), ClusterStatus.UP));

        if (clusterCriteria != null && clusterCriteria.getTags() != null
                && !clusterCriteria.getTags().isEmpty()) {
            predicates.add(cb.like(root.get(ClusterEntity_.tags),
                    JpaSpecificationUtils.getTagLikeString(clusterCriteria.getTags())));
        }

        predicates.add(cb.equal(commands.get(CommandEntity_.status), CommandStatus.ACTIVE));

        if (commandCriteria != null && !commandCriteria.isEmpty()) {
            predicates.add(cb.like(commands.get(CommandEntity_.tags),
                    JpaSpecificationUtils.getTagLikeString(commandCriteria)));
        }

        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   www  .  j  av  a  2  s .com
 */
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/*  w  w  w.  j  a  va  2s. 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.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  a2s.co m*/
        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

/**
 * 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//  w  ww.  ja v  a  2  s . com
 */
public static Specification<ClusterEntity> find(@Nullable final String name,
        @Nullable final Set<ClusterStatus> statuses, @Nullable final Set<TagEntity> tags,
        @Nullable final Instant minUpdateTime, @Nullable final Instant maxUpdateTime) {
    return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(name)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(ClusterEntity_.name), name));
        }
        if (minUpdateTime != null) {
            predicates.add(cb.greaterThanOrEqualTo(root.get(ClusterEntity_.updated), minUpdateTime));
        }
        if (maxUpdateTime != null) {
            predicates.add(cb.lessThan(root.get(ClusterEntity_.updated), maxUpdateTime));
        }
        if (tags != null && !tags.isEmpty()) {
            final Join<ClusterEntity, TagEntity> tagEntityJoin = root.join(ClusterEntity_.tags);
            predicates.add(tagEntityJoin.in(tags));
            cq.groupBy(root.get(ClusterEntity_.id));
            cq.having(cb.equal(cb.count(root.get(ClusterEntity_.id)), tags.size()));
        }
        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()]));
    };
}