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.RelationListItemJAXBSchema;
30 import org.collectionspace.services.common.relation.RelationJAXBSchema;
31 import org.collectionspace.services.common.document.DocumentException;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.relation.RelationsCommonList;
37 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
38 import org.nuxeo.ecm.core.api.DocumentModel;
39 import org.nuxeo.ecm.core.api.DocumentModelList;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
46 * $LastChangedRevision: $
49 public class RelationsUtils {
51 private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class);
53 public static RelationsCommonList extractCommonPartList(ServiceContext ctx, DocumentWrapper wrapDoc,
54 String serviceContextPath)
56 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
58 RelationsCommonList relList = new RelationsCommonList();
59 List<RelationsCommonList.RelationListItem> list = relList.getRelationListItem();
61 //FIXME: iterating over a long list of documents is not a long term
62 //strategy...need to change to more efficient iterating in future
63 Iterator<DocumentModel> iter = docList.iterator();
64 while (iter.hasNext()) {
65 DocumentModel docModel = iter.next();
66 RelationListItem relationListItem = getRelationListItem(ctx, docModel,
68 list.add(relationListItem);
73 public static RelationListItem getRelationListItem(ServiceContext ctx, DocumentModel docModel,
74 String serviceContextPath) throws Exception {
75 RelationListItem relationListItem = new RelationListItem();
76 String id = NuxeoUtils.extractId(docModel.getPathAsString());
77 relationListItem.setCsid(id);
78 relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(),
79 RelationJAXBSchema.DOCUMENT_ID_1));
80 relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(),
81 RelationJAXBSchema.DOCUMENT_ID_2));
83 relationListItem.setUri(serviceContextPath + id);
84 return relationListItem;
87 public static void fillDublinCoreObject(DocumentWrapper wrapDoc) throws Exception {
88 DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
89 //FIXME property setter should be dynamically set using schema inspection
90 //so it does not require hard coding
91 // a default title for the Dublin Core schema
92 docModel.setPropertyValue("dublincore:title", RelationConstants.NUXEO_DC_TITLE);
96 * Checks if is subject of relation.
98 * @param csid the csid
99 * @param documentModel the document model
101 * @return true, if is subject of relation
105 private static boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
107 boolean result = false;
108 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME, RelationJAXBSchema.DOCUMENT_ID_1);
109 if (valueObject != null && csid != null) {
110 String subjectID = (String) valueObject;
111 result = subjectID.equals(csid);
118 * Checks if is object of relation.
120 * @param csid the csid
121 * @param documentModel the document model
123 * @return true, if is object of relation
127 private static boolean isObjectOfRelation(String csid, DocumentModel documentModel)
129 boolean result = false;
131 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
132 RelationJAXBSchema.DOCUMENT_ID_2);
133 if (valueObject != null && csid != null) {
134 String subjectID = (String) valueObject;
135 result = subjectID.equals(csid);
142 * Checks if is predicate of relation.
144 * @param predicate the predicate
145 * @param documentModel the document model
147 * @return true, if is predicate of relation
151 private static boolean isPredicateOfRelation(String predicate,
152 DocumentModel documentModel) throws Exception {
153 boolean result = false;
155 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
156 RelationJAXBSchema.RELATIONSHIP_TYPE);
157 if (valueObject != null && predicate != null) {
158 String relationType = (String) valueObject;
159 result = predicate.equalsIgnoreCase(relationType);
166 * Gets the object from subject.
168 * @param csid the csid
169 * @param documentModel the document model
171 * @return the object from subject
175 private static String getObjectFromSubject(String csid, DocumentModel documentModel)
177 String result = null;
179 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
180 RelationJAXBSchema.DOCUMENT_ID_1);
181 if (valueObject != null) {
182 String subjectID = (String) valueObject;
183 if (subjectID.equals(csid) == true) {
184 valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
185 RelationJAXBSchema.DOCUMENT_ID_2);
186 if (valueObject != null) {
187 result = (String) valueObject;
196 * Checks if is query match.
198 * @param documentModel the document model
199 * @param subjectCsid the subject csid
200 * @param predicate the predicate
201 * @param objectCsid the object csid
203 * @return true, if is query match
205 * @throws ClientException the client exception
207 public static boolean isQueryMatch(DocumentModel documentModel,
210 String objectCsid) throws DocumentException {
211 boolean result = true;
216 if (subjectCsid != null) {
217 if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
222 if (predicate != null) {
223 if (isPredicateOfRelation(predicate, documentModel) == false) {
228 if (objectCsid != null) {
229 if (isObjectOfRelation(objectCsid, documentModel) == false) {
235 } catch (Exception e) {
236 if (logger.isDebugEnabled() == true) {
239 throw new DocumentException(e);
248 * @param repo the repo
249 * @param uuid the uuid
251 * @return the rel url
253 private static String getRelURL(String repo, String uuid) {
254 return '/' + repo + '/' + uuid;