Skip to content

Commit

Permalink
DATALDAP-7 - Polishing.
Browse files Browse the repository at this point in the history
Reformat code with Spring Data formatter. Add JavaDoc to overrides and constructors. Add missing assertions to public entry points. Remove since tags from JavaDoc.
  • Loading branch information
mp911de committed Dec 6, 2016
1 parent d594183 commit 5096ae3
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 109 deletions.
Expand Up @@ -24,7 +24,6 @@
* Ldap specific extensions to CrudRepository.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
public interface LdapRepository<T> extends CrudRepository<T, Name> {

Expand Down
Expand Up @@ -28,16 +28,15 @@
* automatic query methods based on statically defined queries.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
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()}
*/
Expand Down
Expand Up @@ -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<? extends Annotation> getAnnotation() {
return EnableLdapRepositories.class;
}

/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension()
*/
@Override
protected RepositoryConfigurationExtension getExtension() {
return new LdapRepositoryConfigurationExtension();
Expand Down
Expand Up @@ -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;

Expand All @@ -36,7 +36,6 @@
*
* @author Mattias Hellborg Arthursson
* @author Mark Paluch
* @since 2.0
*/
public class LdapRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {

Expand Down Expand Up @@ -86,18 +85,25 @@ protected Collection<Class<?>> getIdentifyingTypes() {
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";
}

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) {

Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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 {

Expand All @@ -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) {

Expand Down
Expand Up @@ -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<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);
Expand Down Expand Up @@ -95,9 +108,10 @@ private ContainerCriteria appendCondition(Part part, Iterator<Object> 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) {
Expand All @@ -106,21 +120,30 @@ 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<Object> iterator) {
ConditionCriteria criteria = base.and(getAttribute(part));

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;
Expand Down
Expand Up @@ -18,17 +18,16 @@
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 {

Expand All @@ -50,8 +49,8 @@ public LdapQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFac
/**
* 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;
Expand Down

0 comments on commit 5096ae3

Please sign in to comment.