DATACMNS-380 - MappingContext exposes PersistentPropertyPath by dot path.

Introduced MappingContext.getPersistentPropertyPath(String, Class<?>) to allow looking up a PersistentPropertyPath from a plain dot-notated path expression (e.g. foo.bar.foobar). This removes the need to use a PropertyPath in case you really only want to look up plain path expressions and the additional functionality in PropertyPath (camel case traversal and interpreting _ as hard delimiter) is not needed or wanted.
This commit is contained in:
Oliver Gierke
2013-10-10 12:54:24 +02:00
parent 2d699b6b36
commit 06c2f41584
3 changed files with 59 additions and 12 deletions

View File

@@ -22,10 +22,12 @@ import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -187,23 +189,47 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
* @see org.springframework.data.mapping.context.MappingContext#getPersistentPropertyPath(java.lang.Class, java.lang.String)
*/
public PersistentPropertyPath<P> getPersistentPropertyPath(PropertyPath propertyPath) {
return getPersistentPropertyPath(propertyPath.toDotPath(), propertyPath.getOwningType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.context.MappingContext#getPersistentPropertyPath(java.lang.String, java.lang.Class)
*/
public PersistentPropertyPath<P> getPersistentPropertyPath(String propertyPath, Class<?> type) {
return getPersistentPropertyPath(propertyPath, ClassTypeInformation.from(type));
}
private PersistentPropertyPath<P> getPersistentPropertyPath(String propertyPath, TypeInformation<?> type) {
return getPersistentPropertyPath(Arrays.asList(propertyPath.split("\\.")), type);
}
/**
* Creates a {@link PersistentPropertyPath} for the given parts and {@link TypeInformation}.
*
* @param parts must not be {@literal null} or empty.
* @param type must not be {@literal null}.
* @return
*/
private PersistentPropertyPath<P> getPersistentPropertyPath(Iterable<String> parts, TypeInformation<?> type) {
List<P> result = new ArrayList<P>();
E current = getPersistentEntity(propertyPath.getOwningType());
Iterator<String> iterator = parts.iterator();
E current = getPersistentEntity(type);
for (PropertyPath segment : propertyPath) {
while (iterator.hasNext()) {
P persistentProperty = current.getPersistentProperty(segment.getSegment());
String segment = iterator.next();
P persistentProperty = current.getPersistentProperty(segment);
if (persistentProperty == null) {
throw new IllegalArgumentException(String.format("No property %s found on %s!", segment.getSegment(),
current.getName()));
throw new IllegalArgumentException(String.format("No property %s found on %s!", segment, current.getName()));
}
result.add(persistentProperty);
if (segment.hasNext()) {
current = getPersistentEntity(segment.getType());
if (iterator.hasNext()) {
current = getPersistentEntity(persistentProperty.getActualType());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2013 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.
@@ -74,10 +74,17 @@ public interface MappingContext<E extends PersistentEntity<?, P>, P extends Pers
/**
* Returns all {@link PersistentProperty}s for the given path expression based on the given {@link PropertyPath}.
*
* @param <T>
* @param type
* @param path
* @return
* @param propertyPath must not be {@literal null}.
* @return the {@link PersistentPropertyPath} representing the given {@link PropertyPath}.
*/
PersistentPropertyPath<P> getPersistentPropertyPath(PropertyPath propertyPath);
/**
* Returns all {@link PersistentProperty}s for the given dot path notation based on the given type.
*
* @param propertyPath must not be {@literal null}.
* @param type must not be {@literal null}.
* @return the {@link PersistentPropertyPath} representing the given property path on the given type.
*/
PersistentPropertyPath<P> getPersistentPropertyPath(String propertyPath, Class<?> type);
}