]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
048da5ab694719b91a62bf9e9317d43848396944
[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 = getSubjectCsid(relationsCommon);\r
42             String objectCsid = getObjectCsid(relationsCommon);\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 String getSubjectCsid(RelationsCommon relationsCommon) {\r
88         String subjectCsid = relationsCommon.getSubjectCsid();\r
89         // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:\r
90         if (Tools.isBlank(subjectCsid)) {\r
91             subjectCsid = relationsCommon.getDocumentId1();\r
92         }\r
93         return subjectCsid;\r
94     }\r
95 \r
96     private String getObjectCsid(RelationsCommon relationsCommon) {\r
97         String objectCsid = relationsCommon.getObjectCsid();\r
98         // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:\r
99         if (Tools.isBlank(objectCsid)) {\r
100             objectCsid = relationsCommon.getDocumentId2();\r
101 }\r
102         return objectCsid;\r
103     }\r
104 \r
105     private boolean hasCsid(String csid) {\r
106         boolean hasCsid = false;\r
107         if (Tools.notBlank(csid)) {\r
108             hasCsid = true;\r
109         }\r
110         return hasCsid;\r
111     }\r
112 \r
113     private boolean hasSubjectRefname(RelationsCommon relationsCommon) {\r
114         String subjectRefName = relationsCommon.getSubjectRefName();\r
115         return hasRefName(subjectRefName);\r
116     }\r
117 \r
118     private boolean hasObjectRefname(RelationsCommon relationsCommon) {\r
119         String objectRefName = relationsCommon.getObjectRefName();\r
120         return hasRefName(objectRefName);\r
121     }\r
122 \r
123     private boolean hasRefName(String refName) {\r
124         boolean hasRefname = false;\r
125         if (Tools.isBlank(refName)) {\r
126             return hasRefname;\r
127         } else {\r
128             Authority authority = Authority.parse(refName);\r
129             AuthorityItem authItem = AuthorityItem.parse(refName);\r
130             if (authority != null || authItem != null) {\r
131                 hasRefname = true;\r
132             }\r
133             return hasRefname;\r
134         }\r
135 \r
136     }\r
137 }\r