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.jboss.resteasy.plugins.providers.multipart.MultipartInput;
\r
14 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
16 import org.slf4j.Logger;
\r
17 import org.slf4j.LoggerFactory;
\r
18 //import org.testng.Assert;
\r
20 public class RelationValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {
\r
23 private final Logger logger = LoggerFactory.getLogger(RelationValidatorHandler.class);
\r
26 private static final String VALIDATION_ERROR = "The relation record payload was invalid. See log file for more details.";
\r
27 private static final String SUBJECT_EQUALS_OBJECT_ERROR = "The subject ID and object ID cannot be the same.";
\r
30 protected Class<?> getCommonPartClass() {
\r
31 return RelationsCommon.class;
\r
35 protected void handleCreate()
\r
36 throws InvalidDocumentException {
\r
38 RelationsCommon relationsCommon = (RelationsCommon) getCommonPart();
\r
39 assert (relationsCommon != null);
\r
40 if (logger.isTraceEnabled() == true) {
\r
41 logger.trace(relationsCommon.toString());
\r
44 String subjectCsid = getSubjectCsid(relationsCommon);
\r
45 String objectCsid = getObjectCsid(relationsCommon);
\r
47 // If no CSID for a subject or object is included in the create payload,
\r
48 // a refName must be provided for that subject or object as an alternate identifier.
\r
49 assert (hasCsid(subjectCsid) || hasSubjectRefname(relationsCommon));
\r
50 assert (hasCsid(objectCsid) || hasObjectRefname(relationsCommon));
\r
52 // The Subject identifier and Object ID must not be identical:
\r
53 // that is, a resource cannot be related to itself.
\r
54 if (hasCsid(subjectCsid) && hasCsid(objectCsid)) {
\r
55 assert (subjectCsid.trim().equalsIgnoreCase(objectCsid.trim()) == false) :
\r
56 SUBJECT_EQUALS_OBJECT_ERROR;
\r
59 // A relationship type must be provided.
\r
60 assert (relationsCommon.getRelationshipType() != null);
\r
62 } catch (AssertionError e) {
\r
63 if (logger.isErrorEnabled() == true) {
\r
64 logger.error(e.getMessage(), e);
\r
66 throw new InvalidDocumentException(VALIDATION_ERROR, e);
\r
71 protected void handleGet() {
\r
72 // TODO Auto-generated method stub
\r
76 protected void handleGetAll() {
\r
77 // TODO Auto-generated method stub
\r
81 protected void handleUpdate() {
\r
82 // TODO Auto-generated method stub
\r
86 protected void handleDelete() {
\r
87 // TODO Auto-generated method stub
\r
90 private String getSubjectCsid(RelationsCommon relationsCommon) {
\r
91 String subjectCsid = relationsCommon.getSubjectCsid();
\r
92 // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:
\r
93 if (Tools.isBlank(subjectCsid)) {
\r
94 subjectCsid = relationsCommon.getDocumentId1();
\r
99 private String getObjectCsid(RelationsCommon relationsCommon) {
\r
100 String objectCsid = relationsCommon.getObjectCsid();
\r
101 // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:
\r
102 if (Tools.isBlank(objectCsid)) {
\r
103 objectCsid = relationsCommon.getDocumentId2();
\r
108 private boolean hasCsid(String csid) {
\r
109 boolean hasCsid = false;
\r
110 if (Tools.notBlank(csid)) {
\r
116 private boolean hasSubjectRefname(RelationsCommon relationsCommon) {
\r
117 String subjectRefName = relationsCommon.getSubjectRefName();
\r
118 return hasRefName(subjectRefName);
\r
121 private boolean hasObjectRefname(RelationsCommon relationsCommon) {
\r
122 String objectRefName = relationsCommon.getObjectRefName();
\r
123 return hasRefName(objectRefName);
\r
126 private boolean hasRefName(String refName) {
\r
127 boolean hasRefname = false;
\r
128 if (Tools.isBlank(refName)) {
\r
131 Authority authority = Authority.parse(refName);
\r
132 AuthorityItem authItem = AuthorityItem.parse(refName);
\r
133 if (authority != null || authItem != null) {
\r