Search in sources :

Example 1 with CollectionPathBase

use of com.querydsl.core.types.dsl.CollectionPathBase in project spring-data-commons by spring-projects.

the class PropertyPathInformation method reifyPath.

private static Path<?> reifyPath(EntityPathResolver resolver, PropertyPath path, Optional<Path<?>> base) {
    Optional<Path<?>> map = // 
    base.filter(it -> it instanceof CollectionPathBase).map(CollectionPathBase.class::cast).map(// 
    CollectionPathBase::any).map(// 
    Path.class::cast).map(it -> reifyPath(resolver, path, Optional.of(it)));
    return map.orElseGet(() -> {
        Path<?> entityPath = base.orElseGet(() -> resolver.createPath(path.getOwningType().getType()));
        Field field = org.springframework.data.util.ReflectionUtils.findRequiredField(entityPath.getClass(), path.getSegment());
        Object value = ReflectionUtils.getField(field, entityPath);
        PropertyPath next = path.next();
        if (next != null) {
            return reifyPath(resolver, next, Optional.of((Path<?>) value));
        }
        return (Path<?>) value;
    });
}
Also used : Path(com.querydsl.core.types.Path) PropertyPath(org.springframework.data.mapping.PropertyPath) Field(java.lang.reflect.Field) PropertyPath(org.springframework.data.mapping.PropertyPath) CollectionPathBase(com.querydsl.core.types.dsl.CollectionPathBase)

Example 2 with CollectionPathBase

use of com.querydsl.core.types.dsl.CollectionPathBase in project spring-data-commons by spring-projects.

the class QuerydslDefaultBinding method bind.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.web.querydsl.QueryDslPredicateBuilder#buildPredicate(org.springframework.data.mapping.PropertyPath, java.lang.Object)
	 */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Optional<Predicate> bind(Path<?> path, Collection<? extends Object> value) {
    Assert.notNull(path, "Path must not be null!");
    Assert.notNull(value, "Value must not be null!");
    if (value.isEmpty()) {
        return Optional.empty();
    }
    if (path instanceof CollectionPathBase) {
        BooleanBuilder builder = new BooleanBuilder();
        for (Object element : value) {
            builder.and(((CollectionPathBase) path).contains(element));
        }
        return Optional.of(builder.getValue());
    }
    if (path instanceof SimpleExpression) {
        if (value.size() > 1) {
            return Optional.of(((SimpleExpression) path).in(value));
        }
        return Optional.of(((SimpleExpression) path).eq(value.iterator().next()));
    }
    throw new IllegalArgumentException(String.format("Cannot create predicate for path '%s' with type '%s'.", path, path.getMetadata().getPathType()));
}
Also used : BooleanBuilder(com.querydsl.core.BooleanBuilder) CollectionPathBase(com.querydsl.core.types.dsl.CollectionPathBase) SimpleExpression(com.querydsl.core.types.dsl.SimpleExpression)

Aggregations

CollectionPathBase (com.querydsl.core.types.dsl.CollectionPathBase)2 BooleanBuilder (com.querydsl.core.BooleanBuilder)1 Path (com.querydsl.core.types.Path)1 SimpleExpression (com.querydsl.core.types.dsl.SimpleExpression)1 Field (java.lang.reflect.Field)1 PropertyPath (org.springframework.data.mapping.PropertyPath)1