DATACMNS-32 - Removed Querydsl support classes and use the ones from Spring Data Commons Core.

This commit is contained in:
Oliver Gierke
2011-04-20 20:37:06 +02:00
parent 9380a88f26
commit 399beff795
3 changed files with 5 additions and 129 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.document.mongodb.repository;
import static org.springframework.data.querydsl.QueryDslUtils.*;
import java.io.Serializable;
import java.lang.reflect.Method;
@@ -107,9 +109,6 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
*/
public static class MongoRepositoryFactory extends RepositoryFactorySupport {
private static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils.isPresent(
"com.mysema.query.types.Predicate", MongoRepositoryFactory.class.getClassLoader());
private final MongoTemplate template;
private final EntityInformationCreator entityInformationCreator;

View File

@@ -16,8 +16,6 @@
package org.springframework.data.document.mongodb.repository;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import org.apache.commons.collections15.Transformer;
@@ -29,9 +27,9 @@ 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.querydsl.EntityPathResolver;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.repository.support.EntityMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import com.mongodb.DBObject;
import com.mysema.query.mongodb.MongodbQuery;
@@ -288,126 +286,4 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends
return converter.read(getEntityInformation().getJavaType(), input);
}
}
/**
* Strategy interface to abstract the ways to translate an plain domain
* class into a {@link EntityPath}.
*
* @author Oliver Gierke
*/
public static interface EntityPathResolver {
<T> EntityPath<T> createPath(Class<T> domainClass);
}
/**
* Simple implementation of {@link EntityPathResolver} to lookup a query
* class by reflection and using the static field of the same type.
*
* @author Oliver Gierke
*/
static enum SimpleEntityPathResolver implements EntityPathResolver {
INSTANCE;
private static final String NO_CLASS_FOUND_TEMPLATE =
"Did not find a query class %s for domain class %s!";
private static final String NO_FIELD_FOUND_TEMPLATE =
"Did not find a static field of the same type in %s!";
/**
* Creates an {@link EntityPath} instance for the given domain class.
* Tries to lookup a class matching the naming convention (prepend Q to
* the simple name of the class, same package) and find a static field
* of the same type in it.
*
* @param domainClass
* @return
*/
@SuppressWarnings("unchecked")
public <T> EntityPath<T> createPath(Class<T> domainClass) {
String pathClassName = getQueryClassName(domainClass);
try {
Class<?> pathClass =
ClassUtils.forName(pathClassName,
QueryDslMongoRepository.class.getClassLoader());
Field field = getStaticFieldOfType(pathClass);
if (field == null) {
throw new IllegalStateException(String.format(
NO_FIELD_FOUND_TEMPLATE, pathClass));
} else {
return (EntityPath<T>) ReflectionUtils
.getField(field, null);
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format(
NO_CLASS_FOUND_TEMPLATE, pathClassName,
domainClass.getName()), e);
}
}
/**
* Returns the first static field of the given type inside the given
* type.
*
* @param type
* @return
*/
private Field getStaticFieldOfType(Class<?> type) {
for (Field field : type.getDeclaredFields()) {
boolean isStatic = Modifier.isStatic(field.getModifiers());
boolean hasSameType = type.equals(field.getType());
if (isStatic && hasSameType) {
return field;
}
}
Class<?> superclass = type.getSuperclass();
return Object.class.equals(superclass) ? null
: getStaticFieldOfType(superclass);
}
/**
* Returns the name of the query class for the given domain class.
*
* @param domainClass
* @return
*/
private String getQueryClassName(Class<?> domainClass) {
String simpleClassName = ClassUtils.getShortName(domainClass);
return String.format("%s.Q%s%s",
domainClass.getPackage().getName(),
getClassBase(simpleClassName), domainClass.getSimpleName());
}
/**
* Analyzes the short class name and potentially returns the outer
* class.
*
* @param shortName
* @return
*/
private String getClassBase(String shortName) {
String[] parts = shortName.split("\\.");
if (parts.length < 2) {
return "";
}
return parts[0] + "_";
}
}
}

View File

@@ -18,6 +18,7 @@ Import-Template:
org.springframework.data.mapping.model.*;version="[1.0.0, 2.0.0)",
org.springframework.data.domain.*;version="[1.0.0, 2.0.0)",
org.springframework.data.document.*;version="[1.0.0, 2.0.0)",
org.springframework.data.querydsl.*;version="[1.0.0, 2.0.0)",
org.springframework.data.repository.*;version="[1.0.0, 2.0.0)",
org.springframework.data.util.*;version="[1.0.0, 2.0.0)",
org.springframework.expression.*;version="[3.0.0, 4.0.0)",