From afd1027d36af8f20853dede7d36fddf4db2a0f2b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 24 Nov 2014 13:25:43 +0100 Subject: [PATCH] DATAREST-416 - Switched to IdentifierAccessor API where needed. We now use the newly introduced IdentifierAccessor API to make sure we benefit from store specific optimizations when looking up identifier values. Added some user class lookups to make sure the projection and resource mapping lookup works if proxies types are handed around. --- .../ProjectionDefinitionConfiguration.java | 4 +++- .../mapping/RepositoryResourceMappings.java | 3 ++- .../PersistentEntityResourceAssembler.java | 5 ++-- ...RepositoryPropertyReferenceController.java | 24 +++++++++---------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java index 94612a1bc..7be870f39 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java @@ -22,6 +22,7 @@ import java.util.Map.Entry; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.rest.core.projection.ProjectionDefinitions; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -153,10 +154,11 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions Assert.notNull(sourceType, "Source type must not be null!"); + Class userType = ClassUtils.getUserClass(sourceType); Map> result = new HashMap>(); for (Entry> entry : projectionDefinitions.entrySet()) { - if (entry.getKey().sourceType.isAssignableFrom(sourceType)) { + if (entry.getKey().sourceType.isAssignableFrom(userType)) { result.put(entry.getKey().name, entry.getValue()); } } diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java index 44f8de13e..2a6d13bc7 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java @@ -33,6 +33,7 @@ import org.springframework.data.rest.core.support.RepositoriesUtils; import org.springframework.hateoas.RelProvider; import org.springframework.hateoas.core.EvoInflectorRelProvider; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -89,7 +90,7 @@ public class RepositoryResourceMappings implements ResourceMappings { public ResourceMetadata getMappingFor(Class type) { Assert.notNull(type, "Type must not be null!"); - return cache.get(type); + return cache.get(ClassUtils.getUserClass(type)); } private final void populateCache(Repositories repositories) { diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java index cdaf984d4..ccb4aebe1 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java @@ -123,7 +123,7 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler associationProjections = new ArrayList(); final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance); final AssociationLinks associationLinks = new AssociationLinks(mappings); - final ResourceMetadata metadata = mappings.getMappingFor(instance.getClass()); + final ResourceMetadata metadata = mappings.getMappingFor(entity.getType()); entity.doWithAssociations(new SimpleAssociationHandler() { @@ -197,8 +197,7 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler) prop.propertyValue) { - PersistentPropertyAccessor accessor = prop.entity.getPropertyAccessor(obj); - String sId = accessor.getProperty(prop.entity.getIdProperty()).toString(); - - if (propertyId.equals(sId)) { + IdentifierAccessor accessor = prop.entity.getIdentifierAccessor(obj); + if (propertyId.equals(accessor.getIdentifier().toString())) { PersistentEntityResource resource = assembler.toResource(obj); headers.set("Content-Location", resource.getId().getHref()); @@ -222,10 +221,8 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro } else if (prop.property.isMap()) { for (Map.Entry entry : ((Map) prop.propertyValue).entrySet()) { - PersistentPropertyAccessor accessor = prop.entity.getPropertyAccessor(entry.getValue()); - String sId = accessor.getProperty(prop.entity.getIdProperty()).toString(); - - if (propertyId.equals(sId)) { + IdentifierAccessor accessor = prop.entity.getIdentifierAccessor(entry.getValue()); + if (propertyId.equals(accessor.getIdentifier().toString())) { PersistentEntityResource resource = assembler.toResource(entry.getValue()); headers.set("Content-Location", resource.getId().getHref()); @@ -401,8 +398,9 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro while (itr.hasNext()) { Object obj = itr.next(); - PersistentPropertyAccessor accessor = prop.entity.getPropertyAccessor(obj); - String s = accessor.getProperty(prop.entity.getIdProperty()).toString(); + IdentifierAccessor accessor = prop.entity.getIdentifierAccessor(obj); + String s = accessor.getIdentifier().toString(); + if (propertyId.equals(s)) { itr.remove(); } @@ -412,8 +410,10 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro Iterator itr = m.keySet().iterator(); while (itr.hasNext()) { Object key = itr.next(); - PersistentPropertyAccessor accessor = prop.entity.getPropertyAccessor(m.get(key)); - String s = accessor.getProperty(prop.entity.getIdProperty()).toString(); + + IdentifierAccessor accessor = prop.entity.getIdentifierAccessor(m.get(key)); + String s = accessor.getIdentifier().toString(); + if (propertyId.equals(s)) { itr.remove(); }