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:org.querybyexample.jpa.JpaUtil.java

public static <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode,
        SearchParameters sp, CriteriaBuilder builder) {
    if (!sp.isCaseSensitive()) {
        path = builder.lower(path);/*www. j a v  a  2  s  .  com*/
        attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale());
    }

    switch (searchMode != null ? searchMode : sp.getSearchMode()) {
    case EQUALS:
        return builder.equal(path, attrValue);
    case ENDING_LIKE:
        return builder.like(path, "%" + attrValue);
    case STARTING_LIKE:
        return builder.like(path, attrValue + "%");
    case ANYWHERE:
        return builder.like(path, "%" + attrValue + "%");
    case LIKE:
        return builder.like(path, (String) attrValue); // assume user provide the wild cards
    default:
        throw new IllegalStateException("expecting a search mode!");
    }
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

public static <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode,
        SearchParameters sp, CriteriaBuilder builder) {
    if (!sp.isCaseSensitive()) {
        path = builder.lower(path);//from   ww  w  . j  av  a 2 s .  c o m
        //   attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale());
        attrValue = ((String) attrValue).toLowerCase();
    }
    switch (searchMode != null ? searchMode : sp.getSearchMode()) {
    case EQUALS:
        return builder.equal(path, attrValue);
    case NOT_EQUALS:
        return builder.notEqual(path, attrValue);
    case ENDING_LIKE:
        return builder.like(path, "%" + attrValue);
    case STARTING_LIKE:
        return builder.like(path, attrValue + "%");
    case ANYWHERE:
        return builder.like(path, "%" + attrValue + "%");
    case LIKE:
        return builder.like(path, (String) attrValue); // assume user
                                                       // provide the wild
                                                       // cards
    default:
        throw new IllegalStateException("expecting a search mode!");
    }
}

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

/**
 * Find jobs based on the parameters./*from  w  w  w .j  a va2  s.  c o  m*/
 *
 * @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:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate hasAttributeName(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeName) {/*w  w w.  j a  v a 2s . c  o m*/
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(NAME)), attributeName.toLowerCase()))));
}

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

private static Predicate hasAttributeLevel(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeLevel) {//from   www. j  a  v  a 2  s  .co m
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(LEVEL)), attributeLevel.toLowerCase()))));
}

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

private static Predicate hasAttributeReqLevel(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeReqLevel) {
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(REQ_LEVEL)), attributeReqLevel.toLowerCase()))));
}

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

private static Predicate existsInData(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String term) {/*  ww w  .java  2 s .c  o m*/
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot)
            .where(cb.and(cb.equal(subroot.get(ANNO), root), cb.like(cb.lower(subroot.get(VALUE)), term))));
}

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

/**
 * Find jobs that are zombies.
 *
 * @param currentTime The current time//from  ww w  . j  ava 2  s .c  om
 * @param zombieTime  The time that zombies should be marked by
 * @return The specification for this query
 */
public static Specification<Job> findZombies(final long currentTime, final long zombieTime) {
    return new Specification<Job>() {
        @Override
        public Predicate toPredicate(final Root<Job> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            // the equivalent query is as follows:
            // update Job set status='FAILED', finishTime=$max, exitCode=$zombie_code,
            // statusMsg='Job has been marked as a zombie'
            // where updateTime < $min and (status='RUNNING' or status='INIT')"
            final List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.lessThan(root.get(Job_.updated), new Date(currentTime - zombieTime)));
            final Predicate orPredicate1 = cb.equal(root.get(Job_.status), JobStatus.RUNNING);
            final Predicate orPredicate2 = cb.equal(root.get(Job_.status), JobStatus.INIT);
            predicates.add(cb.or(orPredicate1, orPredicate2));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

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

/**
 * Requester access predicate/* w  w w. j a  v a 2  s .  c o  m*/
 * <p>
 * annotation has public access OR annotation has private access AND
 * annotation belongs to requester OR annotation has group access AND
 * requester is in group
 */
private static Predicate authFilter(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester) {
    final List<Predicate> predicates = new ArrayList<>(0);

    // public access
    predicates.add(cb.like(root.get(ACCESS).get(NAME), PUBLIC_ACCESS));

    // private access AND belongs to requester
    predicates.add(
            cb.and(cb.like(root.get(ACCESS).get(NAME), PRIVATE_ACCESS), cb.equal(root.get(USER), requester)));

    // group access AND requester in group
    // criteriabuilder's in clause throws
    // sql error if collection has no elements
    if (requester.getGroups().size() > 0) {
        predicates.add(cb.and(cb.like(root.get(ACCESS).get(NAME), GROUP_ACCESS),
                root.get(GROUP).in(requester.getGroups())));
    }
    return cb.or(predicates.toArray(new Predicate[predicates.size()]));
}

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

/**
 * Create a modulo predicate/*from  www . ja v a  2s . c om*/
 * 
 * @param builder
 * @param filter
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Predicate createModuloPredicate(CriteriaBuilder builder, Root<?> root, Filter filter) {
    Modulo modulo = (Modulo) filter;
    if (modulo.getModExpression() != null) {
        // compare to a literal expression
        return builder.equal(builder.mod((Expression) getPropertyPath(root, modulo.getPropertyId()),
                (Expression) getPropertyPath(root, modulo.getModExpression())), modulo.getResult());
    } else {
        // compare to a property
        return builder.equal(builder.mod((Expression) getPropertyPath(root, modulo.getPropertyId()),
                modulo.getModValue().intValue()), modulo.getResult());
    }
}