Browse Source

DATAMONGO-313 [Refactoring] - Use MongoOperations interface instead of MongoTemplate class

pull/1/head
Mark Pollack 14 years ago
parent
commit
68a31d75f3
  1. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java
  2. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java
  3. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java
  4. 23
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java
  5. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java
  6. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

24
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

@ -15,14 +15,14 @@
*/ */
package org.springframework.data.mongodb.repository.query; package org.springframework.data.mongodb.repository.query;
import static org.springframework.data.mongodb.repository.query.QueryUtils.*; import static org.springframework.data.mongodb.repository.query.QueryUtils.applyPagination;
import java.util.List; import java.util.List;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.CollectionCallback; import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.geo.Distance; import org.springframework.data.mongodb.core.geo.Distance;
import org.springframework.data.mongodb.core.geo.GeoResult; import org.springframework.data.mongodb.core.geo.GeoResult;
import org.springframework.data.mongodb.core.geo.GeoResults; import org.springframework.data.mongodb.core.geo.GeoResults;
@ -46,21 +46,21 @@ import com.mongodb.DBObject;
public abstract class AbstractMongoQuery implements RepositoryQuery { public abstract class AbstractMongoQuery implements RepositoryQuery {
private final MongoQueryMethod method; private final MongoQueryMethod method;
private final MongoTemplate template; private final MongoOperations mongoOperations;
/** /**
* Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoTemplate}. * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}.
* *
* @param method * @param method
* @param template * @param template
*/ */
public AbstractMongoQuery(MongoQueryMethod method, MongoTemplate template) { public AbstractMongoQuery(MongoQueryMethod method, MongoOperations template) {
Assert.notNull(template); Assert.notNull(template);
Assert.notNull(method); Assert.notNull(method);
this.method = method; this.method = method;
this.template = template; this.mongoOperations = template;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -79,7 +79,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
public Object execute(Object[] parameters) { public Object execute(Object[] parameters) {
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters); MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters);
Query query = createQuery(new ConvertingParameterAccessor(template.getConverter(), accessor)); Query query = createQuery(new ConvertingParameterAccessor(mongoOperations.getConverter(), accessor));
if (method.isGeoNearQuery()) { if (method.isGeoNearQuery()) {
return new GeoNearExecution(accessor).execute(query); return new GeoNearExecution(accessor).execute(query);
@ -110,7 +110,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
MongoEntityInformation<?, ?> metadata = method.getEntityInformation(); MongoEntityInformation<?, ?> metadata = method.getEntityInformation();
String collectionName = metadata.getCollectionName(); String collectionName = metadata.getCollectionName();
return template.find(query, metadata.getJavaType(), collectionName); return mongoOperations.find(query, metadata.getJavaType(), collectionName);
} }
} }
@ -164,7 +164,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
MongoEntityInformation<?, ?> metadata = method.getEntityInformation(); MongoEntityInformation<?, ?> metadata = method.getEntityInformation();
int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count(); int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count();
List<?> result = template.find(applyPagination(query, pageable), metadata.getJavaType(), List<?> result = mongoOperations.find(applyPagination(query, pageable), metadata.getJavaType(),
metadata.getCollectionName()); metadata.getCollectionName());
return new PageImpl(result, pageable, count); return new PageImpl(result, pageable, count);
@ -172,7 +172,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
private DBCursor getCollectionCursor(String collectionName, final DBObject query) { private DBCursor getCollectionCursor(String collectionName, final DBObject query) {
return template.execute(collectionName, new CollectionCallback<DBCursor>() { return mongoOperations.execute(collectionName, new CollectionCallback<DBCursor>() {
public DBCursor doInCollection(DBCollection collection) { public DBCursor doInCollection(DBCollection collection) {
@ -197,7 +197,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
Object execute(Query query) { Object execute(Query query) {
MongoEntityInformation<?, ?> entityInformation = method.getEntityInformation(); MongoEntityInformation<?, ?> entityInformation = method.getEntityInformation();
return template.findOne(query, entityInformation.getJavaType()); return mongoOperations.findOne(query, entityInformation.getJavaType());
} }
} }
@ -234,7 +234,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
} }
MongoEntityInformation<?,?> entityInformation = method.getEntityInformation(); MongoEntityInformation<?,?> entityInformation = method.getEntityInformation();
GeoResults<?> results = template.geoNear(nearQuery, entityInformation.getJavaType(), entityInformation.getCollectionName()); GeoResults<?> results = mongoOperations.geoNear(nearQuery, entityInformation.getJavaType(), entityInformation.getCollectionName());
return isListOfGeoResult() ? results.getContent() : results; return isListOfGeoResult() ? results.getContent() : results;
} }

7
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java

@ -16,6 +16,7 @@
package org.springframework.data.mongodb.repository.query; package org.springframework.data.mongodb.repository.query;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -40,12 +41,12 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
* @param method * @param method
* @param template * @param template
*/ */
public PartTreeMongoQuery(MongoQueryMethod method, MongoTemplate template) { public PartTreeMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations) {
super(method, template); super(method, mongoOperations);
this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType());
this.isGeoNearQuery = method.isGeoNearQuery(); this.isGeoNearQuery = method.isGeoNearQuery();
this.context = template.getConverter().getMappingContext(); this.context = mongoOperations.getConverter().getMappingContext();
} }
/** /**

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java

@ -21,7 +21,7 @@ import java.util.regex.Pattern;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -44,14 +44,14 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
* @param method * @param method
* @param template * @param template
*/ */
public StringBasedMongoQuery(String query, MongoQueryMethod method, MongoTemplate template) { public StringBasedMongoQuery(String query, MongoQueryMethod method, MongoOperations mongoOperations) {
super(method, template); super(method, mongoOperations);
this.query = query; this.query = query;
this.fieldSpec = method.getFieldSpecification(); this.fieldSpec = method.getFieldSpecification();
} }
public StringBasedMongoQuery(MongoQueryMethod method, MongoTemplate template) { public StringBasedMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations) {
this(method.getAnnotatedQuery(), method, template); this(method.getAnnotatedQuery(), method, mongoOperations);
} }
/* /*

23
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

@ -15,12 +15,13 @@
*/ */
package org.springframework.data.mongodb.repository.support; package org.springframework.data.mongodb.repository.support;
import static org.springframework.data.querydsl.QueryDslUtils.*; import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes; import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
@ -46,7 +47,7 @@ import org.springframework.util.StringUtils;
*/ */
public class MongoRepositoryFactory extends RepositoryFactorySupport { public class MongoRepositoryFactory extends RepositoryFactorySupport {
private final MongoTemplate template; private final MongoOperations mongoOperations;
private final EntityInformationCreator entityInformationCreator; private final EntityInformationCreator entityInformationCreator;
/** /**
@ -55,11 +56,11 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
* @param template must not be {@literal null} * @param template must not be {@literal null}
* @param mappingContext * @param mappingContext
*/ */
public MongoRepositoryFactory(MongoTemplate template) { public MongoRepositoryFactory(MongoOperations mongoOperations) {
Assert.notNull(template); Assert.notNull(mongoOperations);
this.template = template; this.mongoOperations = mongoOperations;
this.entityInformationCreator = new DefaultEntityInformationCreator(template.getConverter().getMappingContext()); this.entityInformationCreator = new DefaultEntityInformationCreator(mongoOperations.getConverter().getMappingContext());
} }
/* /*
@ -84,9 +85,9 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainClass()); MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainClass());
if (isQueryDslRepository(repositoryInterface)) { if (isQueryDslRepository(repositoryInterface)) {
return new QueryDslMongoRepository(entityInformation, template); return new QueryDslMongoRepository(entityInformation, mongoOperations);
} else { } else {
return new SimpleMongoRepository(entityInformation, template); return new SimpleMongoRepository(entityInformation, mongoOperations);
} }
} }
@ -122,11 +123,11 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
if (namedQueries.hasQuery(namedQueryName)) { if (namedQueries.hasQuery(namedQueryName)) {
String namedQuery = namedQueries.getQuery(namedQueryName); String namedQuery = namedQueries.getQuery(namedQueryName);
return new StringBasedMongoQuery(namedQuery, queryMethod, template); return new StringBasedMongoQuery(namedQuery, queryMethod, mongoOperations);
} else if (queryMethod.hasAnnotatedQuery()) { } else if (queryMethod.hasAnnotatedQuery()) {
return new StringBasedMongoQuery(queryMethod, template); return new StringBasedMongoQuery(queryMethod, mongoOperations);
} else { } else {
return new PartTreeMongoQuery(queryMethod, template); return new PartTreeMongoQuery(queryMethod, mongoOperations);
} }
} }
} }

13
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java

@ -25,6 +25,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order; import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
@ -64,9 +65,9 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @param entityInformation * @param entityInformation
* @param template * @param template
*/ */
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoTemplate template) { public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations) {
this(entityInformation, template, SimpleEntityPathResolver.INSTANCE); this(entityInformation, mongoOperations, SimpleEntityPathResolver.INSTANCE);
} }
/** /**
@ -74,17 +75,17 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* and {@link EntityPathResolver}. * and {@link EntityPathResolver}.
* *
* @param entityInformation * @param entityInformation
* @param template * @param mongoOperations
* @param resolver * @param resolver
*/ */
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoTemplate template, public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
EntityPathResolver resolver) { EntityPathResolver resolver) {
super(entityInformation, template); super(entityInformation, mongoOperations);
Assert.notNull(resolver); Assert.notNull(resolver);
EntityPath<T> path = resolver.createPath(entityInformation.getJavaType()); EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
this.builder = new PathBuilder<T>(path.getType(), path.getMetadata()); this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
this.serializer = new SpringDataMongodbSerializer(template.getConverter().getMappingContext()); this.serializer = new SpringDataMongodbSerializer(mongoOperations.getConverter().getMappingContext());
} }
/* /*

24
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

@ -42,7 +42,7 @@ import org.springframework.util.Assert;
*/ */
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> { public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> {
private final MongoTemplate template; private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation; private final MongoEntityInformation<T, ID> entityInformation;
/** /**
@ -51,12 +51,12 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
* @param metadata * @param metadata
* @param template * @param template
*/ */
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoTemplate template) { public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
Assert.notNull(template); Assert.notNull(mongoOperations);
Assert.notNull(metadata); Assert.notNull(metadata);
this.entityInformation = metadata; this.entityInformation = metadata;
this.template = template; this.mongoOperations = mongoOperations;
} }
/* /*
@ -67,7 +67,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
*/ */
public T save(T entity) { public T save(T entity) {
template.save(entity, entityInformation.getCollectionName()); mongoOperations.save(entity, entityInformation.getCollectionName());
return entity; return entity;
} }
@ -98,7 +98,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
*/ */
public T findOne(ID id) { public T findOne(ID id) {
Assert.notNull(id, "The given id must not be null!"); Assert.notNull(id, "The given id must not be null!");
return template.findById(id, entityInformation.getJavaType()); return mongoOperations.findById(id, entityInformation.getJavaType());
} }
private Query getIdQuery(Object id) { private Query getIdQuery(Object id) {
@ -119,7 +119,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
public boolean exists(ID id) { public boolean exists(ID id) {
Assert.notNull(id, "The given id must not be null!"); Assert.notNull(id, "The given id must not be null!");
return template.findOne(new Query(Criteria.where("_id").is(id)), Object.class, return mongoOperations.findOne(new Query(Criteria.where("_id").is(id)), Object.class,
entityInformation.getCollectionName()) != null; entityInformation.getCollectionName()) != null;
} }
@ -130,7 +130,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
*/ */
public long count() { public long count() {
return template.getCollection(entityInformation.getCollectionName()).count(); return mongoOperations.getCollection(entityInformation.getCollectionName()).count();
} }
/* /*
@ -139,7 +139,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
*/ */
public void delete(ID id) { public void delete(ID id) {
Assert.notNull(id, "The given id must not be null!"); Assert.notNull(id, "The given id must not be null!");
template.remove(getIdQuery(id), entityInformation.getJavaType()); mongoOperations.remove(getIdQuery(id), entityInformation.getJavaType());
} }
/* /*
@ -173,7 +173,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
*/ */
public void deleteAll() { public void deleteAll() {
template.remove(new Query(), entityInformation.getCollectionName()); mongoOperations.remove(new Query(), entityInformation.getCollectionName());
} }
/* /*
@ -246,7 +246,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
return Collections.emptyList(); return Collections.emptyList();
} }
return template.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName()); return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
} }
/** /**
@ -256,7 +256,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
*/ */
protected MongoOperations getMongoOperations() { protected MongoOperations getMongoOperations() {
return this.template; return this.mongoOperations;
} }
/** /**

Loading…
Cancel
Save