]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
90ffd2f0dc3b5056ecb6841f92cd261842317bb7
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.relation.nuxeo;
2
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;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 //import org.testng.Assert;
16
17 public class RelationValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut>  {
18
19     /** The logger. */
20     private final Logger logger = LoggerFactory.getLogger(RelationValidatorHandler.class);
21     /* Error messages 
22      */
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.";
25     
26     @Override
27     protected Class<?> getCommonPartClass() {
28         return RelationsCommon.class;
29     }
30     
31     @Override
32     protected void handleCreate()
33                 throws InvalidDocumentException{
34         try {
35                 RelationsCommon relationsCommon = (RelationsCommon)getCommonPart();
36                 CS_ASSERT(relationsCommon != null);
37                 if (logger.isTraceEnabled() == true) {
38                         logger.trace(relationsCommon.toString());
39                 }
40                 
41             String subjectCsid = relationsCommon.getSubjectCsid();
42             String objectCsid = relationsCommon.getObjectCsid();
43                 
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));
48                 
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);
54             }
55
56             // A relationship type must be provided.
57             CS_ASSERT(relationsCommon.getRelationshipType() != null);
58
59         } catch (AssertionError e) {
60                 if (logger.isErrorEnabled() == true) {
61                         logger.error(e.getMessage(), e);
62                 }
63                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
64         }
65     }
66
67         @Override
68         protected void handleGet() {
69                 // TODO Auto-generated method stub
70         }
71
72         @Override
73         protected void handleGetAll() {
74                 // TODO Auto-generated method stub
75         }
76
77         @Override
78         protected void handleUpdate() {
79                 // TODO Auto-generated method stub
80         }
81
82         @Override
83         protected void handleDelete() {
84                 // TODO Auto-generated method stub
85     }
86
87     private boolean hasCsid(String csid) {
88         boolean hasCsid = false;
89         if (Tools.notBlank(csid)) {
90             hasCsid = true;
91         }
92         return hasCsid;
93     }
94
95     private boolean hasSubjectRefname(RelationsCommon relationsCommon) {
96         String subjectRefName = relationsCommon.getSubjectRefName();
97         return hasRefName(subjectRefName);
98     }
99
100     private boolean hasObjectRefname(RelationsCommon relationsCommon) {
101         String objectRefName = relationsCommon.getObjectRefName();
102         return hasRefName(objectRefName);
103     }
104
105     /**
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.
107      * @param refName
108      * @return
109      */
110     private boolean hasRefName(String refName) {
111         boolean result = false; // assume it's not a valid refname
112         
113         if (Tools.isBlank(refName) == false) {
114                 try {
115                     Authority authority = Authority.parse(refName); // Will also parse refname to an object or procedure record
116                     if (authority != null) {
117                         result = true;
118                     } else {
119                         AuthorityItem authItem = AuthorityItem.parse(refName); // See if it is a refname to an authority item or vocabulary term
120                         result = authItem != null;
121                     }
122                 } catch (IllegalArgumentException e) {
123                         // Ignore exception
124                 }               
125         }
126
127         return result;
128     }
129 }