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