Java Code Examples for javax.persistence.criteria.CriteriaBuilder#lessThanOrEqualTo()

The following examples show how to use javax.persistence.criteria.CriteriaBuilder#lessThanOrEqualTo() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private Predicate documentPredicate_date(CriteriaBuilder cb, Root<Document> root) throws Exception {
	if (null == this.dateRange || (!this.dateRange.available())) {
		return null;
	}
	Expression var1 = root.get(Document_.publishTime);
	if(this.draft){
		var1 = root.get(Document_.updateTime);
	}
	if (null == this.dateRange.start) {
		return cb.lessThanOrEqualTo(var1, this.dateRange.completed);
	} else if (null == this.dateRange.completed) {
		return cb.greaterThanOrEqualTo(var1, this.dateRange.start);
	} else {
		return cb.between(var1, this.dateRange.start, this.dateRange.completed);
	}
}
 
Example 2
public List<String> listNeedAlarmEventIds(Date date) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Calendar_Event.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<Calendar_Event> root = cq.from(Calendar_Event.class);
	Predicate p = cb.lessThanOrEqualTo( root.get(Calendar_Event_.alarmTime), date );
	p = cb.and( p, cb.isFalse( root.get(Calendar_Event_.alarmAlready ) ));
	cq.select(root.get(Calendar_Event_.id));
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 3
public List<String> listNeedAlarmEventIds(Date date) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Calendar_Event.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<Calendar_Event> root = cq.from(Calendar_Event.class);
	Predicate p = cb.lessThanOrEqualTo( root.get(Calendar_Event_.alarmTime), date );
	p = cb.and( p, cb.isFalse( root.get(Calendar_Event_.alarmAlready ) ));
	cq.select(root.get(Calendar_Event_.id));
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 4
private Predicate workPredicate_date(CriteriaBuilder cb, Root<Work> root) throws Exception {
	if (null == this.dateRange || (!this.dateRange.available())) {
		return null;
	}
	if (null == this.dateRange.start) {
		return cb.lessThanOrEqualTo(root.get(Work_.startTime), this.dateRange.completed);
	} else if (null == this.dateRange.completed) {
		return cb.greaterThanOrEqualTo(root.get(Work_.startTime), this.dateRange.start);
	} else {
		return cb.between(root.get(Work_.startTime), this.dateRange.start, this.dateRange.completed);
	}
}
 
Example 5
private Predicate workCompletedPredicate_date(CriteriaBuilder cb, Root<WorkCompleted> root) throws Exception {
	if (null == this.dateRange || (!this.dateRange.available())) {
		return null;
	}
	if (null == this.dateRange.start) {
		return cb.lessThanOrEqualTo(root.get(WorkCompleted_.completedTime), this.dateRange.completed);
	} else if (null == this.dateRange.completed) {
		return cb.greaterThanOrEqualTo(root.get(WorkCompleted_.startTime), this.dateRange.start);
	} else {
		return cb.and(cb.greaterThanOrEqualTo(root.get(WorkCompleted_.startTime), this.dateRange.start),
				cb.lessThanOrEqualTo(root.get(WorkCompleted_.completedTime), this.dateRange.completed));
	}
}
 
Example 6
Source Project: docs-manage   File: RangeSpecification.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Predicate buildRangePredicate(Range<T> range, Root<T> root, CriteriaBuilder cb) {
    if (range.isBetween()) {
        return cb.between(root.get(range.getField()), range.getFrom(), range.getTo());
    } else if (range.isFromSet()) {
        return cb.greaterThanOrEqualTo(root.get(range.getField()), range.getFrom());
    } else if (range.isToSet()) {
        return cb.lessThanOrEqualTo(root.get(range.getField()), range.getTo());
    }
    return null;
}
 
Example 7
Source Project: modeldb   File: RdbmsUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the where clause predicate based on given parameters
 *
 * @param builder : Hibernate criteria builder
 * @param valueExpression : field value path for criteria query
 * @param operator : operator like EQ, GTE, LE etc.
 * @param operatorValue : field value which set to where clause based on field, Operator and this
 *     value
 * @return {@link Predicate} : return predicate (where clause condition) for criteria query
 */
private static Predicate getOperatorPredicate(
    CriteriaBuilder builder,
    Expression valueExpression,
    Operator operator,
    Object operatorValue) {
  switch (operator.ordinal()) {
    case Operator.GT_VALUE:
      return builder.greaterThan(valueExpression, (Comparable) operatorValue);
    case Operator.GTE_VALUE:
      return builder.greaterThanOrEqualTo(valueExpression, (Comparable) operatorValue);
    case Operator.LT_VALUE:
      return builder.lessThan(valueExpression, (Comparable) operatorValue);
    case Operator.LTE_VALUE:
      return builder.lessThanOrEqualTo(valueExpression, (Comparable) operatorValue);
    case Operator.NE_VALUE:
      return builder.notEqual(valueExpression, operatorValue);
    case Operator.CONTAIN_VALUE:
      return builder.like(
          builder.lower(valueExpression),
          ("%" + Pattern.compile((String) operatorValue) + "%").toLowerCase());
    case Operator.NOT_CONTAIN_VALUE:
      return builder.notLike(
          builder.lower(valueExpression),
          ("%" + Pattern.compile((String) operatorValue) + "%").toLowerCase());
    case Operator.IN_VALUE:
      return valueExpression.in(operatorValue);
    default:
      return builder.equal(valueExpression, operatorValue);
  }
}
 
Example 8
Source Project: javaee-lab   File: ByRangeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static <D extends Comparable<? super D>, E> Predicate buildRangePredicate(Range<E, D> range, Root<E> root, CriteriaBuilder builder) {
    Predicate rangePredicate = null;
    Path<D> path = JpaUtil.getInstance().getPath(root, range.getAttributes());
    if (range.isBetween()) {
        rangePredicate = builder.between(path, range.getFrom(), range.getTo());
    } else if (range.isFromSet()) {
        rangePredicate = builder.greaterThanOrEqualTo(path, range.getFrom());
    } else if (range.isToSet()) {
        rangePredicate = builder.lessThanOrEqualTo(path, range.getTo());
    }

    if (rangePredicate != null) {
        if (!range.isIncludeNullSet() || range.getIncludeNull() == FALSE) {
            return rangePredicate;
        } else {
            return builder.or(rangePredicate, builder.isNull(path));
        }
    } else {
        // no from/to is set, but include null or not could be:
        if (TRUE == range.getIncludeNull()) {
            return builder.isNull(path);
        } else if (FALSE == range.getIncludeNull()) {
            return builder.isNotNull(path);
        }
    }
    return null;
}
 
Example 9
Source Project: sctalk   File: SimpleExpression.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    Path expression = null;
    if (fieldName.contains(".")) {
        
        System.out.println(root);
        String[] names = StringUtils.split(fieldName, ".");
        expression = root.get(names[0]);
        for (int i = 1; i < names.length; i++) {
            expression = expression.get(names[i]);
        }
    } else {
        expression = root.get(fieldName);
    }

    switch (operator) {
    case EQ:
        return builder.equal(expression, value);
    case NE:
        return builder.notEqual(expression, value);
    case LIKE:
        return builder.like((Expression<String>) expression, "%" + value + "%");
    case RLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LT:
        return builder.lessThan(expression, (Comparable) value);
    case GT:
        return builder.greaterThan(expression, (Comparable) value);
    case LTE:
        return builder.lessThanOrEqualTo(expression, (Comparable) value);
    case GTE:
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);
    case ISNULL:
        return builder.isNull(expression);
    case NOTNULL:
        return builder.isNotNull(expression);
    default:
        return null;
    }
}
 
Example 10
Source Project: sctalk   File: SimpleExpression.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    Path expression = null;
    if (fieldName.contains(".")) {
        
        System.out.println(root);
        String[] names = StringUtils.split(fieldName, ".");
        expression = root.get(names[0]);
        for (int i = 1; i < names.length; i++) {
            expression = expression.get(names[i]);
        }
    } else {
        expression = root.get(fieldName);
    }

    switch (operator) {
    case EQ:
        return builder.equal(expression, value);
    case NE:
        return builder.notEqual(expression, value);
    case LIKE:
        return builder.like((Expression<String>) expression, "%" + value + "%");
    case RLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LT:
        return builder.lessThan(expression, (Comparable) value);
    case GT:
        return builder.greaterThan(expression, (Comparable) value);
    case LTE:
        return builder.lessThanOrEqualTo(expression, (Comparable) value);
    case GTE:
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);
    case ISNULL:
        return builder.isNull(expression);
    case NOTNULL:
        return builder.isNotNull(expression);
    case IN:
        return builder.in(expression);
    default:
        return null;
    }
}
 
Example 11
Source Project: jpa-spec   File: LeSpecification.java    License: MIT License 4 votes vote down vote up
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    From from = getRoot(property, root);
    String field = getProperty(property);
    return cb.lessThanOrEqualTo(from.get(field), compare);
}
 
Example 12
@Override
public Page<AuditRecord> findByActionTypeAndOperationTypeAndDate(AuditOperationType[] operations,
		AuditActionType[] actions, Instant fromDate, Instant toDate, Pageable pageable) {

	final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
	final CriteriaQuery<AuditRecord> query = cb.createQuery(AuditRecord.class);
	final Root<AuditRecord> auditRecordRoot = query.from(AuditRecord.class);

	final Path<AuditActionType> auditAction = auditRecordRoot.get("auditAction");
	final Path<AuditOperationType> auditOperation = auditRecordRoot.get("auditOperation");
	final Path<Instant> createdOn = auditRecordRoot.get("createdOn");

	final Predicate datePredicate;
	if (fromDate != null && toDate == null) {
		datePredicate = cb.greaterThanOrEqualTo(createdOn, fromDate);
	}
	else if (fromDate == null && toDate != null) {
		datePredicate = cb.lessThanOrEqualTo(createdOn, toDate);
	}
	else if (fromDate != null && toDate != null) {
		datePredicate = cb.between(createdOn, fromDate, toDate);
	}
	else {
		datePredicate = null;
	}

	final List<Predicate> auditActionPredicates = new ArrayList<>();
	if (actions != null && actions.length > 0) {
		for (AuditActionType action : actions) {
			auditActionPredicates.add(cb.equal(auditAction, action));
		}
	}

	final List<Predicate> auditOperationsPredicates = new ArrayList<>();
	if (operations != null && operations.length > 0) {
		for (AuditOperationType operation : operations) {
			auditOperationsPredicates.add(cb.equal(auditOperation, operation));
		}
	}

	final List<Predicate> finalQueryPredicates = new ArrayList<>();
	if (!auditActionPredicates.isEmpty()) {
		final Predicate auditActionPredicatesOr = cb.or(auditActionPredicates.toArray(new Predicate[0]));
		finalQueryPredicates.add(auditActionPredicatesOr);
	}
	if (datePredicate != null) {
		finalQueryPredicates.add(datePredicate);
	}
	if (!auditOperationsPredicates.isEmpty()) {
		final Predicate auditOperationsPredicatesOr = cb.or(auditOperationsPredicates.toArray(new Predicate[0]));
		finalQueryPredicates.add(auditOperationsPredicatesOr);
	}

	final CriteriaQuery<AuditRecord> select = query.select(auditRecordRoot);

	if (!finalQueryPredicates.isEmpty()) {
		select.where(finalQueryPredicates.toArray(new Predicate[0]));
	}

	if (pageable.getSort().isUnsorted()) {
		select.orderBy(QueryUtils.toOrders(pageable.getSort().and(Sort.by("id")).ascending(), auditRecordRoot, cb));
	}
	else {
		select.orderBy(QueryUtils.toOrders(pageable.getSort(), auditRecordRoot, cb));
	}

	final TypedQuery<AuditRecord> typedQuery = entityManager.createQuery(select);
	typedQuery.setFirstResult((int) pageable.getOffset());
	typedQuery.setMaxResults(pageable.getPageSize());

	final List<AuditRecord> resultList = typedQuery.getResultList();

	final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
	countQuery.select(cb.count(countQuery.from(AuditRecord.class)));

	if (!finalQueryPredicates.isEmpty()) {
		countQuery.where(finalQueryPredicates.toArray(new Predicate[0]));
	}

	final Long totalCount = entityManager.createQuery(countQuery)
			  .getSingleResult();

	return new PageImpl<>(resultList, pageable, totalCount);
}
 
Example 13
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    return cb.lessThanOrEqualTo(this.<Date>path(root), date);
}
 
Example 14
@Override
protected <Y extends Comparable<? super Y>> Predicate makePredicate(CriteriaBuilder cb, Expression<? extends Y> x, Y y) {
	return cb.lessThanOrEqualTo(x, y);
}
 
Example 15
Source Project: tutorials   File: GenericRsqlSpecification.java    License: MIT License 4 votes vote down vote up
@Override
public Predicate toPredicate(final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
    final List<Object> args = castArguments(root);
    final Object argument = args.get(0);
    switch (RsqlSearchOperation.getSimpleOperator(operator)) {

    case EQUAL: {
        if (argument instanceof String) {
            return builder.like(root.get(property), argument.toString().replace('*', '%'));
        } else if (argument == null) {
            return builder.isNull(root.get(property));
        } else {
            return builder.equal(root.get(property), argument);
        }
    }
    case NOT_EQUAL: {
        if (argument instanceof String) {
            return builder.notLike(root.<String> get(property), argument.toString().replace('*', '%'));
        } else if (argument == null) {
            return builder.isNotNull(root.get(property));
        } else {
            return builder.notEqual(root.get(property), argument);
        }
    }
    case GREATER_THAN: {
        return builder.greaterThan(root.<String> get(property), argument.toString());
    }
    case GREATER_THAN_OR_EQUAL: {
        return builder.greaterThanOrEqualTo(root.<String> get(property), argument.toString());
    }
    case LESS_THAN: {
        return builder.lessThan(root.<String> get(property), argument.toString());
    }
    case LESS_THAN_OR_EQUAL: {
        return builder.lessThanOrEqualTo(root.<String> get(property), argument.toString());
    }
    case IN:
        return root.get(property).in(args);
    case NOT_IN:
        return builder.not(root.get(property).in(args));
    }

    return null;
}
 
Example 16
Source Project: activejpa   File: Condition.java    License: Apache License 2.0 4 votes vote down vote up
@Override
Predicate createPredicate(CriteriaBuilder builder, Path path, Expression... parameter) {
	return builder.lessThanOrEqualTo(path, parameter[0]);
}
 
Example 17
Source Project: base-framework   File: LeRestriction.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public Predicate build(Path expression, Object value,CriteriaBuilder builder) {
	return builder.lessThanOrEqualTo(expression, (Comparable)value);
}