private <Q extends CommonAbstractCriteria> FilterResult<Q> filterQuery(Q query, Root<?> root) { Map<Path, Class<?>> selectedTypes = new HashMap<Path, Class<?>>(); Path path = getSelectedPath(0, root); selectedTypes.put(path, root.getJavaType()); AccessDefinition accessDefinition = createAccessDefinition( selectedTypes, AccessType.READ, root.getAlias() != null? singleton(new Alias(root.getAlias())): Collections.<Alias>emptySet()); FilterResult<Q> filterResult = getAlwaysEvaluatableResult(new JpqlCompiledStatement(null), query, accessDefinition); if (filterResult != null) { return filterResult; } optimize(accessDefinition); Set<String> parameterNames = compiler.getNamedParameters(accessDefinition.getAccessRules()); Map<String, Object> parameters = accessDefinition.getQueryParameters(); parameters.keySet().retainAll(parameterNames); CriteriaHolder criteriaHolder = new CriteriaHolder(query); getQueryPreparator().createWhere(accessDefinition.getAccessRules()).visit(criteriaVisitor, criteriaHolder); return new CriteriaFilterResult<Q>( query, parameters.size() > 0? parameters: null, criteriaHolder.getParameters()); }
private <C extends CommonAbstractCriteria, Q extends Query> Q createQuery(Q query, FilterResult<C> filterResult) { Q secureQuery = (Q)new SecureQuery(query, null, // TODO how to extract this? filterResult.getSelectedPaths(), super.getFlushMode()); if (filterResult.getParameters() != null && filterResult instanceof CriteriaFilterResult) { CriteriaFilterResult<C> criteriaResult = (CriteriaFilterResult<C>)filterResult; for (Parameter<?> parameter: criteriaResult.getCriteriaParameters()) { Object value = filterResult.getParameters().get(parameter.getName()); secureQuery.setParameter((Parameter<Object>)parameter, value); } } return secureQuery; }
private From<?, ?> getFrom(CommonAbstractCriteria query, Alias alias) { if (query instanceof CriteriaUpdate) { return ((CriteriaUpdate<?>)query).getRoot(); } else if (query instanceof CriteriaDelete) { return ((CriteriaUpdate<?>)query).getRoot(); } else { return getFrom((AbstractQuery<?>)query, alias); } }
@Nullable private <U, V extends CommonAbstractCriteria, W extends Query> W buildQuery(EntityManager entityManager, BiFunction<EntityManager, V, W> queryBuilder, V criteriaQuery, BiFunction<V, Predicate[], V> whereBuilder, Root<T> root, @Nullable Integer maxResults) { Predicate predicate = null; if (this.hasKeywords()) { Long[] fullTextEntityIds = this.processKeywords(entityManager, maxResults); if (ArrayUtils.isEmpty(fullTextEntityIds)) { return null; } Path<BigInteger> fullTextEntityIdPath = root.get(DbPropertyNames.ENTITY_ID); predicate = ((fullTextEntityIds.length == 1) ? this.criteriaBuilder.equal(fullTextEntityIdPath, fullTextEntityIds[0]) : this.criteriaBuilder.in(fullTextEntityIdPath).in(((Object[]) fullTextEntityIds))); maxResults = ((maxResults != null) ? Math.min(maxResults, fullTextEntityIds.length) : fullTextEntityIds.length); } if (this.hasCriterions()) { Predicate criterionsPredicate = this.criteriaBuilder .and(this.criterions.stream().map(criterion -> criterion.toPredicate(this.criteriaBuilder, criteriaQuery, root)).toArray(Predicate[]::new)); predicate = ((predicate != null) ? this.criteriaBuilder.and(predicate, criterionsPredicate) : criterionsPredicate); } if (predicate != null) { whereBuilder.apply(criteriaQuery, ArrayUtils.toArray(predicate)); } W query = queryBuilder.apply(entityManager, criteriaQuery); if (maxResults != null) { query.setMaxResults(maxResults); } return query; }
public CriteriaHolder(CommonAbstractCriteria query) { criteriaQuery = query; setValue(query); }
public Predicate toPredicate(CriteriaBuilder builder, CommonAbstractCriteria query, Root<T> root);