diff --git a/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java b/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java index e09b3755..d23e9ef8 100644 --- a/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java +++ b/src/main/java/org/springframework/data/ldap/repository/LdapRepository.java @@ -24,7 +24,6 @@ * Ldap specific extensions to CrudRepository. * * @author Mattias Hellborg Arthursson - * @since 2.0 */ public interface LdapRepository extends CrudRepository { diff --git a/src/main/java/org/springframework/data/ldap/repository/Query.java b/src/main/java/org/springframework/data/ldap/repository/Query.java index d26d3cba..91d72aa0 100644 --- a/src/main/java/org/springframework/data/ldap/repository/Query.java +++ b/src/main/java/org/springframework/data/ldap/repository/Query.java @@ -28,7 +28,6 @@ * 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 @@ 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()} */ diff --git a/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrar.java b/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrar.java index 3fbe7616..47c3e8e1 100644 --- a/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrar.java +++ b/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrar.java @@ -24,15 +24,20 @@ * 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 getAnnotation() { return EnableLdapRepositories.class; } + /* (non-Javadoc) + * @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension() + */ @Override protected RepositoryConfigurationExtension getExtension() { return new LdapRepositoryConfigurationExtension(); diff --git a/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoryConfigurationExtension.java b/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoryConfigurationExtension.java index 3c1f88e9..00493524 100644 --- a/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoryConfigurationExtension.java +++ b/src/main/java/org/springframework/data/ldap/repository/config/LdapRepositoryConfigurationExtension.java @@ -22,12 +22,12 @@ 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 @@ * * @author Mattias Hellborg Arthursson * @author Mark Paluch - * @since 2.0 */ public class LdapRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport { @@ -86,11 +85,15 @@ protected Collection> getIdentifyingTypes() { return Collections.> 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 void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfiguratio 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) { diff --git a/src/main/java/org/springframework/data/ldap/repository/query/AbstractLdapRepositoryQuery.java b/src/main/java/org/springframework/data/ldap/repository/query/AbstractLdapRepositoryQuery.java index 22298c08..b9340ebc 100644 --- a/src/main/java/org/springframework/data/ldap/repository/query/AbstractLdapRepositoryQuery.java +++ b/src/main/java/org/springframework/data/ldap/repository/query/AbstractLdapRepositoryQuery.java @@ -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; diff --git a/src/main/java/org/springframework/data/ldap/repository/query/AnnotatedLdapRepositoryQuery.java b/src/main/java/org/springframework/data/ldap/repository/query/AnnotatedLdapRepositoryQuery.java index 4a981541..eeb5b56a 100644 --- a/src/main/java/org/springframework/data/ldap/repository/query/AnnotatedLdapRepositoryQuery.java +++ b/src/main/java/org/springframework/data/ldap/repository/query/AnnotatedLdapRepositoryQuery.java @@ -17,16 +17,15 @@ 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); - queryAnnotation = queryMethod.getQueryAnnotation(); + Assert.notNull(queryMethod.getQueryAnnotation(), "Annotation must be present"); + Assert.hasLength(queryMethod.getQueryAnnotation().value(), "Query filter must be specified"); - Assert.notNull(queryMethod, "Annotation must be present"); - Assert.hasLength(queryAnnotation.value(), "Query filter must be specified"); + queryAnnotation = queryMethod.getQueryAnnotation(); } + /* (non-Javadoc) + * @see org.springframework.data.ldap.repository.query.AbstractLdapRepositoryQuery#createQuery(java.lang.Object[]) + */ @Override protected LdapQuery createQuery(Object[] parameters) { diff --git a/src/main/java/org/springframework/data/ldap/repository/query/LdapQueryCreator.java b/src/main/java/org/springframework/data/ldap/repository/query/LdapQueryCreator.java index 1eac3b8a..8419d65d 100644 --- a/src/main/java/org/springframework/data/ldap/repository/query/LdapQueryCreator.java +++ b/src/main/java/org/springframework/data/ldap/repository/query/LdapQueryCreator.java @@ -31,34 +31,47 @@ 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 { +class LdapQueryCreator extends AbstractQueryCreator { - 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 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 @@ private ContainerCriteria appendCondition(Part part, Iterator iterator, 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 @@ private String getAttribute(Part part) { 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 iterator) { ConditionCriteria criteria = base.and(getAttribute(part)); @@ -116,11 +133,17 @@ protected ContainerCriteria and(Part part, ContainerCriteria base, Iteratortrue if the target method is annotated with {@link org.springframework.data.ldap.repository.Query}, - * false otherwise. + * @return true if the target method is annotated with + * {@link org.springframework.data.ldap.repository.Query}, false otherwise. */ public boolean hasQueryAnnotation() { return getQueryAnnotation() != null; diff --git a/src/main/java/org/springframework/data/ldap/repository/query/PartTreeLdapRepositoryQuery.java b/src/main/java/org/springframework/data/ldap/repository/query/PartTreeLdapRepositoryQuery.java index d442a6e9..9ff5ba0c 100644 --- a/src/main/java/org/springframework/data/ldap/repository/query/PartTreeLdapRepositoryQuery.java +++ b/src/main/java/org/springframework/data/ldap/repository/query/PartTreeLdapRepositoryQuery.java @@ -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(); } } diff --git a/src/main/java/org/springframework/data/ldap/repository/support/DefaultLdapAnnotationProcessorConfiguration.java b/src/main/java/org/springframework/data/ldap/repository/support/DefaultLdapAnnotationProcessorConfiguration.java index dd325c76..2a037646 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/DefaultLdapAnnotationProcessorConfiguration.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/DefaultLdapAnnotationProcessorConfiguration.java @@ -27,33 +27,34 @@ 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 options, - Collection keywords, - Class entitiesAnn, - Class entityAnn, - Class superTypeAnn, - Class embeddableAnn, - Class embeddedAnn, - Class 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; - } - - @Override - public boolean isValidField(VariableElement field) { - return super.isValidField(field) && field.getAnnotation(Id.class) == null; - } + public DefaultLdapAnnotationProcessorConfiguration(RoundEnvironment roundEnv, Map options, + Collection keywords, Class entitiesAnn, Class entityAnn, + Class superTypeAnn, Class embeddableAnn, + Class embeddedAnn, Class skipAnn) { + + super(roundEnv, options, keywords, entitiesAnn, entityAnn, superTypeAnn, embeddableAnn, embeddedAnn, skipAnn); + } + + /* (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; + } + + /* (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; + } } diff --git a/src/main/java/org/springframework/data/ldap/repository/support/LdapAnnotationProcessor.java b/src/main/java/org/springframework/data/ldap/repository/support/LdapAnnotationProcessor.java index 0851397c..4e669419 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/LdapAnnotationProcessor.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/LdapAnnotationProcessor.java @@ -36,12 +36,14 @@ * * @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()); diff --git a/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java b/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java index 7d345bf4..7dcdbc87 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java @@ -35,6 +35,7 @@ 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 @@ */ 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 EntityInformation getEntityInformation(Class domainClass) { return null; @@ -80,17 +94,32 @@ protected Object getTargetRepository(RepositoryInformation information) { 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, diff --git a/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactoryBean.java b/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactoryBean.java index 59883420..a5b23d38 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactoryBean.java @@ -21,7 +21,6 @@ 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 @@ * {@link org.springframework.data.ldap.repository.LdapRepository} instances. * * @author Mattias Hellborg Arthursson - * @since 2.0 */ public class LdapRepositoryFactoryBean, S> extends RepositoryFactoryBeanSupport { @@ -40,14 +38,23 @@ public void setLdapOperations(LdapOperations ldapOperations) { 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(); + } } diff --git a/src/main/java/org/springframework/data/ldap/repository/support/LdapSerializer.java b/src/main/java/org/springframework/data/ldap/repository/support/LdapSerializer.java index 9a490431..6ba6539b 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/LdapSerializer.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/LdapSerializer.java @@ -33,33 +33,47 @@ * * @author Mattias Hellborg Arthursson * @author Eddu Melendez - * @since 2.0 */ class LdapSerializer implements Visitor { 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 @@ private String value(Operation expr) { } 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(); diff --git a/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapQuery.java b/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapQuery.java index f22555ce..1cc18310 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapQuery.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapQuery.java @@ -21,6 +21,7 @@ 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 @@ * * @author Mattias Hellborg Arthursson * @author Eddu Melendez - * @since 2.0 */ public class QueryDslLdapQuery implements FilteredClause> { private final LdapOperations ldapOperations; - private final Class clazz; + private final Class entityType; + private final LdapSerializer filterGenerator; private QueryMixin> queryMixin = new QueryMixin>(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 entityPath) { - this(ldapOperations, (Class) entityPath.getType()); + this(ldapOperations, entityPath.getType()); } - public QueryDslLdapQuery(LdapOperations ldapOperations, Class 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 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 where(Predicate... o) { return queryMixin.where(o); @@ -64,11 +80,11 @@ public QueryDslLdapQuery where(Predicate... o) { @SuppressWarnings("unchecked") public List list() { - return (List) ldapOperations.find(buildQuery(), clazz); + return (List) ldapOperations.find(buildQuery(), entityType); } public K uniqueResult() { - return ldapOperations.findOne(buildQuery(), clazz); + return ldapOperations.findOne(buildQuery(), entityType); } LdapQuery buildQuery() { diff --git a/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java b/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java index fd3a8b1d..6b10543d 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/QueryDslLdapRepository.java @@ -23,8 +23,6 @@ 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 @@ * * @author Mattias Hellborg Arthursson * @author Eddu Melendez - * @since 2.0 */ public class QueryDslLdapRepository extends SimpleLdapRepository implements QueryDslPredicateExecutor { - public QueryDslLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class 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 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 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 findAll(Predicate predicate, Sort sort) { throw new UnsupportedOperationException(); } private QueryDslLdapQuery queryFor(Predicate predicate) { - return new QueryDslLdapQuery(getLdapOperations(), getClazz()).where(predicate); + return new QueryDslLdapQuery(getLdapOperations(), getEntityType()).where(predicate); } + /* (non-Javadoc) + * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.OrderSpecifier[]) + */ public Iterable 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 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 findAll(Predicate predicate, Pageable pageable) { throw new UnsupportedOperationException(); diff --git a/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java b/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java index 152e19dc..9b210b6c 100644 --- a/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java +++ b/src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java @@ -25,47 +25,62 @@ 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 implements LdapRepository { private static final String OBJECTCLASS_ATTRIBUTE = "objectclass"; + private final LdapOperations ldapOperations; private final ObjectDirectoryMapper odm; - private final Class clazz; + private final Class entityType; - public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class 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 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 getClazz() { - return clazz; + protected Class 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 @@ private boolean isNew(S entity, Name id) { } } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object) + */ @Override public S save(S entity) { @@ -99,6 +117,9 @@ public S save(S entity) { return entity; } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable) + */ @Override public Iterable save(Iterable entities) { @@ -110,34 +131,49 @@ public S transform(S entry) { }); } + /* (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 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 boolean exists(Name name) { return findOne(name) != null; } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#findAll() + */ @Override public List findAll() { - return ldapOperations.findAll(clazz); + return ldapOperations.findAll(entityType); } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable) + */ @Override public List findAll(final Iterable names) { @@ -171,6 +213,9 @@ public T transform(Name name) { 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 void delete(Name name) { 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 void delete(T entity) { ldapOperations.delete(entity); } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable) + */ @Override public void delete(Iterable entities) { @@ -195,6 +246,9 @@ public void delete(Iterable entities) { } } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#deleteAll() + */ @Override public void deleteAll() { delete(findAll()); @@ -235,8 +289,6 @@ public void remove() { } private interface Function { - T transform(F entry); - } }