Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2014 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.query;
 
 import java.util.List;
 
 import  org.springframework.core.convert.ConversionService;
 import  org.springframework.core.convert.support.DefaultConversionService;
 import  org.springframework.util.Assert;

Base class for RepositoryQuery implementations for Mongo.

Author(s):
Oliver Gierke
Thomas Darimont
 
 public abstract class AbstractMongoQuery implements RepositoryQuery {
 
 	private static final ConversionService CONVERSION_SERVICE = new DefaultConversionService();
 
 	private final MongoQueryMethod method;
 	private final MongoOperations operations;

Creates a new AbstractMongoQuery from the given MongoQueryMethod and MongoOperations.

Parameters:
method must not be null.
operations must not be null.
 
 	public AbstractMongoQuery(MongoQueryMethod methodMongoOperations operations) {
 
 		Assert.notNull(operations);
 		Assert.notNull(method);
 
 		this. = method;
 		this. = operations;
 	}
 
 	/* 
 	 * (non-Javadoc)
 	 * @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod()
 	 */
 		return ;
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[])
 	 */
 	public Object execute(Object[] parameters) {
 
 
 
 			MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(parameters);
 			Query countQuery = createCountQuery(new ConvertingParameterAccessor(.getConverter(), countAccessor));
 
 			return new GeoNearExecution(accessor).execute(querycountQuery);
 		} else if (.isGeoNearQuery()) {
 			return new GeoNearExecution(accessor).execute(query);
 		} else if (.isCollectionQuery()) {
 			return new CollectionExecution(accessor.getPageable()).execute(query);
 		} else if (.isPageQuery()) {
 			return new PagedExecution(accessor.getPageable()).execute(query);
 		}
 
 		Object result = new SingleEntityExecution(isCountQuery()).execute(query);
 
 		if (result == null) {
 			return result;
		}
		Class<?> expectedReturnType = .getReturnType().getType();
		if (expectedReturnType.isAssignableFrom(result.getClass())) {
			return result;
		}
		return .convert(resultexpectedReturnType);
	}

Creates a Query instance using the given ConvertingParameterAccessor. Will delegate to createQuery(ConvertingParameterAccessor) by default but allows customization of the count query to be triggered.

Parameters:
accessor must not be null.
Returns:
		return createQuery(accessor);
	}

Creates a Query instance using the given ParameterAccessor

Parameters:
accessor must not be null.
Returns:
	protected abstract Query createQuery(ConvertingParameterAccessor accessor);

Returns whether the query should get a count projection applied.

Returns:
	protected abstract boolean isCountQuery();
	private abstract class Execution {
		abstract Object execute(Query query);
		protected List<?> readCollection(Query query) {
			String collectionName = metadata.getCollectionName();
			return .find(querymetadata.getJavaType(), collectionName);
		}
	}

Execution for collection returning queries.

Author(s):
Oliver Gierke
	final class CollectionExecution extends Execution {
		private final Pageable pageable;
			this. = pageable;
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.query.Query)
		 */
		public Object execute(Query query) {
			return readCollection(query.with());
		}
	}

Execution for pagination queries.

Author(s):
Oliver Gierke
	final class PagedExecution extends Execution {
		private final Pageable pageable;

Creates a new PagedExecution.

Parameters:
pageable
		public PagedExecution(Pageable pageable) {
			Assert.notNull(pageable);
			this. = pageable;
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.repository.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.query.Query)
		 */
		@SuppressWarnings({ "rawtypes""unchecked" })
		Object execute(Query query) {
			long count = .count(querymetadata.getCollectionName());
			List<?> result = .find(query.with(), metadata.getJavaType(), metadata.getCollectionName());
			return new PageImpl(resultcount);
		}
	}

Execution to return a single entity.

Author(s):
Oliver Gierke
	final class SingleEntityExecution extends Execution {
		private final boolean countProjection;
		private SingleEntityExecution(boolean countProjection) {
			this. = countProjection;
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.repository.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.core.query.Query)
		 */
		Object execute(Query query) {
			return  ? .count(querymetadata.getJavaType()) : .findOne(query,
					metadata.getJavaType());
		}
	}

Execution to execute geo-near queries.

Author(s):
Oliver Gierke
	final class GeoNearExecution extends Execution {
			this. = accessor;
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.repository.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.query.Query)
		 */
		Object execute(Query query) {
			GeoResults<?> results = doExecuteQuery(query);
			return isListOfGeoResult() ? results.getContent() : results;
		}

Executes the given Query to return a page.

Parameters:
query must not be null.
countQuery must not be null.
Returns:
		Object execute(Query queryQuery countQuery) {
			long count = .count(countQuerymetadata.getCollectionName());
			return new GeoPage<Object>(doExecuteQuery(query), .getPageable(), count);
		}
		@SuppressWarnings("unchecked")
		private GeoResults<ObjectdoExecuteQuery(Query query) {
			Point nearLocation = .getGeoNearLocation();
			NearQuery nearQuery = NearQuery.near(nearLocation);
			if (query != null) {
				nearQuery.query(query);
			}
			Distance maxDistance = .getMaxDistance();
			if (maxDistance != null) {
				nearQuery.maxDistance(maxDistance).in(maxDistance.getMetric());
			}
			Pageable pageable = .getPageable();
			if (pageable != null) {
				nearQuery.with(pageable);
			}
			return (GeoResults<Object>) .geoNear(nearQuerymetadata.getJavaType(), metadata.getCollectionName());
		}
		private boolean isListOfGeoResult() {
			TypeInformation<?> returnType = .getReturnType();
			if (!returnType.getType().equals(List.class)) {
				return false;
			}
			TypeInformation<?> componentType = returnType.getComponentType();
			return componentType == null ? false : GeoResult.class.equals(componentType.getType());
		}
	}
New to GrepCode? Check out our FAQ X