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

The following examples show how to use javax.persistence.criteria.CriteriaBuilder#greaterThan() . 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
Source Project: onedev   File: NumberCriteria.java    License: MIT License 6 votes vote down vote up
@Override
public Predicate getPredicate(Root<PullRequest> root, CriteriaBuilder builder) {
	Path<Long> attribute = root.get(PullRequest.PROP_NUMBER);
	Predicate numberPredicate;
	
	if (operator == PullRequestQueryLexer.Is)
		numberPredicate = builder.equal(attribute, number.getNumber());
	else if (operator == PullRequestQueryLexer.IsGreaterThan)
		numberPredicate = builder.greaterThan(attribute, number.getNumber());
	else
		numberPredicate = builder.lessThan(attribute, number.getNumber());
	
	return builder.and(
			builder.equal(root.get(PullRequest.PROP_TARGET_PROJECT), number.getProject()),
			numberPredicate);
}
 
Example 2
private Code get(EntityManagerContainer emc, String mobile, String answer) throws Exception {
	EntityManager em = emc.get(Code.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Code> cq = cb.createQuery(Code.class);
	Root<Code> root = cq.from(Code.class);
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.MINUTE, -30);
	Predicate p = cb.greaterThan(root.get(Code_.createTime), cal.getTime());
	p = cb.and(p, cb.equal(root.get(Code_.mobile), mobile));
	p = cb.and(p, cb.equal(root.get(Code_.answer), answer));
	List<Code> list = em.createQuery(cq.where(p)).getResultList();
	if (list.isEmpty()) {
		return null;
	} else {
		return list.get(0);
	}
}
 
Example 3
private Long durationWork(Business business, Date start, Date current, ApplicationStub applicationStub)
		throws Exception {
	EntityManager em = business.entityManagerContainer().get(Work.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Date> cq = cb.createQuery(Date.class);
	Root<Work> root = cq.from(Work.class);
	Predicate p = cb.greaterThan(root.get(Work_.startTime), start);
	p = cb.and(p, cb.equal(root.get(Work_.application), applicationStub.getValue()));
	cq.select(root.get(Work_.startTime)).where(p);
	List<Date> os = em.createQuery(cq).getResultList();
	long duration = 0;
	for (Date o : os) {
		duration += current.getTime() - o.getTime();
	}
	duration = duration / (1000L * 60L);
	return duration;
}
 
Example 4
private Long durationWork(Business business, Date start, Date current, ActivityStub activityStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Work.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Date> cq = cb.createQuery(Date.class);
	Root<Work> root = cq.from(Work.class);
	Predicate p = cb.greaterThan(root.get(Work_.startTime), start);
	p = cb.and(p, cb.equal(root.get(Work_.activity), activityStub.getValue()));
	cq.select(root.get(Work_.startTime)).where(p);
	List<Date> os = em.createQuery(cq).getResultList();
	long duration = 0;
	for (Date o : os) {
		duration += current.getTime() - o.getTime();
	}
	duration = duration / (1000L * 60L);
	return duration;
}
 
Example 5
public Long count(Date start, PersonStub personStub) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Task.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Task> root = cq.from(Task.class);
	Predicate p = cb.greaterThan(root.get(Task_.startTime), start);
	p = cb.and(p, cb.equal(root.get(Task_.person), personStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 6
private Long countWithUnit(Business business, Date start, List<String> units) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Task.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Task> root = cq.from(Task.class);
	Predicate p = cb.greaterThan(root.get(Task_.startTime), start);
	p = cb.and(p, root.get(Task_.unit).in(units));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 7
private Long durationTaskCompleted(Business business, Date start, ApplicationStub applicationStub)
		throws Exception {
	EntityManager em = business.entityManagerContainer().get(TaskCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<TaskCompleted> root = cq.from(TaskCompleted.class);
	Predicate p = cb.greaterThan(root.get(TaskCompleted_.startTime), start);
	p = cb.and(p, cb.equal(root.get(TaskCompleted_.application), applicationStub.getValue()));
	cq.select(cb.sum(root.get(TaskCompleted_.duration))).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 8
private Long countWorkCompleted(Business business, Date start, ProcessStub processStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(WorkCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<WorkCompleted> root = cq.from(WorkCompleted.class);
	Predicate p = cb.greaterThan(root.get(WorkCompleted_.startTime), start);
	p = cb.and(p, cb.equal(root.get(WorkCompleted_.process), processStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 9
private Long countWork(Business business, Date start, ProcessStub processStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Work.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Work> root = cq.from(Work.class);
	Predicate p = cb.greaterThan(root.get(Work_.startTime), start);
	p = cb.and(p, cb.equal(root.get(Work_.process), processStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 10
public Long count(Date start, UnitStub unitStub) throws Exception {
	EntityManager em = this.entityManagerContainer().get(TaskCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<TaskCompleted> root = cq.from(TaskCompleted.class);
	Predicate p = cb.greaterThan(root.get(TaskCompleted_.completedTime), start);
	p = cb.and(p, cb.equal(root.get(TaskCompleted_.unit), unitStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 11
private Long countTaskCompleted(Business business, Date start, ProcessStub processStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(TaskCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<TaskCompleted> root = cq.from(TaskCompleted.class);
	Predicate p = cb.greaterThan(root.get(TaskCompleted_.startTime), start);
	p = cb.and(p, cb.equal(root.get(TaskCompleted_.process), processStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 12
private Long countWork(Business business, Date start, ApplicationStub applicationStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(Work.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Work> root = cq.from(Work.class);
	Predicate p = cb.greaterThan(root.get(Work_.startTime), start);
	p = cb.and(p, cb.equal(root.get(Work_.application), applicationStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 13
private Long countCompletedWithUnit(Business business, Date start, List<String> units) throws Exception {
	EntityManager em = business.entityManagerContainer().get(TaskCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<TaskCompleted> root = cq.from(TaskCompleted.class);
	Predicate p = cb.greaterThan(root.get(TaskCompleted_.completedTime), start);
	p = cb.and(p, root.get(TaskCompleted_.unit).in(units));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 14
Source Project: onedev   File: NumericFieldCriteria.java    License: MIT License 5 votes vote down vote up
@Override
protected Predicate getValuePredicate(Join<?, ?> field, CriteriaBuilder builder) {
	Path<Integer> attribute = field.get(IssueField.PROP_ORDINAL);
	if (operator == IssueQueryLexer.Is)
		return builder.equal(attribute, value);
	else if (operator == IssueQueryLexer.IsGreaterThan)
		return builder.greaterThan(attribute, value);
	else
		return builder.lessThan(attribute, value);
}
 
Example 15
private Long durationTaskCompleted(Business business, Date start, ActivityStub activityStub) throws Exception {
	EntityManager em = business.entityManagerContainer().get(TaskCompleted.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<TaskCompleted> root = cq.from(TaskCompleted.class);
	Predicate p = cb.greaterThan(root.get(TaskCompleted_.startTime), start);
	p = cb.and(p, cb.equal(root.get(TaskCompleted_.activity), activityStub.getValue()));
	cq.select(cb.sum(root.get(TaskCompleted_.duration))).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 16
private Long countWorkExpired(Business business, Date start, Date current, ApplicationStub applicationStub)
		throws Exception {
	EntityManager em = business.entityManagerContainer().get(Work.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Work> root = cq.from(Work.class);
	Predicate p = cb.greaterThan(root.get(Work_.startTime), start);
	p = cb.and(p, cb.lessThan(root.get(Work_.expireTime), current));
	p = cb.and(p, cb.equal(root.get(Work_.application), applicationStub.getValue()));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 17
private Long countExpiredWithUnit(Business business, Date start, Date current, List<String> units)
		throws Exception {
	EntityManager em = business.entityManagerContainer().get(Task.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Task> root = cq.from(Task.class);
	Predicate p = cb.greaterThan(root.get(Task_.startTime), start);
	p = cb.and(p, cb.lessThan(root.get(Task_.expireTime), current));
	p = cb.and(p, root.get(Task_.unit).in(units));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 18
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 19
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 20
public static <E, D extends Comparable<? super D>> Specification<E> toSpecification(final Range<E, D> range) {
    Validate.isTrue(range.isSet(), "You must pass an exploitable range");
    return new Specification<E>() {
        @Override
        public Predicate toPredicate(final Root<E> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
            Predicate rangePredicate = null;

            if (range.isBetween()) {
                rangePredicate = builder.between(root.get(range.getField()), range.getFrom(), range.getTo());
            } else if (range.isFromSet()) {
                // rangePredicate =
                // builder.greaterThanOrEqualTo(root.get(range.getField()),
                // range.getFrom());
                rangePredicate = builder.greaterThan(root.get(range.getField()), range.getFrom());
            } else if (range.isToSet()) {
                // rangePredicate =
                // builder.lessThanOrEqualTo(root.get(range.getField()),
                // range.getTo());
                rangePredicate = builder.lessThan(root.get(range.getField()), range.getTo());
            }

            if (rangePredicate != null) {
                if (!range.isIncludeNullSet() || Boolean.FALSE.equals(range.getIncludeNull())) {
                    return rangePredicate;
                } else {
                    return builder.or(rangePredicate, builder.isNull(root.get(range.getField())));
                }
            }

            // no range at all
            // take the opportunity to keep only null...
            if (Boolean.TRUE.equals(range.getIncludeNull())) {
                return builder.isNull(root.get(range.getField()));
            }

            // ... or non-null only...
            if (Boolean.FALSE.equals(range.getIncludeNull())) {
                return builder.isNotNull(root.get(range.getField()));
            }

            throw new IllegalStateException("You must pass an exploitable range (should not happen here)");
        }
    };
}