Example usage for javax.persistence.criteria CriteriaBuilder like

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

Introduction

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

Prototype

Predicate like(Expression<String> x, String pattern);

Source Link

Document

Create a predicate for testing whether the expression satisfies the given pattern.

Usage

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

public static <E> Predicate stringPredicate(Expression<String> path, Object attrValue,
        final SearchParameters sp, final CriteriaBuilder builder) {
    if (sp.isCaseInsensitive()) {
        path = builder.lower(path);//  w  ww  .  ja  v a  2 s. c o  m
        attrValue = ((String) attrValue).toLowerCase(Locale.ENGLISH);
    }

    switch (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:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Operator> root,
        OperatorQueryCommand command) {//from w w  w .  j  av  a  2  s .  c om
    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.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 a2 s .co  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:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static Predicate createLikePredicate(CriteriaBuilder builder, Root<?> root, Filter filter) {
    Like like = (Like) filter;/*ww  w.  j av a 2  s.  c om*/
    if (like.isCaseSensitive()) {
        return builder.like((Expression) getPropertyPath(root, like.getPropertyId()), like.getValue());
    } else {
        return builder.like(builder.lower((Expression) getPropertyPath(root, like.getPropertyId())),
                like.getValue().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) {/*  w  w w . j ava 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(VALUE)), term))));
}

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  va2  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(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  w ww.j a v  a 2  s.  c om*/
    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: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);//from  w w w .j ava  2s.  co m
        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);/* w  w  w  .  j av a2s .c om*/
        //   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!");
    }
}