2 * RelationUtilsNuxeoImpl.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\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
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\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
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.common.relation.nuxeo;
\r
29 import java.io.IOException;
\r
30 import java.io.Serializable;
\r
31 import java.util.ArrayList;
\r
32 import java.util.Iterator;
\r
33 import java.util.List;
\r
34 import java.util.Map;
\r
36 import org.collectionspace.services.common.relation.RelationJAXBSchema;
\r
37 import org.collectionspace.services.common.relation.RelationListItemJAXBSchema;
\r
38 import org.collectionspace.services.common.relation.IRelationsManager;
\r
40 import org.collectionspace.services.relation.Relation;
\r
41 import org.collectionspace.services.relation.RelationList;
\r
42 import org.collectionspace.services.relation.RelationshipType;
\r
43 import org.collectionspace.services.relation.RelationList.RelationListItem;
\r
44 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
45 import org.collectionspace.services.common.repository.DocumentException;
\r
47 import org.dom4j.Document;
\r
48 import org.dom4j.Element;
\r
49 import org.dom4j.dom.DOMElement;
\r
50 import org.dom4j.dom.DOMDocument;
\r
51 import org.dom4j.dom.DOMDocumentFactory;
\r
52 //import org.dom4j.DocumentException;
\r
54 import org.nuxeo.common.utils.IdUtils;
\r
55 import org.nuxeo.ecm.core.api.ClientException;
\r
56 import org.nuxeo.ecm.core.api.DocumentModel;
\r
57 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
58 import org.nuxeo.ecm.core.api.model.DocumentPart;
\r
59 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
60 import org.slf4j.Logger;
\r
61 import org.slf4j.LoggerFactory;
\r
62 //import org.w3c.dom.DOMException;
\r
63 //import org.w3c.dom.Element;
\r
66 * The Class RelationUtilsNuxeoImpl.
\r
68 public class RelationsManagerNuxeoImpl implements IRelationsManager {
\r
70 /** The C s_ relatio n_ servic e_ name. */
\r
71 static public String CS_RELATION_SERVICE_NAME = "relations";
\r
73 /** The C s_ empt y_ string. */
\r
74 static public String CS_EMPTY_STRING = "";
\r
76 /** The Constant REL_NUXEO_DOCTYPE. */
\r
77 final public static String REL_NUXEO_DOCTYPE = "Relation";
\r
79 /** The Constant REL_NUXEO_SCHEMA_NAME. */
\r
80 final public static String REL_NUXEO_SCHEMA_NAME = "relation";
\r
82 /** The Constant REL_NUXEO_SCHEMA_ROOT_ELEMENT. */
\r
83 final public static String REL_NUXEO_SCHEMA_ROOT_ELEMENT = "relationtype";
\r
85 /** The Constant REL_NUXEO_DC_TITLE. */
\r
86 final public static String REL_NUXEO_DC_TITLE = "CollectionSpace-Relation";
\r
89 private static Logger logger = LoggerFactory
\r
90 .getLogger(RelationsManagerNuxeoImpl.class);
\r
94 * Fill relation from doc model.
\r
96 * @param relation the relation
\r
97 * @param relDocModel the rel doc model
\r
99 * @throws ClientException the client exception
\r
101 static public void fillRelationFromDocModel(Relation relation, DocumentModel relDocModel)
\r
102 throws ClientException {
\r
103 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
104 Object valueObject = null;
\r
106 relation.setCsid(relDocModel.getId());
\r
108 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
109 + RelationJAXBSchema.DOCUMENT_ID_1);
\r
110 relation.setDocumentId1((String) valueObject);
\r
112 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
113 + RelationJAXBSchema.DOCUMENT_TYPE_1);
\r
114 relation.setDocumentType1((String) valueObject);
\r
116 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
117 + RelationJAXBSchema.DOCUMENT_ID_2);
\r
118 relation.setDocumentId2((String) valueObject);
\r
120 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
121 + RelationJAXBSchema.DOCUMENT_TYPE_2);
\r
122 relation.setDocumentType2((String) valueObject);
\r
124 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
125 + RelationJAXBSchema.RELATIONSHIP_TYPE);
\r
126 relation.setRelationshipType(RelationshipType
\r
127 .fromValue((String) valueObject));
\r
129 if (logger.isDebugEnabled() == true) {
\r
130 System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
\r
131 System.out.println(relation.toString());
\r
132 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
\r
137 * Fill relation list item from doc model.
\r
139 * @param relationListItem the relation list item
\r
140 * @param relDocModel the rel doc model
\r
142 * @throws DocumentException the document exception
\r
144 static public void fillRelationListItemFromDocModel(RelationListItem relationListItem,
\r
145 DocumentModel relDocModel)
\r
146 throws DocumentException {
\r
149 relationListItem.setCsid(
\r
150 relDocModel.getId());
\r
151 relationListItem.setUri(
\r
152 getRelURL(CS_RELATION_SERVICE_NAME, relDocModel.getId()));
\r
153 } catch (Exception e) {
\r
154 if (logger.isDebugEnabled()) {
\r
155 logger.debug("Caught exception in fillRelationListItemFromDocModel", e);
\r
157 throw new DocumentException(e);
\r
162 * Fill doc model list from relation list.
\r
164 * @param relationList the relation list
\r
165 * @param relDocModelList the rel doc model list
\r
167 * @throws Exception the exception
\r
169 static public void fillDocModelListFromRelationList(RelationList relationList,
\r
170 DocumentModelList relDocModelList)
\r
175 * Fill doc model from relation.
\r
178 * @param relDocModel the rel doc model
\r
180 * @throws Exception the exception
\r
182 static public void fillDocModelFromRelation(Relation p, DocumentModel relDocModel)
\r
185 // set the DublinCore title (this works)
\r
186 relDocModel.setPropertyValue("dublincore:title", "default title");
\r
188 // // set value for <documentType1> element
\r
190 // relDocModel.setProperty("relation", "/relationtype/documentId1",
\r
192 // } catch (Exception x) {
\r
193 // x.printStackTrace();
\r
196 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
197 if (p.getDocumentId1() != null) {
\r
198 String property = xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1;
\r
199 relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p
\r
200 .getDocumentId1());
\r
202 if (p.getDocumentType1() != null) {
\r
203 String property = xpathRoot + RelationJAXBSchema.DOCUMENT_TYPE_1;
\r
204 relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p
\r
205 .getDocumentType1());
\r
207 if (p.getDocumentId2() != null) {
\r
208 String property = xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2;
\r
209 relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p
\r
210 .getDocumentId2());
\r
212 if (p.getDocumentType2() != null) {
\r
213 String property = xpathRoot + "/"
\r
214 + RelationJAXBSchema.DOCUMENT_TYPE_2;
\r
215 relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p
\r
216 .getDocumentType2());
\r
219 if (p.getRelationshipType() != null) {
\r
220 String property = xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE;
\r
221 relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p
\r
222 .getRelationshipType().value());
\r
227 * Prints the document model.
\r
229 * @param documentModel the document model
\r
231 static public void printDocumentModel(DocumentModel documentModel) {
\r
232 System.out.println(documentModel);
\r
236 * Describe document model.
\r
238 * @param docModel the doc model
\r
240 * @throws Exception the exception
\r
242 static private void describeDocumentModel(DocumentModel docModel) throws Exception {
\r
243 String[] schemas = docModel.getDeclaredSchemas();
\r
244 for (int i = 0; schemas != null && i < schemas.length; i++) {
\r
245 System.out.println("Schema-" + i + "=" + schemas[i]);
\r
248 DocumentPart[] parts = docModel.getParts();
\r
249 Map<String,Serializable> propertyValues = null;
\r
250 for (int i = 0; parts != null && i < parts.length; i++) {
\r
251 System.out.println("Part-" + i + " name =" + parts[i].getName());
\r
252 System.out.println("Part-" + i + " path =" + parts[i].getPath());
\r
253 System.out.println("Part-" + i + " schema =" + parts[i].getSchema().getName());
\r
254 propertyValues = parts[i].exportValues();
\r
260 * Creates the relationship.
\r
262 * @param nuxeoRepoSession the nuxeo repo session
\r
263 * @param newRelation the new relation
\r
265 * @return the document model
\r
267 * @throws DocumentException the document exception
\r
269 static public DocumentModel createRelationship(Object nuxeoRepoSession, Relation newRelation)
\r
270 throws DocumentException {
\r
271 DocumentModel result = null;
\r
272 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;
\r
275 // get the Nuxeo 'Relations' workspace
\r
276 DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,
\r
277 CS_RELATION_SERVICE_NAME);
\r
279 String docType = REL_NUXEO_DOCTYPE;
\r
280 String id = IdUtils.generateId("New " + docType);
\r
282 //create document model
\r
283 String workspacePath = workspaceModel.getPathAsString();
\r
284 DocumentModel newRelDocModel = repoSession.createDocumentModel(workspacePath, id, docType);
\r
286 newRelation.setCsid(newRelDocModel.getId());
\r
287 fillDocModelFromRelation(newRelation, newRelDocModel);
\r
289 //create document with the new DocumentModel
\r
290 result = repoSession.createDocument(newRelDocModel);
\r
291 repoSession.save();
\r
292 } catch (Exception e) {
\r
293 e.printStackTrace();
\r
294 throw new DocumentException(e);
\r
301 * @see org.collectionspace.services.common.RelationUtils#createRelationship(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)
\r
303 public Relation createRelationship(Object nuxeoRepoSession, String subjectCsid, String predicate,
\r
304 String objectCsid) throws DocumentException {
\r
305 Relation result = null;
\r
306 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;
\r
308 Relation temp = new Relation();
\r
309 temp.setDocumentId1(subjectCsid);
\r
310 temp.setRelationshipType(null);
\r
311 temp.setDocumentId2(objectCsid);
\r
312 createRelationship(repoSession, temp);
\r
318 * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object)
\r
320 public List<Relation> getRelationships(Object nuxeoRepoSession)
\r
321 throws DocumentException {
\r
322 List<Relation> result = null;
\r
323 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;
\r
326 DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(
\r
327 repoSession, CS_RELATION_SERVICE_NAME);
\r
328 DocumentModelList children = repoSession.getChildren(relationWorkspace
\r
331 result = new ArrayList<Relation>();
\r
332 Relation relation = null;
\r
333 for (DocumentModel child : children) {
\r
334 relation = new Relation();
\r
335 fillRelationFromDocModel(relation, child);
\r
336 result.add(relation);
\r
338 } catch (Exception e) {
\r
339 e.printStackTrace();
\r
340 throw new DocumentException(e);
\r
347 * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String)
\r
349 public List<Relation> getRelationships(Object nuxeoRepoSession, String csid)
\r
350 throws DocumentException {
\r
351 List<Relation> result = null;
\r
352 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;
\r
355 DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(
\r
356 repoSession, CS_RELATION_SERVICE_NAME);
\r
357 DocumentModelList children = repoSession.getChildren(relationWorkspace
\r
360 result = new ArrayList<Relation>();
\r
361 Relation relation = null;
\r
362 for (DocumentModel child : children) {
\r
363 if ((isSubjectOfRelation(csid, child) == true) ||
\r
364 (isObjectOfRelation(csid, child) == true)) {
\r
365 relation = new Relation();
\r
366 fillRelationFromDocModel(relation, child);
\r
367 result.add(relation);
\r
370 } catch (Exception e) {
\r
371 e.printStackTrace();
\r
372 throw new DocumentException(e);
\r
380 * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)
\r
382 public List<Relation> getRelationships(Object nuxeoRepoSession,
\r
383 String subjectCsid,
\r
385 String objectCsid) throws DocumentException {
\r
386 List<Relation> result = null;
\r
387 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;
\r
390 DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(
\r
391 repoSession, CS_RELATION_SERVICE_NAME);
\r
392 DocumentModelList children = repoSession.getChildren(relationWorkspace
\r
395 result = new ArrayList<Relation>();
\r
396 Relation relation = null;
\r
397 for (DocumentModel child : children) {
\r
398 if (isQueryMatch(child, subjectCsid, predicate, objectCsid) == true) {
\r
399 relation = new Relation();
\r
400 fillRelationFromDocModel(relation, child);
\r
401 result.add(relation); }
\r
403 } catch (Exception e) {
\r
404 e.printStackTrace();
\r
405 throw new DocumentException(e);
\r
412 * @see org.collectionspace.services.common.relation.RelationUtils#getQPropertyName(java.lang.String)
\r
414 public String getQPropertyName(String propertyName) {
\r
415 return "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/" + propertyName;
\r
419 * Checks if is subject of relation.
\r
421 * @param csid the csid
\r
422 * @param documentModel the document model
\r
424 * @return true, if is subject of relation
\r
426 * @throws ClientException the client exception
\r
428 private boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
\r
429 throws ClientException {
\r
430 boolean result = false;
\r
431 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
433 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
434 + RelationJAXBSchema.DOCUMENT_ID_1);
\r
435 if (valueObject != null && csid != null) {
\r
436 String subjectID = (String) valueObject;
\r
437 result = subjectID.equals(csid);
\r
444 * Checks if is object of relation.
\r
446 * @param csid the csid
\r
447 * @param documentModel the document model
\r
449 * @return true, if is object of relation
\r
451 * @throws ClientException the client exception
\r
453 private boolean isObjectOfRelation(String csid, DocumentModel documentModel)
\r
454 throws ClientException {
\r
455 boolean result = false;
\r
456 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
458 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
459 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);
\r
460 if (valueObject != null && csid != null) {
\r
461 String subjectID = (String) valueObject;
\r
462 result = subjectID.equals(csid);
\r
469 * Checks if is predicate of relation.
\r
471 * @param predicate the predicate
\r
472 * @param documentModel the document model
\r
474 * @return true, if is predicate of relation
\r
476 * @throws ClientException the client exception
\r
478 private boolean isPredicateOfRelation(String predicate,
\r
479 DocumentModel documentModel) throws ClientException {
\r
480 boolean result = false;
\r
481 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
483 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
484 xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE);
\r
485 if (valueObject != null && predicate != null) {
\r
486 String relationType = (String) valueObject;
\r
487 result = predicate.equalsIgnoreCase(relationType);
\r
494 * Gets the object from subject.
\r
496 * @param csid the csid
\r
497 * @param documentModel the document model
\r
499 * @return the object from subject
\r
501 * @throws ClientException the client exception
\r
503 private String getObjectFromSubject(String csid, DocumentModel documentModel)
\r
504 throws ClientException {
\r
505 String result = null;
\r
506 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
508 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
509 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1);
\r
510 if (valueObject != null) {
\r
511 String subjectID = (String) valueObject;
\r
512 if (subjectID.equals(csid) == true) {
\r
513 valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
514 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);
\r
515 if (valueObject != null) {
\r
516 result = (String) valueObject;
\r
525 * Gets the document.
\r
527 * @param relationList the relation list
\r
529 * @return the document
\r
531 * @throws DocumentException the document exception
\r
533 static public Document getDocument(List<Relation> relationList)
\r
534 throws DocumentException {
\r
535 DOMDocumentFactory domfactory = new DOMDocumentFactory();
\r
536 DOMDocument result = (DOMDocument) domfactory.createDocument();
\r
539 // setup the root element
\r
540 DOMElement root = (DOMElement) result
\r
541 .createElement(RelationListItemJAXBSchema.REL_ROOT_ELEM_NAME);
\r
542 result.setRootElement((org.dom4j.Element) root);
\r
544 // populate the document with child elements
\r
545 for (Relation child : relationList) {
\r
546 DOMElement el = (DOMElement) result.createElement(RelationJAXBSchema.REL_ROOT_ELEM_NAME);
\r
547 el.setAttribute(RelationListItemJAXBSchema.CSID, child
\r
549 el.setAttribute(RelationListItemJAXBSchema.URI, getRelURL(
\r
550 CS_RELATION_SERVICE_NAME, child.getCsid()));
\r
552 if (logger.isDebugEnabled() == true) {
\r
553 System.out.println(el.asXML());
\r
556 root.appendChild(el);
\r
558 } catch (Exception e) {
\r
559 if (logger.isDebugEnabled()) {
\r
560 logger.debug("Caught exception ", e);
\r
562 throw new DocumentException(e);
\r
565 if (logger.isDebugEnabled() == true) {
\r
566 System.out.println(result.asXML());
\r
573 * Checks if is query match.
\r
575 * @param documentModel the document model
\r
576 * @param subjectCsid the subject csid
\r
577 * @param predicate the predicate
\r
578 * @param objectCsid the object csid
\r
580 * @return true, if is query match
\r
582 * @throws ClientException the client exception
\r
584 public boolean isQueryMatch(DocumentModel documentModel,
\r
585 String subjectCsid,
\r
587 String objectCsid) throws DocumentException {
\r
588 boolean result = true;
\r
592 if (subjectCsid != null) {
\r
593 if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
\r
598 if (predicate != null) {
\r
599 if (isPredicateOfRelation(predicate, documentModel) == false) {
\r
604 if (objectCsid != null) {
\r
605 if (isObjectOfRelation(objectCsid, documentModel) == false) {
\r
611 } catch (ClientException e) {
\r
612 if (logger.isDebugEnabled() == true) {
\r
613 e.printStackTrace();
\r
615 throw new DocumentException(e);
\r
622 * Gets the rel url.
\r
624 * @param repo the repo
\r
625 * @param uuid the uuid
\r
627 * @return the rel url
\r
629 private static String getRelURL(String repo, String uuid) {
\r
630 return '/' + repo + '/' + uuid;
\r