@SuppressWarnings("unchecked") public static <T, N extends Number> Expression<N> getExpression(CriteriaBuilder cb, Root<T> root, String input) { StringTokenizer tokenizer = new StringTokenizer(input, "+-*/", true); Expression<N> expr = getPath(root, tokenizer.nextToken()); if (tokenizer.hasMoreTokens()) { String op = tokenizer.nextToken(); String name = tokenizer.nextToken(); Expression<N> expr2 = getPath(root, name); if ("+".equals(op)) { expr = cb.sum(expr, expr2); } else if ("-".equals(op)) { expr = cb.diff(expr, expr2); } else if ("*".equals(op)) { expr = cb.prod(expr, expr2); } else if ("/".equals(op)) { expr = (Expression<N>) cb.quot(expr, expr2); } } return expr; }
private Predicate getPredicateGlobalSearch(final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder cb, Predicate predicate) { // check whether a global filter value exists final String globalFilterValue = input.getSearch().getValue(); if (globalFilterValue != null && globalFilterValue.trim().length() > 0) { LOGGER.fine("filtre global: {"+ globalFilterValue+"}"); Predicate matchOneColumnPredicate = cb.disjunction(); // add a 'WHERE .. LIKE' clause on each searchable column for (final DatatableColumn column : input.getColumns()) { if (column.getSearchable()) { LOGGER.log(Level.FINE, "filtre global pour colonne: {}", column); final Expression<String> expression = DatatableHelper.getExpression(root, column.getData(), String.class); matchOneColumnPredicate = cb.or(matchOneColumnPredicate, cb.like(cb.lower(expression), getLikeFilterValue(globalFilterValue), ESCAPE_CHAR)); } } predicate = cb.and(predicate, matchOneColumnPredicate); } return predicate; }
@Override public Predicate toPredicate(final Root<?> leveringsautorisatieRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) { final Root<Dienstbundel> dienstbundelRoot = query.from(Dienstbundel.class); // Join dienst op leveringsautorisatie final Predicate joinPredicateLeveringsautorisatie = leveringsautorisatieRoot.get(ID).in(dienstbundelRoot.get(LEVERINGSAUTORISATIE).get(ID)); // Dienstbundel final Expression<String> attributeExpression = cb.lower(cb.function(UNACCENT, String.class, dienstbundelRoot.get("naam"))); final Expression<String> valueExpression = cb.lower( cb.function( UNACCENT, String.class, cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD)))); final Predicate dienstBundelPredicate = cb.like(attributeExpression, valueExpression); return cb.and(joinPredicateLeveringsautorisatie, dienstBundelPredicate); }
@Override public <Y> Linu set(Path<Y> attribute, Expression<? extends Y> value) { if (!beforeMethodInvoke()) { return this; } criteria.set(attribute, value); return this; }
public List<Pessoa> filtrarPorNome(String filtro){ final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); final CriteriaQuery<Pessoa> cquery = cb.createQuery(Pessoa.class); final Root<Pessoa> root = cquery.from(Pessoa.class); final List<Predicate> condicoes = new ArrayList<Predicate>(); Expression<String> path = root.get("nome"); condicoes.add(cb.like(path, "%"+filtro+"%")); cquery.select(root).where(condicoes.toArray(new Predicate[]{})); List<Pessoa> pessoas = getEntityManager().createQuery(cquery).getResultList(); return pessoas; }
@Override @SuppressWarnings("unchecked") public Predicate buildJpaPredicate(CriteriaBuilder cb, Root<?> root) { Expression<Y> path = QueryFormHelper.getPath(root, field); Y num = (Y) value; return cb.lessThan(path, num); }
@Override @SuppressWarnings({ "unchecked" }) public Predicate buildJpaPredicate(CriteriaBuilder cb, Root<?> root) { Expression<Y> path = QueryFormHelper.getPath(root, field); Y num = (Y) value; return cb.lessThanOrEqualTo(path, num); }
@Override @SuppressWarnings({ "rawtypes", "unchecked" }) public Predicate buildJpaPredicate(CriteriaBuilder builder, Root root) { Expression<Comparable> path = QueryFormHelper.getPath(root, field); Comparable value1 = (Comparable) Array.get(value, 0); Comparable value2 = (Comparable) Array.get(value, 1); return builder.between(path, value1, value2); }
@Override public T like(Expression<String> x, Expression<String> pattern, char escapeChar) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.like(x, pattern, escapeChar)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T lessThan(String x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } Expression<Y> xe = root.get(x); add(cb.lessThan(xe, y)); return (T) this; }
/** * Return a predicate from a rule. */ @SuppressWarnings({ "unchecked", "rawtypes" }) private Predicate getPredicate(final Root<U> root, final CriteriaBuilder cb, final BasicRule rule, final CriteriaQuery<?> query) { if (rule.getOp() == RuleOperator.CT) { return getCustomPredicate(root, cb, rule, query); } final Expression expression = getOrmPath(root, rule); if (expression == null) { // Non matched expression, ignore it... log.info(String.format("SQL injection attack ? Unable to map request rule for property %s", rule.getField())); return null; } return getPredicate(cb, rule, expression); }
@SuppressWarnings ({ "unchecked", "rawtypes" }) protected Predicate buildPredicate(Path<T> root, SearchField field) { Path<T> tt = (!field.getField().contains(".")) ? root.get(field.getField()) : fetchNestedPath(root, field.getField()); CriteriaBuilder criteriaBuilder = this.entitymanager.getCriteriaBuilder(); Class<?> javaType = tt.getJavaType(); if (!classCompatibleWithOperator(javaType, field.getOperator())) { throw new RuntimeException("operator incompatible with field"); } Object valueObject = convertStringValueToObject(field.getValue(), javaType); switch (field.getOperator()) { case GE: return criteriaBuilder.greaterThan((Expression) tt, (Comparable) valueObject); case GTE: return criteriaBuilder.greaterThanOrEqualTo((Expression) tt, (Comparable) valueObject); case LE: return criteriaBuilder.lessThan((Expression) tt, (Comparable) valueObject); case LTE: return criteriaBuilder.lessThanOrEqualTo((Expression) tt, (Comparable) valueObject); case NE: return criteriaBuilder.notEqual(tt, valueObject); case EX: return criteriaBuilder.like((Expression) tt, "%"+field.getValue()+"%"); default: { //EQ return criteriaBuilder.equal(tt, valueObject); } } }
@SuppressWarnings("rawtypes") @Override public T isEmpty(String property) { if (!beforeMethodInvoke()) { return (T) this; } Expression e = root.get(property); add(cb.isEmpty(e)); return (T) this; }
@Override public T isNull(Expression<?> expression) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.isNull(expression)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T lessThan(Expression<? extends Y> x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.lessThan(x, y)); return (T) this; }
@Override public <Y> Linu set(SingularAttribute<? super Object, Y> attribute, Expression<? extends Y> value) { if (!beforeMethodInvoke()) { return this; } criteria.set(attribute, value); return this; }
@Override public <Y extends Number> T lt(Expression<? extends Y> x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.lt(x, y)); return (T) this; }
@Override public T notLike(Expression<String> x, String pattern, Expression<Character> escapeChar) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.notLike(x, pattern, escapeChar)); return (T) this; }
@Override public <E, C extends Collection<E>> T isNotMember(Expression<E> elem, Expression<C> collection) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.isNotMember(elem, collection)); return (T) this; }
@Override public T like(Expression<String> x, String pattern) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.like(x, pattern)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T lessThan(Expression<? extends Y> x, Expression<? extends Y> y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.lessThan(x, y)); return (T) this; }
@Override public <E> T notIn(Expression<E> expression, Expression<?> ...values) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.not(expression.in(values))); return (T) this; }
@Override public Predicate toPredicate(final Root<?> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) { // Like en case insensitive final Path<String> path = PredicateBuilderUtil.<String>getPath(root, naam); final Expression<String> attributeExpression = cb.lower(cb.function(UNACCENT, String.class, path)); final Expression<String> valueExpression = cb.lower(cb.function(UNACCENT, String.class, cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD)))); return cb.like(attributeExpression, valueExpression); }
@Override public <E> T in(Expression<E> expression, Expression<?> ...values) { if (!beforeMethodInvoke()) { return (T) this; } add(expression.in(values)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T between(Expression<? extends Y> v, Y x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.between(v, x, y)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T lessThanOrEqualTo(Expression<? extends Y> x, Expression<? extends Y> y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.lessThanOrEqualTo(x, y)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T lessThanOrEqualTo(Expression<? extends Y> x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.lessThanOrEqualTo(x, y)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T between(String v, Y x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } Expression<Y> ve = root.get(v); add(cb.between(ve, x, y)); return (T) this; }
@Override public <Y extends Number> T le(String x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } Expression<Y> xe = root.get(x); add(cb.le(xe, y)); return (T) this; }
@Override public <Y extends Comparable<? super Y>> T greaterThanOrEqualTo(Expression<? extends Y> x, Y y) { if (!beforeMethodInvoke()) { return (T) this; } add(cb.greaterThanOrEqualTo(x, y)); return (T) this; }
public OrderImpl(Expression<?> expression) { this(expression, true); }
public OrderImpl(Expression<?> expression, boolean ascending) { this.expression = expression; this.ascending = ascending; }
@Override public Expression<String> trim(Expression<Character> t, Expression<String> x) { // TODO Auto-generated method stub return null; }
@Override public Predicate buildJpaPredicate(CriteriaBuilder cb, Root<?> root) { Expression<?> path = QueryFormHelper.getPath(root,field); return cb.isNull(path); }
@Override public Predicate buildJpaPredicate(CriteriaBuilder cb, Root<?> root) { Expression<Object> path = QueryFormHelper.getPath(root, field); return cb.equal(path, value); }
@Override public <E, C extends Collection<E>> Predicate isNotMember( Expression<E> elem, Expression<C> collection) { // TODO Auto-generated method stub return null; }
@Override public Predicate buildJpaPredicate(CriteriaBuilder builder, Root<?> root) { Expression<String> path = QueryFormHelper.getPath(root, field); return builder.like(path, "%" + value); }
@Override public <C extends Collection<?>> Expression<Integer> size( Expression<C> collection) { // TODO Auto-generated method stub return null; }
@Override public Predicate buildJpaPredicate(CriteriaBuilder cb, Root<?> root) { Expression<?> path = QueryFormHelper.getPath(root, field); AbstractExpression expr = (AbstractExpression) value; return cb.or(cb.isNull(path), expr.buildJpaPredicate(cb, root)); }