DATALDAP-7 - Polishing.

Reformat code with Spring Data formatter. Add JavaDoc to overrides and constructors. Add missing assertions to public entry points. Remove since tags from JavaDoc.
This commit is contained in:
Mark Paluch
2016-12-06 11:59:54 +01:00
parent d594183e8d
commit 5096ae32a2
17 changed files with 346 additions and 106 deletions

View File

@@ -24,7 +24,6 @@ import org.springframework.ldap.query.LdapQuery;
* Ldap specific extensions to CrudRepository.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
public interface LdapRepository<T> extends CrudRepository<T, Name> {

View File

@@ -28,7 +28,6 @@ import org.springframework.ldap.query.SearchScope;
* automatic query methods based on statically defined queries.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@@ -36,8 +35,8 @@ import org.springframework.ldap.query.SearchScope;
public @interface Query {
/**
* Search base, to be used as input to
* {@link org.springframework.ldap.query.LdapQueryBuilder#base(javax.naming.Name)}.
* Search base, to be used as input to {@link org.springframework.ldap.query.LdapQueryBuilder#base(javax.naming.Name)}
* .
*
* @return the search base, default is {@link org.springframework.ldap.support.LdapUtils#emptyLdapName()}
*/

View File

@@ -24,15 +24,20 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
* LDAP-specific {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
class LdapRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getAnnotation()
*/
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableLdapRepositories.class;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension()
*/
@Override
protected RepositoryConfigurationExtension getExtension() {
return new LdapRepositoryConfigurationExtension();

View File

@@ -22,12 +22,12 @@ import java.util.Collections;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
@@ -36,7 +36,6 @@ import org.w3c.dom.Element;
*
* @author Mattias Hellborg Arthursson
* @author Mark Paluch
* @since 2.0
*/
public class LdapRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
@@ -86,11 +85,15 @@ public class LdapRepositoryConfigurationExtension extends RepositoryConfiguratio
return Collections.<Class<?>> singleton(LdapRepository.class);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.XmlRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
Element element = config.getElement();
String ldapTemplateRef = element.getAttribute(ATT_LDAP_TEMPLATE_REF);
if (!StringUtils.hasText(ldapTemplateRef)) {
ldapTemplateRef = "ldapTemplate";
}
@@ -98,6 +101,9 @@ public class LdapRepositoryConfigurationExtension extends RepositoryConfiguratio
builder.addPropertyReference("ldapOperations", ldapTemplateRef);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {

View File

@@ -16,49 +16,81 @@
package org.springframework.data.ldap.repository.query;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.ldap.repository.Query;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
/**
* Base class for {@link RepositoryQuery} implementations for LDAP.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
* @author Mark Paluch
*/
abstract class AbstractLdapRepositoryQuery implements RepositoryQuery {
public abstract class AbstractLdapRepositoryQuery implements RepositoryQuery {
private final LdapQueryMethod queryMethod;
private final Class<?> clazz;
private final Class<?> entityType;
private final LdapOperations ldapOperations;
public AbstractLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
/**
* Creates a new {@link AbstractLdapRepositoryQuery} instance given {@link LdapQuery}, {@link Class} and
* {@link LdapOperations}.
*
* @param queryMethod must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param ldapOperations must not be {@literal null}.
*/
public AbstractLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations) {
Assert.notNull(queryMethod, "MongoQueryMethod must not be null!");
Assert.notNull(entityType, "Entity type must not be null!");
Assert.notNull(ldapOperations, "LdapOperations must not be null!");
this.queryMethod = queryMethod;
this.clazz = clazz;
this.entityType = entityType;
this.ldapOperations = ldapOperations;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[])
*/
@Override
public final Object execute(Object[] parameters) {
LdapQuery query = createQuery(parameters);
if (queryMethod.isCollectionQuery()) {
return ldapOperations.find(query, clazz);
return ldapOperations.find(query, entityType);
} else {
try {
return ldapOperations.findOne(query, clazz);
return ldapOperations.findOne(query, entityType);
} catch (EmptyResultDataAccessException e) {
return null;
}
}
}
/**
* Creates a {@link Query} instance using the given {@literal parameters}.
*
* @param parameters must not be {@literal null}.
* @return
*/
protected abstract LdapQuery createQuery(Object[] parameters);
Class<?> getClazz() {
return clazz;
/**
* @return
*/
protected Class<?> getEntityClass() {
return entityType;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod()
*/
@Override
public final QueryMethod getQueryMethod() {
return queryMethod;

View File

@@ -17,16 +17,15 @@ package org.springframework.data.ldap.repository.query;
import static org.springframework.ldap.query.LdapQueryBuilder.*;
import org.springframework.data.ldap.repository.Query;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.data.ldap.repository.Query;
import org.springframework.util.Assert;
/**
* Handles queries for repository methods annotated with {@link org.springframework.data.ldap.repository.Query}.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
@@ -36,19 +35,22 @@ public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
* Construct a new instance.
*
* @param queryMethod the QueryMethod.
* @param clazz the managed class.
* @param entityType the managed class.
* @param ldapOperations the LdapOperations instance to use.
*/
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations) {
super(queryMethod, clazz, ldapOperations);
super(queryMethod, entityType, ldapOperations);
Assert.notNull(queryMethod.getQueryAnnotation(), "Annotation must be present");
Assert.hasLength(queryMethod.getQueryAnnotation().value(), "Query filter must be specified");
queryAnnotation = queryMethod.getQueryAnnotation();
Assert.notNull(queryMethod, "Annotation must be present");
Assert.hasLength(queryAnnotation.value(), "Query filter must be specified");
}
/* (non-Javadoc)
* @see org.springframework.data.ldap.repository.query.AbstractLdapRepositoryQuery#createQuery(java.lang.Object[])
*/
@Override
protected LdapQuery createQuery(Object[] parameters) {

View File

@@ -31,34 +31,47 @@ import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
import org.springframework.ldap.query.ConditionCriteria;
import org.springframework.ldap.query.ContainerCriteria;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
/**
* Creator of dynamic queries based on method names.
*
*
* @author Mattias Hellborg Arthursson
* @since 2.0
* @author Mark Paluch
*/
public class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerCriteria> {
class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerCriteria> {
private final Class<?> clazz;
private final Class<?> entityType;
private final ObjectDirectoryMapper mapper;
/**
* Construct a new instance.
* Constructs a new {@link LdapQueryCreator}.
*
* @param tree must not be {@literal null}.
* @param parameters must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param mapper must not be {@literal null}.
* @param values must not be {@literal null}.
*/
public LdapQueryCreator(PartTree tree, Parameters<?, ?> parameters, Class<?> clazz, ObjectDirectoryMapper mapper,
LdapQueryCreator(PartTree tree, Parameters<?, ?> parameters, Class<?> entityType, ObjectDirectoryMapper mapper,
Object[] values) {
super(tree, new ParametersParameterAccessor(parameters, values));
this.clazz = clazz;
Assert.notNull(entityType, "Entity type must not be null!");
Assert.notNull(mapper, "ObjectDirectoryMapper must not be null!");
this.entityType = entityType;
this.mapper = mapper;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator)
*/
@Override
protected ContainerCriteria create(Part part, Iterator<Object> iterator) {
String base = clazz.getAnnotation(Entry.class).base();
String base = entityType.getAnnotation(Entry.class).base();
ConditionCriteria criteria = query().base(base).where(getAttribute(part));
return appendCondition(part, iterator, criteria);
@@ -95,9 +108,10 @@ public class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerC
return criteria.isPresent();
case IS_NULL:
return criteria.not().isPresent();
default:
throw new IllegalArgumentException(String.format("%s queries are not supported for LDAP repositories", type));
}
throw new IllegalArgumentException(String.format("%s queries are not supported for LDAP repositories", type));
}
private String getAttribute(Part part) {
@@ -106,9 +120,12 @@ public class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerC
throw new IllegalArgumentException("Nested properties are not supported");
}
return mapper.attributeFor(clazz, path.getSegment());
return mapper.attributeFor(entityType, path.getSegment());
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#and(org.springframework.data.repository.query.parser.Part, java.lang.Object, java.util.Iterator)
*/
@Override
protected ContainerCriteria and(Part part, ContainerCriteria base, Iterator<Object> iterator) {
ConditionCriteria criteria = base.and(getAttribute(part));
@@ -116,11 +133,17 @@ public class LdapQueryCreator extends AbstractQueryCreator<LdapQuery, ContainerC
return appendCondition(part, iterator, criteria);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#or(java.lang.Object, java.lang.Object)
*/
@Override
protected ContainerCriteria or(ContainerCriteria base, ContainerCriteria criteria) {
return base.or(criteria);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#complete(java.lang.Object, org.springframework.data.domain.Sort)
*/
@Override
protected LdapQuery complete(ContainerCriteria criteria, Sort sort) {
return criteria;

View File

@@ -18,17 +18,16 @@ package org.springframework.data.ldap.repository.query;
import java.lang.reflect.Method;
import org.springframework.core.annotation.AnnotationUtils;
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.QueryMethod;
import org.springframework.data.ldap.repository.Query;
/**
* QueryMethod for Ldap Queries.
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
*/
public class LdapQueryMethod extends QueryMethod {
@@ -50,8 +49,8 @@ public class LdapQueryMethod extends QueryMethod {
/**
* Check whether the target method is annotated with {@link org.springframework.data.ldap.repository.Query}.
*
* @return <code>true</code> if the target method is annotated with {@link org.springframework.data.ldap.repository.Query},
* <code>false</code> otherwise.
* @return <code>true</code> if the target method is annotated with
* {@link org.springframework.data.ldap.repository.Query}, <code>false</code> otherwise.
*/
public boolean hasQueryAnnotation() {
return getQueryAnnotation() != null;

View File

@@ -16,13 +16,17 @@
package org.springframework.data.ldap.repository.query;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
import org.springframework.ldap.query.LdapQuery;
/**
* {@link RepositoryQuery} implementation for LDAP.
*
* @author Mattias Hellborg Arthursson
* @author Mark Paluch
*/
public class PartTreeLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
@@ -30,20 +34,30 @@ public class PartTreeLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
private final Parameters<?, ?> parameters;
private final ObjectDirectoryMapper objectDirectoryMapper;
public PartTreeLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> clazz, LdapOperations ldapOperations) {
/**
* Creates a new {@link PartTreeLdapRepositoryQuery}.
*
* @param queryMethod must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param ldapOperations must not be {@literal null}.
*/
public PartTreeLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations) {
super(queryMethod, clazz, ldapOperations);
super(queryMethod, entityType, ldapOperations);
partTree = new PartTree(queryMethod.getName(), clazz);
partTree = new PartTree(queryMethod.getName(), entityType);
parameters = queryMethod.getParameters();
objectDirectoryMapper = ldapOperations.getObjectDirectoryMapper();
}
/* (non-Javadoc)
* @see org.springframework.data.ldap.repository.query.AbstractLdapRepositoryQuery#createQuery(java.lang.Object[])
*/
@Override
protected LdapQuery createQuery(Object[] actualParameters) {
org.springframework.data.ldap.repository.query.LdapQueryCreator queryCreator = new LdapQueryCreator(partTree,
this.parameters, getClazz(), objectDirectoryMapper, actualParameters);
this.parameters, getEntityClass(), objectDirectoryMapper, actualParameters);
return queryCreator.createQuery();
}
}

View File

@@ -27,33 +27,34 @@ import org.springframework.ldap.odm.annotations.Id;
import com.querydsl.apt.DefaultConfiguration;
/**
* Configuration for {@link LdapAnnotationProcessor}.
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
*/
class DefaultLdapAnnotationProcessorConfiguration extends DefaultConfiguration {
public DefaultLdapAnnotationProcessorConfiguration(
RoundEnvironment roundEnv,
Map<String, String> options,
Collection<String> keywords,
Class<? extends Annotation> entitiesAnn,
Class<? extends Annotation> entityAnn,
Class<? extends Annotation> superTypeAnn,
Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> embeddedAnn,
Class<? extends Annotation> skipAnn) {
public DefaultLdapAnnotationProcessorConfiguration(RoundEnvironment roundEnv, Map<String, String> options,
Collection<String> keywords, Class<? extends Annotation> entitiesAnn, Class<? extends Annotation> entityAnn,
Class<? extends Annotation> superTypeAnn, Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> embeddedAnn, Class<? extends Annotation> skipAnn) {
super(roundEnv, options, keywords, entitiesAnn, entityAnn, superTypeAnn, embeddableAnn, embeddedAnn, skipAnn);
}
super(roundEnv, options, keywords, entitiesAnn, entityAnn, superTypeAnn, embeddableAnn, embeddedAnn, skipAnn);
}
@Override
public boolean isBlockedField(VariableElement field) {
return super.isBlockedField(field) || field.getAnnotation(Id.class) != null;
}
/* (non-Javadoc)
* @see com.querydsl.apt.DefaultConfiguration#isBlockedField(javax.lang.model.element.VariableElement)
*/
@Override
public boolean isBlockedField(VariableElement field) {
return super.isBlockedField(field) || field.getAnnotation(Id.class) != null;
}
@Override
public boolean isValidField(VariableElement field) {
return super.isValidField(field) && field.getAnnotation(Id.class) == null;
}
/* (non-Javadoc)
* @see com.querydsl.apt.DefaultConfiguration#isValidField(javax.lang.model.element.VariableElement)
*/
@Override
public boolean isValidField(VariableElement field) {
return super.isValidField(field) && field.getAnnotation(Id.class) == null;
}
}

View File

@@ -36,12 +36,14 @@ import com.querydsl.core.annotations.QueryEntities;
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
*/
@SupportedAnnotationTypes("org.springframework.ldap.odm.annotations.*")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class LdapAnnotationProcessor extends AbstractQuerydslProcessor {
/* (non-Javadoc)
* @see com.querydsl.apt.AbstractQuerydslProcessor#createConfiguration(javax.annotation.processing.RoundEnvironment)
*/
@Override
protected Configuration createConfiguration(RoundEnvironment roundEnv) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Running " + getClass().getSimpleName());

View File

@@ -35,6 +35,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.util.Assert;
/**
* Factory to create {@link org.springframework.data.ldap.repository.LdapRepository} instances.
@@ -45,12 +46,25 @@ import org.springframework.ldap.core.LdapOperations;
*/
public class LdapRepositoryFactory extends RepositoryFactorySupport {
private final LdapQueryLookupStrategy queryLookupStrategy;
private final LdapOperations ldapOperations;
/**
* Creates a new {@link LdapRepositoryFactory}.
*
* @param ldapOperations must not be {@literal null}.
*/
public LdapRepositoryFactory(LdapOperations ldapOperations) {
Assert.notNull(ldapOperations, "LdapOperations must not be null!");
this.queryLookupStrategy = new LdapQueryLookupStrategy(ldapOperations);
this.ldapOperations = ldapOperations;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class)
*/
@Override
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return null;
@@ -80,17 +94,32 @@ public class LdapRepositoryFactory extends RepositoryFactorySupport {
information.getDomainType());
}
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key)
*/
@Override
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
return new LdapQueryLookupStrategy();
return queryLookupStrategy;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider)
*/
@Override
protected QueryLookupStrategy getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) {
return new LdapQueryLookupStrategy();
return queryLookupStrategy;
}
private final class LdapQueryLookupStrategy implements QueryLookupStrategy {
private static final class LdapQueryLookupStrategy implements QueryLookupStrategy {
private LdapOperations ldapOperations;
/**
* @param ldapOperations
*/
public LdapQueryLookupStrategy(LdapOperations ldapOperations) {
this.ldapOperations = ldapOperations;
}
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,

View File

@@ -21,7 +21,6 @@ import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.data.ldap.repository.support.LdapRepositoryFactory;
import org.springframework.util.Assert;
/**
@@ -29,7 +28,6 @@ import org.springframework.util.Assert;
* {@link org.springframework.data.ldap.repository.LdapRepository} instances.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
public class LdapRepositoryFactoryBean<T extends Repository<S, Name>, S>
extends RepositoryFactoryBeanSupport<T, S, Name> {
@@ -40,14 +38,23 @@ public class LdapRepositoryFactoryBean<T extends Repository<S, Name>, S>
this.ldapOperations = ldapOperations;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#createRepositoryFactory()
*/
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
return new LdapRepositoryFactory(ldapOperations);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
Assert.notNull(ldapOperations, "LdapOperations must be set");
super.afterPropertiesSet();
}
}

View File

@@ -33,33 +33,47 @@ import com.querydsl.core.types.*;
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
*/
class LdapSerializer implements Visitor<Object, Void> {
private final ObjectDirectoryMapper odm;
private final Class<?> clazz;
private final Class<?> entityType;
public LdapSerializer(ObjectDirectoryMapper odm, Class<?> clazz) {
/**
* Creates a new {@link LdapSerializer}.
*
* @param odm
* @param entityType
*/
public LdapSerializer(ObjectDirectoryMapper odm, Class<?> entityType) {
this.odm = odm;
this.clazz = clazz;
this.entityType = entityType;
}
public Filter handle(Expression<?> expression) {
return (Filter) expression.accept(this, null);
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.Constant, java.lang.Object)
*/
@Override
public Object visit(Constant<?> expr, Void context) {
return expr.getConstant().toString();
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.FactoryExpression, java.lang.Object)
*/
@Override
public Object visit(FactoryExpression<?> expr, Void context) {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.Operation, java.lang.Object)
*/
@Override
public Object visit(Operation<?> expr, Void context) {
@@ -99,24 +113,36 @@ class LdapSerializer implements Visitor<Object, Void> {
}
private String attribute(Operation<?> expr) {
return odm.attributeFor(clazz, (String) expr.getArg(0).accept(this, null));
return odm.attributeFor(entityType, (String) expr.getArg(0).accept(this, null));
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.ParamExpression, java.lang.Object)
*/
@Override
public Object visit(ParamExpression<?> expr, Void context) {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.Path, java.lang.Object)
*/
@Override
public Object visit(Path<?> expr, Void context) {
return expr.getMetadata().getName();
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.SubQueryExpression, java.lang.Object)
*/
@Override
public Object visit(SubQueryExpression<?> expr, Void context) {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see com.querydsl.core.types.Visitor#visit(com.querydsl.core.types.TemplateExpression, java.lang.Object)
*/
@Override
public Object visit(TemplateExpression<?> expr, Void context) {
throw new UnsupportedOperationException();

View File

@@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.FilteredClause;
@@ -33,30 +34,45 @@ import com.querydsl.core.types.Predicate;
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
*/
public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>> {
private final LdapOperations ldapOperations;
private final Class<? extends K> clazz;
private final Class<? extends K> entityType;
private final LdapSerializer filterGenerator;
private QueryMixin<QueryDslLdapQuery<K>> queryMixin = new QueryMixin<QueryDslLdapQuery<K>>(this,
new DefaultQueryMetadata().noValidate());
private final LdapSerializer filterGenerator;
@SuppressWarnings("unchecked")
/**
* Creates a new {@link QueryDslLdapQuery}.
*
* @param ldapOperations must not be {@literal null}.
* @param entityPath must not be {@literal null}.
*/
public QueryDslLdapQuery(LdapOperations ldapOperations, EntityPath<K> entityPath) {
this(ldapOperations, (Class<K>) entityPath.getType());
this(ldapOperations, entityPath.getType());
}
public QueryDslLdapQuery(LdapOperations ldapOperations, Class<K> clazz) {
/**
* Creates a new {@link QueryDslLdapQuery}.
*
* @param ldapOperations must not be {@literal null}.
* @param entityType must not be {@literal null}.
*/
public QueryDslLdapQuery(LdapOperations ldapOperations, Class<? extends K> entityType) {
Assert.notNull(ldapOperations, "LdapOperations must not be null!");
Assert.notNull(entityType, "Type must not be null!");
this.ldapOperations = ldapOperations;
this.clazz = clazz;
this.filterGenerator = new LdapSerializer(ldapOperations.getObjectDirectoryMapper(), clazz);
this.entityType = entityType;
this.filterGenerator = new LdapSerializer(ldapOperations.getObjectDirectoryMapper(), this.entityType);
}
/* (non-Javadoc)
* @see com.querydsl.core.FilteredClause#where(com.querydsl.core.types.Predicate[])
*/
@Override
public QueryDslLdapQuery<K> where(Predicate... o) {
return queryMixin.where(o);
@@ -64,11 +80,11 @@ public class QueryDslLdapQuery<K> implements FilteredClause<QueryDslLdapQuery<K>
@SuppressWarnings("unchecked")
public List<K> list() {
return (List<K>) ldapOperations.find(buildQuery(), clazz);
return (List<K>) ldapOperations.find(buildQuery(), entityType);
}
public K uniqueResult() {
return ldapOperations.findOne(buildQuery(), clazz);
return ldapOperations.findOne(buildQuery(), entityType);
}
LdapQuery buildQuery() {

View File

@@ -23,8 +23,6 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
import org.springframework.data.ldap.repository.support.QueryDslLdapQuery;
import org.springframework.data.ldap.repository.support.SimpleLdapRepository;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
@@ -34,50 +32,80 @@ import com.querydsl.core.types.Predicate;
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
*/
public class QueryDslLdapRepository<T> extends SimpleLdapRepository<T> implements QueryDslPredicateExecutor<T> {
public QueryDslLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> clazz) {
super(ldapOperations, odm, clazz);
/**
* Creates a new {@link QueryDslLdapRepository}.
*
* @param ldapOperations must not be {@literal null}.
* @param odm must not be {@literal null}.
* @param entityType must not be {@literal null}.
*/
public QueryDslLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> entityType) {
super(ldapOperations, odm, entityType);
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findOne(com.querydsl.core.types.Predicate)
*/
@Override
public T findOne(Predicate predicate) {
return queryFor(predicate).uniqueResult();
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate)
*/
@Override
public List<T> findAll(Predicate predicate) {
return queryFor(predicate).list();
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.querydsl.core.types.Predicate)
*/
@Override
public long count(Predicate predicate) {
return findAll(predicate).size();
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#exists(com.querydsl.core.types.Predicate)
*/
public boolean exists(Predicate predicate) {
return count(predicate) > 0;
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Sort)
*/
public Iterable<T> findAll(Predicate predicate, Sort sort) {
throw new UnsupportedOperationException();
}
private QueryDslLdapQuery<T> queryFor(Predicate predicate) {
return new QueryDslLdapQuery<T>(getLdapOperations(), getClazz()).where(predicate);
return new QueryDslLdapQuery<T>(getLdapOperations(), getEntityType()).where(predicate);
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.OrderSpecifier[])
*/
public Iterable<T> findAll(OrderSpecifier<?>... orders) {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, com.querydsl.core.types.OrderSpecifier[])
*/
@Override
public Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Pageable)
*/
@Override
public Page<T> findAll(Predicate predicate, Pageable pageable) {
throw new UnsupportedOperationException();

View File

@@ -25,47 +25,62 @@ import javax.naming.Name;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Persistable;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler;
import org.springframework.ldap.filter.Filter;
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.util.Assert;
/**
* Base repository implementation for LDAP.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
* @author Mark Paluch
*/
public class SimpleLdapRepository<T> implements LdapRepository<T> {
private static final String OBJECTCLASS_ATTRIBUTE = "objectclass";
private final LdapOperations ldapOperations;
private final ObjectDirectoryMapper odm;
private final Class<T> clazz;
private final Class<T> entityType;
public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> clazz) {
/**
* Creates a new {@link SimpleLdapRepository}.
*
* @param ldapOperations must not be {@literal null}.
* @param odm must not be {@literal null}.
* @param entityType must not be {@literal null}.
*/
public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> entityType) {
Assert.notNull(ldapOperations, "LdapOperations must not be null!");
Assert.notNull(odm, "ObjectDirectoryMapper must not be null!");
Assert.notNull(entityType, "Entity type must not be null!");
this.ldapOperations = ldapOperations;
this.odm = odm;
this.clazz = clazz;
this.entityType = entityType;
}
protected LdapOperations getLdapOperations() {
return ldapOperations;
}
protected Class<T> getClazz() {
return clazz;
protected Class<T> getEntityType() {
return entityType;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#count()
*/
@Override
public long count() {
Filter filter = odm.filterFor(clazz, null);
Filter filter = odm.filterFor(entityType, null);
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
ldapOperations.search(query, callback);
@@ -83,6 +98,9 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Override
public <S extends T> S save(S entity) {
@@ -99,6 +117,9 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
return entity;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
*/
@Override
public <S extends T> Iterable<S> save(Iterable<S> entities) {
@@ -110,34 +131,49 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
});
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
*/
@Override
public T findOne(Name name) {
Assert.notNull(name, "Id must not be null");
try {
return ldapOperations.findByDn(name, clazz);
return ldapOperations.findByDn(name, entityType);
} catch (NameNotFoundException e) {
return null;
}
}
/* (non-Javadoc)
* @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
*/
@Override
public List<T> findAll(LdapQuery ldapQuery) {
Assert.notNull(ldapQuery, "LdapQuery must not be null");
return ldapOperations.find(ldapQuery, clazz);
return ldapOperations.find(ldapQuery, entityType);
}
/* (non-Javadoc)
* @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
*/
@Override
public T findOne(LdapQuery ldapQuery) {
Assert.notNull(ldapQuery, "LdapQuery must not be null");
try {
return ldapOperations.findOne(ldapQuery, clazz);
return ldapOperations.findOne(ldapQuery, entityType);
} catch (EmptyResultDataAccessException e) {
return null;
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
*/
@Override
public boolean exists(Name name) {
@@ -146,11 +182,17 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
return findOne(name) != null;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
public List<T> findAll() {
return ldapOperations.findAll(clazz);
return ldapOperations.findAll(entityType);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
*/
@Override
public List<T> findAll(final Iterable<Name> names) {
@@ -171,6 +213,9 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
return list;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
*/
@Override
public void delete(Name name) {
@@ -179,6 +224,9 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
ldapOperations.unbind(name);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
*/
@Override
public void delete(T entity) {
@@ -187,6 +235,9 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
ldapOperations.delete(entity);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
*/
@Override
public void delete(Iterable<? extends T> entities) {
@@ -195,6 +246,9 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
*/
@Override
public void deleteAll() {
delete(findAll());
@@ -235,8 +289,6 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
}
private interface Function<F, T> {
T transform(F entry);
}
}