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.List;
\r
33 import java.util.Map;
\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
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
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
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
63 * The Class RelationUtilsNuxeoImpl.
\r
65 public class RelationUtilsNuxeoImpl implements RelationUtils {
\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
71 /** The Constant REL_NUXEO_DOCTYPE. */
\r
72 final public static String REL_NUXEO_DOCTYPE = "Relation";
\r
74 /** The Constant REL_NUXEO_SCHEMA_NAME. */
\r
75 final public static String REL_NUXEO_SCHEMA_NAME = "relation";
\r
77 /** The Constant REL_NUXEO_SCHEMA_ROOT_ELEMENT. */
\r
78 final public static String REL_NUXEO_SCHEMA_ROOT_ELEMENT = "relationtype";
\r
80 /** The Constant REL_NUXEO_DC_TITLE. */
\r
81 final public static String REL_NUXEO_DC_TITLE = "CollectionSpace-Relation";
\r
84 private static Logger logger = LoggerFactory
\r
85 .getLogger(RelationUtilsNuxeoImpl.class);
\r
89 * Fill relation from doc model.
\r
91 * @param relation the relation
\r
92 * @param relDocModel the rel doc model
\r
94 * @throws ClientException the client exception
\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
101 relation.setCsid(relDocModel.getId());
\r
103 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
104 + RelationJAXBSchema.DOCUMENT_ID_1);
\r
105 relation.setDocumentId1((String) valueObject);
\r
107 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
108 + RelationJAXBSchema.DOCUMENT_TYPE_1);
\r
109 relation.setDocumentType1((String) valueObject);
\r
111 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
112 + RelationJAXBSchema.DOCUMENT_ID_2);
\r
113 relation.setDocumentId2((String) valueObject);
\r
115 valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
116 + RelationJAXBSchema.DOCUMENT_TYPE_1);
\r
117 relation.setDocumentType2((String) valueObject);
\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
124 if (logger.isDebugEnabled() == true) {
\r
125 System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
\r
126 System.out.println(relation.toString());
\r
127 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
\r
132 * Fill doc model from relation.
\r
135 * @param relDocModel the rel doc model
\r
137 * @throws Exception the exception
\r
139 static public void fillDocModelFromRelation(Relation p, DocumentModel relDocModel)
\r
142 // set the DublinCore title (this works)
\r
143 relDocModel.setPropertyValue("dublincore:title", "default title");
\r
145 // // set value for <documentType1> element
\r
147 // relDocModel.setProperty("relation", "/relationtype/documentId1",
\r
149 // } catch (Exception x) {
\r
150 // x.printStackTrace();
\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
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
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
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
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
184 * Prints the document model.
\r
186 * @param documentModel the document model
\r
188 static public void printDocumentModel(DocumentModel documentModel) {
\r
189 System.out.println(documentModel);
\r
193 * Describe document model.
\r
195 * @param docModel the doc model
\r
197 * @throws Exception the exception
\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
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
217 * Creates the relationship.
\r
219 * @param nuxeoRepoSession the nuxeo repo session
\r
220 * @param newRelation the new relation
\r
222 * @return the document model
\r
224 * @throws DocumentException the document exception
\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
232 // get the Nuxeo 'Relations' workspace
\r
233 DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,
\r
234 CS_RELATION_SERVICE_NAME);
\r
236 String docType = REL_NUXEO_DOCTYPE;
\r
237 String id = IdUtils.generateId("New " + docType);
\r
239 //create document model
\r
240 String workspacePath = workspaceModel.getPathAsString();
\r
241 DocumentModel newRelDocModel = repoSession.createDocumentModel(workspacePath, id, docType);
\r
243 newRelation.setCsid(newRelDocModel.getId());
\r
244 fillDocModelFromRelation(newRelation, newRelDocModel);
\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
258 * @see org.collectionspace.services.common.RelationUtils#createRelationship(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)
\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
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
275 * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object)
\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
283 DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(
\r
284 repoSession, CS_RELATION_SERVICE_NAME);
\r
285 DocumentModelList children = repoSession.getChildren(relationWorkspace
\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
295 } catch (Exception e) {
\r
296 e.printStackTrace();
\r
297 throw new DocumentException(e);
\r
304 * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String)
\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
312 DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(
\r
313 repoSession, CS_RELATION_SERVICE_NAME);
\r
314 DocumentModelList children = repoSession.getChildren(relationWorkspace
\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
327 } catch (Exception e) {
\r
328 e.printStackTrace();
\r
329 throw new DocumentException(e);
\r
337 * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)
\r
339 public List<Relation> getRelationships(Object nuxeoRepoSession,
\r
340 String subjectCsid,
\r
342 String objectCsid) throws DocumentException {
\r
343 List<Relation> result = null;
\r
344 RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;
\r
347 DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(
\r
348 repoSession, CS_RELATION_SERVICE_NAME);
\r
349 DocumentModelList children = repoSession.getChildren(relationWorkspace
\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
360 } catch (Exception e) {
\r
361 e.printStackTrace();
\r
362 throw new DocumentException(e);
\r
368 public String getQPropertyName(String propertyName) {
\r
369 return "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/" + propertyName;
\r
373 * Checks if is subject of relation.
\r
375 * @param csid the csid
\r
376 * @param documentModel the document model
\r
378 * @return true, if is subject of relation
\r
380 * @throws ClientException the client exception
\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
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
398 * Checks if is object of relation.
\r
400 * @param csid the csid
\r
401 * @param documentModel the document model
\r
403 * @return true, if is object of relation
\r
405 * @throws ClientException the client exception
\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
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
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
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
438 * Gets the object ID from of the (Subject-Predicate-Object) relationship.
\r
440 * @param csid the ID of the 'Subject' (Subject-Predicate-Object)
\r
441 * @param documentModel represents the relation entry.
\r
443 * @return the object csid of the relationship
\r
445 * @throws ClientException the client exception
\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
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
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
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
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
484 el.setAttribute(RelationListItemJAXBSchema.URI, getRelURL(
\r
485 CS_RELATION_SERVICE_NAME, child.getCsid()));
\r
487 if (logger.isDebugEnabled() == true) {
\r
488 System.out.println(el.asXML());
\r
491 root.appendChild(el);
\r
493 } catch (Exception e) {
\r
494 if (logger.isDebugEnabled()) {
\r
495 logger.debug("Caught exception ", e);
\r
497 throw new DocumentException(e);
\r
500 if (logger.isDebugEnabled() == true) {
\r
501 System.out.println(result.asXML());
\r
507 private boolean isQueryMatch(DocumentModel documentModel,
\r
508 String subjectCsid,
\r
510 String objectCsid) throws ClientException {
\r
511 boolean result = true;
\r
514 if (subjectCsid != null) {
\r
515 if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
\r
520 if (predicate != null) {
\r
521 if (isPredicateOfRelation(predicate, documentModel) == false) {
\r
526 if (objectCsid != null) {
\r
527 if (isObjectOfRelation(objectCsid, documentModel) == false) {
\r
537 private static String getRelURL(String repo, String uuid) {
\r
538 return '/' + repo + '/' + uuid;
\r