|
|
|
|
@ -18,17 +18,15 @@ package org.springframework.data.mongodb.repository.query;
@@ -18,17 +18,15 @@ package org.springframework.data.mongodb.repository.query;
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import org.aopalliance.intercept.MethodInterceptor; |
|
|
|
|
import org.aopalliance.intercept.MethodInvocation; |
|
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
|
import org.bson.Document; |
|
|
|
|
import org.springframework.aop.framework.ProxyFactory; |
|
|
|
|
|
|
|
|
|
import org.springframework.data.mongodb.core.query.BasicQuery; |
|
|
|
|
import org.springframework.data.mongodb.core.query.Collation; |
|
|
|
|
import org.springframework.data.mongodb.core.query.Query; |
|
|
|
|
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; |
|
|
|
|
import org.springframework.expression.ExpressionParser; |
|
|
|
|
import org.springframework.lang.NonNull; |
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
|
|
|
|
|
|
@ -41,7 +39,7 @@ import org.springframework.util.ClassUtils;
@@ -41,7 +39,7 @@ import org.springframework.util.ClassUtils;
|
|
|
|
|
* @since 2.1 |
|
|
|
|
* @currentRead Assassin's Apprentice - Robin Hobb |
|
|
|
|
*/ |
|
|
|
|
public class QueryUtils { |
|
|
|
|
class QueryUtils { |
|
|
|
|
|
|
|
|
|
protected static final Log LOGGER = LogFactory.getLog(QueryUtils.class); |
|
|
|
|
|
|
|
|
|
@ -53,25 +51,19 @@ public class QueryUtils {
@@ -53,25 +51,19 @@ public class QueryUtils {
|
|
|
|
|
* @param defaultSort the default sort expression to apply to the query. |
|
|
|
|
* @return the query having the given {@code sort} applied. |
|
|
|
|
*/ |
|
|
|
|
public static Query decorateSort(Query query, Document defaultSort) { |
|
|
|
|
static Query decorateSort(Query query, Document defaultSort) { |
|
|
|
|
|
|
|
|
|
if (defaultSort.isEmpty()) { |
|
|
|
|
return query; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ProxyFactory factory = prepareQueryProxy(query.getClass(), defaultSort); |
|
|
|
|
factory.setTarget(query); |
|
|
|
|
return (Query) factory.getProxy(query.getClass().getClassLoader()); |
|
|
|
|
} |
|
|
|
|
BasicQuery defaultSortQuery = query instanceof BasicQuery bq ? bq : new BasicQuery(query); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Decorate {@link Query} and add a default sort expression to the given {@link Query}. Attributes of the given |
|
|
|
|
* {@code sort} may be overwritten by the sort explicitly defined by the {@link Query} itself. |
|
|
|
|
* |
|
|
|
|
* @param classLoader the {@link ClassLoader} to use for generating the proxy type with. |
|
|
|
|
*/ |
|
|
|
|
public static Class<?> queryProxyType(Class<? extends Query> baseType, ClassLoader classLoader) { |
|
|
|
|
return prepareQueryProxy(baseType, new Document()).getProxyClass(classLoader); |
|
|
|
|
Document combinedSort = new Document(defaultSort); |
|
|
|
|
combinedSort.putAll(defaultSortQuery.getSortObject()); |
|
|
|
|
defaultSortQuery.setSortObject(combinedSort); |
|
|
|
|
|
|
|
|
|
return defaultSortQuery; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -116,13 +108,13 @@ public class QueryUtils {
@@ -116,13 +108,13 @@ public class QueryUtils {
|
|
|
|
|
*/ |
|
|
|
|
static int indexOfAssignableParameter(Class<?> type, List<Class<?>> parameters) { |
|
|
|
|
|
|
|
|
|
if(parameters.isEmpty()) { |
|
|
|
|
if (parameters.isEmpty()) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int i = 0; |
|
|
|
|
for(Class<?> parameterType : parameters) { |
|
|
|
|
if(ClassUtils.isAssignable(type, parameterType)) { |
|
|
|
|
for (Class<?> parameterType : parameters) { |
|
|
|
|
if (ClassUtils.isAssignable(type, parameterType)) { |
|
|
|
|
return i; |
|
|
|
|
} |
|
|
|
|
i++; |
|
|
|
|
@ -130,34 +122,4 @@ public class QueryUtils {
@@ -130,34 +122,4 @@ public class QueryUtils {
|
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static ProxyFactory prepareQueryProxy(Class<? extends Query> query, Document defaultSort) { |
|
|
|
|
|
|
|
|
|
ProxyFactory factory = new ProxyFactory(); |
|
|
|
|
factory.setTargetClass(query); |
|
|
|
|
factory.addAdvice(new DefaultSortingInterceptor(defaultSort)); |
|
|
|
|
factory.setInterfaces(new Class[0]); |
|
|
|
|
return factory; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static class DefaultSortingInterceptor implements MethodInterceptor { |
|
|
|
|
|
|
|
|
|
private final Document defaultSort; |
|
|
|
|
|
|
|
|
|
public DefaultSortingInterceptor(Document defaultSort) { |
|
|
|
|
this.defaultSort = defaultSort; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
@Override |
|
|
|
|
public Object invoke(@NonNull MethodInvocation invocation) throws Throwable { |
|
|
|
|
|
|
|
|
|
if (!invocation.getMethod().getName().equals("getSortObject")) { |
|
|
|
|
return invocation.proceed(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Document combinedSort = new Document(defaultSort); |
|
|
|
|
combinedSort.putAll((Document) invocation.proceed()); |
|
|
|
|
return combinedSort; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|