Example usage for javax.persistence.criteria CriteriaBuilder notLike

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

Introduction

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

Prototype

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

Source Link

Document

Create a predicate for testing whether the expression does not satisfy the given pattern.

Usage

From source file:com.dbs.sdwt.jpa.JpaUtil.java

public <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode,
        SearchParameters sp, CriteriaBuilder builder) {
    if (sp.isCaseInsensitive()) {
        path = builder.lower(path);// www  .j  av  a  2s . c  om
        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
    case NOT_IN:
        return builder.notLike(path, (String) attrValue);
    default:
        throw new IllegalStateException("expecting a search mode!");
    }
}

From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Predicate generatePredicate(Root root, CriteriaBuilder builder,
        QueryParameter... queryParameters) {
    Predicate condition = builder.conjunction();
    for (QueryParameter queryParameter : queryParameters) {
        Object value = queryParameter.getValue();
        if (value == null || StringUtils.isBlank(value.toString())) {
            continue;
        }/*from www  .  j  a v a2s. co  m*/
        Path path = root.get(queryParameter.getKey());
        switch (queryParameter.getMode()) {
        case BETWEEN:
            Object[] values = asArray(value);
            if (values != null) {
                condition = builder.and(builder.between((Path<Comparable>) path, asComparable(values[0]),
                        asComparable(values[1])));
            }
            break;
        case GREATER_THAN:
            condition = builder.and(condition,
                    builder.greaterThan((Path<Comparable>) path, asComparable(value)));
            break;
        case GREATER_EQUALS:
            condition = builder.and(condition,
                    builder.greaterThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_THAN:
            condition = builder.and(condition, builder.lessThan((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_EQUALS:
            condition = builder.and(condition,
                    builder.lessThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case IS_NULL:
            condition = builder.and(condition, builder.isNull(path));
            break;
        case IS_NOT_NULL:
            condition = builder.and(condition, builder.isNotNull(path));
            break;
        case IN:
            condition = builder.and(condition, path.in(asArray(value)));
            break;
        case NOT_IN:
            condition = builder.and(condition, builder.not(path.in(asArray(value))));
            break;
        case LIKE:
            condition = builder.and(condition, builder.like(path, "%" + String.valueOf(value) + "%"));
            break;
        case NOT_LIKE:
            condition = builder.and(condition, builder.notLike(path, "%" + String.valueOf(value) + "%"));
            break;
        case EQUALS:
            condition = builder.and(condition, builder.equal(path, value));
            break;
        case NOT_EQUALS:
            condition = builder.and(condition, builder.notEqual(path, value));
            break;
        default:
            break;
        }
    }
    return condition;
}

From source file:com.yunguchang.data.ApplicationRepository.java

private TSysOrgEntity findRightFleetOfCoordinator(String coordinatorId) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TSysOrgEntity> cq = cb.createQuery(TSysOrgEntity.class);
    Root<TBusBusinessRelaEntity> mapRoot = cq.from(TBusBusinessRelaEntity.class);

    Root<TSysUserEntity> userRoot = cq.from(TSysUserEntity.class);

    cq.select(mapRoot.get(TBusBusinessRelaEntity_.fleet));

    Path<TSysOrgEntity> coordinatorOrg = userRoot.get(TSysUserEntity_.department);
    cq.where(cb.or(cb.and(cb.like(coordinatorOrg.get(TSysOrgEntity_.orgid), "001%"

    ), cb.equal(mapRoot.get(TBusBusinessRelaEntity_.fleet), coordinatorOrg)),
            cb.and(cb.notLike(coordinatorOrg.get(TSysOrgEntity_.orgid), "001%"),
                    cb.equal(mapRoot.get(TBusBusinessRelaEntity_.busOrg), coordinatorOrg))

    ),/*from   w  w  w . j ava2 s  .  co m*/

            cb.equal(userRoot.get(TSysUserEntity_.userid), coordinatorId)

    );

    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

From source file:org.oncoblocks.centromere.jpa.QueryCriteriaSpecification.java

public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    String key = queryCriteria.getKey();
    Object value = queryCriteria.getValue();
    Evaluation eval = queryCriteria.getEvaluation();
    Path path = null;/*  ww  w  .j av a  2  s  .c o m*/
    if (key.contains(".")) {
        String[] bits = key.split("\\.");
        path = root.join(bits[0]).get(bits[1]);
    } else {
        path = root.get(key);
    }
    logger.debug(String.format("[CENTROMERE] Converting QueryCriteria to JPA specification: %s",
            queryCriteria.toString()));
    switch (eval) {
    case EQUALS:
        return criteriaBuilder.equal(path, value);
    case NOT_EQUALS:
        return criteriaBuilder.notEqual(path, value);
    case IN:
        return path.in(value);
    case NOT_IN:
        return criteriaBuilder.not(path.in(value));
    case IS_NULL:
        return criteriaBuilder.isNull(path);
    case NOT_NULL:
        return criteriaBuilder.isNotNull(path);
    case GREATER_THAN:
        return criteriaBuilder.greaterThan(path, value.toString());
    case GREATER_THAN_EQUALS:
        return criteriaBuilder.greaterThanOrEqualTo(path, value.toString());
    case LESS_THAN:
        return criteriaBuilder.lessThan(path, value.toString());
    case LESS_THAN_EQUALS:
        return criteriaBuilder.lessThanOrEqualTo(path, value.toString());
    case BETWEEN:
        return criteriaBuilder.and(criteriaBuilder.greaterThan(path, ((List<?>) value).get(0).toString()),
                criteriaBuilder.lessThan(path, ((List<?>) value).get(1).toString()));
    case OUTSIDE:
        return criteriaBuilder.or(criteriaBuilder.greaterThan(path, ((List<?>) value).get(1).toString()),
                criteriaBuilder.lessThan(path, ((List<?>) value).get(0).toString()));
    case BETWEEN_INCLUSIVE:
        return criteriaBuilder.and(
                criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(0).toString()),
                criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(1).toString()));
    case OUTSIDE_INCLUSIVE:
        return criteriaBuilder.or(
                criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(1).toString()),
                criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(0).toString()));
    case LIKE:
        return criteriaBuilder.like(path, "%" + value.toString() + "%");
    case NOT_LIKE:
        return criteriaBuilder.notLike(path, "%" + value.toString() + "%");
    case STARTS_WITH:
        return criteriaBuilder.like(path, value.toString() + "%");
    case ENDS_WITH:
        return criteriaBuilder.like(path, "%" + value.toString());
    default:
        return criteriaBuilder.equal(root.get(queryCriteria.getKey()), queryCriteria.getValue());
    }
}

From source file:org.pushio.webapp.support.persistence.DynamicSpecifications.java

/**
 * @see ??JPA//from w  ww  . ja v a2  s .com
 * @param filters
 * @param entityClazz
 * @param isDistinct  trueSQLdistinctfalse?
 * @return
 */
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz, final boolean isDistinct, final List<OrderParam> orderParams) {
    return new Specification<T>() {
        @Override
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (orderParams != null && orderParams.size() > 0) {
                /*
                CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
                Root<Foo> from = criteriaQuery.from(Foo.class);
                CriteriaQuery<Foo> select = criteriaQuery.select(from);
                criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
                */
                List<Order> orders = new ArrayList<Order>(orderParams.size());
                for (OrderParam orderParam : orderParams) {
                    if (orderParam != null && orderParam.getField() != null) {
                        String fields[] = StringUtil.split(orderParam.getField(), '.');
                        Path expression = (fields.length > 1) ? root.join(fields[0]) : root.get(fields[0]);
                        for (int i = 1, len = fields.length; i < len; ++i) {
                            expression = expression.get(fields[i]);
                        }
                        if (expression != null) {
                            Order order = (orderParam.getType() == null
                                    || orderParam.getType().equalsIgnoreCase("asc")) ? builder.asc(expression)
                                            : builder.desc(expression);
                            orders.add(order);
                            //                        query.orderBy(order);
                        }
                    }
                }
                query.orderBy(orders);
            }
            query.distinct(isDistinct);
            if (Collections3.isNotEmpty(filters)) {
                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, '.');
                    Path expression = null;

                    //////  ?
                    boolean hasJoin = names[0].startsWith("[join]");
                    String fn = hasJoin ? names[0].substring(6) : names[0];
                    boolean isNotDateType = !(filter.value instanceof Date);
                    try {
                        expression = hasJoin ? root.join(fn) : root.get(fn);
                        if (isNotDateType && isDateType(expression.getJavaType())) {
                            // filter.value??
                            filter.value = parseDate(filter.value.toString(), filter.operator);
                        }
                    } catch (Exception e) {
                        //                     logger.error(e.getMessage(), e);
                        continue; // ??
                    }
                    boolean isPropertyNotValid = false;
                    for (int i = 1; i < names.length; i++) {
                        try {
                            expression = expression.get(names[i]);
                            if (isNotDateType && isDateType(expression.getJavaType())) {
                                filter.value = parseDate(filter.value.toString(), filter.operator);
                            }
                        } catch (Exception e) {
                            //                        logger.error(e.getMessage(), e);
                            isPropertyNotValid = true; // 
                            break; // ??
                        }
                    }
                    if (expression == null || isPropertyNotValid) {
                        continue;
                    }
                    ///////

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case ORLIKE:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.like(expression, "%" + values[i] + "%");
                            }
                            predicates.add(builder.or(like));
                        }

                        break;
                    case ANDLIKE:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.like(expression, "%" + values[i] + "%");
                            }
                            predicates.add(builder.and(like));
                        }
                        break;
                    case ANDNOTLIKE:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.notLike(expression, "%" + values[i] + "%");
                            }
                            predicates.add(builder.and(like));
                        }
                        break;
                    case OREQ:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.equal(expression, values[i]);
                            }
                            predicates.add(builder.or(like));
                        }
                        break;

                    case ANDNOTEQ:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.notEqual(expression, values[i]);
                            }
                            predicates.add(builder.and(like));
                        }
                        break;
                    case NOTEQ:
                        predicates.add(builder.notEqual(expression, (Comparable) filter.value));
                        break;
                    case NOTLIKE:
                        predicates.add(builder.notLike(expression, "%" + filter.value + "%"));
                        break;
                    case NULL:
                        predicates.add(builder.isNull(expression));
                        break;
                    case NOTNULL:
                        predicates.add(builder.isNotNull(expression));
                        break;
                    //                  case IN:
                    //                     predicates.add(builder.in(expression).in(values));
                    //                     break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}