Tweaked rel generation to make it hierarchical.

This commit is contained in:
Jon Brisbin
2013-02-19 17:37:14 -06:00
committed by Jon Brisbin
parent fc307e3dfa
commit f4f7427263
3 changed files with 40 additions and 16 deletions

View File

@@ -72,6 +72,8 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
public static boolean maybeAddAssociationLink(Repositories repositories,
RepositoryRestConfiguration config,
URI baseEntityUri,
RepositoryInformation repoInfo,
ResourceMapping entityMapping,
ResourceMapping propertyMapping,
PersistentProperty persistentProperty,
List<Link> links) {
@@ -87,10 +89,7 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
if(null == propertyPath) {
propertyPath = persistentProperty.getName();
}
// entityRel + "." +
String propertyRel = (null != propertyMapping
? propertyMapping.getRel()
: propertyPath);
String propertyRel = formatRel(config, repoInfo, persistentProperty);
if(repositories.hasRepositoryFor(propertyType)) {
// This is a managed type, generate a Link
RepositoryInformation linkedRepoInfo = repositories.getRepositoryInformationFor(propertyType);
@@ -319,6 +318,8 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
if(persistentProperty.isEntity() && maybeAddAssociationLink(repositories,
config,
baseEntityUri,
repoInfo,
entityMapping,
propertyMapping,
persistentProperty,
links)) {
@@ -347,6 +348,8 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
if(maybeAddAssociationLink(repositories,
config,
baseEntityUri,
repoInfo,
entityMapping,
propertyMapping,
persistentProperty,
links)) {

View File

@@ -21,6 +21,7 @@ import org.springframework.data.mapping.AssociationHandler;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.rest.config.ResourceMapping;
import org.springframework.data.rest.repository.annotation.Description;
import org.springframework.data.rest.repository.support.RepositoryInformationSupport;
@@ -59,8 +60,8 @@ public class PersistentEntityToJsonSchemaConverter
@SuppressWarnings({"unchecked"})
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
PersistentEntity persistentEntity = repositories.getPersistentEntity((Class<?>)source);
final ResourceMapping repoMapping = getResourceMapping(config,
repositories.getRepositoryInformationFor(persistentEntity.getType()));
final RepositoryInformation repoInfo = repositories.getRepositoryInformationFor(persistentEntity.getType());
final ResourceMapping repoMapping = getResourceMapping(config, repoInfo);
final ResourceMapping entityMapping = getResourceMapping(config, persistentEntity);
final URI baseEntityUri = buildUri(config.getBaseUri(), repoMapping.getPath(), "{id}");
String entityDesc = persistentEntity.getType().isAnnotationPresent(Description.class)
@@ -103,6 +104,8 @@ public class PersistentEntityToJsonSchemaConverter
maybeAddAssociationLink(repositories,
config,
baseEntityUri,
repoInfo,
entityMapping,
propertyMapping,
persistentProperty,
links);

View File

@@ -6,6 +6,7 @@ import static org.springframework.util.StringUtils.*;
import java.lang.reflect.Method;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.config.ResourceMapping;
@@ -43,6 +44,23 @@ public abstract class ResourceMappingUtils {
return method.getName();
}
public static String formatRel(RepositoryRestConfiguration config,
RepositoryInformation repoInfo,
PersistentProperty persistentProperty) {
if(null == persistentProperty) {
return null;
}
ResourceMapping repoMapping = getResourceMapping(config, repoInfo);
ResourceMapping entityMapping = getResourceMapping(config, persistentProperty.getOwner());
ResourceMapping propertyMapping = entityMapping.getResourceMappingFor(persistentProperty.getName());
return String.format("%s.%s.%s",
repoMapping.getRel(),
entityMapping.getRel(),
(null != propertyMapping ? propertyMapping.getRel() : persistentProperty.getName()));
}
public static String findPath(Class<?> type) {
RestResource anno;
if(null != (anno = findAnnotation(type, RestResource.class))) {
@@ -75,16 +93,6 @@ public abstract class ResourceMappingUtils {
return null == (anno = findAnnotation(method, RestResource.class)) || anno.exported();
}
public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config,
PersistentEntity persistentEntity) {
if(null == persistentEntity) {
return null;
}
Class<?> domainType = persistentEntity.getType();
ResourceMapping mapping = (null != config ? config.getResourceMappingForDomainType(domainType) : null);
return merge(domainType, mapping);
}
public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config,
RepositoryInformation repoInfo) {
if(null == repoInfo) {
@@ -95,6 +103,16 @@ public abstract class ResourceMappingUtils {
return merge(repoType, mapping);
}
public static ResourceMapping getResourceMapping(RepositoryRestConfiguration config,
PersistentEntity persistentEntity) {
if(null == persistentEntity) {
return null;
}
Class<?> domainType = persistentEntity.getType();
ResourceMapping mapping = (null != config ? config.getResourceMappingForDomainType(domainType) : null);
return merge(domainType, mapping);
}
public static ResourceMapping merge(Method method, ResourceMapping mapping) {
ResourceMapping defaultMapping = new ResourceMapping(
findRel(method),