DATACMNS-266 - Use new common Maven build infrastructure.
Simplified project setup to be a single module build again. Using Spring Data Build parent POM to simplify project setup. See https://github.com/SpringSource/spring-data-build#spring-data-build-infrastructure
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2011 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.querydsl;
|
||||
|
||||
import com.mysema.query.types.EntityPath;
|
||||
|
||||
/**
|
||||
* Strategy interface to abstract the ways to translate an plain domain class into a {@link EntityPath}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface EntityPathResolver {
|
||||
|
||||
<T> EntityPath<T> createPath(Class<T> domainClass);
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2011 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.querydsl;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
import com.mysema.query.types.Predicate;
|
||||
|
||||
/**
|
||||
* Interface to allow execution of QueryDsl {@link Predicate} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface QueryDslPredicateExecutor<T> {
|
||||
|
||||
/**
|
||||
* Returns a single entity matching the given {@link Predicate}.
|
||||
*
|
||||
* @param spec
|
||||
* @return
|
||||
*/
|
||||
T findOne(Predicate predicate);
|
||||
|
||||
/**
|
||||
* Returns all entities matching the given {@link Predicate}.
|
||||
*
|
||||
* @param spec
|
||||
* @return
|
||||
*/
|
||||
Iterable<T> findAll(Predicate predicate);
|
||||
|
||||
/**
|
||||
* Returns all entities matching the given {@link Predicate} applying the given {@link OrderSpecifier}s.
|
||||
*
|
||||
* @param predicate
|
||||
* @param orders
|
||||
* @return
|
||||
*/
|
||||
Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
|
||||
|
||||
/**
|
||||
* Returns a {@link Page} of entities matching the given {@link Predicate}.
|
||||
*
|
||||
* @param predicate
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
Page<T> findAll(Predicate predicate, Pageable pageable);
|
||||
|
||||
/**
|
||||
* Returns the number of instances that the given {@link Predicate} will return.
|
||||
*
|
||||
* @param predicate the {@link Predicate} to count instances for
|
||||
* @return the number of instances
|
||||
*/
|
||||
long count(Predicate predicate);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2011 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.querydsl;
|
||||
|
||||
/**
|
||||
* Utility class for Querydsl.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class QueryDslUtils {
|
||||
|
||||
public static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils.isPresent(
|
||||
"com.mysema.query.types.Predicate", QueryDslUtils.class.getClassLoader());
|
||||
|
||||
private QueryDslUtils() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2011 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.querydsl;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.mysema.query.types.EntityPath;
|
||||
|
||||
/**
|
||||
* Simple implementation of {@link EntityPathResolver} to lookup a query class by reflection and using the static field
|
||||
* of the same type.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public 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, domainClass.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] + "_";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Querydsl integration support classes.
|
||||
*
|
||||
* @see http://www.querydsl.com
|
||||
*/
|
||||
package org.springframework.data.querydsl;
|
||||
Reference in New Issue
Block a user