]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c93a8bc1e88e664380e065cd3cf8b9f3c1439bfd
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.relation.nuxeo;\r
2 \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
12 \r
13 import org.slf4j.Logger;\r
14 import org.slf4j.LoggerFactory;\r
15 //import org.testng.Assert;\r
16 \r
17 public class RelationValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut>  {\r
18 \r
19     /** The logger. */\r
20     private final Logger logger = LoggerFactory.getLogger(RelationValidatorHandler.class);\r
21     /* Error messages \r
22      */\r
23     private static final String VALIDATION_ERROR = "The relation record payload was invalid. See log file for more details.";\r
24     private static final String SUBJECT_EQUALS_OBJECT_ERROR = "The subject ID and object ID cannot be the same.";\r
25     \r
26     @Override\r
27     protected Class<?> getCommonPartClass() {\r
28         return RelationsCommon.class;\r
29     }\r
30     \r
31     @Override\r
32     protected void handleCreate()\r
33                 throws InvalidDocumentException{\r
34         try {\r
35                 RelationsCommon relationsCommon = (RelationsCommon)getCommonPart();\r
36                 CS_ASSERT(relationsCommon != null);\r
37                 if (logger.isTraceEnabled() == true) {\r
38                         logger.trace(relationsCommon.toString());\r
39                 }\r
40                 \r
41             String subjectCsid = relationsCommon.getSubjectCsid();\r
42             String objectCsid = relationsCommon.getObjectCsid();\r
43                 \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             CS_ASSERT(hasCsid(subjectCsid) || hasSubjectRefname(relationsCommon));\r
47             CS_ASSERT(hasCsid(objectCsid) || hasObjectRefname(relationsCommon));\r
48                 \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             if (hasCsid(subjectCsid) && hasCsid(objectCsid)) {\r
52                 CS_ASSERT (subjectCsid.trim().equalsIgnoreCase(objectCsid.trim()) == false,\r
53                         SUBJECT_EQUALS_OBJECT_ERROR);\r
54             }\r
55 \r
56             // A relationship type must be provided.\r
57             CS_ASSERT(relationsCommon.getRelationshipType() != null);\r
58 \r
59         } catch (AssertionError e) {\r
60                 if (logger.isErrorEnabled() == true) {\r
61                         logger.error(e.getMessage(), e);\r
62                 }\r
63                 throw new InvalidDocumentException(VALIDATION_ERROR, e);\r
64         }\r
65     }\r
66 \r
67         @Override\r
68         protected void handleGet() {\r
69                 // TODO Auto-generated method stub\r
70         }\r
71 \r
72         @Override\r
73         protected void handleGetAll() {\r
74                 // TODO Auto-generated method stub\r
75         }\r
76 \r
77         @Override\r
78         protected void handleUpdate() {\r
79                 // TODO Auto-generated method stub\r
80         }\r
81 \r
82         @Override\r
83         protected void handleDelete() {\r
84                 // TODO Auto-generated method stub\r
85     }\r
86 \r
87     private boolean hasCsid(String csid) {\r
88         boolean hasCsid = false;\r
89         if (Tools.notBlank(csid)) {\r
90             hasCsid = true;\r
91         }\r
92         return hasCsid;\r
93     }\r
94 \r
95     private boolean hasSubjectRefname(RelationsCommon relationsCommon) {\r
96         String subjectRefName = relationsCommon.getSubjectRefName();\r
97         return hasRefName(subjectRefName);\r
98     }\r
99 \r
100     private boolean hasObjectRefname(RelationsCommon relationsCommon) {\r
101         String objectRefName = relationsCommon.getObjectRefName();\r
102         return hasRefName(objectRefName);\r
103     }\r
104 \r
105     private boolean hasRefName(String refName) {\r
106         boolean hasRefname = false;\r
107         if (Tools.isBlank(refName)) {\r
108             return hasRefname;\r
109         } else {\r
110             Authority authority = Authority.parse(refName);\r
111             AuthorityItem authItem = AuthorityItem.parse(refName);\r
112             if (authority != null || authItem != null) {\r
113                 hasRefname = true;\r
114             }\r
115             return hasRefname;\r
116         }\r
117 \r
118     }\r
119 }\r