List of usage examples for javax.persistence.criteria CriteriaBuilder gt
Predicate gt(Expression<? extends Number> x, Number y);
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateQuantity(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }/*from ww w .j a v a 2 s . c o m*/ CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamQuantity> from = cq.from(ResourceIndexedSearchParamQuantity.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; String systemValue; String unitsValue; QuantityCompararatorEnum cmpValue; BigDecimal valueValue; boolean approx = false; if (params instanceof BaseQuantityDt) { BaseQuantityDt param = (BaseQuantityDt) params; systemValue = param.getSystemElement().getValueAsString(); unitsValue = param.getUnitsElement().getValueAsString(); cmpValue = QuantityCompararatorEnum.VALUESET_BINDER .fromCodeString(param.getComparatorElement().getValueAsString()); valueValue = param.getValueElement().getValue(); } else if (params instanceof QuantityParam) { QuantityParam param = (QuantityParam) params; systemValue = param.getSystem().getValueAsString(); unitsValue = param.getUnits(); cmpValue = param.getComparator(); valueValue = param.getValue().getValue(); approx = param.isApproximate(); } else { throw new IllegalArgumentException("Invalid quantity type: " + params.getClass()); } Predicate system = null; if (!isBlank(systemValue)) { system = builder.equal(from.get("mySystem"), systemValue); } Predicate code = null; if (!isBlank(unitsValue)) { code = builder.equal(from.get("myUnits"), unitsValue); } Predicate num; if (cmpValue == null) { BigDecimal mul = approx ? new BigDecimal(0.1) : new BigDecimal(0.01); BigDecimal low = valueValue.subtract(valueValue.multiply(mul)); BigDecimal high = valueValue.add(valueValue.multiply(mul)); Predicate lowPred = builder.gt(from.get("myValue").as(BigDecimal.class), low); Predicate highPred = builder.lt(from.get("myValue").as(BigDecimal.class), high); num = builder.and(lowPred, highPred); } else { switch (cmpValue) { case GREATERTHAN: Expression<Number> path = from.get("myValue"); num = builder.gt(path, valueValue); break; case GREATERTHAN_OR_EQUALS: path = from.get("myValue"); num = builder.ge(path, valueValue); break; case LESSTHAN: path = from.get("myValue"); num = builder.lt(path, valueValue); break; case LESSTHAN_OR_EQUALS: path = from.get("myValue"); num = builder.le(path, valueValue); break; default: throw new IllegalStateException(cmpValue.getCode()); } } if (system == null && code == null) { codePredicates.add(num); } else if (system == null) { Predicate singleCode = builder.and(code, num); codePredicates.add(singleCode); } else if (code == null) { Predicate singleCode = builder.and(system, num); codePredicates.add(singleCode); } else { Predicate singleCode = builder.and(system, code, num); codePredicates.add(singleCode); } } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:com.sammyun.dao.impl.BaseDaoImpl.java
private void addRestrictions(CriteriaQuery<T> criteriaQuery, Pageable pageable) { if (criteriaQuery == null || pageable == null) { return;/*from ww w. ja v a 2s . 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(); if (StringUtils.isNotEmpty(pageable.getSearchProperty()) && StringUtils.isNotEmpty(pageable.getSearchValue())) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder .like(root.<String>get(pageable.getSearchProperty()), "%" + pageable.getSearchValue() + "%")); } if (pageable.getFilters() != null) { for (Filter filter : pageable.getFilters()) { 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); }