Browse Source

DATADOC-18 - Let Repository subsystem use MongoOperations instead of MongoTemplate.

pull/1/head
Oliver Gierke 15 years ago
parent
commit
95ac7e638c
  1. 20
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java
  2. 18
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java
  3. 30
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java
  4. 2
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryConfigDefinitionParser.java
  5. 2
      spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml
  6. 91
      src/docbkx/index.xml

20
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java

@ -20,7 +20,7 @@ import static org.springframework.data.document.mongodb.repository.MongoCursorUt
import java.util.List; import java.util.List;
import org.springframework.data.document.mongodb.CollectionCallback; import org.springframework.data.document.mongodb.CollectionCallback;
import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.MongoOperations;
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.repository.query.QueryMethod; import org.springframework.data.repository.query.QueryMethod;
@ -42,24 +42,24 @@ import com.mongodb.DBObject;
public class MongoQuery implements RepositoryQuery { public class MongoQuery implements RepositoryQuery {
private final QueryMethod method; private final QueryMethod method;
private final MongoTemplate template; private final MongoOperations operations;
private final PartTree tree; private final PartTree tree;
/** /**
* Creates a new {@link MongoQuery} from the given {@link QueryMethod} and * Creates a new {@link MongoQuery} from the given {@link QueryMethod} and
* {@link MongoTemplate}. * {@link MongoOperations}.
* *
* @param method * @param method
* @param template * @param operations
*/ */
public MongoQuery(QueryMethod method, MongoTemplate template) { public MongoQuery(QueryMethod method, MongoOperations operations) {
Assert.notNull(template); Assert.notNull(operations);
Assert.notNull(method); Assert.notNull(method);
this.method = method; this.method = method;
this.template = template; this.operations = operations;
this.tree = new PartTree(method.getName(), method.getDomainClass()); this.tree = new PartTree(method.getName(), method.getDomainClass());
} }
@ -95,7 +95,7 @@ public class MongoQuery implements RepositoryQuery {
protected List<?> readCollection(DBObject query) { protected List<?> readCollection(DBObject query) {
return template.query(template.getDefaultCollectionName(), query, return operations.query(operations.getDefaultCollectionName(), query,
method.getDomainClass()); method.getDomainClass());
} }
} }
@ -147,7 +147,7 @@ public class MongoQuery implements RepositoryQuery {
int count = getCollectionCursor(creator.createQuery()).count(); int count = getCollectionCursor(creator.createQuery()).count();
List<?> result = List<?> result =
template.query(query, method.getDomainClass(), operations.query(query, method.getDomainClass(),
withPagination(pageable)); withPagination(pageable));
return new PageImpl(result, pageable, count); return new PageImpl(result, pageable, count);
@ -156,7 +156,7 @@ public class MongoQuery implements RepositoryQuery {
private DBCursor getCollectionCursor(final DBObject query) { private DBCursor getCollectionCursor(final DBObject query) {
return template.execute(new CollectionCallback<DBCursor>() { return operations.execute(new CollectionCallback<DBCursor>() {
public DBCursor doInCollection(DBCollection collection) { public DBCursor doInCollection(DBCollection collection) {

18
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java

@ -19,7 +19,7 @@ import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key; import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@ -39,17 +39,17 @@ import org.springframework.util.Assert;
public class MongoRepositoryFactoryBean extends public class MongoRepositoryFactoryBean extends
RepositoryFactoryBeanSupport<Repository<?, ?>> { RepositoryFactoryBeanSupport<Repository<?, ?>> {
private MongoTemplate template; private MongoOperations operations;
/** /**
* Configures the {@link MongoTemplate} to be used. * Configures the {@link MongoOperations} to be used.
* *
* @param template the template to set * @param operations the template to set
*/ */
public void setTemplate(MongoTemplate template) { public void setOperations(MongoOperations operations) {
this.template = template; this.operations = operations;
} }
@ -78,7 +78,7 @@ public class MongoRepositoryFactoryBean extends
public void afterPropertiesSet() { public void afterPropertiesSet() {
super.afterPropertiesSet(); super.afterPropertiesSet();
Assert.notNull(template, "MongoTemplate must not be null!"); Assert.notNull(operations, "MongoTemplate must not be null!");
} }
/** /**
@ -92,7 +92,7 @@ public class MongoRepositoryFactoryBean extends
protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository( protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository(
Class<T> domainClass) { Class<T> domainClass) {
return new SimpleMongoRepository<T, ID>(domainClass, template); return new SimpleMongoRepository<T, ID>(domainClass, operations);
} }
@ -119,7 +119,7 @@ public class MongoRepositoryFactoryBean extends
public RepositoryQuery resolveQuery(Method method) { public RepositoryQuery resolveQuery(Method method) {
return new MongoQuery(new QueryMethod(method), template); return new MongoQuery(new QueryMethod(method), operations);
} }
} }
} }

30
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java

@ -21,7 +21,7 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -43,20 +43,20 @@ import com.mongodb.QueryBuilder;
public class SimpleMongoRepository<T, ID extends Serializable> extends public class SimpleMongoRepository<T, ID extends Serializable> extends
RepositorySupport<T, ID> implements PagingAndSortingRepository<T, ID> { RepositorySupport<T, ID> implements PagingAndSortingRepository<T, ID> {
private final MongoTemplate template; private final MongoOperations operations;
private MongoEntityInformation entityInformation; private MongoEntityInformation entityInformation;
/** /**
* @param domainClass * @param domainClass
* @param template * @param operations
*/ */
public SimpleMongoRepository(Class<T> domainClass, MongoTemplate template) { public SimpleMongoRepository(Class<T> domainClass, MongoOperations operations) {
super(domainClass); super(domainClass);
Assert.notNull(template); Assert.notNull(operations);
this.template = template; this.operations = operations;
} }
@ -68,7 +68,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public T save(T entity) { public T save(T entity) {
template.save(entity); operations.save(entity);
return entity; return entity;
} }
@ -84,7 +84,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
List<T> result = new ArrayList<T>(); List<T> result = new ArrayList<T>();
for (T entity : entities) { for (T entity : entities) {
template.save(entity); operations.save(entity);
result.add(entity); result.add(entity);
} }
@ -102,7 +102,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
public T findById(ID id) { public T findById(ID id) {
List<T> result = List<T> result =
template.query(template.getDefaultCollectionName(), operations.query(operations.getDefaultCollectionName(),
QueryBuilder.start("_id").get(), getDomainClass()); QueryBuilder.start("_id").get(), getDomainClass());
return result.isEmpty() ? null : result.get(0); return result.isEmpty() ? null : result.get(0);
@ -129,7 +129,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public List<T> findAll() { public List<T> findAll() {
return template.getCollection(getDomainClass()); return operations.getCollection(getDomainClass());
} }
@ -140,7 +140,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public Long count() { public Long count() {
return template.getCollection(template.getDefaultCollectionName()) return operations.getCollection(operations.getDefaultCollectionName())
.count(); .count();
} }
@ -156,7 +156,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
QueryBuilder builder = QueryBuilder builder =
QueryBuilder.start(entityInformation.getFieldName()).is( QueryBuilder.start(entityInformation.getFieldName()).is(
entityInformation.getId(entity)); entityInformation.getId(entity));
template.remove(builder.get()); operations.remove(builder.get());
} }
@ -181,7 +181,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public void deleteAll() { public void deleteAll() {
template.dropCollection(template.getDefaultCollectionName()); operations.dropCollection(operations.getDefaultCollectionName());
} }
@ -197,7 +197,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
Long count = count(); Long count = count();
List<T> list = List<T> list =
template.query(new BasicDBObject(), getDomainClass(), operations.query(new BasicDBObject(), getDomainClass(),
withPagination(pageable)); withPagination(pageable));
return new PageImpl<T>(list, pageable, count); return new PageImpl<T>(list, pageable, count);
@ -213,7 +213,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public List<T> findAll(final Sort sort) { public List<T> findAll(final Sort sort) {
return template.query(new BasicDBObject(), getDomainClass(), return operations.query(new BasicDBObject(), getDomainClass(),
withSorting(sort)); withSorting(sort));
} }

2
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryConfigDefinitionParser.java

@ -46,6 +46,6 @@ public class MongoRepositoryConfigDefinitionParser
MongoRepositoryConfiguration context, MongoRepositoryConfiguration context,
BeanDefinitionBuilder builder, Object beanSource) { BeanDefinitionBuilder builder, Object beanSource) {
builder.addPropertyReference("template", context.getMongoTemplateRef()); builder.addPropertyReference("operations", context.getMongoTemplateRef());
} }
} }

2
spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml

@ -6,7 +6,7 @@
<import resource="infrastructure.xml" /> <import resource="infrastructure.xml" />
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean"> <bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="template" ref="mongoTemplate" /> <property name="operations" ref="mongoTemplate" />
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository" /> <property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository" />
</bean> </bean>

91
src/docbkx/index.xml

@ -1,43 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" <?xml version="1.0" encoding="UTF-8"?>
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<book xmlns:xi="http://www.w3.org/2001/XInclude"> <book xmlns:xi="http://www.w3.org/2001/XInclude">
<bookinfo>
<title>Spring Datastore Document - Reference Documentation</title>
<bookinfo> <releaseinfo>version;</releaseinfo>
<title>Spring Datastore Document - Reference Documentation</title>
<releaseinfo>&version;</releaseinfo> <authorgroup>
<author>
<authorgroup> <firstname>Mark</firstname>
<author>
<firstname>Mark</firstname> <surname>Pollack</surname>
<surname>Pollack</surname> </author>
</author>
<author> <author>
<firstname>Thomas</firstname> <firstname>Thomas</firstname>
<surname>Risberg</surname>
</author> <surname>Risberg</surname>
</authorgroup> </author>
<legalnotice> <author>
<para> <firstname>Oliver</firstname>
Copies of this document may be made for your own use and for distribution
to others, provided that you do not charge any fee for such copies and <surname>Gierke</surname>
further provided that each copy contains this Copyright Notice, whether </author>
distributed in print or electronically. </authorgroup>
</para>
</legalnotice> <legalnotice>
</bookinfo> <para>Copies of this document may be made for your own use and for
distribution to others, provided that you do not charge any fee for such
<toc/> copies and further provided that each copy contains this Copyright
Notice, whether distributed in print or electronically.</para>
<xi:include href="preface.xml"/> </legalnotice>
</bookinfo>
<part>
<title>Reference</title> <toc />
<partintro>
<para> <xi:include href="preface.xml" />
This part of the reference documentation details the ... <xi:include href="mongo.xml" />
</para>
</partintro> <part>
<!-- <xi:include href="amqp.xml"/> --> <title>Reference</title>
</part>
</book> <partintro>
<para>This part of the reference documentation details the ...</para>
</partintro>
<!-- <xi:include href="amqp.xml"/> -->
</part>
</book>
Loading…
Cancel
Save