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:net.groupbuy.dao.impl.MemberDaoImpl.java
public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Member> member = criteriaQuery.from(Member.class); Join<Product, Order> orders = member.join("orders"); criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance"), criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(orders.<Date>get("createDate"), beginDate)); }//from www. java 2 s. c om if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(orders.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid")))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.shopxx.dao.impl.CouponCodeDaoImpl.java
public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class); Root<CouponCode> root = criteriaQuery.from(CouponCode.class); criteriaQuery.select(root);/*w w w . ja v a 2 s .c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); Path<Coupon> couponPath = root.get("coupon"); if (coupon != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date())); } } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("endDate").isNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("endDate"), new Date()))); } } if (isUsed != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a predicate based on a "Compare" filter * /*from www. j a v a 2 s. c om*/ * @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: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 va2 s . 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:net.shopxx.dao.impl.OrderDaoImpl.java
public BigDecimal createOrderAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from w ww . j a v a 2 s. c o m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult(); return result != null ? result : BigDecimal.ZERO; }
From source file:in.bookmylab.jpa.JpaDAO.java
public List<ResourceBooking> searchResourceBooking(BookingSearchInput si) { EntityManager em = emf.createEntityManager(); try {/*from w w w . j a v a2s. c o m*/ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<ResourceBooking> cq = cb.createQuery(ResourceBooking.class); Root<ResourceBooking> s = cq.from(ResourceBooking.class); List<Predicate> pred = new ArrayList<Predicate>(); if (si.userId != null) { pred.add(cb.equal(s.<User>get("user").<Integer>get("userId"), si.userId)); } if (si.bookingDateFrom != null) { pred.add(cb.greaterThanOrEqualTo(s.<Date>get("bookingDate"), si.bookingDateFrom)); } if (si.bookingDateTo != null) { pred.add(cb.lessThanOrEqualTo(s.<Date>get("bookingDate"), si.bookingDateTo)); } if (!StringUtils.isEmpty(si.lab)) { pred.add(cb.equal(s.<String>get("lab"), si.lab)); } if (!StringUtils.isEmpty(si.status)) { pred.add(cb.equal(s.<String>get("status"), si.status)); } cq.select(s).where(pred.toArray(new Predicate[] {})); return em.createQuery(cq).getResultList(); } finally { em.close(); } }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public BigDecimal completeOrderAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate)); }// w w w.ja va 2 s. c om if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate)); } criteriaQuery.where(restrictions); BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult(); return result != null ? result : BigDecimal.ZERO; }
From source file:net.groupbuy.dao.impl.CouponCodeDaoImpl.java
public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class); Root<CouponCode> root = criteriaQuery.from(CouponCode.class); criteriaQuery.select(root);/*from ww w . j a v a 2 s . c om*/ Predicate restrictions = criteriaBuilder.conjunction(); Path<Coupon> couponPath = root.get("coupon"); if (coupon != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date())); } } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(), criteriaBuilder.lessThan(couponPath.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("endDate").isNull(), criteriaBuilder.greaterThanOrEqualTo(couponPath.<Date>get("endDate"), new Date()))); } } if (isUsed != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public BigDecimal getSalesAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from w ww . jav a2s. co m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public Integer getSalesVolume(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer>get("shippedQuantity"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from w w w . j a v a 2s . c o m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }