diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QueryDslMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QueryDslMongoRepository.java index 1fcfefdcd..702548ac6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QueryDslMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QueryDslMongoRepository.java @@ -24,14 +24,14 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; -import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.querydsl.EntityPathResolver; import org.springframework.data.querydsl.QueryDslPredicateExecutor; import org.springframework.data.querydsl.SimpleEntityPathResolver; import org.springframework.data.repository.core.EntityMetadata; +import org.springframework.util.Assert; +import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mysema.query.mongodb.MongodbQuery; import com.mysema.query.mongodb.MongodbSerializer; @@ -42,16 +42,13 @@ import com.mysema.query.types.Predicate; import com.mysema.query.types.path.PathBuilder; /** - * Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms. TODO: - * Extract {@link EntityPathResolver} into Spring Data Commons TODO: Refactor Spring Data JPA to use this common - * infrastructure + * Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms. * * @author Oliver Gierke */ public class QueryDslMongoRepository extends SimpleMongoRepository implements QueryDslPredicateExecutor { - private final MongoConverterTransformer transformer; private final MongodbSerializer serializer; private final PathBuilder builder; @@ -79,11 +76,10 @@ public class QueryDslMongoRepository extends SimpleM EntityPathResolver resolver) { super(entityInformation, template); - this.transformer = new MongoConverterTransformer(template.getConverter()); - this.serializer = new MongodbSerializer(); - + Assert.notNull(resolver); EntityPath path = resolver.createPath(entityInformation.getJavaType()); this.builder = new PathBuilder(path.getType(), path.getMetadata()); + this.serializer = new MongodbSerializer(); } /* @@ -158,8 +154,14 @@ public class QueryDslMongoRepository extends SimpleM * @return */ private MongodbQuery createQueryFor(Predicate predicate) { - - MongodbQuery query = new MongoTemplateQuery(getMongoOperations()); + + DBCollection collection = getMongoOperations().getCollection(getEntityInformation().getCollectionName()); + MongodbQuery query = new MongodbQuery(collection, new Transformer() { + public T transform(DBObject input) { + Class type = getEntityInformation().getJavaType(); + return getMongoOperations().getConverter().read(type, input); + } + }, serializer); return query.where(predicate); } @@ -214,48 +216,4 @@ public class QueryDslMongoRepository extends SimpleM return new OrderSpecifier(order.isAscending() ? com.mysema.query.types.Order.ASC : com.mysema.query.types.Order.DESC, property); } - - /** - * Special {@link MongodbQuery} implementation to use our {@link MongoOperations} for actually accessing Mongo. - * - * @author Oliver Gierke - */ - private class MongoTemplateQuery extends MongodbQuery { - - public MongoTemplateQuery(MongoOperations operations) { - super(operations.getCollection(getEntityInformation().getCollectionName()), transformer, serializer); - } - } - - /** - * {@link Transformer} implementation to delegate to a {@link MongoConverter}. - * - * @author Oliver Gierke - */ - private class MongoConverterTransformer implements Transformer { - - private final MongoConverter converter; - - /** - * Creates a new {@link MongoConverterTransformer} with the given {@link MongoConverter}. - * - * @param converter - */ - public MongoConverterTransformer(MongoConverter converter) { - - this.converter = converter; - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.commons.collections15.Transformer#transform(java.lang. - * Object) - */ - public T transform(DBObject input) { - - return converter.read(getEntityInformation().getJavaType(), input); - } - } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QuerydslRepositorySupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QuerydslRepositorySupport.java new file mode 100644 index 000000000..7b6f93ce6 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/QuerydslRepositorySupport.java @@ -0,0 +1,85 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository; + +import org.apache.commons.collections15.Transformer; +import org.springframework.data.mapping.context.MappingContext; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; +import org.springframework.util.Assert; + +import com.mongodb.DBCollection; +import com.mongodb.DBObject; +import com.mysema.query.mongodb.MongodbQuery; +import com.mysema.query.mongodb.MongodbSerializer; +import com.mysema.query.types.EntityPath; + +/** + * Base class to create repository implementations based on Querydsl. + * + * @author Oliver Gierke + */ +public abstract class QuerydslRepositorySupport { + + private final MongoOperations template; + private final MappingContext, ?> context; + private final MongodbSerializer serializer; + + /** + * Creates a new {@link QuerydslRepositorySupport} for the given {@link MongoOperations}. + * + * @param operations must not be {@literal null} + */ + public QuerydslRepositorySupport(MongoOperations operations) { + Assert.notNull(operations); + this.template = operations; + this.context = operations.getConverter().getMappingContext(); + this.serializer = new MongodbSerializer(); + } + + /** + * Returns a {@link MongodbQuery} for the given {@link EntityPath}. The collection being queried is derived from the + * entity metadata. + * + * @param path + * @return + */ + protected MongodbQuery from(final EntityPath path) { + Assert.notNull(path); + MongoPersistentEntity entity = context.getPersistentEntity(path.getType()); + return from(path, entity.getCollection()); + } + + /** + * Returns a {@link MongodbQuery} for the given {@link EntityPath} querying the given collection. + * + * @param path must not be {@literal null} + * @param collection must not be blank or {@literal null} + * @return + */ + protected MongodbQuery from(final EntityPath path, String collection) { + + Assert.notNull(path); + Assert.hasText(collection); + + DBCollection dbCollection = template.getCollection(collection); + return new MongodbQuery(dbCollection, new Transformer() { + public T transform(DBObject input) { + return template.getConverter().read(path.getType(), input); + } + }, serializer); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/QuerydslRepositorySupportUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/QuerydslRepositorySupportUnitTests.java new file mode 100644 index 000000000..e85fcb518 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/QuerydslRepositorySupportUnitTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.mysema.query.mongodb.MongodbQuery; + +/** + * Unit tests for {@link QuerydslRepositorySupport}. + * + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:infrastructure.xml") +public class QuerydslRepositorySupportUnitTests { + + @Autowired + MongoOperations operations; + Person person; + + @Before + public void setUp() { + operations.remove(new Query(), Person.class); + person = new Person("Dave", "Matthews"); + operations.save(person); + } + + @Test + public void providesMongoQuery() { + QPerson p = QPerson.person; + QuerydslRepositorySupport support = new QuerydslRepositorySupport(operations) {}; + MongodbQuery query = support.from(p).where(p.lastname.eq("Matthews")); + assertThat(query.uniqueResult(), is(person)); + } +}