diff --git a/pom.xml b/pom.xml
index c0ed261c5..a04484ee5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -36,7 +36,6 @@
1.4.0.BUILD-SNAPSHOT
4.3.10.Final
-
false
diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupConfiguration.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupConfiguration.java
index 64e082b30..52f5a2fcd 100644
--- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupConfiguration.java
+++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupConfiguration.java
@@ -26,6 +26,7 @@ import java.util.List;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryInformation;
+import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.config.EntityLookupRegistrar.LookupRegistrar.Lookup;
import org.springframework.data.rest.core.support.EntityLookup;
@@ -33,15 +34,15 @@ import org.springframework.util.Assert;
/**
* Configuration instance to implement {@link EntityLookupRegistrar}. Exposed via
- * {@link RepositoryRestConfiguration#withCustomEntityLookup()}.
+ * {@link RepositoryRestConfiguration#withEntityLookup()}.
*
* @author Oliver Gierke
* @since 2.5
*/
-@RequiredArgsConstructor
class EntityLookupConfiguration implements EntityLookupRegistrar {
private final List>> lookupInformation = new ArrayList>>();
+ private final List> lookupTypes = new ArrayList>();
/*
* (non-Javadoc)
@@ -55,6 +56,17 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {
return this;
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.rest.core.config.EntityLookupRegistrar#forValueRepository(java.lang.Class)
+ */
+ @Override
+ public > IdMappingRegistrar forValueRepository(
+ Class type) {
+ this.lookupTypes.add(AbstractRepositoryMetadata.getMetadata(type).getDomainType());
+ return forRepository(type);
+ }
+
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.config.EntityLookupRegistrar#forRepository(java.lang.Class)
@@ -65,6 +77,18 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {
return new MappingBuilder(type);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.rest.core.config.EntityLookupRegistrar#forValueRepository(java.lang.Class, org.springframework.core.convert.converter.Converter, org.springframework.data.rest.core.config.EntityLookupRegistrar.LookupRegistrar.Lookup)
+ */
+ @Override
+ public > EntityLookupRegistrar forValueRepository(
+ Class type, Converter identifierMapping, Lookup lookup) {
+
+ this.lookupTypes.add(AbstractRepositoryMetadata.getMetadata(type).getDomainType());
+ return forRepository(type, identifierMapping, lookup);
+ }
+
/**
* Custom builder implementation to back {@link LookupRegistrar} and {@link IdMappingRegistrar}.
*
@@ -136,6 +160,10 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {
return lookups;
}
+ public boolean isLookupType(Class> type) {
+ return this.lookupTypes.contains(type);
+ }
+
/**
* An {@link EntityLookup} backed by a repository instance and a {@link LookupInformation}.
*
diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupRegistrar.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupRegistrar.java
index cea2b4a3d..2cec18c28 100644
--- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupRegistrar.java
+++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/EntityLookupRegistrar.java
@@ -38,15 +38,7 @@ public interface EntityLookupRegistrar {
*/
> IdMappingRegistrar forRepository(Class type);
- /**
- * Registers an {@link EntityLookup} for the given repository type, identifier mapping and lookup operation.
- *
- * @param type must not be {@literal null}.
- * @param identifierMapping must not be {@literal null}.
- * @param lookup must not be {@literal null}.
- */
- > EntityLookupRegistrar forRepository(Class type,
- Converter identifierMapping, Lookup lookup);
+ > IdMappingRegistrar forValueRepository(Class type);
interface IdMappingRegistrar> {
@@ -59,6 +51,19 @@ public interface EntityLookupRegistrar {
LookupRegistrar withIdMapping(Converter mapping);
}
+ /**
+ * Registers an {@link EntityLookup} for the given repository type, identifier mapping and lookup operation.
+ *
+ * @param type must not be {@literal null}.
+ * @param identifierMapping must not be {@literal null}.
+ * @param lookup must not be {@literal null}.
+ */
+ > EntityLookupRegistrar forRepository(Class type,
+ Converter identifierMapping, Lookup lookup);
+
+ > EntityLookupRegistrar forValueRepository(Class type,
+ Converter identifierMapping, Lookup lookup);
+
interface LookupRegistrar> {
/**
diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java
index 97c785dfe..406a834fb 100644
--- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java
+++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java
@@ -61,6 +61,7 @@ public class RepositoryRestConfiguration {
private final ProjectionDefinitionConfiguration projectionConfiguration;
private final MetadataConfiguration metadataConfiguration;
private final EntityLookupConfiguration entityLookupConfiguration;
+ private final List> valueTypes = new ArrayList>();
private final EnumTranslationConfiguration enumTranslationConfiguration;
private boolean enableEnumTranslation = false;
@@ -555,7 +556,7 @@ public class RepositoryRestConfiguration {
* @return the {@link EntityLookupRegistrar} to build custom {@link EntityLookup}s.
* @since 2.5
*/
- public EntityLookupRegistrar withCustomEntityLookup() {
+ public EntityLookupRegistrar withEntityLookup() {
return entityLookupConfiguration;
}
@@ -571,4 +572,8 @@ public class RepositoryRestConfiguration {
return entityLookupConfiguration.getEntityLookups(repositories);
}
+
+ public boolean isLookupType(Class> type) {
+ return this.entityLookupConfiguration.isLookupType(type);
+ }
}
diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EmbeddedResourcesAssembler.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EmbeddedResourcesAssembler.java
new file mode 100644
index 000000000..e87ba4ea0
--- /dev/null
+++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EmbeddedResourcesAssembler.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.data.rest.webmvc;
+
+import lombok.NonNull;
+import lombok.RequiredArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.springframework.data.mapping.Association;
+import org.springframework.data.mapping.PersistentEntity;
+import org.springframework.data.mapping.PersistentProperty;
+import org.springframework.data.mapping.PersistentPropertyAccessor;
+import org.springframework.data.mapping.SimpleAssociationHandler;
+import org.springframework.data.mapping.context.PersistentEntities;
+import org.springframework.data.rest.core.mapping.ResourceMetadata;
+import org.springframework.data.rest.webmvc.mapping.AssociationLinks;
+import org.springframework.data.rest.webmvc.support.ExcerptProjector;
+import org.springframework.hateoas.core.EmbeddedWrapper;
+import org.springframework.hateoas.core.EmbeddedWrappers;
+import org.springframework.util.Assert;
+
+/**
+ * @author Oliver Gierke
+ */
+@RequiredArgsConstructor
+public class EmbeddedResourcesAssembler {
+
+ private final @NonNull PersistentEntities entities;
+ private final @NonNull AssociationLinks associations;
+ private final @NonNull ExcerptProjector projector;
+ private final @NonNull EmbeddedWrappers wrappers = new EmbeddedWrappers(false);
+
+ /**
+ * Returns the embedded resources to render. This will add an {@link RelatedResource} for linkable associations if
+ * they have an excerpt projection registered.
+ *
+ * @param instance must not be {@literal null}.
+ * @return
+ */
+ public Iterable getEmbeddedResources(Object instance) {
+
+ Assert.notNull(instance, "Entity instance must not be null!");
+
+ PersistentEntity, ?> entity = entities.getPersistentEntity(instance.getClass());
+
+ final List associationProjections = new ArrayList();
+ final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
+ final ResourceMetadata metadata = associations.getMetadataFor(entity.getType());
+
+ entity.doWithAssociations(new SimpleAssociationHandler() {
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.SimpleAssociationHandler#doWithAssociation(org.springframework.data.mapping.Association)
+ */
+ @Override
+ public void doWithAssociation(Association extends PersistentProperty>> association) {
+
+ PersistentProperty> property = association.getInverse();
+
+ if (!associations.isLinkableAssociation(property)) {
+ return;
+ }
+
+ if (!projector.hasExcerptProjection(property.getActualType())) {
+ return;
+ }
+
+ Object value = accessor.getProperty(association.getInverse());
+
+ if (value == null) {
+ return;
+ }
+
+ String rel = metadata.getMappingFor(property).getRel();
+
+ if (value instanceof Collection) {
+
+ Collection> collection = (Collection>) value;
+
+ if (collection.isEmpty()) {
+ return;
+ }
+
+ List