@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2011 - 2012 the original author or authors .
* Copyright 2011 - 2014 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -27,6 +27,7 @@ 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.repository.query.MongoEntityInformation ;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation ;
import org.springframework.data.querydsl.EntityPathResolver ;
import org.springframework.data.querydsl.EntityPathResolver ;
import org.springframework.data.querydsl.QSort ;
import org.springframework.data.querydsl.QueryDslPredicateExecutor ;
import org.springframework.data.querydsl.QueryDslPredicateExecutor ;
import org.springframework.data.querydsl.SimpleEntityPathResolver ;
import org.springframework.data.querydsl.SimpleEntityPathResolver ;
import org.springframework.data.repository.core.EntityInformation ;
import org.springframework.data.repository.core.EntityInformation ;
@ -44,6 +45,7 @@ import com.mysema.query.types.path.PathBuilder;
* Special QueryDsl based repository implementation that allows execution { @link Predicate } s in various forms .
* Special QueryDsl based repository implementation that allows execution { @link Predicate } s in various forms .
*
*
* @author Oliver Gierke
* @author Oliver Gierke
* @author Thomas Darimont
* /
* /
public class QueryDslMongoRepository < T , ID extends Serializable > extends SimpleMongoRepository < T , ID > implements
public class QueryDslMongoRepository < T , ID extends Serializable > extends SimpleMongoRepository < T , ID > implements
QueryDslPredicateExecutor < T > {
QueryDslPredicateExecutor < T > {
@ -105,7 +107,6 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @see org . springframework . data . querydsl . QueryDslPredicateExecutor # findAll ( com . mysema . query . types . Predicate , com . mysema . query . types . OrderSpecifier < ? > [ ] )
* @see org . springframework . data . querydsl . QueryDslPredicateExecutor # findAll ( com . mysema . query . types . Predicate , com . mysema . query . types . OrderSpecifier < ? > [ ] )
* /
* /
public List < T > findAll ( Predicate predicate , OrderSpecifier < ? > . . . orders ) {
public List < T > findAll ( Predicate predicate , OrderSpecifier < ? > . . . orders ) {
return createQueryFor ( predicate ) . orderBy ( orders ) . list ( ) ;
return createQueryFor ( predicate ) . orderBy ( orders ) . list ( ) ;
}
}
@ -115,7 +116,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* /
* /
@Override
@Override
public Iterable < T > findAll ( OrderSpecifier < ? > . . . orders ) {
public Iterable < T > findAll ( OrderSpecifier < ? > . . . orders ) {
return createQueryFor ( new Predicate [ 0 ] ) . orderBy ( orders ) . list ( ) ;
return createQuery ( ) . orderBy ( orders ) . list ( ) ;
}
}
/ *
/ *
@ -130,6 +131,28 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
return new PageImpl < T > ( applyPagination ( query , pageable ) . list ( ) , pageable , countQuery . count ( ) ) ;
return new PageImpl < T > ( applyPagination ( query , pageable ) . list ( ) , pageable , countQuery . count ( ) ) ;
}
}
/ *
* ( non - Javadoc )
* @see org . springframework . data . mongodb . repository . support . SimpleMongoRepository # findAll ( org . springframework . data . domain . Pageable )
* /
@Override
public Page < T > findAll ( Pageable pageable ) {
MongodbQuery < T > countQuery = createQuery ( ) ;
MongodbQuery < T > query = createQuery ( ) ;
return new PageImpl < T > ( applyPagination ( query , pageable ) . list ( ) , pageable , countQuery . count ( ) ) ;
}
/ *
* ( non - Javadoc )
* @see org . springframework . data . mongodb . repository . support . SimpleMongoRepository # findAll ( org . springframework . data . domain . Sort )
* /
@Override
public List < T > findAll ( Sort sort ) {
return applySorting ( createQuery ( ) , sort ) . list ( ) ;
}
/ *
/ *
* ( non - Javadoc )
* ( non - Javadoc )
* @see org . springframework . data . querydsl . QueryDslPredicateExecutor # count ( com . mysema . query . types . Predicate )
* @see org . springframework . data . querydsl . QueryDslPredicateExecutor # count ( com . mysema . query . types . Predicate )
@ -144,12 +167,17 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @param predicate
* @param predicate
* @return
* @return
* /
* /
private MongodbQuery < T > createQueryFor ( Predicate . . . predicate ) {
private MongodbQuery < T > createQueryFor ( Predicate predicate ) {
return createQuery ( ) . where ( predicate ) ;
Class < T > domainType = entityInformation . getJavaType ( ) ;
}
MongodbQuery < T > query = new SpringDataMongodbQuery < T > ( mongoOperations , domainType ) ;
/ * *
return query . where ( predicate ) ;
* Creates a { @link MongodbQuery } .
*
* @return
* /
private MongodbQuery < T > createQuery ( ) {
return new SpringDataMongodbQuery < T > ( mongoOperations , entityInformation . getJavaType ( ) ) ;
}
}
/ * *
/ * *
@ -182,6 +210,15 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
return query ;
return query ;
}
}
// TODO: find better solution than instanceof check
if ( sort instanceof QSort ) {
List < OrderSpecifier < ? > > orderSpecifiers = ( ( QSort ) sort ) . getOrderSpecifiers ( ) ;
query . orderBy ( orderSpecifiers . toArray ( new OrderSpecifier < ? > [ orderSpecifiers . size ( ) ] ) ) ;
return query ;
}
for ( Order order : sort ) {
for ( Order order : sort ) {
query . orderBy ( toOrder ( order ) ) ;
query . orderBy ( toOrder ( order ) ) ;
}
}