Example usage for javax.persistence.criteria Root get

List of usage examples for javax.persistence.criteria Root get

Introduction

In this page you can find the example usage for javax.persistence.criteria Root get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate hasAttributeReqLevel(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeReqLevel) {
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(REQ_LEVEL)), attributeReqLevel.toLowerCase()))));
}

From source file:org.unidle.feature.step.StepDefs.java

public static Specification<UserConnection> hasService(final String service) {
    return new Specification<UserConnection>() {
        @Override/*  w w w.  j a va  2  s. co m*/
        public Predicate toPredicate(final Root<UserConnection> root, final CriteriaQuery<?> criteriaQuery,
                final CriteriaBuilder criteriaBuilder) {
            return criteriaBuilder.equal(root.get(providerId), service);
        }
    };
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate existsInData(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String term) {/*  w w w .ja v  a 2 s .c om*/
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot)
            .where(cb.and(cb.equal(subroot.get(ANNO), root), cb.like(cb.lower(subroot.get(VALUE)), term))));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

/**
 * Requester access predicate//from   w ww .  jav  a2 s. com
 * <p>
 * annotation has public access OR annotation has private access AND
 * annotation belongs to requester OR annotation has group access AND
 * requester is in group
 */
private static Predicate authFilter(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester) {
    final List<Predicate> predicates = new ArrayList<>(0);

    // public access
    predicates.add(cb.like(root.get(ACCESS).get(NAME), PUBLIC_ACCESS));

    // private access AND belongs to requester
    predicates.add(
            cb.and(cb.like(root.get(ACCESS).get(NAME), PRIVATE_ACCESS), cb.equal(root.get(USER), requester)));

    // group access AND requester in group
    // criteriabuilder's in clause throws
    // sql error if collection has no elements
    if (requester.getGroups().size() > 0) {
        predicates.add(cb.and(cb.like(root.get(ACCESS).get(NAME), GROUP_ACCESS),
                root.get(GROUP).in(requester.getGroups())));
    }
    return cb.or(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate buildIdSpec(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester,
        Long id) {//from  w w w . j ava 2 s .  c om
    final Predicate authPredicate = authFilter(root, cb, requester);
    final Predicate idPredicate = cb.equal(root.get(ID), id);
    final Predicate[] predicates = new Predicate[] { authPredicate, idPredicate };
    return cb.and(predicates);
}

From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Predicate generatePredicate(Root root, CriteriaBuilder builder,
        QueryParameter... queryParameters) {
    Predicate condition = builder.conjunction();
    for (QueryParameter queryParameter : queryParameters) {
        Object value = queryParameter.getValue();
        if (value == null || StringUtils.isBlank(value.toString())) {
            continue;
        }//from  ww  w.j  ava2 s  .co  m
        Path path = root.get(queryParameter.getKey());
        switch (queryParameter.getMode()) {
        case BETWEEN:
            Object[] values = asArray(value);
            if (values != null) {
                condition = builder.and(builder.between((Path<Comparable>) path, asComparable(values[0]),
                        asComparable(values[1])));
            }
            break;
        case GREATER_THAN:
            condition = builder.and(condition,
                    builder.greaterThan((Path<Comparable>) path, asComparable(value)));
            break;
        case GREATER_EQUALS:
            condition = builder.and(condition,
                    builder.greaterThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_THAN:
            condition = builder.and(condition, builder.lessThan((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_EQUALS:
            condition = builder.and(condition,
                    builder.lessThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case IS_NULL:
            condition = builder.and(condition, builder.isNull(path));
            break;
        case IS_NOT_NULL:
            condition = builder.and(condition, builder.isNotNull(path));
            break;
        case IN:
            condition = builder.and(condition, path.in(asArray(value)));
            break;
        case NOT_IN:
            condition = builder.and(condition, builder.not(path.in(asArray(value))));
            break;
        case LIKE:
            condition = builder.and(condition, builder.like(path, "%" + String.valueOf(value) + "%"));
            break;
        case NOT_LIKE:
            condition = builder.and(condition, builder.notLike(path, "%" + String.valueOf(value) + "%"));
            break;
        case EQUALS:
            condition = builder.and(condition, builder.equal(path, value));
            break;
        case NOT_EQUALS:
            condition = builder.and(condition, builder.notEqual(path, value));
            break;
        default:
            break;
        }
    }
    return condition;
}

From source file:com.mxep.web.common.persistence.DynamicSpecifications.java

private static List<Predicate> getPredicate(Collection<SearchFilter> filters, Root<?> root,
        CriteriaQuery<?> query, CriteriaBuilder builder) {
    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]);
        }//w  ww .  j  a  v a  2 s. c  o  m

        /*????*/
        if (expression.getJavaType().equals(Integer.class)
                && !filter.operator.equals(SearchFilter.Operator.IN)) {
            filter.value = Integer.parseInt(filter.value.toString());
        }

        // logic operator
        switch (filter.operator) {
        case EQ:
            predicates.add(builder.equal(expression, filter.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;
        case NEQ:
            predicates.add(builder.notEqual(expression, filter.value));
            break;
        case IN:
            In in = builder.in(expression);
            String value = filter.value.toString();
            String[] values = StringUtils.split(value, ",");
            for (int i = 0; i < values.length; i++) {
                /*????*/
                if (expression.getJavaType().equals(Integer.class) || expression.getJavaType().equals(int.class)
                        || expression.getJavaType().equals(byte.class)) {
                    in.value(Integer.valueOf(values[i]));
                } else {
                    in.value(values[i]);
                }

            }
            predicates.add(in);
            break;
        case NULL:
            predicates.add(builder.isNull(expression));
            break;
        case NOTNULL:
            predicates.add(builder.isNotNull(expression));
            break;
        }
    }
    return predicates;
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate buildParentSpec(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester,
        Long id, Boolean showRedacted) {
    final Predicate authPredicate = authFilter(root, cb, requester);
    final Predicate idPredicate = cb.equal(root.get(PARENT).get(ID), id);
    Predicate[] predicates;//ww w .jav a 2 s  .c o m
    if (showRedacted) {
        predicates = new Predicate[] { authPredicate, idPredicate };
    } else {
        predicates = new Predicate[] { authPredicate, idPredicate, notRedacted(root, cb) };
    }
    return cb.and(predicates);
}

From source file:com.atschx.summer.core.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/* w  w  w.ja v  a  2  s  .co  m*/
        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:
                        predicates.add(builder.equal(expression, filter.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();
        }
    };
}

From source file:com.the3.base.repository.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override// w  w w. ja  v  a  2  s  .c  o m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && !filters.isEmpty()) {

                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:
                        predicates.add(builder.equal(expression, filter.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();
        }
    };
}