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.relation.RelationsCommonList;
33 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
34 import org.nuxeo.ecm.core.api.ClientException;
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 relationListItem.setUri(serviceContextPath + docModel.getId());
74 relationListItem.setCsid(docModel.getId());
75 return relationListItem;
78 public static void fillDublinCoreObject(DocumentWrapper wrapDoc) throws Exception {
79 DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
80 //FIXME property setter should be dynamically set using schema inspection
81 //so it does not require hard coding
82 // a default title for the Dublin Core schema
83 docModel.setPropertyValue("dublincore:title", RelationConstants.NUXEO_DC_TITLE);
87 * Checks if is subject of relation.
89 * @param csid the csid
90 * @param documentModel the document model
92 * @return true, if is subject of relation
96 private static boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
98 boolean result = false;
99 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME, RelationJAXBSchema.DOCUMENT_ID_1);
100 if(valueObject != null && csid != null){
101 String subjectID = (String) valueObject;
102 result = subjectID.equals(csid);
109 * Checks if is object of relation.
111 * @param csid the csid
112 * @param documentModel the document model
114 * @return true, if is object of relation
118 private static boolean isObjectOfRelation(String csid, DocumentModel documentModel)
120 boolean result = false;
122 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
123 RelationJAXBSchema.DOCUMENT_ID_2);
124 if(valueObject != null && csid != null){
125 String subjectID = (String) valueObject;
126 result = subjectID.equals(csid);
133 * Checks if is predicate of relation.
135 * @param predicate the predicate
136 * @param documentModel the document model
138 * @return true, if is predicate of relation
142 private static boolean isPredicateOfRelation(String predicate,
143 DocumentModel documentModel) throws Exception {
144 boolean result = false;
146 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
147 RelationJAXBSchema.RELATIONSHIP_TYPE);
148 if(valueObject != null && predicate != null){
149 String relationType = (String) valueObject;
150 result = predicate.equalsIgnoreCase(relationType);
157 * Gets the object from subject.
159 * @param csid the csid
160 * @param documentModel the document model
162 * @return the object from subject
166 private static String getObjectFromSubject(String csid, DocumentModel documentModel)
168 String result = null;
170 Object valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
171 RelationJAXBSchema.DOCUMENT_ID_1);
172 if(valueObject != null){
173 String subjectID = (String) valueObject;
174 if(subjectID.equals(csid) == true){
175 valueObject = documentModel.getProperty(RelationConstants.NUXEO_SCHEMA_NAME,
176 RelationJAXBSchema.DOCUMENT_ID_2);
177 if(valueObject != null){
178 result = (String) valueObject;
187 * Checks if is query match.
189 * @param documentModel the document model
190 * @param subjectCsid the subject csid
191 * @param predicate the predicate
192 * @param objectCsid the object csid
194 * @return true, if is query match
196 * @throws ClientException the client exception
198 public static boolean isQueryMatch(DocumentModel documentModel,
201 String objectCsid) throws DocumentException {
202 boolean result = true;
207 if(subjectCsid != null){
208 if(isSubjectOfRelation(subjectCsid, documentModel) == false){
213 if(predicate != null){
214 if(isPredicateOfRelation(predicate, documentModel) == false){
219 if(objectCsid != null){
220 if(isObjectOfRelation(objectCsid, documentModel) == false){
227 if(logger.isDebugEnabled() == true){
230 throw new DocumentException(e);
239 * @param repo the repo
240 * @param uuid the uuid
242 * @return the rel url
244 private static String getRelURL(String repo, String uuid) {
245 return '/' + repo + '/' + uuid;