diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java index d5480829d..54d6f0606 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java @@ -15,86 +15,85 @@ */ package org.springframework.data.document.mongodb.repository; +import java.lang.reflect.Method; + import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.repository.query.QueryMethod; import org.springframework.data.repository.util.ClassUtils; import org.springframework.util.StringUtils; -import java.lang.reflect.Method; - /** + * * TODO - Extract methods for {@link #getAnnotatedQuery()} into superclass as it is currently copied from Spring Data * JPA - * * @author Oliver Gierke */ class MongoQueryMethod extends QueryMethod { - - private final Method method; - private final MongoEntityInformation entityInformation; - - /** - * Creates a new {@link MongoQueryMethod} from the given {@link Method}. - * - * @param method - */ - @SuppressWarnings({"unchecked"}) - public MongoQueryMethod(Method method, Class domainClass) { - super(method); - this.method = method; - this.entityInformation = new MongoEntityInformation(ClassUtils.getReturnedDomainClass(method)); - } - - - /** - * Returns whether the method has an annotated query. - * - * @return - */ - boolean hasAnnotatedQuery() { - return getAnnotatedQuery() != null; - } - - /** - * Returns the query string declared in a {@link Query} annotation or {@literal null} if neither the annotation - * found nor the attribute was specified. - * - * @return - */ - String getAnnotatedQuery() { - - String query = (String) AnnotationUtils.getValue(getQueryAnnotation()); - return StringUtils.hasText(query) ? query : null; - } - - /** - * Returns the field specification to be used for the query. - * - * @return - */ - String getFieldSpecification() { - - String value = (String) AnnotationUtils.getValue(getQueryAnnotation(), "fields"); - return StringUtils.hasText(value) ? value : null; - } - - - /* (non-Javadoc) - * @see org.springframework.data.repository.query.QueryMethod#getEntityMetadata() - */ - @Override - public MongoEntityInformation getEntityInformation() { - - return entityInformation; - } - - /** - * Returns the {@link Query} annotation that is applied to the method or {@code null} if none available. - * - * @return - */ - private Query getQueryAnnotation() { - - return method.getAnnotation(Query.class); - } + + private final Method method; + private final MongoEntityInformation entityInformation; + + /** + * Creates a new {@link MongoQueryMethod} from the given {@link Method}. + * + * @param method + */ + public MongoQueryMethod(Method method, Class domainClass) { + super(method); + this.method = method; + this.entityInformation = new MongoEntityInformation(ClassUtils.getReturnedDomainClass(method)); + } + + + /** + * Returns whether the method has an annotated query. + * + * @return + */ + boolean hasAnnotatedQuery() { + return getAnnotatedQuery() != null; + } + + /** + * Returns the query string declared in a {@link Query} annotation or {@literal null} if neither the annotation + * found nor the attribute was specified. + * + * @return + */ + String getAnnotatedQuery() { + + String query = (String) AnnotationUtils.getValue(getQueryAnnotation()); + return StringUtils.hasText(query) ? query : null; + } + + /** + * Returns the field specification to be used for the query. + * + * @return + */ + String getFieldSpecification() { + + String value = (String) AnnotationUtils.getValue(getQueryAnnotation(), "fields"); + return StringUtils.hasText(value) ? value : null; + } + + + /* (non-Javadoc) + * @see org.springframework.data.repository.query.QueryMethod#getEntityMetadata() + */ + @Override + public MongoEntityInformation getEntityInformation() { + + return entityInformation; + } + + /** + * Returns the {@link Query} annotation that is applied to the method or {@code null} if none available. + * + * @return + */ + private Query getQueryAnnotation() { + + return method.getAnnotation(Query.class); + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java index 1a06728eb..813529021 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java @@ -15,6 +15,13 @@ */ package org.springframework.data.document.mongodb.repository; +import static org.springframework.data.document.mongodb.query.Criteria.*; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import org.bson.types.ObjectId; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.query.Criteria; @@ -26,199 +33,189 @@ import org.springframework.data.domain.Sort; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.util.Assert; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.springframework.data.document.mongodb.query.Criteria.where; - /** * Repository base implementation for Mongo. - * + * * @author Oliver Gierke */ public class SimpleMongoRepository implements PagingAndSortingRepository { - private final MongoTemplate template; - private final MongoEntityInformation entityInformation; - - /** - * Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}. - * - * @param metadata - * @param template - */ - public SimpleMongoRepository(MongoEntityInformation metadata, MongoTemplate template) { - - Assert.notNull(template); - Assert.notNull(metadata); - this.entityInformation = metadata; - this.template = template; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#save(java.lang.Object) - */ - public T save(T entity) { - - template.save(entityInformation.getCollectionName(), entity); - return entity; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#save(java.lang.Iterable) - */ - public List save(Iterable entities) { - - List result = new ArrayList(); - - for (T entity : entities) { - save(entity); - result.add(entity); - } - - return result; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#findById(java.io.Serializable ) - */ - public T findById(ID id) { - - return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); - } - - private Query getIdQuery(Object id) { - - return new Query(getIdCriteria(id)); - } - - private Criteria getIdCriteria(Object id) { - ObjectId objectId = template.getConverter().convertObjectId(id); - return where(entityInformation.getIdAttribute()).is(objectId); - } - - public T findOne(ID id) { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#exists(java.io.Serializable ) - */ - public boolean exists(ID id) { - - return findById(id) != null; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#count() - */ - public Long count() { - - return template.getCollection(entityInformation.getCollectionName()).count(); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#delete(java.lang.Object) - */ - public void delete(T entity) { - - template.remove(entityInformation.getCollectionName(), getIdQuery(entityInformation.getId(entity))); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#delete(java.lang.Iterable) - */ - public void delete(Iterable entities) { - - for (T entity : entities) { - delete(entity); - } - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#deleteAll() - */ - public void deleteAll() { - - template.dropCollection(entityInformation.getCollectionName()); - } - - /* (non-Javadoc) - * @see org.springframework.data.repository.Repository#findAll() - */ - public List findAll() { - return findAll(new Query()); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.PagingAndSortingRepository#findAll - * (org.springframework.data.domain.Pageable) - */ - public Page findAll(final Pageable pageable) { - - Long count = count(); - List list = findAll(QueryUtils.applyPagination(new Query(), pageable)); - - return new PageImpl(list, pageable, count); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.PagingAndSortingRepository#findAll - * (org.springframework.data.domain.Sort) - */ - public List findAll(final Sort sort) { - - return findAll(QueryUtils.applySorting(new Query(), sort)); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable) - */ - public List findAll(Iterable ids) { - - Query query = null; - - for (ID id : ids) { - if (query == null) { - query = getIdQuery(id); - } else { - query = new Query().or(getIdQuery(id)); - } - } - - return findAll(query); - } - - private List findAll(Query query) { - - if (query == null) { - return Collections.emptyList(); - } - - return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType()); - } + private final MongoTemplate template; + private final MongoEntityInformation entityInformation; + + /** + * Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}. + * + * @param metadata + * @param template + */ + public SimpleMongoRepository(MongoEntityInformation metadata, MongoTemplate template) { + + Assert.notNull(template); + Assert.notNull(metadata); + this.entityInformation = metadata; + this.template = template; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#save(java.lang.Object) + */ + public T save(T entity) { + + template.save(entityInformation.getCollectionName(), entity); + return entity; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#save(java.lang.Iterable) + */ + public List save(Iterable entities) { + + List result = new ArrayList(); + + for (T entity : entities) { + save(entity); + result.add(entity); + } + + return result; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#findById(java.io.Serializable ) + */ + public T findById(ID id) { + + return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); + } + + private Query getIdQuery(Object id) { + + return new Query(getIdCriteria(id)); + } + + private Criteria getIdCriteria(Object id) { + ObjectId objectId = template.getConverter().convertObjectId(id); + return where(entityInformation.getIdAttribute()).is(objectId); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#exists(java.io.Serializable ) + */ + public boolean exists(ID id) { + + return findById(id) != null; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#count() + */ + public Long count() { + + return template.getCollection(entityInformation.getCollectionName()).count(); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#delete(java.lang.Object) + */ + public void delete(T entity) { + + template.remove(entityInformation.getCollectionName(), getIdQuery(entityInformation.getId(entity))); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#delete(java.lang.Iterable) + */ + public void delete(Iterable entities) { + + for (T entity : entities) { + delete(entity); + } + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#deleteAll() + */ + public void deleteAll() { + + template.dropCollection(entityInformation.getCollectionName()); + } + + /* (non-Javadoc) + * @see org.springframework.data.repository.Repository#findAll() + */ + public List findAll() { + return findAll(new Query()); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.PagingAndSortingRepository#findAll + * (org.springframework.data.domain.Pageable) + */ + public Page findAll(final Pageable pageable) { + + Long count = count(); + List list = findAll(QueryUtils.applyPagination(new Query(), pageable)); + + return new PageImpl(list, pageable, count); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.PagingAndSortingRepository#findAll + * (org.springframework.data.domain.Sort) + */ + public List findAll(final Sort sort) { + + return findAll(QueryUtils.applySorting(new Query(), sort)); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable) + */ + public List findAll(Iterable ids) { + + Query query = null; + + for (ID id : ids) { + if (query == null) { + query = getIdQuery(id); + } else { + query = new Query().or(getIdQuery(id)); + } + } + + return findAll(query); + } + + private List findAll(Query query) { + + if (query == null) { + return Collections.emptyList(); + } + + return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType()); + } }