]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dff09ae20a39463786d7677ee86fb84e93cc0fd4
[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     private boolean hasRefName(String refName) {
106         boolean hasRefname = false;
107         if (Tools.isBlank(refName)) {
108             return hasRefname;
109         } else {
110             Authority authority = Authority.parse(refName);
111             AuthorityItem authItem = AuthorityItem.parse(refName);
112             if (authority != null || authItem != null) {
113                 hasRefname = true;
114             }
115             return hasRefname;
116         }
117
118     }
119 }