2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.common.relation.nuxeo;
26 import java.util.Iterator;
27 import java.util.List;
29 import org.collectionspace.services.common.relation.RelationJAXBSchema;
30 import org.collectionspace.services.common.repository.DocumentException;
31 import org.collectionspace.services.common.repository.DocumentWrapper;
32 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
33 import org.collectionspace.services.relation.RelationsCommonList;
34 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
35 import org.nuxeo.ecm.core.api.DocumentModel;
36 import org.nuxeo.ecm.core.api.DocumentModelList;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
43 * $LastChangedRevision: $
46 public class RelationsUtils {
48 private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class);
50 public static RelationsCommonList extractCommonPartList(DocumentWrapper wrapDoc,
51 String serviceContextPath)
53 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
55 RelationsCommonList relList = new RelationsCommonList();
56 List<RelationsCommonList.RelationListItem> list = relList.getRelationListItem();
58 //FIXME: iterating over a long list of documents is not a long term
59 //strategy...need to change to more efficient iterating in future
60 Iterator<DocumentModel> iter = docList.iterator();
61 while (iter.hasNext()) {
62 DocumentModel docModel = iter.next();
63 RelationListItem relationListItem = getRelationListItem(docModel,
65 list.add(relationListItem);
70 public static RelationListItem getRelationListItem(DocumentModel docModel,
71 String serviceContextPath) throws Exception {
72 RelationListItem relationListItem = new RelationListItem();
73 String id = NuxeoUtils.extractId(docModel.getPathAsString());
74 relationListItem.setCsid(id);
75 relationListItem.setUri(serviceContextPath + id);
76 return relationListItem;
79 public static void fillDublinCoreObject(DocumentWrapper wrapDoc) throws Exception {
80 DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
81 //FIXME property setter should be dynamically set using schema inspection
82 //so it does not require hard coding
83 // a default title for the Dublin Core schema
84 docModel.setPropertyValue("dublincore:title", RelationConstants.NUXEO_DC_TITLE);
88 * Checks if is subject of relation.
90 * @param csid the csid
91 * @param documentModel the document model
93 * @return true, if is subject of relation
97 private static boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
99 boolean result = false;
100 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME, RelationJAXBSchema.DOCUMENT_ID_1);
101 if (valueObject != null && csid != null) {
102 String subjectID = (String) valueObject;
103 result = subjectID.equals(csid);
110 * Checks if is object of relation.
112 * @param csid the csid
113 * @param documentModel the document model
115 * @return true, if is object of relation
119 private static boolean isObjectOfRelation(String csid, DocumentModel documentModel)
121 boolean result = false;
123 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
124 RelationJAXBSchema.DOCUMENT_ID_2);
125 if (valueObject != null && csid != null) {
126 String subjectID = (String) valueObject;
127 result = subjectID.equals(csid);
134 * Checks if is predicate of relation.
136 * @param predicate the predicate
137 * @param documentModel the document model
139 * @return true, if is predicate of relation
143 private static boolean isPredicateOfRelation(String predicate,
144 DocumentModel documentModel) throws Exception {
145 boolean result = false;
147 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
148 RelationJAXBSchema.RELATIONSHIP_TYPE);
149 if (valueObject != null && predicate != null) {
150 String relationType = (String) valueObject;
151 result = predicate.equalsIgnoreCase(relationType);
158 * Gets the object from subject.
160 * @param csid the csid
161 * @param documentModel the document model
163 * @return the object from subject
167 private static String getObjectFromSubject(String csid, DocumentModel documentModel)
169 String result = null;
171 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
172 RelationJAXBSchema.DOCUMENT_ID_1);
173 if (valueObject != null) {
174 String subjectID = (String) valueObject;
175 if (subjectID.equals(csid) == true) {
176 valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
177 RelationJAXBSchema.DOCUMENT_ID_2);
178 if (valueObject != null) {
179 result = (String) valueObject;
188 * Checks if is query match.
190 * @param documentModel the document model
191 * @param subjectCsid the subject csid
192 * @param predicate the predicate
193 * @param objectCsid the object csid
195 * @return true, if is query match
197 * @throws ClientException the client exception
199 public static boolean isQueryMatch(DocumentModel documentModel,
202 String objectCsid) throws DocumentException {
203 boolean result = true;
208 if (subjectCsid != null) {
209 if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
214 if (predicate != null) {
215 if (isPredicateOfRelation(predicate, documentModel) == false) {
220 if (objectCsid != null) {
221 if (isObjectOfRelation(objectCsid, documentModel) == false) {
227 } catch (Exception e) {
228 if (logger.isDebugEnabled() == true) {
231 throw new DocumentException(e);
240 * @param repo the repo
241 * @param uuid the uuid
243 * @return the rel url
245 private static String getRelURL(String repo, String uuid) {
246 return '/' + repo + '/' + uuid;