DATAREST-39 Introduced new property on AttributeMetadata to encapsulate the decision of whether or not a property is nullable. Removed references to JPA-specific classes from everything but the JpaAttributeMetadata class.

This commit is contained in:
Jon Brisbin
2012-09-05 08:57:43 -05:00
committed by Jon Brisbin
parent a2288b7242
commit e3dde1a2cd
3 changed files with 23 additions and 6 deletions

View File

@@ -40,6 +40,13 @@ public interface AttributeMetadata {
*/
Class<?> elementType();
/**
* Whether this attribute can be nulled or not.
*
* @return
*/
boolean isNullable();
/**
* Can this attribute look like a {@link Collection}?
*

View File

@@ -7,6 +7,8 @@ import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.MapAttribute;
@@ -72,6 +74,18 @@ public class JpaAttributeMetadata implements AttributeMetadata {
: null);
}
@Override public boolean isNullable() {
if(hasAnnotation(ManyToOne.class)) {
return annotation(ManyToOne.class).optional();
}
if(hasAnnotation(OneToOne.class)) {
return annotation(OneToOne.class).optional();
}
return true;
}
@Override public boolean isCollectionLike() {
if(attribute instanceof PluralAttribute) {
PluralAttribute plattr = (PluralAttribute)attribute;

View File

@@ -21,8 +21,6 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.servlet.http.HttpServletRequest;
import org.codehaus.jackson.map.ObjectMapper;
@@ -1301,8 +1299,7 @@ public class RepositoryRestController
}
// Check if this is a @*ToOne relationship and is optional and if not, fail with a 405 Method Not Allowed
if((attrMeta.hasAnnotation(ManyToOne.class) && !attrMeta.annotation(ManyToOne.class).optional())
|| (attrMeta.hasAnnotation(OneToOne.class) && !attrMeta.annotation(OneToOne.class).optional())) {
if(!attrMeta.isNullable()) {
return negotiateResponse(request, HttpStatus.METHOD_NOT_ALLOWED, new HttpHeaders(), null);
}
@@ -1450,8 +1447,7 @@ public class RepositoryRestController
}
// Check if this @*ToOne relationship is optional and if not, fail with a 405 Method Not Allowed
if((attrMeta.hasAnnotation(ManyToOne.class) && !attrMeta.annotation(ManyToOne.class).optional())
|| (attrMeta.hasAnnotation(OneToOne.class) && !attrMeta.annotation(OneToOne.class).optional())) {
if(!attrMeta.isNullable()) {
return negotiateResponse(request, HttpStatus.METHOD_NOT_ALLOWED, new HttpHeaders(), null);
}