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 e09b375..d23e9ef 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 @@ import org.springframework.ldap.query.LdapQuery; * 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 d26d3cb..91d72aa 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 @@ 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()} */ 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 3fbe761..47c3e8e 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 @@ 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 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 3c1f88e..0049352 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 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.> 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) { 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 22298c0..b9340eb 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 4a98154..eeb5b56 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 @@ 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) { 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 1eac3b8..8419d65 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.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 { +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 @@ public class LdapQueryCreator extends AbstractQueryCreator iterator) { ConditionCriteria criteria = base.and(getAttribute(part)); @@ -116,11 +133,17 @@ public class LdapQueryCreator extends AbstractQueryCreatortrue 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 d442a6e..9ff5ba0 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 dd325c7..2a03764 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 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 options, - Collection keywords, - Class entitiesAnn, - Class entityAnn, - Class superTypeAnn, - Class embeddableAnn, - Class embeddedAnn, - Class skipAnn) { + 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); - } + 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; + } } 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 0851397..4e66941 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 @@ 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()); 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 7d345bf..7dcdbc8 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; 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 EntityInformation getEntityInformation(Class 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, 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 5988342..a5b23d3 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.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, S> extends RepositoryFactoryBeanSupport { @@ -40,14 +38,23 @@ public class LdapRepositoryFactoryBean, 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(); + } } 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 9a49043..6ba6539 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 @@ import com.querydsl.core.types.*; * * @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 @@ class LdapSerializer implements Visitor { } 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 f22555c..1cc1831 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 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 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 class QueryDslLdapQuery implements FilteredClause @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 fd3a8b1..6b10543 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.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 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 152e19d..9b210b6 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 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 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 @@ public class SimpleLdapRepository implements LdapRepository { } } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object) + */ @Override public S save(S entity) { @@ -99,6 +117,9 @@ public class SimpleLdapRepository implements LdapRepository { 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 class SimpleLdapRepository implements LdapRepository { }); } + /* (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 class SimpleLdapRepository implements LdapRepository { 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 class SimpleLdapRepository implements LdapRepository { 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 implements LdapRepository { 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 implements LdapRepository { 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 class SimpleLdapRepository implements LdapRepository { } } + /* (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#deleteAll() + */ @Override public void deleteAll() { delete(findAll()); @@ -235,8 +289,6 @@ public class SimpleLdapRepository implements LdapRepository { } private interface Function { - T transform(F entry); - } }