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.
This commit is contained in:
Oliver Gierke
2014-11-24 13:25:43 +01:00
parent 780d674012
commit afd1027d36
4 changed files with 19 additions and 17 deletions

View File

@@ -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<String, Class<?>> result = new HashMap<String, Class<?>>();
for (Entry<ProjectionDefinitionKey, Class<?>> entry : projectionDefinitions.entrySet()) {
if (entry.getKey().sourceType.isAssignableFrom(sourceType)) {
if (entry.getKey().sourceType.isAssignableFrom(userType)) {
result.put(entry.getKey().name, entry.getValue());
}
}

View File

@@ -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) {

View File

@@ -123,7 +123,7 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler<Obje
final List<EmbeddedWrapper> associationProjections = new ArrayList<EmbeddedWrapper>();
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<Obje
instanceType));
}
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
Object id = accessor.getProperty(entity.getIdProperty());
Object id = entity.getIdentifierAccessor(instance).getIdentifier();
Link resourceLink = entityLinks.linkToSingleResource(entity.getType(), id);
return new Link(resourceLink.getHref(), Link.REL_SELF);

View File

@@ -33,6 +33,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
@@ -209,10 +210,8 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro
if (prop.property.isCollectionLike()) {
for (Object obj : (Iterable<?>) 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<Object, Object> entry : ((Map<Object, Object>) 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<Object> 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();
}