Example usage for javax.persistence.criteria CriteriaBuilder and

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

Introduction

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

Prototype

Predicate and(Predicate... restrictions);

Source Link

Document

Create a conjunction of the given restriction predicates.

Usage

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 v a  2 s .c  o  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.github.dactiv.orm.core.spring.data.jpa.specification.support.PropertySpecification.java

public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

    Predicate predicate = builder.and(JpaRestrictionBuilder.getRestriction(propertyName, value, restrictionName,
            new SpecificationEntity(root, query, builder)));

    return predicate;
}

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//from  w  w w.  j  av a 2 s  .  c o m
 */
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:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate buildFilterSpec(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        UserAccount requester, String username, String group, Long target, String vocab, String attributeLevel,
        String attributeName, String attributeReqLevel, Boolean showRedacted, Boolean parentless,
        Date createdBefore, Date createdAfter, Date modifiedBefore, Date modifiedAfter) {
    final List<Predicate> predicates = new ArrayList<>(0);
    final Predicate authPredicate = authFilter(root, cb, requester);
    final List<Predicate> filterPredicates = buildFilterPredicates(root, query, cb, username, group, target,
            vocab, attributeLevel, attributeName, attributeReqLevel, showRedacted, parentless, createdBefore,
            createdAfter, modifiedBefore, modifiedAfter);

    predicates.add(authPredicate);//from  w  ww  .jav a2s  . c  om
    predicates.addAll(filterPredicates);

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

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

/**
 * Get a specification using the specified parameters.
 *
 * @param name     The name of the command
 * @param user     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
 *//*  w w w .  j  a v  a2  s  . co  m*/
public static Specification<CommandEntity> find(final String name, final String user,
        final Set<CommandStatus> statuses, final Set<String> tags) {
    return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(name)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(CommandEntity_.name), name));
        }
        if (StringUtils.isNotBlank(user)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(CommandEntity_.user), user));
        }
        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()])));
        }
        if (tags != null && !tags.isEmpty()) {
            predicates
                    .add(cb.like(root.get(CommandEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags)));
        }
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
}

From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java

@Deprecated
protected static Predicate getPredicate(final Class clazz, final Restriction searchTerms,
        Root<Persistable> root, CriteriaBuilder cb) {
    LinkedList<Predicate> predicates = new LinkedList<Predicate>();
    Predicate predicate;//from  ww w .  ja v  a 2 s  .c o m
    // process child restrictions
    if (!CollectionUtils.isEmpty(searchTerms.getRestrictions())) {
        for (Restriction restriction : searchTerms.getRestrictions()) {
            predicates.add(getPredicate(clazz, restriction, root, cb));
        }
    }
    // process main restriction
    if (StringUtils.isNotBlank(searchTerms.getField())) {
        String propertyName = searchTerms.getField();
        addPredicate(clazz, root, cb, predicates,
                searchTerms.getValues().toArray(new String[searchTerms.getValues().size()]), propertyName);
    }
    if (searchTerms.getJunction().equals(Restriction.Junction.OR)) {
        predicate = cb.or(predicates.toArray(new Predicate[predicates.size()]));
    } else {
        predicate = cb.and(predicates.toArray(new Predicate[predicates.size()]));
    }
    return predicate;
}

From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaCommandSpecs.java

/**
 * Get a specification using the specified parameters.
 *
 * @param name     The name of the command
 * @param user     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
 *//* www. ja v  a 2s.c  om*/
public static Specification<CommandEntity> find(@Nullable final String name, @Nullable final String user,
        @Nullable final Set<CommandStatus> statuses, @Nullable final Set<TagEntity> tags) {
    return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(name)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(CommandEntity_.name), name));
        }
        if (StringUtils.isNotBlank(user)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(CommandEntity_.user), user));
        }
        if (statuses != null && !statuses.isEmpty()) {
            predicates.add(
                    cb.or(statuses.stream().map(status -> cb.equal(root.get(CommandEntity_.status), status))
                            .toArray(Predicate[]::new)));
        }
        if (tags != null && !tags.isEmpty()) {
            final Join<CommandEntity, TagEntity> tagEntityJoin = root.join(CommandEntity_.tags);
            predicates.add(tagEntityJoin.in(tags));
            cq.groupBy(root.get(CommandEntity_.id));
            cq.having(cb.equal(cb.count(root.get(CommandEntity_.id)), tags.size()));
        }
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
}

From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java

protected static Predicate getRootPredicate(final Class clazz, final Map<String, String[]> searchTerms,
        Root<Persistable> root, CriteriaBuilder cb, boolean skipSimpleSearch) {
    LinkedList<Predicate> predicates = new LinkedList<Predicate>();
    Predicate predicate;/*from w  ww  .  ja va  2 s  . c om*/

    parseSearchTerms(clazz, searchTerms, root, cb, predicates);
    if (!skipSimpleSearch) {
        // handle "_all", i.e. simple search
        if (searchTerms.containsKey(SIMPLE_SEARCH_PARAM_NAME) && predicates.size() == 0) {
            Map<String, String[]> simpleSearchTerms = getSimpleSearchTerms(clazz,
                    searchTerms.get(SIMPLE_SEARCH_PARAM_NAME));
            parseSearchTerms(clazz, simpleSearchTerms, root, cb, predicates);
        }
    }
    if (searchTerms.containsKey(SEARCH_MODE) && searchTerms.get(SEARCH_MODE)[0].equalsIgnoreCase(OR)
    // A disjunction of zero predicates is false
            && predicates.size() > 0) {
        predicate = cb.or(predicates.toArray(new Predicate[predicates.size()]));
    } else {
        predicate = cb.and(predicates.toArray(new Predicate[predicates.size()]));
    }
    return predicate;
}

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
 */// w w  w  .j  a v  a2 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.greendot.db.jpa.core.AbstractSearchDao.java

@Transactional(readOnly = true)
public Optional<E> findByCharacterType(final String key, final String value) {

    notNull(key, "Mandatory argument 'key' is missing.");
    notNull(value, "Mandatory argument 'value' is missing.");
    final CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<E> query = builder.createQuery(getEntityType());
    final Root<E> root = query.from(getEntityType());
    query.select(root).where(builder.and(builder.equal(root.get(key), value))).distinct(true);
    final List<E> results = getEntityManager().createQuery(query).getResultList();
    if ((results != null) && (results.size() == 1))
        return Optional.of(results.get(0));
    return Optional.empty();
}