List of usage examples for javax.persistence.criteria CriteriaBuilder lessThan
<Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y);
From source file:com.netflix.genie.server.metrics.impl.JobCountManagerImpl.java
/** * {@inheritDoc}// ww w. j ava 2s .co m */ @Override @Transactional(readOnly = true) //TODO: Move to specification public int getNumInstanceJobs(final String hostName, final Long minStartTime, final Long maxStartTime) throws GenieException { LOG.debug("called"); final String finalHostName; // initialize host name if (StringUtils.isBlank(hostName)) { finalHostName = NetUtil.getHostName(); } else { finalHostName = hostName; } final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<Long> cq = cb.createQuery(Long.class); final Root<Job> j = cq.from(Job.class); cq.select(cb.count(j)); final Predicate runningStatus = cb.equal(j.get(Job_.status), JobStatus.RUNNING); final Predicate initStatus = cb.equal(j.get(Job_.status), JobStatus.INIT); final List<Predicate> predicates = new ArrayList<>(); predicates.add(cb.equal(j.get(Job_.hostName), finalHostName)); predicates.add(cb.or(runningStatus, initStatus)); if (minStartTime != null) { predicates.add(cb.greaterThanOrEqualTo(j.get(Job_.started), new Date(minStartTime))); } if (maxStartTime != null) { predicates.add(cb.lessThan(j.get(Job_.started), new Date(maxStartTime))); } //documentation says that by default predicate array is conjuncted together cq.where(predicates.toArray(new Predicate[predicates.size()])); final TypedQuery<Long> query = this.em.createQuery(cq); //Downgrading to an int since all the code seems to want ints //Don't feel like changing everthing right now can figure out later //if need be return query.getSingleResult().intValue(); }
From source file:net.groupbuy.dao.impl.PromotionDaoImpl.java
public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Promotion> criteriaQuery = criteriaBuilder.createQuery(Promotion.class); Root<Promotion> root = criteriaQuery.from(Promotion.class); criteriaQuery.select(root);// w ww . ja va 2 s. co m Predicate restrictions = criteriaBuilder.conjunction(); if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, root.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(root.<Date>get("beginDate"), new Date())); } } if (hasEnded != null) { if (hasEnded) { 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.findList(criteriaQuery, null, count, filters, orders); }
From source file:com.sammyun.dao.impl.BaseDaoImpl.java
private void addRestrictions(CriteriaQuery<T> criteriaQuery, List<Filter> filters) { if (criteriaQuery == null || filters == null || filters.isEmpty()) { return;//from www .ja v a2s . c o m } Root<T> root = getRoot(criteriaQuery); if (root == null) { return; } CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Predicate restrictions = criteriaQuery.getRestriction() != null ? criteriaQuery.getRestriction() : criteriaBuilder.conjunction(); for (Filter filter : filters) { if (filter == null || StringUtils.isEmpty(filter.getProperty())) { continue; } /** */ if (filter.getMold() == Mold.dl || filter.getMold() == Mold.dg) { if (filter.getOperator() == Operator.lt && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThan(root.<Date>get(filter.getProperty()), DateUtil.parseDate(filter.getValue().toString()))); } else if (filter.getOperator() == Operator.gt && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThan(root.<Date>get(filter.getProperty()), DateUtil.parseDate(filter.getValue().toString()))); } else if (filter.getOperator() == Operator.le && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get(filter.getProperty()), DateUtil.parseDate(filter.getValue().toString()))); } else if (filter.getOperator() == Operator.ge && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get(filter.getProperty()), DateUtil.parseDate(filter.getValue().toString()))); } } else { if (filter.getOperator() == Operator.eq && filter.getValue() != null) { if (filter.getIgnoreCase() != null && filter.getIgnoreCase() && filter.getValue() instanceof String) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(criteriaBuilder.lower(root.<String>get(filter.getProperty())), ((String) filter.getValue()).toLowerCase())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get(filter.getProperty()), filter.getValue())); } } else if (filter.getOperator() == Operator.ne && filter.getValue() != null) { if (filter.getIgnoreCase() != null && filter.getIgnoreCase() && filter.getValue() instanceof String) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.notEqual( criteriaBuilder.lower(root.<String>get(filter.getProperty())), ((String) filter.getValue()).toLowerCase())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.notEqual(root.get(filter.getProperty()), filter.getValue())); } } else if (filter.getOperator() == Operator.gt && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.gt(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.lt && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lt(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.ge && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.le && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.like && filter.getValue() != null && filter.getValue() instanceof String) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder .like(root.<String>get(filter.getProperty()), (String) filter.getValue())); } else if (filter.getOperator() == Operator.in && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).in(filter.getValue())); } else if (filter.getOperator() == Operator.isNull) { restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).isNull()); } else if (filter.getOperator() == Operator.isNotNull) { restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).isNotNull()); } } } criteriaQuery.where(restrictions); }
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public Page<Order> findPage(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus, Boolean hasExpired, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(root);//from w w w . j av a2 s . c o m Predicate restrictions = criteriaBuilder.conjunction(); if (orderStatus != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), orderStatus)); } if (paymentStatus != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus)); } if (shippingStatus != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus)); } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(), criteriaBuilder.lessThan(root.<Date>get("expire"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date()))); } } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public Long count(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus, Boolean hasExpired) {//from www. ja va2 s . c om CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (orderStatus != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), orderStatus)); } if (paymentStatus != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus)); } if (shippingStatus != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus)); } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(), criteriaBuilder.lessThan(root.<Date>get("expire"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date()))); } } criteriaQuery.where(restrictions); return super.count(criteriaQuery, 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;/*from w w w . j a v 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:com.netflix.genie.core.jpa.specifications.JpaJobSpecs.java
/** * Generate a criteria query predicate for a where clause based on the given parameters. * * @param root The root to use/*from w w w. jav a 2 s. c om*/ * @param cb The criteria builder to use * @param id The job id * @param name The job name * @param user The user who created the job * @param statuses The job statuses * @param tags The tags for the jobs to find * @param clusterName The cluster name * @param cluster The cluster the job should have been run on * @param commandName The command name * @param command The command the job should have been run with * @param minStarted The time which the job had to start after in order to be return (inclusive) * @param maxStarted The time which the job had to start before in order to be returned (exclusive) * @param minFinished The time which the job had to finish after in order to be return (inclusive) * @param maxFinished The time which the job had to finish before in order to be returned (exclusive) * @return The specification */ public static Predicate getFindPredicate(final Root<JobEntity> root, final CriteriaBuilder cb, final String id, final String name, final String user, final Set<JobStatus> statuses, final Set<String> tags, final String clusterName, final ClusterEntity cluster, final String commandName, final CommandEntity command, final Date minStarted, final Date maxStarted, final Date minFinished, final Date maxFinished) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(id)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.id), id)); } if (StringUtils.isNotBlank(name)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(JobEntity_.status), status)).collect(Collectors.toList()); predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } if (tags != null && !tags.isEmpty()) { predicates.add(cb.like(root.get(JobEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } if (cluster != null) { predicates.add(cb.equal(root.get(JobEntity_.cluster), cluster)); } if (StringUtils.isNotBlank(clusterName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.clusterName), clusterName)); } if (command != null) { predicates.add(cb.equal(root.get(JobEntity_.command), command)); } if (StringUtils.isNotBlank(commandName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.commandName), commandName)); } if (minStarted != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.started), minStarted)); } if (maxStarted != null) { predicates.add(cb.lessThan(root.get(JobEntity_.started), maxStarted)); } if (minFinished != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.finished), minFinished)); } if (maxFinished != null) { predicates.add(cb.lessThan(root.get(JobEntity_.finished), maxFinished)); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }
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);/*w ww. j av a2s. 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.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:com.ims.service.ProductStockInfoService.java
public List<ProductStockInfo> findProdStockInfoListFrom(final ProdStockSearchCriteria stockSearchCriteria) { Specification<ProductStockInfo> speci = new Specification<ProductStockInfo>() { @Override//from w ww.j a va2 s .c om public Predicate toPredicate(Root<ProductStockInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(root.get("stockType"), stockSearchCriteria.getStockType())); if (StringUtils.isNotEmpty(stockSearchCriteria.getProdCategoryCode())) { predicates.add(cb.equal(root.get("categoryCode"), stockSearchCriteria.getProdCategoryCode())); } if (StringUtils.isNotEmpty(stockSearchCriteria.getProdCode())) { predicates.add(cb.like(root.<String>get("productCode"), "%" + stockSearchCriteria.getProdCode() + "%")); } String compareCode = stockSearchCriteria.getCompareCode(); if (StringUtils.isNotEmpty(compareCode) && stockSearchCriteria.isIncludeComparedValue()) { Path<ProductAmount> productAmount = root.<ProductAmount>get("productAmount"); if (CompareCode.isEqual(compareCode)) { predicates.add( cb.equal(productAmount.get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isGreater(compareCode)) { predicates.add(cb.greaterThan(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isLess(compareCode)) { predicates.add(cb.lessThan(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isGreaterOrEqual(compareCode)) { predicates.add(cb.greaterThanOrEqualTo(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isLessOrEqual(compareCode)) { predicates.add(cb.lessThanOrEqualTo(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isEqualAlertAmount(compareCode)) { predicates.add(cb.equal(productAmount.get("totalAmount"), root.get("alertStockAmount"))); } else if (CompareCode.isGreaterThanAlertAmount(compareCode)) { predicates.add(cb.greaterThan(productAmount.<String>get("totalAmount"), root.<String>get("alertStockAmount"))); } else if (CompareCode.isLessThanAlertAmount(compareCode)) { predicates.add(cb.lessThan(productAmount.<String>get("totalAmount"), root.<String>get("alertStockAmount"))); } if (stockSearchCriteria.isTransformAction() && ((CompareCode.isLess(compareCode) || CompareCode.isLessOrEqual(compareCode)))) { predicates.add(cb.greaterThan(productAmount.<Integer>get("totalAmount"), 0)); } } query.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))) .orderBy(cb.desc(root.get("categoryCode")), cb.asc(root.get("productCode"))); return null; } }; return productStockInfoRepository.findAll(speci); }