List of usage examples for javax.persistence.criteria CriteriaBuilder lessThanOrEqualTo
<Y extends Comparable<? super Y>> Predicate lessThanOrEqualTo(Expression<? extends Y> x, Y y);
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 ww w . j a v a 2s . com*/ 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:eu.domibus.common.dao.ErrorLogDao.java
public List<ErrorLogEntry> findPaged(int from, int max, String column, boolean asc, HashMap<String, Object> filters) { CriteriaBuilder cb = this.em.getCriteriaBuilder(); CriteriaQuery<ErrorLogEntry> cq = cb.createQuery(ErrorLogEntry.class); Root<ErrorLogEntry> ele = cq.from(ErrorLogEntry.class); cq.select(ele);// w w w .j av a2 s .c om List<Predicate> predicates = new ArrayList<Predicate>(); for (Map.Entry<String, Object> filter : filters.entrySet()) { if (filter.getValue() != null) { if (filter.getValue() instanceof String) { if (!filter.getValue().toString().isEmpty()) { switch (filter.getKey().toString()) { case "": break; case "timestampFrom": predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("timestamp"), Timestamp.valueOf(filter.getValue().toString()))); break; case "timestampTo": predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("timestamp"), Timestamp.valueOf(filter.getValue().toString()))); break; case "notifiedFrom": predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("notified"), Timestamp.valueOf(filter.getValue().toString()))); break; case "notifiedTo": predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("notified"), Timestamp.valueOf(filter.getValue().toString()))); break; default: predicates.add(cb.like(ele.<String>get(filter.getKey()), (String) filter.getValue())); break; } } } else { predicates.add(cb.equal(ele.<String>get(filter.getKey()), filter.getValue())); } } } cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); if (column != null) { if (asc) { cq.orderBy(cb.asc(ele.get(column))); } else { cq.orderBy(cb.desc(ele.get(column))); } } final TypedQuery<ErrorLogEntry> query = this.em.createQuery(cq); query.setFirstResult(from); query.setMaxResults(max); return query.getResultList(); }
From source file:eu.domibus.common.dao.MessageLogDao.java
public List<MessageLogEntry> findPaged(int from, int max, String column, boolean asc, HashMap<String, Object> filters) { CriteriaBuilder cb = this.em.getCriteriaBuilder(); CriteriaQuery<MessageLogEntry> cq = cb.createQuery(MessageLogEntry.class); Root<MessageLogEntry> mle = cq.from(MessageLogEntry.class); cq.select(mle);/*from w ww . j a va 2 s. c o m*/ List<Predicate> predicates = new ArrayList<Predicate>(); for (Map.Entry<String, Object> filter : filters.entrySet()) { if (filter.getValue() != null) { if (filter.getValue() instanceof String) { if (!filter.getValue().toString().isEmpty()) { switch (filter.getKey().toString()) { case "receivedFrom": predicates.add(cb.greaterThanOrEqualTo(mle.<Date>get("received"), Timestamp.valueOf(filter.getValue().toString()))); break; case "receivedTo": predicates.add(cb.lessThanOrEqualTo(mle.<Date>get("received"), Timestamp.valueOf(filter.getValue().toString()))); break; default: predicates.add(cb.like(mle.<String>get(filter.getKey()), (String) filter.getValue())); break; } } } else { predicates.add(cb.equal(mle.<String>get(filter.getKey()), filter.getValue())); } } } cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); if (column != null) { if (asc) { cq.orderBy(cb.asc(mle.get(column))); } else { cq.orderBy(cb.desc(mle.get(column))); } } final TypedQuery<MessageLogEntry> query = this.em.createQuery(cq); query.setFirstResult(from); query.setMaxResults(max); return query.getResultList(); }
From source file:net.groupbuy.dao.impl.ProductNotifyDaoImpl.java
public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root);/* w w w. jav a 2 s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:net.groupbuy.dao.impl.ProductNotifyDaoImpl.java
public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent, Pageable pageable) {/*from w ww .ja v a 2s .c om*/ CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java
public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root);/*www . ja v a2s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java
public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent, Pageable pageable) {/*from w w w.j a va2s. c o m*/ CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
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//from ww w .jav a 2s . c o 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(); } }; }
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 ww. j a 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(); 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.deloitte.smt.service.SignalDetectionService.java
/** * @param searchDto//from w ww . java2 s . c om * @param criteriaBuilder * @param rootSignalDetection * @param predicates */ private void addCreatedOrLastRunDate(SearchDto searchDto, CriteriaBuilder criteriaBuilder, Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) { if (DateKeyType.searchIn(searchDto.getDateKey())) { if (searchDto.getDateKey().equalsIgnoreCase(DateKeyType.CREATED.name())) { predicates.add(criteriaBuilder.greaterThanOrEqualTo( rootSignalDetection.get(SmtConstant.CREATED_DATE.getDescription()), searchDto.getStartDate())); if (null != searchDto.getEndDate()) { predicates.add(criteriaBuilder.lessThanOrEqualTo( rootSignalDetection.get(SmtConstant.CREATED_DATE.getDescription()), searchDto.getEndDate())); } } else if (searchDto.getDateKey().equalsIgnoreCase(DateKeyType.LASTRUN.name())) { predicates.add(criteriaBuilder.greaterThanOrEqualTo(rootSignalDetection.get("lastRunDate"), searchDto.getStartDate())); if (null != searchDto.getEndDate()) { predicates.add(criteriaBuilder.lessThanOrEqualTo(rootSignalDetection.get("lastRunDate"), searchDto.getEndDate())); } } else if (searchDto.getDateKey().equalsIgnoreCase(DateKeyType.NEXTRUN.name())) { predicates.add(criteriaBuilder.greaterThanOrEqualTo(rootSignalDetection.get("nextRunDate"), searchDto.getStartDate())); if (null != searchDto.getEndDate()) { predicates.add(criteriaBuilder.lessThanOrEqualTo(rootSignalDetection.get("nextRunDate"), searchDto.getEndDate())); } } } }