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:com.shz.foundation.service.dynamic.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//from   w w  w.ja  va  2 s . c  o m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            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 = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        //??
                        Object value = filter.value;
                        if (isBoolean(value)) {
                            value = Boolean.parseBoolean(value.toString());
                        }
                        predicates.add(builder.equal(expression, 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;
                    }
                }

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

            return builder.conjunction();
        }

        private boolean isBoolean(Object value) {
            String valueString = value.toString().toLowerCase();
            if (valueString.equals("true") || valueString.equals("false"))
                return true;
            return false;
        }
    };
}

From source file:com.expressui.sample.view.user.UserQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<User> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(loginName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "loginName");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("loginName")), p));
    }/*from  w  w w . j a  v a2  s  .co  m*/

    if (!isEmpty(doesNotBelongToRole)) {
        ParameterExpression<Role> p = builder.parameter(Role.class, "doesNotBelongToRole");
        Join join = rootEntity.join("userRoles", JoinType.LEFT);
        criteria.add(builder.or(builder.notEqual(join.get("role"), p), builder.isNull(join.get("role"))));
    }

    return criteria;
}

From source file:com.aimdek.ccm.dao.impl.test.StatementDaoImplTest.java

/**
 * Test filtering.//  ww w . j  a  v a2s .co  m
 */
@Test
public void testFiltering() {

    String cardNumber = "0008";
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statement> query = builder.createQuery(Statement.class);
    Root<Statement> root = query.from(Statement.class);
    query.select(root);
    query.where(builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER), "%" + cardNumber));
    List<Statement> statementList = entityManager.createQuery(query).getResultList();

    assertEquals(4, statementList.size());
}

From source file:com.expressui.sample.view.opportunity.OpportunityQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Opportunity> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(accountName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "accountName");
        criteria.add(builder.like(builder.upper(rootEntity.get("account").<String>get("name")), p));
    }//from ww  w .ja v  a 2s  .c om
    if (!isEmpty(salesStages)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "salesStages");
        criteria.add(builder.in(rootEntity.get("salesStage")).value(p));
    }

    return criteria;
}

From source file:com.expressui.sample.view.account.AccountQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Account> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(name)) {
        ParameterExpression<String> p = builder.parameter(String.class, "name");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p));
    }//w w  w .ja v a 2 s. co m
    if (!isEmpty(states)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "states");
        criteria.add(builder.in(rootEntity.get("billingAddress").get("state")).value(p));
    }
    if (!isEmpty(country)) {
        ParameterExpression<Country> p = builder.parameter(Country.class, "country");
        criteria.add(builder.equal(rootEntity.get("billingAddress").get("country"), p));
    }

    return criteria;
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> forDeviceModel(final String deviceModel) throws ArgumentNullOrEmptyException {
    if (deviceModel == null) {
        throw new ArgumentNullOrEmptyException("deviceModel");
    }/*w ww .j  a v a 2 s . c  om*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {
            return cb.like(cb.upper(deviceRoot.<String>get("deviceModel").get("modelCode").as(String.class)),
                    deviceModel.toUpperCase());
        }
    };
}

From source file:com.expressui.sample.view.contact.ContactQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Contact> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(lastName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "lastName");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("lastName")), p));
    }/*from  w  ww. j ava 2  s .  c o  m*/
    if (!isEmpty(states)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "states");
        criteria.add(builder.in(rootEntity.get("mailingAddress").get("state")).value(p));
    }
    if (!isEmpty(country)) {
        ParameterExpression<Country> p = builder.parameter(Country.class, "country");
        criteria.add(builder.equal(rootEntity.get("mailingAddress").get("country"), p));
    }

    return criteria;
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> hasCity(final String city) throws ArgumentNullOrEmptyException {
    if (StringUtils.isEmpty(city)) {
        throw new ArgumentNullOrEmptyException("city");
    }//  w w w  .j  ava  2  s .co m

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            return cb.like(cb.upper(deviceRoot.<String>get("containerCity")), city.toUpperCase());
        }
    };
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> hasPostalCode(final String postalCode) throws ArgumentNullOrEmptyException {
    if (StringUtils.isEmpty(postalCode)) {
        throw new ArgumentNullOrEmptyException("postalCode");
    }/* w w w.j a  v  a 2  s.  c  o m*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            return cb.like(cb.upper(deviceRoot.<String>get("containerPostalCode")), postalCode.toUpperCase());
        }
    };
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> hasStreet(final String street) throws ArgumentNullOrEmptyException {
    if (StringUtils.isEmpty(street)) {
        throw new ArgumentNullOrEmptyException("street");
    }/*from  ww  w  . j a v  a 2  s.  c o  m*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            return cb.like(cb.upper(deviceRoot.<String>get("containerStreet")), street.toUpperCase());
        }
    };
}