Skip to content

Adds support for parameterized @Query including ValueExpressions #505

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-ldap</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-453-SNAPSHOT</version>

<name>Spring Data LDAP</name>
<description>Spring Data integration for LDAP</description>
Expand All @@ -21,6 +21,7 @@
<spring-ldap>3.2.6</spring-ldap>
<springdata.commons>3.4.0-SNAPSHOT</springdata.commons>
<java-module-name>spring.data.ldap</java-module-name>
<unboundid-ldapsdk>7.0.1</unboundid-ldapsdk>
</properties>

<developers>
Expand Down Expand Up @@ -109,6 +110,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-test</artifactId>
<version>${spring-ldap}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>${unboundid-ldapsdk}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import static org.springframework.ldap.query.LdapQueryBuilder.*;

import org.springframework.data.expression.ValueEvaluationContext;
import org.springframework.data.expression.ValueEvaluationContextProvider;
import org.springframework.data.ldap.repository.Query;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.repository.query.ValueExpressionDelegate;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
Expand All @@ -31,10 +34,14 @@
*
* @author Mattias Hellborg Arthursson
* @author Mark Paluch
* @author Marcin Grzejszczak
*/
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {

private final Query queryAnnotation;
private final StringBasedQuery query;
private final StringBasedQuery base;
private final ValueEvaluationContextProvider valueContextProvider;

/**
* Construct a new instance.
Expand All @@ -44,26 +51,62 @@ public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
* @param ldapOperations the LdapOperations instance to use.
* @param mappingContext must not be {@literal null}.
* @param instantiators must not be {@literal null}.
* @deprecated use the constructor with {@link ValueExpressionDelegate}
*/
@Deprecated(since = "3.4")
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations,
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
EntityInstantiators instantiators) {

this(queryMethod, entityType, ldapOperations, mappingContext, instantiators, ValueExpressionDelegate.create());
}

/**
* Construct a new instance.
*
* @param queryMethod the QueryMethod.
* @param entityType the managed class.
* @param ldapOperations the LdapOperations instance to use.
* @param mappingContext must not be {@literal null}.
* @param instantiators must not be {@literal null}.
* @param valueExpressionDelegate must not be {@literal null}
* @since 3.4
*/
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations,
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
EntityInstantiators instantiators, ValueExpressionDelegate valueExpressionDelegate) {

super(queryMethod, entityType, ldapOperations, mappingContext, instantiators);

Assert.notNull(queryMethod.getQueryAnnotation(), "Annotation must be present");
Assert.hasLength(queryMethod.getQueryAnnotation().value(), "Query filter must be specified");

queryAnnotation = queryMethod.getRequiredQueryAnnotation();
this.queryAnnotation = queryMethod.getRequiredQueryAnnotation();
this.query = new StringBasedQuery(queryAnnotation.value(), queryMethod.getParameters(), valueExpressionDelegate);
this.base = new StringBasedQuery(queryAnnotation.base(), queryMethod.getParameters(), valueExpressionDelegate);
this.valueContextProvider = valueExpressionDelegate.createValueContextProvider(getQueryMethod().getParameters());
}

@Override
protected LdapQuery createQuery(LdapParameterAccessor parameters) {

return query().base(queryAnnotation.base()) //
String query = bind(parameters, valueContextProvider, this.query);
String base = bind(parameters, valueContextProvider, this.base);

return query().base(base) //
.searchScope(queryAnnotation.searchScope()) //
.countLimit(queryAnnotation.countLimit()) //
.timeLimit(queryAnnotation.timeLimit()) //
.filter(queryAnnotation.value(), parameters.getBindableParameterValues());
.filter(query, parameters.getBindableParameterValues());
}

private String bind(LdapParameterAccessor parameters, ValueEvaluationContextProvider valueContextProvider, StringBasedQuery query) {

ValueEvaluationContext evaluationContext = valueContextProvider
.getEvaluationContext(parameters.getBindableParameterValues(), query.getExpressionDependencies());

return query.bindQuery(parameters,
expression -> expression.evaluate(evaluationContext));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Mark Paluch
* @since 2.6
*/
interface LdapParameterAccessor extends ParameterAccessor {
public interface LdapParameterAccessor extends ParameterAccessor {

/**
* Returns the bindable parameter values of the underlying query method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.data.ldap.repository.Query;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersSource;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.lang.Nullable;

Expand Down Expand Up @@ -85,4 +87,5 @@ Query getRequiredQueryAnnotation() {

throw new IllegalStateException("Required @Query annotation is not present");
}

}
Loading