diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index c39bf4dcc..000ee0845 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -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 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

getPersistentPropertyPath(String propertyPath, Class type) { + return getPersistentPropertyPath(propertyPath, ClassTypeInformation.from(type)); + } + + private PersistentPropertyPath

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

getPersistentPropertyPath(Iterable parts, TypeInformation type) { List

result = new ArrayList

(); - E current = getPersistentEntity(propertyPath.getOwningType()); + Iterator 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()); } } diff --git a/src/main/java/org/springframework/data/mapping/context/MappingContext.java b/src/main/java/org/springframework/data/mapping/context/MappingContext.java index 622c0bd58..0d5ea6d2c 100644 --- a/src/main/java/org/springframework/data/mapping/context/MappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/MappingContext.java @@ -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, P extends Pers /** * Returns all {@link PersistentProperty}s for the given path expression based on the given {@link PropertyPath}. * - * @param - * @param type - * @param path - * @return + * @param propertyPath must not be {@literal null}. + * @return the {@link PersistentPropertyPath} representing the given {@link PropertyPath}. */ PersistentPropertyPath

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

getPersistentPropertyPath(String propertyPath, Class type); } diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index d783f8845..771dd8a75 100644 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -171,6 +171,20 @@ public class AbstractMappingContextUnitTests { assertThat(propertyEntity.getType(), is(equalTo((Class) Person.class))); } + /** + * @see DATACMNS-380 + */ + @Test + public void returnsPersistentPropertyPathForDotPath() { + + PersistentPropertyPath path = context.getPersistentPropertyPath("persons.name", + Sample.class); + + assertThat(path.getLength(), is(2)); + assertThat(path.getBaseProperty().getName(), is("persons")); + assertThat(path.getLeafProperty().getName(), is("name")); + } + class Person { String name; }