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.nuxeo.relation;
\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.relation.RelationJAXBSchema;
\r
36 import org.collectionspace.services.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
369 * Checks if is subject of relation.
\r
371 * @param csid the csid
\r
372 * @param documentModel the document model
\r
374 * @return true, if is subject of relation
\r
376 * @throws ClientException the client exception
\r
378 private boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
\r
379 throws ClientException {
\r
380 boolean result = false;
\r
381 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
383 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot
\r
384 + RelationJAXBSchema.DOCUMENT_ID_1);
\r
385 if (valueObject != null && csid != null) {
\r
386 String subjectID = (String) valueObject;
\r
387 result = subjectID.equals(csid);
\r
394 * Checks if is object of relation.
\r
396 * @param csid the csid
\r
397 * @param documentModel the document model
\r
399 * @return true, if is object of relation
\r
401 * @throws ClientException the client exception
\r
403 private boolean isObjectOfRelation(String csid, DocumentModel documentModel)
\r
404 throws ClientException {
\r
405 boolean result = false;
\r
406 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
408 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
409 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);
\r
410 if (valueObject != null && csid != null) {
\r
411 String subjectID = (String) valueObject;
\r
412 result = subjectID.equals(csid);
\r
418 private boolean isPredicateOfRelation(String predicate,
\r
419 DocumentModel documentModel) throws ClientException {
\r
420 boolean result = false;
\r
421 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
423 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
424 xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE);
\r
425 if (valueObject != null && predicate != null) {
\r
426 String relationType = (String) valueObject;
\r
427 result = predicate.equalsIgnoreCase(relationType);
\r
434 * Gets the object ID from of the (Subject-Predicate-Object) relationship.
\r
436 * @param csid the ID of the 'Subject' (Subject-Predicate-Object)
\r
437 * @param documentModel represents the relation entry.
\r
439 * @return the object csid of the relationship
\r
441 * @throws ClientException the client exception
\r
443 private String getObjectFromSubject(String csid, DocumentModel documentModel)
\r
444 throws ClientException {
\r
445 String result = null;
\r
446 String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";
\r
448 Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
449 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1);
\r
450 if (valueObject != null) {
\r
451 String subjectID = (String) valueObject;
\r
452 if (subjectID.equals(csid) == true) {
\r
453 valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,
\r
454 xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);
\r
455 if (valueObject != null) {
\r
456 result = (String) valueObject;
\r
464 static public Document getDocument(List<Relation> relationList)
\r
465 throws DocumentException {
\r
466 DOMDocumentFactory domfactory = new DOMDocumentFactory();
\r
467 DOMDocument result = (DOMDocument) domfactory.createDocument();
\r
470 // setup the root element
\r
471 DOMElement root = (DOMElement) result
\r
472 .createElement(RelationListItemJAXBSchema.REL_ROOT_ELEM_NAME);
\r
473 result.setRootElement((org.dom4j.Element) root);
\r
475 // populate the document with child elements
\r
476 for (Relation child : relationList) {
\r
477 DOMElement el = (DOMElement) result.createElement(RelationJAXBSchema.REL_ROOT_ELEM_NAME);
\r
478 el.setAttribute(RelationListItemJAXBSchema.CSID, child
\r
480 el.setAttribute(RelationListItemJAXBSchema.URI, getRelURL(
\r
481 CS_RELATION_SERVICE_NAME, child.getCsid()));
\r
483 if (logger.isDebugEnabled() == true) {
\r
484 System.out.println(el.asXML());
\r
487 root.appendChild(el);
\r
489 } catch (Exception e) {
\r
490 if (logger.isDebugEnabled()) {
\r
491 logger.debug("Caught exception ", e);
\r
493 throw new DocumentException(e);
\r
496 if (logger.isDebugEnabled() == true) {
\r
497 System.out.println(result.asXML());
\r
503 private boolean isQueryMatch(DocumentModel documentModel,
\r
504 String subjectCsid,
\r
506 String objectCsid) throws ClientException {
\r
507 boolean result = true;
\r
510 if (subjectCsid != null) {
\r
511 if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
\r
516 if (predicate != null) {
\r
517 if (isPredicateOfRelation(predicate, documentModel) == false) {
\r
522 if (objectCsid != null) {
\r
523 if (isObjectOfRelation(objectCsid, documentModel) == false) {
\r
532 private static String getRelURL(String repo, String uuid) {
\r
533 return '/' + repo + '/' + uuid;
\r