From cb326dd98730debcf2c1f4d8761f9c2322e151f9 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Mon, 10 Oct 2011 23:37:00 +0000 Subject: [PATCH] CSPACE-4468: Validation for creation of new Relation records now accepts either CSIDs or refNames as identifiers for the subject and object of the relation. --- .../nuxeo/RelationValidatorHandler.java | 155 +++++++++++------- 1 file changed, 100 insertions(+), 55 deletions(-) diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java index e1bc98dad..b49e7786c 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java @@ -1,11 +1,13 @@ package org.collectionspace.services.relation.nuxeo; //import junit.framework.Assert; - import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.document.InvalidDocumentException; import org.collectionspace.services.common.document.ValidatorHandlerImpl; +import org.collectionspace.services.common.api.RefName.Authority; +import org.collectionspace.services.common.api.RefName.AuthorityItem; +import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.relation.RelationsCommon; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; @@ -15,72 +17,115 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; //import org.testng.Assert; -public class RelationValidatorHandler extends ValidatorHandlerImpl { +public class RelationValidatorHandler extends ValidatorHandlerImpl { /** The logger. */ private final Logger logger = LoggerFactory.getLogger(RelationValidatorHandler.class); - /* Error messages */ private static final String VALIDATION_ERROR = "The relation record payload was invalid. See log file for more details."; - private static final String SUBJECT_EQUALS_PREDICATE_ERROR = "The subject ID and object ID cannot be the same."; - + private static final String SUBJECT_EQUALS_OBJECT_ERROR = "The subject ID and object ID cannot be the same."; + @Override protected Class getCommonPartClass() { - return RelationsCommon.class; + return RelationsCommon.class; } - + @Override protected void handleCreate() - throws InvalidDocumentException{ - try { - RelationsCommon relationsCommon = (RelationsCommon)getCommonPart(); - assert(relationsCommon != null); - if (logger.isTraceEnabled() == true) { - logger.trace(relationsCommon.toString()); - } - - assert(relationsCommon.getDocumentId1() != null); - assert(relationsCommon.getDocumentId1().length() != 0); - - assert(relationsCommon.getDocumentId2() != null); - assert(relationsCommon.getDocumentId2().length() != 0); - - assert(relationsCommon.getRelationshipType() != null); - // - // Assert that the Subject ID and Predicate ID are not the same - // - assert(relationsCommon.getDocumentId1().equalsIgnoreCase(relationsCommon.getDocumentId2()) == false) : SUBJECT_EQUALS_PREDICATE_ERROR; - } catch (AssertionError e) { - if (logger.isErrorEnabled() == true) { - logger.error(e.getMessage(), e); - } - throw new InvalidDocumentException(VALIDATION_ERROR, e); - } + throws InvalidDocumentException { + try { + RelationsCommon relationsCommon = (RelationsCommon) getCommonPart(); + assert (relationsCommon != null); + if (logger.isTraceEnabled() == true) { + logger.trace(relationsCommon.toString()); + } + + // If no CSID for a subject or object is included in the create payload, + // a refName must be provided for that subject or object as an alternate identifier. + assert (hasObjectCsid(relationsCommon) || hasObjectRefname(relationsCommon)); + assert (hasSubjectCsid(relationsCommon) || hasSubjectRefname(relationsCommon)); + + // The Subject identifier and Object ID must not be identical: + // that is, a resource cannot be related to itself. + // FIXME: Can store values of calls above if desired to save additional checks here. + if (hasObjectCsid(relationsCommon) && hasSubjectCsid(relationsCommon)) { + assert (relationsCommon.getObjectCsid().trim().equalsIgnoreCase( + relationsCommon.getSubjectCsid().trim()) == false) : + SUBJECT_EQUALS_OBJECT_ERROR; + } + + // A relationship type must be provided. + assert (relationsCommon.getRelationshipType() != null); + + } catch (AssertionError e) { + if (logger.isErrorEnabled() == true) { + logger.error(e.getMessage(), e); + } + throw new InvalidDocumentException(VALIDATION_ERROR, e); + } + } + + @Override + protected void handleGet() { + // TODO Auto-generated method stub + } + + @Override + protected void handleGetAll() { + // TODO Auto-generated method stub + } + + @Override + protected void handleUpdate() { + // TODO Auto-generated method stub + } + + @Override + protected void handleDelete() { + // TODO Auto-generated method stub } - @Override - protected void handleGet() { - // TODO Auto-generated method stub - - } - - @Override - protected void handleGetAll() { - // TODO Auto-generated method stub - - } - - @Override - protected void handleUpdate() { - // TODO Auto-generated method stub - - } - - @Override - protected void handleDelete() { - // TODO Auto-generated method stub - - } + private boolean hasObjectCsid(RelationsCommon relationsCommon) { + String objectCsid = relationsCommon.getObjectCsid(); + return hasCsid(objectCsid); + } + + private boolean hasSubjectCsid(RelationsCommon relationsCommon) { + String subjectCsid = relationsCommon.getSubjectCsid(); + return hasCsid(subjectCsid); + } + private boolean hasCsid(String csid) { + boolean hasCsid = false; + if (csid != null && Tools.notBlank(csid)) { + hasCsid = true; + } + return hasCsid; + } + + private boolean hasObjectRefname(RelationsCommon relationsCommon) { + String objectRefName = relationsCommon.getObjectRefName(); + return hasRefName(objectRefName); + } + + private boolean hasSubjectRefname(RelationsCommon relationsCommon) { + String subjectRefName = relationsCommon.getSubjectRefName(); + return hasRefName(subjectRefName); + } + + private boolean hasRefName(String refName) { + boolean hasRefname = false; + if (refName == null || Tools.isBlank(refName)) { + return hasRefname; + } else { + Authority authority = Authority.parse(refName); + AuthorityItem authItem = AuthorityItem.parse(refName); + if (authority != null || authItem != null) { + hasRefname = true; + } + return hasRefname; + } + + } } -- 2.47.3