1 package org.collectionspace.services.relation.nuxeo;
\r
3 //import junit.framework.Assert;
\r
4 import org.collectionspace.services.client.PoxPayloadIn;
\r
5 import org.collectionspace.services.client.PoxPayloadOut;
\r
6 import org.collectionspace.services.common.document.InvalidDocumentException;
\r
7 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
\r
8 import org.collectionspace.services.common.api.RefName.Authority;
\r
9 import org.collectionspace.services.common.api.RefName.AuthorityItem;
\r
10 import org.collectionspace.services.common.api.Tools;
\r
11 import org.collectionspace.services.relation.RelationsCommon;
\r
13 import org.slf4j.Logger;
\r
14 import org.slf4j.LoggerFactory;
\r
15 //import org.testng.Assert;
\r
17 public class RelationValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {
\r
20 private final Logger logger = LoggerFactory.getLogger(RelationValidatorHandler.class);
\r
23 private static final String VALIDATION_ERROR = "The relation record payload was invalid. See log file for more details.";
\r
24 private static final String SUBJECT_EQUALS_OBJECT_ERROR = "The subject ID and object ID cannot be the same.";
\r
27 protected Class<?> getCommonPartClass() {
\r
28 return RelationsCommon.class;
\r
32 protected void handleCreate()
\r
33 throws InvalidDocumentException{
\r
35 RelationsCommon relationsCommon = (RelationsCommon)getCommonPart();
\r
36 CS_ASSERT(relationsCommon != null);
\r
37 if (logger.isTraceEnabled() == true) {
\r
38 logger.trace(relationsCommon.toString());
\r
41 String subjectCsid = relationsCommon.getSubjectCsid();
\r
42 String objectCsid = relationsCommon.getObjectCsid();
\r
44 // If no CSID for a subject or object is included in the create payload,
\r
45 // a refName must be provided for that subject or object as an alternate identifier.
\r
46 CS_ASSERT(hasCsid(subjectCsid) || hasSubjectRefname(relationsCommon));
\r
47 CS_ASSERT(hasCsid(objectCsid) || hasObjectRefname(relationsCommon));
\r
49 // The Subject identifier and Object ID must not be identical:
\r
50 // that is, a resource cannot be related to itself.
\r
51 if (hasCsid(subjectCsid) && hasCsid(objectCsid)) {
\r
52 CS_ASSERT (subjectCsid.trim().equalsIgnoreCase(objectCsid.trim()) == false,
\r
53 SUBJECT_EQUALS_OBJECT_ERROR);
\r
56 // A relationship type must be provided.
\r
57 CS_ASSERT(relationsCommon.getRelationshipType() != null);
\r
59 } catch (AssertionError e) {
\r
60 if (logger.isErrorEnabled() == true) {
\r
61 logger.error(e.getMessage(), e);
\r
63 throw new InvalidDocumentException(VALIDATION_ERROR, e);
\r
68 protected void handleGet() {
\r
69 // TODO Auto-generated method stub
\r
73 protected void handleGetAll() {
\r
74 // TODO Auto-generated method stub
\r
78 protected void handleUpdate() {
\r
79 // TODO Auto-generated method stub
\r
83 protected void handleDelete() {
\r
84 // TODO Auto-generated method stub
\r
87 private boolean hasCsid(String csid) {
\r
88 boolean hasCsid = false;
\r
89 if (Tools.notBlank(csid)) {
\r
95 private boolean hasSubjectRefname(RelationsCommon relationsCommon) {
\r
96 String subjectRefName = relationsCommon.getSubjectRefName();
\r
97 return hasRefName(subjectRefName);
\r
100 private boolean hasObjectRefname(RelationsCommon relationsCommon) {
\r
101 String objectRefName = relationsCommon.getObjectRefName();
\r
102 return hasRefName(objectRefName);
\r
105 private boolean hasRefName(String refName) {
\r
106 boolean hasRefname = false;
\r
107 if (Tools.isBlank(refName)) {
\r
110 Authority authority = Authority.parse(refName);
\r
111 AuthorityItem authItem = AuthorityItem.parse(refName);
\r
112 if (authority != null || authItem != null) {
\r