]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3c49ff3e10b42d1f97c4c7033ce2a42badde68f8
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.common.relation.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28
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;
39
40 /**
41  * RelationsUtils
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public class RelationsUtils {
47
48     private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class);
49
50     public static RelationsCommonList extractCommonPartList(DocumentWrapper wrapDoc,
51             String serviceContextPath)
52             throws Exception {
53         DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
54
55         RelationsCommonList relList = new RelationsCommonList();
56         List<RelationsCommonList.RelationListItem> list = relList.getRelationListItem();
57
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,
64                     serviceContextPath);
65             list.add(relationListItem);
66         }
67         return relList;
68     }
69
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;
77     }
78
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);
85     }
86
87     /**
88      * Checks if is subject of relation.
89      *
90      * @param csid the csid
91      * @param documentModel the document model
92      *
93      * @return true, if is subject of relation
94      *
95      * @throws Exception 
96      */
97     private static boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
98             throws Exception {
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);
104         }
105
106         return result;
107     }
108
109     /**
110      * Checks if is object of relation.
111      *
112      * @param csid the csid
113      * @param documentModel the document model
114      *
115      * @return true, if is object of relation
116      *
117      * @throws Exception 
118      */
119     private static boolean isObjectOfRelation(String csid, DocumentModel documentModel)
120             throws Exception {
121         boolean result = false;
122
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);
128         }
129
130         return result;
131     }
132
133     /**
134      * Checks if is predicate of relation.
135      *
136      * @param predicate the predicate
137      * @param documentModel the document model
138      *
139      * @return true, if is predicate of relation
140      *
141      * @throws Exception 
142      */
143     private static boolean isPredicateOfRelation(String predicate,
144             DocumentModel documentModel) throws Exception {
145         boolean result = false;
146
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);
152         }
153
154         return result;
155     }
156
157     /**
158      * Gets the object from subject.
159      *
160      * @param csid the csid
161      * @param documentModel the document model
162      *
163      * @return the object from subject
164      *
165      * @throws Exception 
166      */
167     private static String getObjectFromSubject(String csid, DocumentModel documentModel)
168             throws Exception {
169         String result = null;
170
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;
180                 }
181             }
182         }
183
184         return result;
185     }
186
187     /**
188      * Checks if is query match.
189      *
190      * @param documentModel the document model
191      * @param subjectCsid the subject csid
192      * @param predicate the predicate
193      * @param objectCsid the object csid
194      *
195      * @return true, if is query match
196      *
197      * @throws ClientException the client exception
198      */
199     public static boolean isQueryMatch(DocumentModel documentModel,
200             String subjectCsid,
201             String predicate,
202             String objectCsid) throws DocumentException {
203         boolean result = true;
204
205         try {
206             block:
207             {
208                 if (subjectCsid != null) {
209                     if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
210                         result = false;
211                         break block;
212                     }
213                 }
214                 if (predicate != null) {
215                     if (isPredicateOfRelation(predicate, documentModel) == false) {
216                         result = false;
217                         break block;
218                     }
219                 }
220                 if (objectCsid != null) {
221                     if (isObjectOfRelation(objectCsid, documentModel) == false) {
222                         result = false;
223                         break block;
224                     }
225                 }
226             }
227         } catch (Exception e) {
228             if (logger.isDebugEnabled() == true) {
229                 e.printStackTrace();
230             }
231             throw new DocumentException(e);
232         }
233
234         return result;
235     }
236
237     /**
238      * Gets the rel url.
239      *
240      * @param repo the repo
241      * @param uuid the uuid
242      *
243      * @return the rel url
244      */
245     private static String getRelURL(String repo, String uuid) {
246         return '/' + repo + '/' + uuid;
247     }
248 }
249