Example usage for javax.persistence.criteria CriteriaBuilder lessThan

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

Introduction

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

Prototype

<Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y);

Source Link

Document

Create a predicate for testing whether the first argument is less than the second.

Usage

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java

/**
 * Erase all {@link SystemUserLogType}s of the application scope older than
 * the given date./* ww w  . j av a  2 s  .  c  om*/
 *
 * @param toDate
 *            the date.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void eraseSystemUserLog(final Date toDate) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaDelete<SystemUserLogType> q = cb.createCriteriaDelete(SystemUserLogType.class);
    final Root<SystemUserLogType> c = q.from(SystemUserLogType.class);
    q.where(cb.lessThan(c.<Date>get(SystemUserLogType_.logDateItem), toDate),
            cb.equal(c.<String>get(SystemUserLogType_.context), this.contextNameProvider.getContextName()));
    try {
        this.em.createQuery(q).executeUpdate();
    } catch (final Exception e) {
        this.logger.error("eraseSystemUserLog: {}", e.getMessage());
    }
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java

/**
 * Erase all {@link FlowLogMessageType}s of the application scope older than
 * the given date.//  ww  w. j  a v a  2s  . c o  m
 *
 * @param toDate
 *            the date.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void eraseFlowLogMessage(final Date toDate) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaDelete<FlowLogMessageType> q = cb.createCriteriaDelete(FlowLogMessageType.class);
    final Root<FlowLogMessageType> c = q.from(FlowLogMessageType.class);
    q.where(cb.lessThan(c.<Date>get(FlowLogMessageType_.logTimeItem), toDate));
    try {
        this.em.createQuery(q).executeUpdate();
    } catch (final Exception e) {
        this.logger.error("eraseFlowLogMessage: {}", e.getMessage());
    }
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java

/**
 * Erase all {@link ApplicationUserLogType}s of the application scope older
 * than the given date.//from www.  j  a  va2 s  . c  o  m
 *
 * @param toDate
 *            the date.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void eraseApplicationUserLog(final Date toDate) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaDelete<ApplicationUserLogType> q = cb.createCriteriaDelete(ApplicationUserLogType.class);
    final Root<ApplicationUserLogType> c = q.from(ApplicationUserLogType.class);
    q.where(cb.lessThan(c.<Date>get(ApplicationUserLogType_.stopItem), toDate), cb
            .equal(c.<String>get(ApplicationUserLogType_.context), this.contextNameProvider.getContextName()));
    try {
        this.em.createQuery(q).executeUpdate();
    } catch (final Exception e) {
        this.logger.error("eraseApplicationUserLog: {}", e.getMessage());
    }
}

From source file:cn.imethan.common.repository.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//from   ww w  .j a v a 2  s . co  m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && !filters.isEmpty()) {

                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]);
                    try {
                        for (int i = 1; i < names.length; i++) {
                            expression = expression.get(names[i]);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // 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 IN:
                        In in = builder.in(expression);
                        String[] valueStrings = StringUtils.split(filter.value.toString(), ",");
                        List<Long> list = new ArrayList<Long>();
                        for (String value : valueStrings) {
                            list.add(Long.valueOf(value.trim()));
                        }
                        in.value(list);

                        predicates.add(in);

                        break;
                    case NOTIN:
                        In in1 = builder.in(expression);
                        String[] valueStrings1 = StringUtils.split(filter.value.toString(), ",");
                        List<Long> list1 = new ArrayList<Long>();
                        for (String value : valueStrings1) {
                            list1.add(Long.valueOf(value.trim()));
                        }
                        in1.value(list1);

                        predicates.add(builder.not(in1));
                        break;
                    }
                }

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

            //            //hibernate
            //            //org.hibernate.ejb.criteria.CriteriaQueryImpl
            //            if(query instanceof org.hibernate.ejb.QueryImpl) {  
            //               @SuppressWarnings("rawtypes")
            //               org.hibernate.ejb.QueryImpl hibernateQuery = (org.hibernate.ejb.QueryImpl)query;  
            //               org.hibernate.Query hQuery = hibernateQuery.getHibernateQuery();
            //               hQuery.setCacheable(true);
            //            }

            return builder.conjunction();
        }
    };
}

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. j a  va2s  .  c om*/
        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:cc.sion.core.persistence.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.j av  a  2 s  . co 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]);
                    }
                    //                  if(log.isDebugEnabled()){
                    //                     log.debug("   {}-{}-{}",names[0],filter.operator,filter.value);
                    //                  }
                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case NE:
                        predicates.add(builder.notEqual(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;
                    }
                }

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

            return builder.conjunction();
        }
    };
}

From source file:net.groupbuy.dao.impl.CouponDaoImpl.java

public Page<Coupon> findPage(Boolean isEnabled, Boolean isExchange, Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Coupon> criteriaQuery = criteriaBuilder.createQuery(Coupon.class);
    Root<Coupon> root = criteriaQuery.from(Coupon.class);
    criteriaQuery.select(root);//from  w  ww . jav a  2 s  .c  o  m
    Predicate restrictions = criteriaBuilder.conjunction();
    if (isEnabled != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isEnabled"), isEnabled));
    }
    if (isExchange != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isExchange"), isExchange));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

/**
 * Creates a predicate based on a "Compare" filter
 * /*from   w  ww.j  a va2s . com*/
 * @param builder
 * @param root
 * @param filter
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Predicate createComparePredicate(CriteriaBuilder builder, Root<?> root, Filter filter) {
    Compare compare = (Compare) filter;
    Expression<Comparable> property = (Expression) getPropertyPath(root, compare.getPropertyId());
    Object value = compare.getValue();

    // number representation may contain locale specific separators.
    // Here, we remove
    // those and make sure a period is used in all cases
    if (value instanceof String) {

        // strip out any "%" sign from decimal fields
        value = ((String) value).replace('%', ' ').trim();

        String str = (String) value;
        if (str != null && org.apache.commons.lang.StringUtils
                .isNumeric(str.replaceAll("\\.", "").replaceAll(",", ""))) {
            // first remove all periods (which may be used as
            // thousand
            // separators), then replace comma by period
            str = str.replaceAll("\\.", "").replace(',', '.');
            value = str;
        }

    }

    switch (compare.getOperation()) {
    case EQUAL:
        return builder.equal(property, value);
    case GREATER:
        return builder.greaterThan(property, (Comparable) value);
    case GREATER_OR_EQUAL:
        return builder.greaterThanOrEqualTo(property, (Comparable) value);
    case LESS:
        return builder.lessThan(property, (Comparable) value);
    case LESS_OR_EQUAL:
        return builder.lessThanOrEqualTo(property, (Comparable) value);
    default:
        return null;
    }
}

From source file:aode.lx.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override// w w w.  jav a  2s .  c o m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                List<Predicate> ORpredicates = 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]);
                    }

                    Object value = filter.value;

                    if (expression.getJavaType().isEnum()) {
                        value = EnumUtils.valueOf(expression.getJavaType(), filter.value.toString());
                        predicates.add(builder.equal(expression, value));
                    } else {
                        // 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 NEQ:
                            predicates.add(builder.notEqual(expression, filter.value));
                            break;
                        case NOTNULL:
                            predicates.add(builder.isNotNull(expression));
                            break;
                        case ISNULL:
                            predicates.add(builder.isNull(expression));
                            break;
                        case OREQ:
                            ORpredicates.add(builder.equal(expression, filter.value));
                            break;
                        case ORLIKE:
                            ORpredicates.add(builder.like(expression, "%" + filter.value + "%"));
                            break;
                        case ORGT:
                            ORpredicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                            break;
                        case ORLT:
                            ORpredicates.add(builder.lessThan(expression, (Comparable) filter.value));
                            break;
                        case ORGTE:
                            ORpredicates
                                    .add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                            break;
                        case ORLTE:
                            ORpredicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                            break;
                        case ORNEQ:
                            ORpredicates.add(builder.notEqual(expression, filter.value));
                            break;
                        case ORNOTNULL:
                            ORpredicates.add(builder.isNotNull(expression));
                            break;
                        case ORISNULL:
                            ORpredicates.add(builder.isNull(expression));
                            break;
                        }
                    }

                }

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

From source file:com.faceye.feature.repository.jpa.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//w  ww .  j a v a  2  s.  co  m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (CollectionUtils.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:
                        if (filter.value instanceof Number) {
                            predicates.add(builder.equal(expression, (Number) filter.value));
                        } else if (filter.value instanceof String) {
                            if (StringUtils.isNotEmpty(filter.value.toString())) {
                                predicates.add(builder.equal(expression, filter.value));
                            }
                        } else {
                            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 ISTRUE:
                        predicates.add(builder.isTrue(expression));
                        break;
                    case ISFALSE:
                        predicates.add(builder.isFalse(expression));
                        break;
                    case ISEMPTY:
                        predicates.add(builder.isEmpty(expression));
                        break;
                    case ISNULL:
                        predicates.add(builder.isNull(expression));
                        break;
                    case NE:
                        predicates.add(builder.notEqual(expression, filter.value));
                    }
                }

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

            return builder.conjunction();
        }
    };
}