diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 44fb23048..9cf33d460 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -26,6 +26,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -47,16 +48,17 @@ import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.FieldCallback; import org.springframework.validation.Validator; - /** - * Base class to build mapping metadata and thus create instances of {@link PersistentEntity} and {@link PersistentProperty}. - * + * Base class to build mapping metadata and thus create instances of {@link PersistentEntity} and + * {@link PersistentProperty}. + * * @param E the concrete {@link PersistentEntity} type the {@link MappingContext} implementation creates * @param P the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates * @author Jon Brisbin * @author Oliver Gierke */ -public abstract class AbstractMappingContext, P extends PersistentProperty

> implements MappingContext, InitializingBean, ApplicationEventPublisherAware { +public abstract class AbstractMappingContext, P extends PersistentProperty

> + implements MappingContext, InitializingBean, ApplicationEventPublisherAware { private static final Set UNMAPPED_FIELDS = new HashSet(Arrays.asList("class", "this$0")); @@ -78,7 +80,7 @@ public abstract class AbstractMappingContext> initialEntitySet) { @@ -90,15 +92,22 @@ public abstract class AbstractMappingContext Iterable

getPersistentPropertyPath(Class type, String path) { + + Iterator parts = Arrays.asList(path.split("\\.")).iterator(); + List

result = new ArrayList

(); + E current = getPersistentEntity(type); + + while (parts.hasNext()) { + String name = parts.next(); + P property = current.getPersistentProperty(name); + + if (property == null) { + throw new IllegalArgumentException(String.format("No property %s found on %s!", name, current.getName())); + } + + result.add(property); + current = getPersistentEntity(property.getTypeInformation().getActualType()); + } + + return result; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.context.MappingContext#getEntityValidators(org.springframework.data.mapping.PersistentEntity) + */ + public List getEntityValidators(E entity) { + return validators.get(entity); + } /** * Adds the given type to the {@link MappingContext}. - * + * * @param type * @return */ @@ -149,7 +191,7 @@ public abstract class AbstractMappingContext descriptors = new HashMap(); @@ -228,9 +270,10 @@ public abstract class AbstractMappingContext getNestedTypeToAdd(P property, PersistentEntity entity) { @@ -258,30 +301,46 @@ public abstract class AbstractMappingContext getComponentTypeRecursively(TypeInformation typeInformation) { TypeInformation componentType = typeInformation.getComponentType(); - + if (componentType == null) { return null; } - - return componentType.isCollectionLike() ? getComponentTypeRecursively(componentType) : componentType; + + return componentType.isCollectionLike() ? getComponentTypeRecursively(componentType) : componentType; } private TypeInformation getTypeInformationIfNotSimpleType(TypeInformation information) { return information == null || simpleTypeHolder.isSimpleType(information.getType()) ? null : information; } - public List getEntityValidators(E entity) { - return validators.get(entity); - } - + /** + * Creates the concrete {@link PersistentEntity} instance. + * + * @param + * @param typeInformation + * @return + */ protected abstract E createPersistentEntity(TypeInformation typeInformation); - protected abstract P createPersistentProperty(Field field, PropertyDescriptor descriptor, E owner, SimpleTypeHolder simpleTypeHolder); - + /** + * Creates the concrete instance of {@link PersistentProperty}. + * + * @param field + * @param descriptor + * @param owner + * @param simpleTypeHolder + * @return + */ + protected abstract P createPersistentProperty(Field field, PropertyDescriptor descriptor, E owner, + SimpleTypeHolder simpleTypeHolder); + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ public void afterPropertiesSet() { for (Class initialEntity : initialEntitySet) { addPersistentEntity(initialEntity); diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java index 9f846a1f4..6be0095a6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java @@ -40,16 +40,39 @@ import org.springframework.validation.Validator; public interface MappingContext, P extends PersistentProperty

> { /** - * Obtains a list of PersistentEntity instances + * Returns all {@link PersistentEntity}s held in the context. * - * @return A list of PersistentEntity instances + * @return */ Collection getPersistentEntities(); + /** + * Returns a {@link PersistentEntity} for the given {@link Class}. + * + * @param type + * @return + */ E getPersistentEntity(Class type); + /** + * Returns a {@link PersistentEntity} for the given {@link TypeInformation}. + * + * @param type + * @return + */ E getPersistentEntity(TypeInformation type); + /** + * Returns all {@link PersistentProperty}s for the given path expression based on the given root {@link Class}. Path + * expression are dot separated, e.g. {@code person.firstname}. + * + * @param + * @param type + * @param path + * @return + */ + Iterable

getPersistentPropertyPath(Class type, String path); + /** * Obtains a validator for the given entity * TODO: Why do we need validators at the {@link MappingContext}?