From 4daab96bc4709b6cd06a7b4ee2a9eb114bb0a725 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 11 Mar 2011 16:32:21 +0100 Subject: [PATCH 1/3] DATAJPA-19 - Adapted generics and metadata changes made in core. --- .../mongodb/repository/MongoQueryMethod.java | 141 ++++--- .../repository/SimpleMongoRepository.java | 375 +++++++++--------- 2 files changed, 256 insertions(+), 260 deletions(-) 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()); + } } From 01a800b2cc723e31e68622603db784a8b133edc7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 11 Mar 2011 18:19:45 +0100 Subject: [PATCH 2/3] =?UTF-8?q?DATACMNS-20=20-=20Adapted=20refactoring=20o?= =?UTF-8?q?f=20Repository.findById(=E2=80=A6)=20to=20findOne(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../document/mongodb/repository/SimpleMongoRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 813529021..94e6e2008 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 @@ -90,7 +90,7 @@ public class SimpleMongoRepository implements Paging * * @see org.springframework.data.repository.Repository#findById(java.io.Serializable ) */ - public T findById(ID id) { + public T findOne(ID id) { return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); } @@ -113,7 +113,7 @@ public class SimpleMongoRepository implements Paging */ public boolean exists(ID id) { - return findById(id) != null; + return findOne(id) != null; } /* From 2947bfeaa60cc83f1a41fd615f2e0f2b7147a513 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 12 Mar 2011 08:36:42 +0100 Subject: [PATCH 3/3] Removed @Override annotations as they cannot be used at interfaces with Java 5. --- .../data/document/mongodb/repository/MongoRepository.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepository.java index 005e3d10b..67fd31489 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepository.java @@ -33,7 +33,6 @@ public interface MongoRepository extends PagingAndSo * * @see org.springframework.data.repository.Repository#findAll() */ - @Override List findAll(); /* @@ -41,6 +40,5 @@ public interface MongoRepository extends PagingAndSo * * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort) */ - @Override List findAll(Sort sort); }