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