package org.collectionspace.services.relation.nuxeo;\r
\r
//import junit.framework.Assert;\r
-\r
import org.collectionspace.services.client.PoxPayloadIn;\r
import org.collectionspace.services.client.PoxPayloadOut;\r
import org.collectionspace.services.common.document.InvalidDocumentException;\r
import org.collectionspace.services.common.document.ValidatorHandlerImpl;\r
+import org.collectionspace.services.common.api.RefName.Authority;\r
+import org.collectionspace.services.common.api.RefName.AuthorityItem;\r
+import org.collectionspace.services.common.api.Tools;\r
import org.collectionspace.services.relation.RelationsCommon;\r
\r
import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
import org.slf4j.LoggerFactory;\r
//import org.testng.Assert;\r
\r
-public class RelationValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {\r
+public class RelationValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {\r
\r
/** The logger. */\r
private final Logger logger = LoggerFactory.getLogger(RelationValidatorHandler.class);\r
- \r
/* Error messages \r
*/\r
private static final String VALIDATION_ERROR = "The relation record payload was invalid. See log file for more details.";\r
- private static final String SUBJECT_EQUALS_PREDICATE_ERROR = "The subject ID and object ID cannot be the same.";\r
- \r
+ private static final String SUBJECT_EQUALS_OBJECT_ERROR = "The subject ID and object ID cannot be the same.";\r
+\r
@Override\r
protected Class<?> getCommonPartClass() {\r
- return RelationsCommon.class;\r
+ return RelationsCommon.class;\r
}\r
- \r
+\r
@Override\r
protected void handleCreate()\r
- throws InvalidDocumentException{\r
- try {\r
- RelationsCommon relationsCommon = (RelationsCommon)getCommonPart();\r
- assert(relationsCommon != null);\r
- if (logger.isTraceEnabled() == true) {\r
- logger.trace(relationsCommon.toString());\r
- }\r
- \r
- assert(relationsCommon.getDocumentId1() != null);\r
- assert(relationsCommon.getDocumentId1().length() != 0);\r
- \r
- assert(relationsCommon.getDocumentId2() != null);\r
- assert(relationsCommon.getDocumentId2().length() != 0);\r
- \r
- assert(relationsCommon.getRelationshipType() != null);\r
- //\r
- // Assert that the Subject ID and Predicate ID are not the same\r
- //\r
- assert(relationsCommon.getDocumentId1().equalsIgnoreCase(relationsCommon.getDocumentId2()) == false) : SUBJECT_EQUALS_PREDICATE_ERROR;\r
- } catch (AssertionError e) {\r
- if (logger.isErrorEnabled() == true) {\r
- logger.error(e.getMessage(), e);\r
- }\r
- throw new InvalidDocumentException(VALIDATION_ERROR, e);\r
- }\r
+ throws InvalidDocumentException {\r
+ try {\r
+ RelationsCommon relationsCommon = (RelationsCommon) getCommonPart();\r
+ assert (relationsCommon != null);\r
+ if (logger.isTraceEnabled() == true) {\r
+ logger.trace(relationsCommon.toString());\r
+ }\r
+\r
+ // If no CSID for a subject or object is included in the create payload,\r
+ // a refName must be provided for that subject or object as an alternate identifier.\r
+ assert (hasObjectCsid(relationsCommon) || hasObjectRefname(relationsCommon));\r
+ assert (hasSubjectCsid(relationsCommon) || hasSubjectRefname(relationsCommon));\r
+\r
+ // The Subject identifier and Object ID must not be identical:\r
+ // that is, a resource cannot be related to itself.\r
+ // FIXME: Can store values of calls above if desired to save additional checks here.\r
+ if (hasObjectCsid(relationsCommon) && hasSubjectCsid(relationsCommon)) {\r
+ assert (relationsCommon.getObjectCsid().trim().equalsIgnoreCase(\r
+ relationsCommon.getSubjectCsid().trim()) == false) :\r
+ SUBJECT_EQUALS_OBJECT_ERROR;\r
+ }\r
+\r
+ // A relationship type must be provided.\r
+ assert (relationsCommon.getRelationshipType() != null);\r
+\r
+ } catch (AssertionError e) {\r
+ if (logger.isErrorEnabled() == true) {\r
+ logger.error(e.getMessage(), e);\r
+ }\r
+ throw new InvalidDocumentException(VALIDATION_ERROR, e);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected void handleGet() {\r
+ // TODO Auto-generated method stub\r
+ }\r
+\r
+ @Override\r
+ protected void handleGetAll() {\r
+ // TODO Auto-generated method stub\r
+ }\r
+\r
+ @Override\r
+ protected void handleUpdate() {\r
+ // TODO Auto-generated method stub\r
+ }\r
+\r
+ @Override\r
+ protected void handleDelete() {\r
+ // TODO Auto-generated method stub\r
}\r
\r
- @Override\r
- protected void handleGet() {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
-\r
- @Override\r
- protected void handleGetAll() {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
-\r
- @Override\r
- protected void handleUpdate() {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
-\r
- @Override\r
- protected void handleDelete() {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
+ private boolean hasObjectCsid(RelationsCommon relationsCommon) {\r
+ String objectCsid = relationsCommon.getObjectCsid();\r
+ return hasCsid(objectCsid);\r
+ }\r
+\r
+ private boolean hasSubjectCsid(RelationsCommon relationsCommon) {\r
+ String subjectCsid = relationsCommon.getSubjectCsid();\r
+ return hasCsid(subjectCsid);\r
+ }\r
\r
+ private boolean hasCsid(String csid) {\r
+ boolean hasCsid = false;\r
+ if (csid != null && Tools.notBlank(csid)) {\r
+ hasCsid = true;\r
+ }\r
+ return hasCsid;\r
+ }\r
+\r
+ private boolean hasObjectRefname(RelationsCommon relationsCommon) {\r
+ String objectRefName = relationsCommon.getObjectRefName();\r
+ return hasRefName(objectRefName);\r
+ }\r
+\r
+ private boolean hasSubjectRefname(RelationsCommon relationsCommon) {\r
+ String subjectRefName = relationsCommon.getSubjectRefName();\r
+ return hasRefName(subjectRefName);\r
+ }\r
+\r
+ private boolean hasRefName(String refName) {\r
+ boolean hasRefname = false;\r
+ if (refName == null || Tools.isBlank(refName)) {\r
+ return hasRefname;\r
+ } else {\r
+ Authority authority = Authority.parse(refName);\r
+ AuthorityItem authItem = AuthorityItem.parse(refName);\r
+ if (authority != null || authItem != null) {\r
+ hasRefname = true;\r
+ }\r
+ return hasRefname;\r
+ }\r
+\r
+ }\r
}\r