Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tmp #3211

Closed
wants to merge 6 commits into from
Closed

tmp #3211

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,28 @@
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-api</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-client-api</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
<repository>
<id>github</id>
<name>GitHub rodrigorodrigues Apache Maven Packages</name>
<url>https://maven.pkg.github.com/rodrigorodrigues/spring-data-commons</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Oliver Gierke
* @since 1.13
*/
interface PathInformation {
public interface PathInformation {

/**
* The root property owner type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ boolean isPathAvailable(String path, Class<?> type) {
* @param type
* @return
*/
boolean isPathAvailable(String path, TypeInformation<?> type) {
public boolean isPathAvailable(String path, TypeInformation<?> type) {

Assert.notNull(path, "Path must not be null");
Assert.notNull(type, "Type must not be null");
Expand Down Expand Up @@ -241,7 +241,7 @@ Optional<Path<?>> getExistingPath(PathInformation path) {
* @return
*/
@Nullable
PathInformation getPropertyPath(String path, TypeInformation<?> type) {
public PathInformation getPropertyPath(String path, TypeInformation<?> type) {

Assert.notNull(path, "Path must not be null");
Assert.notNull(type, "Type information must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,20 @@
*/
package org.springframework.data.querydsl.binding;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.PropertyValues;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.Property;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
* Builder assembling {@link Predicate} out of {@link PropertyValues}.
Expand All @@ -50,12 +39,12 @@
* @author Johannes Englmeier
* @since 1.11
*/
public class QuerydslPredicateBuilder {
public class QuerydslPredicateBuilder implements QuerydslPredicateBuilderCustomizer {

private final ConversionService conversionService;
private final MultiValueBinding<Path<? extends Object>, Object> defaultBinding;
private final Map<PathInformation, Path<?>> paths;
private final EntityPathResolver resolver;
protected final ConversionService conversionService;
protected final MultiValueBinding<Path<? extends Object>, Object> defaultBinding;
protected final Map<PathInformation, Path<?>> paths;
protected final EntityPathResolver resolver;

/**
* Creates a new {@link QuerydslPredicateBuilder} for the given {@link ConversionService} and
Expand Down Expand Up @@ -111,145 +100,12 @@ public Predicate getPredicate(TypeInformation<?> type, MultiValueMap<String, ?>
continue;
}

Collection<Object> value = convertToPropertyPathSpecificType(entry.getValue(), propertyPath);
Optional<Predicate> predicate = invokeBinding(propertyPath, bindings, value);
Collection<Object> value = convertToPropertyPathSpecificType(entry.getValue(), propertyPath, conversionService);
Optional<Predicate> predicate = invokeBinding(propertyPath, bindings, value, resolver, defaultBinding);

predicate.ifPresent(builder::and);
}

return getPredicate(builder);
}

/**
* Returns whether the given {@link Predicate} represents an empty predicate instance.
*
* @param predicate
* @return
* @since 2.5.3
* @see BooleanBuilder
*/
public static boolean isEmpty(Predicate predicate) {
return new BooleanBuilder().equals(predicate);
}

/**
* Invokes the binding of the given values, for the given {@link PropertyPath} and {@link QuerydslBindings}.
*
* @param dotPath must not be {@literal null}.
* @param bindings must not be {@literal null}.
* @param values must not be {@literal null}.
* @return
*/
private Optional<Predicate> invokeBinding(PathInformation dotPath, QuerydslBindings bindings,
Collection<Object> values) {

Path<?> path = getPath(dotPath, bindings);

return bindings.getBindingForPath(dotPath).orElse(defaultBinding).bind(path, values);
}

/**
* Returns the {@link Path} for the given {@link PropertyPath} and {@link QuerydslBindings}. Will try to obtain the
* {@link Path} from the bindings first but fall back to reifying it from the PropertyPath in case no specific binding
* has been configured.
*
* @param path must not be {@literal null}.
* @param bindings must not be {@literal null}.
* @return
*/
private Path<?> getPath(PathInformation path, QuerydslBindings bindings) {

Optional<Path<?>> resolvedPath = bindings.getExistingPath(path);

return resolvedPath.orElseGet(() -> paths.computeIfAbsent(path, it -> it.reifyPath(resolver)));
}

/**
* Converts the given source values into a collection of elements that are of the given {@link PropertyPath}'s type.
* Considers a single element list with an empty object an empty collection because this basically indicates the
* property having been submitted but no value provided.
*
* @param source must not be {@literal null}.
* @param path must not be {@literal null}.
* @return
*/
private Collection<Object> convertToPropertyPathSpecificType(List<?> source, PathInformation path) {

if (source.isEmpty() || isSingleElementCollectionWithEmptyItem(source)) {
return Collections.emptyList();
}

TypeDescriptor targetType = getTargetTypeDescriptor(path);
Collection<Object> target = new ArrayList<>(source.size());

for (Object value : source) {
target.add(getValue(targetType, value));
}

return target;
}

@Nullable
private Object getValue(TypeDescriptor targetType, Object value) {

if (ClassUtils.isAssignableValue(targetType.getType(), value)) {
return value;
}

if (conversionService.canConvert(value.getClass(), targetType.getType())) {
return conversionService.convert(value, TypeDescriptor.forObject(value), targetType);
}

return value;
}

/**
* Returns the target {@link TypeDescriptor} for the given {@link PathInformation} by either inspecting the field or
* property (the latter preferred) to pick up annotations potentially defined for formatting purposes.
*
* @param path must not be {@literal null}.
* @return
*/
private static TypeDescriptor getTargetTypeDescriptor(PathInformation path) {

PropertyDescriptor descriptor = path.getLeafPropertyDescriptor();

Class<?> owningType = path.getLeafParentType();
String leafProperty = path.getLeafProperty();

TypeDescriptor result = descriptor == null //
? TypeDescriptor
.nested(org.springframework.data.util.ReflectionUtils.findRequiredField(owningType, leafProperty), 0)
: TypeDescriptor
.nested(new Property(owningType, descriptor.getReadMethod(), descriptor.getWriteMethod(), leafProperty), 0);

if (result == null) {
throw new IllegalStateException(String.format("Could not obtain TypeDescriptor for PathInformation %s", path));
}

return result;
}

/**
* Returns whether the given collection has exactly one element that is empty (i.e. doesn't contain text). This is
* basically an indicator that a request parameter has been submitted but no value for it.
*
* @param source must not be {@literal null}.
* @return
*/
private static boolean isSingleElementCollectionWithEmptyItem(List<?> source) {
return source.size() == 1 && ObjectUtils.isEmpty(source.get(0));
}

/**
* Returns the {@link Predicate} from {@link BooleanBuilder}.
*
* @param builder
* @return
*/
private static Predicate getPredicate(BooleanBuilder builder) {

Predicate predicate = builder.getValue();
return predicate == null ? new BooleanBuilder() : predicate;
}
}
Loading