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 // 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 assert (hasObjectCsid(relationsCommon) || hasObjectRefname(relationsCommon));
\r
47 assert (hasSubjectCsid(relationsCommon) || hasSubjectRefname(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 // FIXME: Can store values of calls above if desired to save additional checks here.
\r
52 if (hasObjectCsid(relationsCommon) && hasSubjectCsid(relationsCommon)) {
\r
53 assert (relationsCommon.getObjectCsid().trim().equalsIgnoreCase(
\r
54 relationsCommon.getSubjectCsid().trim()) == false) :
\r
55 SUBJECT_EQUALS_OBJECT_ERROR;
\r
58 // A relationship type must be provided.
\r
59 assert (relationsCommon.getRelationshipType() != null);
\r
61 } catch (AssertionError e) {
\r
62 if (logger.isErrorEnabled() == true) {
\r
63 logger.error(e.getMessage(), e);
\r
65 throw new InvalidDocumentException(VALIDATION_ERROR, e);
\r
70 protected void handleGet() {
\r
71 // TODO Auto-generated method stub
\r
75 protected void handleGetAll() {
\r
76 // TODO Auto-generated method stub
\r
80 protected void handleUpdate() {
\r
81 // TODO Auto-generated method stub
\r
85 protected void handleDelete() {
\r
86 // TODO Auto-generated method stub
\r
89 private boolean hasObjectCsid(RelationsCommon relationsCommon) {
\r
90 String objectCsid = relationsCommon.getObjectCsid();
\r
91 return hasCsid(objectCsid);
\r
94 private boolean hasSubjectCsid(RelationsCommon relationsCommon) {
\r
95 String subjectCsid = relationsCommon.getSubjectCsid();
\r
96 return hasCsid(subjectCsid);
\r
99 private boolean hasCsid(String csid) {
\r
100 boolean hasCsid = false;
\r
101 if (csid != null && Tools.notBlank(csid)) {
\r
107 private boolean hasObjectRefname(RelationsCommon relationsCommon) {
\r
108 String objectRefName = relationsCommon.getObjectRefName();
\r
109 return hasRefName(objectRefName);
\r
112 private boolean hasSubjectRefname(RelationsCommon relationsCommon) {
\r
113 String subjectRefName = relationsCommon.getSubjectRefName();
\r
114 return hasRefName(subjectRefName);
\r
117 private boolean hasRefName(String refName) {
\r
118 boolean hasRefname = false;
\r
119 if (refName == null || Tools.isBlank(refName)) {
\r
122 Authority authority = Authority.parse(refName);
\r
123 AuthorityItem authItem = AuthorityItem.parse(refName);
\r
124 if (authority != null || authItem != null) {
\r