]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
115994d9a7fadcab2ea01f27c9a4aa218048571a
[tmp/jakarta-migration.git] /
1 /**     \r
2  * RelationUtilsNuxeoImpl.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.common.relation.nuxeo;\r
28 \r
29 import java.io.IOException;\r
30 import java.io.Serializable;\r
31 import java.util.ArrayList;\r
32 import java.util.List;\r
33 import java.util.Map;\r
34 \r
35 import org.collectionspace.services.common.relation.RelationJAXBSchema;\r
36 import org.collectionspace.services.common.relation.RelationListItemJAXBSchema;\r
37 import org.collectionspace.services.common.relation.RelationUtils;\r
38 \r
39 import org.collectionspace.services.relation.Relation;\r
40 import org.collectionspace.services.relation.RelationshipType;\r
41 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
42 import org.collectionspace.services.common.repository.DocumentException;\r
43 \r
44 import org.dom4j.Document;\r
45 import org.dom4j.Element;\r
46 import org.dom4j.dom.DOMElement;\r
47 import org.dom4j.dom.DOMDocument;\r
48 import org.dom4j.dom.DOMDocumentFactory;\r
49 //import org.dom4j.DocumentException;\r
50 \r
51 import org.nuxeo.common.utils.IdUtils;\r
52 import org.nuxeo.ecm.core.api.ClientException;\r
53 import org.nuxeo.ecm.core.api.DocumentModel;\r
54 import org.nuxeo.ecm.core.api.DocumentModelList;\r
55 import org.nuxeo.ecm.core.api.model.DocumentPart;\r
56 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
57 import org.slf4j.Logger;\r
58 import org.slf4j.LoggerFactory;\r
59 //import org.w3c.dom.DOMException;\r
60 //import org.w3c.dom.Element;\r
61 \r
62 /**\r
63  * The Class RelationUtilsNuxeoImpl.\r
64  */\r
65 public class RelationUtilsNuxeoImpl implements RelationUtils {\r
66         \r
67         /** The C s_ relatio n_ servic e_ name. */\r
68         static public String CS_RELATION_SERVICE_NAME = "relations";\r
69         static public String CS_EMPTY_STRING = "";\r
70         \r
71         /** The Constant REL_NUXEO_DOCTYPE. */\r
72         final public static String REL_NUXEO_DOCTYPE = "Relation";\r
73         \r
74         /** The Constant REL_NUXEO_SCHEMA_NAME. */\r
75         final public static String REL_NUXEO_SCHEMA_NAME = "relation";\r
76         \r
77         /** The Constant REL_NUXEO_SCHEMA_ROOT_ELEMENT. */\r
78         final public static String REL_NUXEO_SCHEMA_ROOT_ELEMENT = "relationtype";\r
79         \r
80         /** The Constant REL_NUXEO_DC_TITLE. */\r
81         final public static String REL_NUXEO_DC_TITLE = "CollectionSpace-Relation";\r
82         \r
83         /** The logger. */\r
84         private static Logger logger = LoggerFactory\r
85         .getLogger(RelationUtilsNuxeoImpl.class);       \r
86 \r
87 \r
88         /**\r
89          * Fill relation from doc model.\r
90          * \r
91          * @param relation the relation\r
92          * @param relDocModel the rel doc model\r
93          * \r
94          * @throws ClientException the client exception\r
95          */\r
96         static public void fillRelationFromDocModel(Relation relation, DocumentModel relDocModel)\r
97                         throws ClientException {\r
98                 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
99                 Object valueObject = null;\r
100 \r
101                 relation.setCsid(relDocModel.getId());\r
102 \r
103                 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
104                                 + RelationJAXBSchema.DOCUMENT_ID_1);\r
105                 relation.setDocumentId1((String) valueObject);\r
106 \r
107                 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
108                                 + RelationJAXBSchema.DOCUMENT_TYPE_1);\r
109                 relation.setDocumentType1((String) valueObject);\r
110 \r
111                 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
112                                 + RelationJAXBSchema.DOCUMENT_ID_2);\r
113                 relation.setDocumentId2((String) valueObject);\r
114 \r
115                 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
116                                 + RelationJAXBSchema.DOCUMENT_TYPE_1);\r
117                 relation.setDocumentType2((String) valueObject);\r
118 \r
119                 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
120                                 + RelationJAXBSchema.RELATIONSHIP_TYPE);\r
121                 relation.setRelationshipType(RelationshipType\r
122                                 .fromValue((String) valueObject));\r
123 \r
124                 if (logger.isDebugEnabled() == true) {\r
125                         System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");\r
126                         System.out.println(relation.toString());\r
127                         System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");\r
128                 }\r
129         }\r
130 \r
131         /**\r
132          * Fill doc model from relation.\r
133          * \r
134          * @param p the p\r
135          * @param relDocModel the rel doc model\r
136          * \r
137          * @throws Exception the exception\r
138          */\r
139         static public void fillDocModelFromRelation(Relation p, DocumentModel relDocModel)\r
140                         throws Exception {\r
141 \r
142                 // set the DublinCore title (this works)\r
143                 relDocModel.setPropertyValue("dublincore:title", "default title");\r
144 \r
145                 // // set value for <documentType1> element\r
146                 // try {\r
147                 // relDocModel.setProperty("relation", "/relationtype/documentId1",\r
148                 // "docId1");\r
149                 // } catch (Exception x) {\r
150                 // x.printStackTrace();\r
151                 // }\r
152 \r
153                 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
154                 if (p.getDocumentId1() != null) {\r
155                         String property = xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1;\r
156                         relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p\r
157                                         .getDocumentId1());\r
158                 }\r
159                 if (p.getDocumentType1() != null) {\r
160                         String property = xpathRoot + RelationJAXBSchema.DOCUMENT_TYPE_1;\r
161                         relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p\r
162                                         .getDocumentType1());\r
163                 }\r
164                 if (p.getDocumentId2() != null) {\r
165                         String property = xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2;\r
166                         relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p\r
167                                         .getDocumentId2());\r
168                 }\r
169                 if (p.getDocumentType2() != null) {\r
170                         String property = xpathRoot + "/"\r
171                                         + RelationJAXBSchema.DOCUMENT_TYPE_2;\r
172                         relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p\r
173                                         .getDocumentType2());\r
174                 }\r
175 \r
176                 if (p.getRelationshipType() != null) {\r
177                         String property = xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE;\r
178                         relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p\r
179                                         .getRelationshipType().value());\r
180                 }\r
181         }\r
182         \r
183         /**\r
184          * Prints the document model.\r
185          * \r
186          * @param documentModel the document model\r
187          */\r
188         static public void printDocumentModel(DocumentModel documentModel) {\r
189                 System.out.println(documentModel);\r
190         }\r
191         \r
192         /**\r
193          * Describe document model.\r
194          * \r
195          * @param docModel the doc model\r
196          * \r
197          * @throws Exception the exception\r
198          */\r
199         static private void describeDocumentModel(DocumentModel docModel) throws Exception {\r
200                 String[] schemas = docModel.getDeclaredSchemas();\r
201                 for (int i = 0; schemas != null && i < schemas.length; i++) {\r
202                         System.out.println("Schema-" + i + "=" + schemas[i]);\r
203                 }\r
204                 \r
205                 DocumentPart[] parts = docModel.getParts();\r
206                 Map<String,Serializable> propertyValues = null;\r
207                 for (int i = 0; parts != null && i < parts.length; i++) {\r
208                         System.out.println("Part-" + i + " name =" + parts[i].getName());\r
209                         System.out.println("Part-" + i + " path =" + parts[i].getPath());\r
210                         System.out.println("Part-" + i + " schema =" + parts[i].getSchema().getName());\r
211                         propertyValues = parts[i].exportValues();\r
212                 }\r
213 \r
214         }\r
215 \r
216         /**\r
217          * Creates the relationship.\r
218          * \r
219          * @param nuxeoRepoSession the nuxeo repo session\r
220          * @param newRelation the new relation\r
221          * \r
222          * @return the document model\r
223          * \r
224          * @throws DocumentException the document exception\r
225          */\r
226         static public DocumentModel createRelationship(Object nuxeoRepoSession, Relation newRelation)\r
227                         throws DocumentException {\r
228                 DocumentModel result = null;\r
229                 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
230                 \r
231                 try {\r
232                         // get the Nuxeo 'Relations' workspace\r
233                         DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,\r
234                                         CS_RELATION_SERVICE_NAME);\r
235                         \r
236                 String docType = REL_NUXEO_DOCTYPE;\r
237                 String id = IdUtils.generateId("New " + docType);\r
238                 \r
239                 //create document model\r
240                 String workspacePath = workspaceModel.getPathAsString();            \r
241                 DocumentModel newRelDocModel = repoSession.createDocumentModel(workspacePath, id, docType);\r
242         \r
243                 newRelation.setCsid(newRelDocModel.getId());\r
244                 fillDocModelFromRelation(newRelation, newRelDocModel);\r
245                 \r
246                 //create document with the new DocumentModel\r
247                 result = repoSession.createDocument(newRelDocModel);\r
248                 repoSession.save();\r
249                 } catch (Exception e) {\r
250                         e.printStackTrace();\r
251                         throw new DocumentException(e);\r
252                 }\r
253                 \r
254                 return result;\r
255         }\r
256         \r
257         /* (non-Javadoc)\r
258          * @see org.collectionspace.services.common.RelationUtils#createRelationship(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)\r
259          */\r
260         public Relation createRelationship(Object nuxeoRepoSession, String subjectCsid, String predicate,\r
261                         String objectCsid) throws DocumentException {\r
262                 Relation result = null;\r
263                 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
264                 \r
265         Relation temp = new Relation();\r
266         temp.setDocumentId1(subjectCsid);\r
267         temp.setRelationshipType(null);\r
268         temp.setDocumentId2(objectCsid);\r
269         createRelationship(repoSession, temp);\r
270                 \r
271                 return result;\r
272         }\r
273         \r
274         /* (non-Javadoc)\r
275          * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object)\r
276          */\r
277         public List<Relation> getRelationships(Object nuxeoRepoSession)\r
278                         throws DocumentException {\r
279                 List<Relation> result = null;\r
280                 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
281 \r
282                 try {\r
283                         DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
284                                         repoSession, CS_RELATION_SERVICE_NAME);\r
285                         DocumentModelList children = repoSession.getChildren(relationWorkspace\r
286                                         .getRef());\r
287                         \r
288                         result = new ArrayList<Relation>();\r
289                         Relation relation = null;\r
290                         for (DocumentModel child : children) {\r
291                                 relation = new Relation();\r
292                                 fillRelationFromDocModel(relation, child);\r
293                                 result.add(relation);\r
294                         }\r
295                 } catch (Exception e) {\r
296                         e.printStackTrace();\r
297                         throw new DocumentException(e);\r
298                 }\r
299 \r
300                 return result;\r
301         }\r
302         \r
303         /* (non-Javadoc)\r
304          * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String)\r
305          */\r
306         public List<Relation> getRelationships(Object nuxeoRepoSession, String csid)\r
307                         throws DocumentException {\r
308                 List<Relation> result = null;\r
309                 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
310                                 \r
311                 try {\r
312                         DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
313                                         repoSession, CS_RELATION_SERVICE_NAME);\r
314                         DocumentModelList children = repoSession.getChildren(relationWorkspace\r
315                                         .getRef());\r
316                         \r
317                         result = new ArrayList<Relation>();\r
318                         Relation relation = null;\r
319                         for (DocumentModel child : children) {\r
320                                 if ((isSubjectOfRelation(csid, child) == true) ||\r
321                                                 (isObjectOfRelation(csid, child) == true)) {\r
322                                         relation = new Relation();\r
323                                         fillRelationFromDocModel(relation, child);\r
324                                         result.add(relation);\r
325                                 }\r
326                         }\r
327                 } catch (Exception e) {\r
328                         e.printStackTrace();\r
329                         throw new DocumentException(e);\r
330                 }\r
331                 \r
332                 return result;\r
333         }\r
334         \r
335         \r
336         /* (non-Javadoc)\r
337          * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)\r
338          */\r
339         public List<Relation> getRelationships(Object nuxeoRepoSession,\r
340                         String subjectCsid, \r
341                         String predicate, \r
342                         String objectCsid) throws DocumentException {\r
343                 List<Relation> result = null;\r
344                 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
345                 \r
346                 try {\r
347                         DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
348                                         repoSession, CS_RELATION_SERVICE_NAME);\r
349                         DocumentModelList children = repoSession.getChildren(relationWorkspace\r
350                                         .getRef());\r
351                         \r
352                         result = new ArrayList<Relation>();\r
353                         Relation relation = null;\r
354                         for (DocumentModel child : children) {\r
355                                 if (isQueryMatch(child, subjectCsid, predicate, objectCsid) == true) {\r
356                                         relation = new Relation();\r
357                                         fillRelationFromDocModel(relation, child);\r
358                                         result.add(relation);                   }\r
359                         }\r
360                 } catch (Exception e) {\r
361                         e.printStackTrace();\r
362                         throw new DocumentException(e);\r
363                 }\r
364                 \r
365                 return result;\r
366         }\r
367         \r
368         public String getQPropertyName(String propertyName) {\r
369                 return "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/" + propertyName;\r
370         }\r
371                 \r
372         /**\r
373          * Checks if is subject of relation.\r
374          * \r
375          * @param csid the csid\r
376          * @param documentModel the document model\r
377          * \r
378          * @return true, if is subject of relation\r
379          * \r
380          * @throws ClientException the client exception\r
381          */\r
382         private boolean isSubjectOfRelation(String csid, DocumentModel documentModel)\r
383                         throws ClientException {\r
384                 boolean result = false;\r
385                 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
386 \r
387                 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
388                                 + RelationJAXBSchema.DOCUMENT_ID_1);\r
389                 if (valueObject != null && csid != null) {\r
390                         String subjectID = (String) valueObject;\r
391                         result = subjectID.equals(csid);\r
392                 }\r
393                 \r
394                 return result;\r
395         }\r
396 \r
397         /**\r
398          * Checks if is object of relation.\r
399          * \r
400          * @param csid the csid\r
401          * @param documentModel the document model\r
402          * \r
403          * @return true, if is object of relation\r
404          * \r
405          * @throws ClientException the client exception\r
406          */\r
407         private boolean isObjectOfRelation(String csid, DocumentModel documentModel)\r
408                         throws ClientException {\r
409                 boolean result = false;\r
410                 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
411 \r
412                 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
413                                 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);\r
414                 if (valueObject != null  && csid != null) {\r
415                         String subjectID = (String) valueObject;\r
416                         result = subjectID.equals(csid);\r
417                 }\r
418 \r
419                 return result;\r
420         }\r
421         \r
422         private boolean isPredicateOfRelation(String predicate,\r
423                         DocumentModel documentModel) throws ClientException {\r
424                 boolean result = false;\r
425                 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
426 \r
427                 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
428                                 xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE);\r
429                 if (valueObject != null  && predicate != null) {\r
430                         String relationType = (String) valueObject;\r
431                         result = predicate.equalsIgnoreCase(relationType);\r
432                 }\r
433 \r
434                 return result;\r
435         }\r
436 \r
437         /**\r
438          * Gets the object ID from of the (Subject-Predicate-Object) relationship.\r
439          * \r
440          * @param csid the ID of the 'Subject' (Subject-Predicate-Object)\r
441          * @param documentModel represents the relation entry.\r
442          * \r
443          * @return the object csid of the relationship\r
444          * \r
445          * @throws ClientException the client exception\r
446          */\r
447         private String getObjectFromSubject(String csid, DocumentModel documentModel)\r
448                         throws ClientException {\r
449                 String result = null;\r
450                 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
451 \r
452                 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
453                                 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1);\r
454                 if (valueObject != null) {\r
455                         String subjectID = (String) valueObject;\r
456                         if (subjectID.equals(csid) == true) {\r
457                                 valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
458                                                 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);\r
459                                 if (valueObject != null) {\r
460                                         result = (String) valueObject;\r
461                                 }\r
462                         }\r
463                 }\r
464 \r
465                 return result;\r
466         }\r
467         \r
468         static public Document getDocument(List<Relation> relationList)\r
469                         throws DocumentException {\r
470                 DOMDocumentFactory domfactory = new DOMDocumentFactory();\r
471                 DOMDocument result = (DOMDocument) domfactory.createDocument();\r
472 \r
473                 try {\r
474                         // setup the root element\r
475                         DOMElement root = (DOMElement) result\r
476                                         .createElement(RelationListItemJAXBSchema.REL_ROOT_ELEM_NAME);\r
477                         result.setRootElement((org.dom4j.Element) root);\r
478 \r
479                         // populate the document with child elements\r
480                         for (Relation child : relationList) {\r
481                                 DOMElement el = (DOMElement) result.createElement(RelationJAXBSchema.REL_ROOT_ELEM_NAME);\r
482                                 el.setAttribute(RelationListItemJAXBSchema.CSID, child\r
483                                                 .getCsid());\r
484                                 el.setAttribute(RelationListItemJAXBSchema.URI, getRelURL(\r
485                                                 CS_RELATION_SERVICE_NAME, child.getCsid()));\r
486 \r
487                                 if (logger.isDebugEnabled() == true) {\r
488                                         System.out.println(el.asXML());\r
489                                 }\r
490                                 \r
491                                 root.appendChild(el);\r
492                         }\r
493                 } catch (Exception e) {\r
494                         if (logger.isDebugEnabled()) {\r
495                                 logger.debug("Caught exception ", e);\r
496                         }\r
497                         throw new DocumentException(e);\r
498                 }\r
499 \r
500                 if (logger.isDebugEnabled() == true) {\r
501                         System.out.println(result.asXML());\r
502                 }\r
503 \r
504                 return result;\r
505         }\r
506         \r
507         private boolean isQueryMatch(DocumentModel documentModel,\r
508                         String subjectCsid,\r
509                         String predicate,\r
510                         String objectCsid) throws ClientException {\r
511                 boolean result = true;\r
512                 \r
513                 block: {\r
514                         if (subjectCsid != null) {\r
515                                 if (isSubjectOfRelation(subjectCsid, documentModel) == false) {\r
516                                         result = false;\r
517                                         break block;\r
518                                 }\r
519                         }\r
520                         if (predicate != null) {\r
521                                 if (isPredicateOfRelation(predicate, documentModel) == false) {\r
522                                         result = false;\r
523                                         break block;\r
524                                 }\r
525                         }\r
526                         if (objectCsid != null) {\r
527                                 if (isObjectOfRelation(objectCsid, documentModel) == false) {\r
528                                         result = false;\r
529                                         break block;\r
530                                 }\r
531                         }\r
532                 }\r
533                 \r
534                 return result;\r
535         }\r
536         \r
537     private static String getRelURL(String repo, String uuid) {\r
538         return '/' + repo + '/' + uuid;\r
539     }   \r
540         \r
541 }\r