SGF-507 - Handle case-insensitive OQL queries defined as Repository query methods.
This commit is contained in:
@@ -24,7 +24,7 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author John Blum
|
||||
*/
|
||||
@@ -58,7 +58,7 @@ public class GemfireMappingContext extends AbstractMappingContext<GemfirePersist
|
||||
@Override
|
||||
protected GemfirePersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
|
||||
GemfirePersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
return new GemfirePersistentProperty(field, descriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,21 +36,22 @@ public class Regions implements Iterable<Region<?, ?>> {
|
||||
|
||||
private final Map<String, Region<?, ?>> regions;
|
||||
|
||||
private final MappingContext<? extends GemfirePersistentEntity<?>, ?> context;
|
||||
private final MappingContext<? extends GemfirePersistentEntity<?>, ?> mappingContext;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Regions} wrapper for the given {@link Region}s and
|
||||
* {@link MappingContext}.
|
||||
*
|
||||
*
|
||||
* @param regions must not be {@literal null}.
|
||||
* @param context must not be {@literal null}.
|
||||
* @param mappingContext must not be {@literal null}.
|
||||
*/
|
||||
public Regions(Iterable<Region<?, ?>> regions, MappingContext<? extends GemfirePersistentEntity<?>, ?> context) {
|
||||
public Regions(Iterable<Region<?, ?>> regions,
|
||||
MappingContext<? extends GemfirePersistentEntity<?>, ?> mappingContext) {
|
||||
|
||||
Assert.notNull(regions);
|
||||
Assert.notNull(context);
|
||||
Assert.notNull(regions, "Regions must not be null");
|
||||
Assert.notNull(mappingContext, "MappingContext must not be null");
|
||||
|
||||
Map<String, com.gemstone.gemfire.cache.Region<?, ?>> regionMap = new HashMap<String, Region<?, ?>>();
|
||||
Map<String, Region<?, ?>> regionMap = new HashMap<String, Region<?, ?>>();
|
||||
|
||||
for (Region<?, ?> region : regions) {
|
||||
regionMap.put(region.getName(), region);
|
||||
@@ -58,7 +59,7 @@ public class Regions implements Iterable<Region<?, ?>> {
|
||||
}
|
||||
|
||||
this.regions = Collections.unmodifiableMap(regionMap);
|
||||
this.context = context;
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,15 +68,15 @@ public class Regions implements Iterable<Region<?, ?>> {
|
||||
* information is found.
|
||||
*
|
||||
* @param <T> the Region value class type.
|
||||
* @param type must not be {@literal null}.
|
||||
* @param entityType must not be {@literal null}.
|
||||
* @return the {@link Region} the given type is mapped to.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Region<?, T> getRegion(Class<T> type) {
|
||||
Assert.notNull(type);
|
||||
public <T> Region<?, T> getRegion(Class<T> entityType) {
|
||||
Assert.notNull(entityType, "entityType must not be null");
|
||||
|
||||
GemfirePersistentEntity<?> entity = context.getPersistentEntity(type);
|
||||
String regionName = (entity != null ? entity.getRegionName() : type.getSimpleName());
|
||||
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType);
|
||||
String regionName = (entity != null ? entity.getRegionName() : entityType.getSimpleName());
|
||||
|
||||
return (Region<?, T>) regions.get(regionName);
|
||||
}
|
||||
@@ -90,18 +91,18 @@ public class Regions implements Iterable<Region<?, ?>> {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S, T> Region<S, T> getRegion(String namePath) {
|
||||
Assert.notNull(namePath);
|
||||
Assert.hasText(namePath, "Region name/path is required");
|
||||
|
||||
return (Region<S, T>) regions.get(namePath);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
*
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
@Override
|
||||
public Iterator<Region<?, ?>> iterator() {
|
||||
return regions.values().iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,30 +27,32 @@ import org.springframework.data.repository.query.parser.PartTree;
|
||||
|
||||
/**
|
||||
* Query creator to create {@link QueryString} instances.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author John Blum
|
||||
*/
|
||||
class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates> {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(GemfireQueryCreator.class);
|
||||
|
||||
private final QueryBuilder query;
|
||||
private Iterator<Integer> indexes;
|
||||
|
||||
private final QueryBuilder queryBuilder;
|
||||
|
||||
/**
|
||||
* Creates a new {@link GemfireQueryCreator} using the given {@link PartTree} and domain class.
|
||||
*
|
||||
*
|
||||
* @param tree must not be {@literal null}.
|
||||
* @param entity must not be {@literal null}.
|
||||
*/
|
||||
public GemfireQueryCreator(PartTree tree, GemfirePersistentEntity<?> entity) {
|
||||
super(tree);
|
||||
|
||||
this.query = new QueryBuilder(entity, tree);
|
||||
this.queryBuilder = new QueryBuilder(entity, tree);
|
||||
this.indexes = new IndexProvider();
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#createQuery(org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@@ -69,7 +71,7 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
return Predicates.create(part, this.indexes);
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#and(org.springframework.data.repository.query.parser.Part, java.lang.Object, java.util.Iterator)
|
||||
*/
|
||||
@@ -78,7 +80,7 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
return base.and(Predicates.create(part, this.indexes));
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#or(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
@@ -87,21 +89,28 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
return base.or(criteria);
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#complete(java.lang.Object, org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@Override
|
||||
protected QueryString complete(Predicates criteria, Sort sort) {
|
||||
QueryString result = query.create(criteria).orderBy(sort);
|
||||
QueryString query = queryBuilder.create(criteria).orderBy(sort);
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug(String.format("Created Query '%1$s'", result.toString()));
|
||||
LOG.debug(String.format("Created Query '%1$s'", query.toString()));
|
||||
}
|
||||
|
||||
return result;
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link IndexProvider} is an {@link Iterator} providing sequentially numbered placeholders (starting at 1),
|
||||
* in a generated GemFire OQL statement corresponding to all possible arguments passed to
|
||||
* the query's indexed parameters.
|
||||
*
|
||||
* @see java.util.Iterator
|
||||
*/
|
||||
private static class IndexProvider implements Iterator<Integer> {
|
||||
|
||||
private int index;
|
||||
@@ -110,17 +119,17 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
this.index = 1;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.util.Iterator#hasNext()
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public boolean hasNext() {
|
||||
// TODO really?
|
||||
return index <= Integer.MAX_VALUE;
|
||||
return (index <= Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.util.Iterator#next()
|
||||
*/
|
||||
@@ -129,7 +138,7 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
return index++;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.util.Iterator#remove()
|
||||
*/
|
||||
@@ -138,5 +147,4 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* GemFire specific {@link QueryMethod}.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author John Blum
|
||||
* @see org.springframework.data.repository.query.QueryMethod
|
||||
@@ -50,7 +50,7 @@ public class GemfireQueryMethod extends QueryMethod {
|
||||
|
||||
/**
|
||||
* Creates a new {@link GemfireQueryMethod} from the given {@link Method} and {@link RepositoryMetadata}.
|
||||
*
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @param factory must not be {@literal null}.
|
||||
@@ -77,11 +77,11 @@ public class GemfireQueryMethod extends QueryMethod {
|
||||
* @see org.springframework.data.domain.Pageable
|
||||
* @see java.lang.reflect.Method#getParameterTypes()
|
||||
*/
|
||||
private void assertNonPagingQueryMethod(final Method method) {
|
||||
private void assertNonPagingQueryMethod(Method method) {
|
||||
for (Class<?> type : method.getParameterTypes()) {
|
||||
if (Pageable.class.isAssignableFrom(type)) {
|
||||
throw new IllegalStateException(String.format("Pagination is not supported by GemFire Repositories!"
|
||||
+ " Offending method: %1$s", method.toString()));
|
||||
throw new IllegalStateException(String.format("Pagination is not supported by GemFire Repositories;"
|
||||
+ " Offending method: %1$s", method.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public class GemfireQueryMethod extends QueryMethod {
|
||||
|
||||
/**
|
||||
* Returns the annotated query for the query method if present.
|
||||
*
|
||||
*
|
||||
* @return the annotated query or {@literal null} in case it's empty or not present.
|
||||
* @see org.springframework.data.gemfire.repository.Query
|
||||
* @see java.lang.reflect.Method#getAnnotation(Class)
|
||||
|
||||
@@ -3,4 +3,5 @@ package org.springframework.data.gemfire.repository.query;
|
||||
interface Predicate {
|
||||
|
||||
String toString(String alias);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class Predicates implements Predicate {
|
||||
|
||||
/**
|
||||
* Creates a new {@link Predicates} wrapper instance.
|
||||
*
|
||||
*
|
||||
* @param predicate must not be {@literal null}.
|
||||
*/
|
||||
private Predicates(Predicate predicate) {
|
||||
@@ -40,23 +40,22 @@ class Predicates implements Predicate {
|
||||
|
||||
/**
|
||||
* Creates a new Predicate for the given {@link Part} and index iterator.
|
||||
*
|
||||
*
|
||||
* @param part must not be {@literal null}.
|
||||
* @param value must not be {@literal null}.
|
||||
* @return
|
||||
* @param indexes must not be {@literal null}.
|
||||
* @return an instance of {@link Predicates} wrapping the WHERE clause condition expression ({@link Part}).
|
||||
*/
|
||||
public static Predicates create(Part part, Iterator<Integer> value) {
|
||||
return create(new AtomicPredicate(part, value));
|
||||
public static Predicates create(Part part, Iterator<Integer> indexes) {
|
||||
return create(new AtomicPredicate(part, indexes));
|
||||
}
|
||||
|
||||
/**
|
||||
* And-concatenates the given {@link Predicate} to the current one.
|
||||
*
|
||||
*
|
||||
* @param predicate must not be {@literal null}.
|
||||
* @return
|
||||
* @return an instance of {@link Predicates} wrapping an AND condition.
|
||||
*/
|
||||
public Predicates and(final Predicate predicate) {
|
||||
|
||||
return create(new Predicate() {
|
||||
@Override
|
||||
public String toString(String alias) {
|
||||
@@ -67,12 +66,11 @@ class Predicates implements Predicate {
|
||||
|
||||
/**
|
||||
* Or-concatenates the given {@link Predicate} to the current one.
|
||||
*
|
||||
*
|
||||
* @param predicate must not be {@literal null}.
|
||||
* @return
|
||||
* @return an instance of {@link Predicates} wrapping an OR condition.
|
||||
*/
|
||||
public Predicates or(final Predicate predicate) {
|
||||
|
||||
return create(new Predicate() {
|
||||
@Override
|
||||
public String toString(String alias) {
|
||||
@@ -92,66 +90,96 @@ class Predicates implements Predicate {
|
||||
|
||||
/**
|
||||
* Predicate to create a predicate expression for a {@link Part}.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public static class AtomicPredicate implements Predicate {
|
||||
|
||||
private final Iterator<Integer> indexes;
|
||||
|
||||
private final Part part;
|
||||
private final Iterator<Integer> value;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AtomicPredicate}.
|
||||
*
|
||||
*
|
||||
* @param part must not be {@literal null}.
|
||||
* @param value must not be {@literal null}.
|
||||
* @param indexes must not be {@literal null}.
|
||||
*/
|
||||
public AtomicPredicate(Part part, Iterator<Integer> value) {
|
||||
|
||||
Assert.notNull(part);
|
||||
Assert.notNull(value);
|
||||
public AtomicPredicate(Part part, Iterator<Integer> indexes) {
|
||||
Assert.notNull(part, "Query Predicate Part must not be null");
|
||||
Assert.notNull(indexes, "Iterator of numeric, indexed query parameter placeholders must not be null");
|
||||
|
||||
this.part = part;
|
||||
this.value = value;
|
||||
this.indexes = indexes;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
/**
|
||||
* Builds a conditional expression for the entity property in the WHERE clause of the GemFire OQL
|
||||
* query statement.
|
||||
*
|
||||
* @see org.springframework.data.gemfire.repository.query.Predicate#toString(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String toString(String alias) {
|
||||
Type type = part.getType();
|
||||
if (isIgnoreCase()) {
|
||||
return String.format("%s.equalsIgnoreCase($%d)", resolveProperty(alias), indexes.next());
|
||||
}
|
||||
else {
|
||||
Type partType = part.getType();
|
||||
|
||||
return String.format("%s.%s %s", alias == null ? QueryBuilder.DEFAULT_ALIAS : alias,
|
||||
part.getProperty().toDotPath(), toClause(type));
|
||||
}
|
||||
|
||||
private String toClause(Type type) {
|
||||
switch (type) {
|
||||
case FALSE:
|
||||
case TRUE:
|
||||
return String.format("%1$s %2$s", getOperator(type), Type.TRUE.equals(type));
|
||||
case IS_NULL:
|
||||
case IS_NOT_NULL:
|
||||
return String.format("%s NULL", getOperator(type));
|
||||
default:
|
||||
return String.format("%s $%s", getOperator(type), value.next());
|
||||
switch (partType) {
|
||||
case IS_NULL:
|
||||
case IS_NOT_NULL:
|
||||
return String.format("%s %s NULL", resolveProperty(alias), resolveOperator(partType));
|
||||
case FALSE:
|
||||
case TRUE:
|
||||
return String.format("%s %s %s", resolveProperty(alias), resolveOperator(partType),
|
||||
Type.TRUE.equals(partType));
|
||||
default:
|
||||
return String.format("%s %s $%d", resolveProperty(alias), resolveOperator(partType),
|
||||
indexes.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean isIgnoreCase() {
|
||||
switch (part.shouldIgnoreCase()) {
|
||||
case ALWAYS:
|
||||
case WHEN_POSSIBLE:
|
||||
return true;
|
||||
case NEVER:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
String resolveProperty(String alias) {
|
||||
return String.format("%1$s.%2$s", resolveAlias(alias), part.getProperty().toDotPath());
|
||||
}
|
||||
|
||||
String resolveAlias(String alias) {
|
||||
return (alias != null ? alias : QueryBuilder.DEFAULT_ALIAS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the given {@link Type} to an OQL operator.
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
* Resolves the given {@link Type} as an GemFire OQL operator.
|
||||
*
|
||||
* @param partType the conditional expression (e.g. 'IN') in the query method name.
|
||||
* @return a GemFire OQL operator.
|
||||
*/
|
||||
private String getOperator(Type type) {
|
||||
switch (type) {
|
||||
case IN:
|
||||
return "IN SET";
|
||||
case NOT_IN:
|
||||
return "NOT IN SET";
|
||||
String resolveOperator(Type partType) {
|
||||
switch (partType) {
|
||||
// Equality - Is
|
||||
case FALSE:
|
||||
case IS_NULL:
|
||||
case SIMPLE_PROPERTY:
|
||||
case TRUE:
|
||||
return "=";
|
||||
// Equality - Is Not
|
||||
case IS_NOT_NULL:
|
||||
case NEGATING_SIMPLE_PROPERTY:
|
||||
return "!=";
|
||||
// Relational Comparison
|
||||
case GREATER_THAN:
|
||||
return ">";
|
||||
case GREATER_THAN_EQUAL:
|
||||
@@ -160,26 +188,19 @@ class Predicates implements Predicate {
|
||||
return "<";
|
||||
case LESS_THAN_EQUAL:
|
||||
return "<=";
|
||||
case IS_NOT_NULL:
|
||||
case NEGATING_SIMPLE_PROPERTY:
|
||||
return "!=";
|
||||
/*
|
||||
NOTE unfortunately, 'NOT LIKE' operator is not supported by GemFire's Query/OQL syntax
|
||||
case NOT_LIKE:
|
||||
return "NOT LIKE";
|
||||
*/
|
||||
// Set Containment
|
||||
case IN:
|
||||
return "IN SET";
|
||||
case NOT_IN:
|
||||
return "NOT IN SET";
|
||||
// Wildcard Matching
|
||||
case LIKE:
|
||||
case STARTING_WITH:
|
||||
case ENDING_WITH:
|
||||
case CONTAINING:
|
||||
return "LIKE";
|
||||
case FALSE:
|
||||
case IS_NULL:
|
||||
case SIMPLE_PROPERTY:
|
||||
case TRUE:
|
||||
return "=";
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format("Unsupported operator %s!", type));
|
||||
throw new IllegalArgumentException(String.format("Unsupported operator %s!", partType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,5 +56,4 @@ class QueryBuilder {
|
||||
public String toString() {
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
* Value object to work with OQL query strings.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
* @author John Blum
|
||||
@@ -58,7 +58,7 @@ public class QueryString {
|
||||
|
||||
/**
|
||||
* Creates a {@link QueryString} from the given {@link String} query.
|
||||
*
|
||||
*
|
||||
* @param source a String containing the OQL Query.
|
||||
*/
|
||||
public QueryString(String source) {
|
||||
@@ -68,7 +68,7 @@ public class QueryString {
|
||||
|
||||
/**
|
||||
* Creates a {@literal SELECT} query for the given domain class.
|
||||
*
|
||||
*
|
||||
* @param domainClass must not be {@literal null}.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@@ -78,7 +78,7 @@ public class QueryString {
|
||||
|
||||
/**
|
||||
* Creates a {@literal SELECT} query for the given domain class.
|
||||
*
|
||||
*
|
||||
* @param domainClass must not be {@literal null}.
|
||||
* @param isCountQuery indicates if this is a count query
|
||||
*/
|
||||
@@ -118,7 +118,7 @@ public class QueryString {
|
||||
|
||||
/**
|
||||
* Returns the parameter indexes used in this query.
|
||||
*
|
||||
*
|
||||
* @return the parameter indexes used in this query or an empty {@link Iterable} if none are used.
|
||||
* @see java.lang.Iterable
|
||||
*/
|
||||
@@ -195,5 +195,4 @@ public class QueryString {
|
||||
public String toString() {
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
/**
|
||||
* Creates a new {@link StringBasedGemfireRepositoryQuery} using the given {@link GemfireQueryMethod} and
|
||||
* {@link GemfireTemplate}. The actual query {@link String} will be looked up from the query method.
|
||||
*
|
||||
*
|
||||
* @param queryMethod must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
*/
|
||||
@@ -67,7 +67,7 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
/**
|
||||
* Creates a new {@link StringBasedGemfireRepositoryQuery} using the given query {@link String},
|
||||
* {@link GemfireQueryMethod} and {@link GemfireTemplate}.
|
||||
*
|
||||
*
|
||||
* @param query will fall back to the query annotated to the given {@link GemfireQueryMethod} if {@literal null}.
|
||||
* @param queryMethod must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
|
||||
@@ -13,11 +13,14 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentProperty;
|
||||
@@ -33,42 +36,42 @@ import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.EvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
* {@link RepositoryFactorySupport} implementation creating repository proxies
|
||||
* for Gemfire.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
* @author John Blum
|
||||
*/
|
||||
public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
private final MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> context;
|
||||
private final MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> mappingContext;
|
||||
|
||||
private final Regions regions;
|
||||
|
||||
/**
|
||||
* Creates a new {@link GemfireRepositoryFactory}.
|
||||
*
|
||||
*
|
||||
* @param regions must not be {@literal null}.
|
||||
* @param context the {@link MappingContext} used by the constructed Repository for mapping entities
|
||||
* @param mappingContext the {@link MappingContext} used by the constructed Repository for mapping entities
|
||||
* to the underlying data store, must not be {@literal null}.
|
||||
*/
|
||||
public GemfireRepositoryFactory(Iterable<Region<?, ?>> regions, MappingContext<? extends GemfirePersistentEntity<?>,
|
||||
GemfirePersistentProperty> context) {
|
||||
|
||||
Assert.notNull(regions, "Regions must not be null!");
|
||||
Assert.notNull(context, "MappingContext must not be null!");
|
||||
|
||||
this.context = context;
|
||||
this.regions = new Regions(regions, this.context);
|
||||
public GemfireRepositoryFactory(Iterable<Region<?, ?>> regions,
|
||||
MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> mappingContext) {
|
||||
|
||||
Assert.notNull(regions, "Regions must not be null");
|
||||
Assert.notNull(mappingContext, "MappingContext must not be null");
|
||||
|
||||
this.mappingContext = mappingContext;
|
||||
this.regions = new Regions(regions, this.mappingContext);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -78,7 +81,7 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> GemfireEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
GemfirePersistentEntity<T> entity = (GemfirePersistentEntity<T>) context.getPersistentEntity(domainClass);
|
||||
GemfirePersistentEntity<T> entity = (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(domainClass);
|
||||
return new DefaultGemfireEntityInformation<T, ID>(entity);
|
||||
}
|
||||
|
||||
@@ -96,8 +99,8 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
return getTargetRepositoryViaReflection(repositoryInformation, gemfireTemplate, entityInformation);
|
||||
}
|
||||
|
||||
private GemfireTemplate getTemplate(RepositoryMetadata metadata) {
|
||||
GemfirePersistentEntity<?> entity = context.getPersistentEntity(metadata.getDomainType());
|
||||
GemfireTemplate getTemplate(RepositoryMetadata metadata) {
|
||||
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(metadata.getDomainType());
|
||||
|
||||
String entityRegionName = entity.getRegionName();
|
||||
String repositoryRegionName = getRepositoryRegionName(metadata.getRepositoryInterface());
|
||||
@@ -106,9 +109,9 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
Region<?, ?> region = regions.getRegion(regionName);
|
||||
|
||||
if (region == null) {
|
||||
throw new IllegalStateException(String.format("No Region '%1$s' found for domain class %2$s!"
|
||||
+ " Make sure you have configured a GemFire Region of that name in your application context!",
|
||||
regionName, metadata.getDomainType()));
|
||||
throw new IllegalStateException(String.format("No Region '%1$s' found for domain class %2$s;"
|
||||
+ " Make sure you have configured a GemFire Region of that name in your application context",
|
||||
regionName, metadata.getDomainType().getName()));
|
||||
}
|
||||
|
||||
Class<?> regionKeyType = region.getAttributes().getKeyConstraint();
|
||||
@@ -116,16 +119,16 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
if (regionKeyType != null && entity.getIdProperty() != null) {
|
||||
Assert.isTrue(regionKeyType.isAssignableFrom(entityIdType), String.format(
|
||||
"The Region referenced only supports keys of type %1$s but the entity to be stored has an id of type %2$s!",
|
||||
regionKeyType, entityIdType));
|
||||
"The Region referenced only supports keys of type %1$s, but the entity to be stored has an id of type %2$s",
|
||||
regionKeyType.getName(), entityIdType.getName()));
|
||||
}
|
||||
|
||||
return new GemfireTemplate(region);
|
||||
}
|
||||
|
||||
private String getRepositoryRegionName(final Class<?> repositoryClass) {
|
||||
return (repositoryClass.isAnnotationPresent(org.springframework.data.gemfire.mapping.Region.class) ?
|
||||
repositoryClass.getAnnotation(org.springframework.data.gemfire.mapping.Region.class).value() : null);
|
||||
String getRepositoryRegionName(Class<?> repositoryInterface) {
|
||||
return (repositoryInterface.isAnnotationPresent(org.springframework.data.gemfire.mapping.Region.class) ?
|
||||
repositoryInterface.getAnnotation(org.springframework.data.gemfire.mapping.Region.class).value() : null);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -142,35 +145,32 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see springframework.data.repository.core.support.RepositoryFactorySupport
|
||||
* #getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key)
|
||||
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport
|
||||
* #getQueryLookupStrategy(Key, EvaluationContextProvider)
|
||||
*/
|
||||
@Override
|
||||
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
|
||||
|
||||
protected QueryLookupStrategy getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) {
|
||||
|
||||
return new QueryLookupStrategy() {
|
||||
|
||||
|
||||
@Override
|
||||
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
|
||||
NamedQueries namedQueries) {
|
||||
|
||||
GemfireQueryMethod queryMethod = new GemfireQueryMethod(method, metadata, factory, context);
|
||||
|
||||
GemfireQueryMethod queryMethod = new GemfireQueryMethod(method, metadata, factory, mappingContext);
|
||||
GemfireTemplate template = getTemplate(metadata);
|
||||
|
||||
if (queryMethod.hasAnnotatedQuery()) {
|
||||
return new StringBasedGemfireRepositoryQuery(queryMethod, template).asUserDefinedQuery();
|
||||
}
|
||||
|
||||
String namedQueryName = queryMethod.getNamedQueryName();
|
||||
|
||||
if (namedQueries.hasQuery(namedQueryName)) {
|
||||
return new StringBasedGemfireRepositoryQuery(namedQueries.getQuery(namedQueryName), queryMethod,
|
||||
template).asUserDefinedQuery();
|
||||
if (namedQueries.hasQuery(queryMethod.getNamedQueryName())) {
|
||||
return new StringBasedGemfireRepositoryQuery(namedQueries.getQuery(queryMethod.getNamedQueryName()),
|
||||
queryMethod, template).asUserDefinedQuery();
|
||||
}
|
||||
|
||||
return new PartTreeGemfireRepositoryQuery(queryMethod, template);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user