Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2012 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.support;
 
 import java.util.List;
 
 import  org.springframework.util.Assert;
 
Special QueryDsl based repository implementation that allows execution Predicates in various forms.

Author(s):
Oliver Gierke
 
 public class QueryDslMongoRepository<T, ID extends Serializableextends SimpleMongoRepository<T, ID> implements
 
 	private final PathBuilder<T> builder;

Creates a new QueryDslMongoRepository for the given EntityMetadata and MongoTemplate. Uses the SimpleEntityPathResolver to create an EntityPath for the given domain class.

Parameters:
entityInformation
template
 
 	public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformationMongoOperations mongoOperations) {
 		this(entityInformationmongoOperations.);
 	}

Creates a new QueryDslMongoRepository for the given MongoEntityInformation, MongoTemplate and EntityPathResolver.

Parameters:
entityInformation
mongoOperations
resolver
 
 	public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformationMongoOperations mongoOperations,
 			EntityPathResolver resolver) {
 
 		super(entityInformationmongoOperations);
 		Assert.notNull(resolver);
 		EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
 		this. = new PathBuilder<T>(path.getType(), path.getMetadata());
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findOne(com.mysema.query.types.Predicate)
 	 */
 	public T findOne(Predicate predicate) {
 		return createQueryFor(predicate).uniqueResult();
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate)
 	 */
 	public List<T> findAll(Predicate predicate) {
 		return createQueryFor(predicate).list();
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier<?>[])
 	 */
	public List<T> findAll(Predicate predicateOrderSpecifier<?>... orders) {
		return createQueryFor(predicate).orderBy(orders).list();
	}
	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Pageable)
	 */
	public Page<T> findAll(Predicate predicatePageable pageable) {
		MongodbQuery<T> countQuery = createQueryFor(predicate);
		MongodbQuery<T> query = createQueryFor(predicate);
		return new PageImpl<T>(applyPagination(querypageable).list(), pageablecountQuery.count());
	}
	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.mysema.query.types.Predicate)
	 */
	public long count(Predicate predicate) {
		return createQueryFor(predicate).count();
	}

Creates a MongodbQuery for the given Predicate.

Parameters:
predicate
Returns:
	private MongodbQuery<T> createQueryFor(Predicate predicate) {
		Class<T> domainType = getEntityInformation().getJavaType();
		MongodbQuery<T> query = new SpringDataMongodbQuery<T>(getMongoOperations(), domainType);
		return query.where(predicate);
	}

Applies the given Pageable to the given MongodbQuery.

Parameters:
query
pageable
Returns:
	private MongodbQuery<T> applyPagination(MongodbQuery<T> queryPageable pageable) {
		if (pageable == null) {
			return query;
		}
		query = query.offset(pageable.getOffset()).limit(pageable.getPageSize());
		return applySorting(querypageable.getSort());
	}

Applies the given Sort to the given MongodbQuery.

Parameters:
query
sort
Returns:
	private MongodbQuery<T> applySorting(MongodbQuery<T> querySort sort) {
		if (sort == null) {
			return query;
		}
		for (Order order : sort) {
			query.orderBy(toOrder(order));
		}
		return query;
	}

Transforms a plain Order into a QueryDsl specific OrderSpecifier.

Parameters:
order
Returns:
	@SuppressWarnings({ "rawtypes""unchecked" })
	private OrderSpecifier<?> toOrder(Order order) {
		Expression<Objectproperty = .get(order.getProperty());
.....property);
	}
New to GrepCode? Check out our FAQ X