]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
af2cabd6c1e079c9a1515b0cd16ed03a15987f79
[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.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;
34
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;
42
43 /**
44  * RelationsUtils
45  *
46  * $LastChangedRevision: $
47  * $LastChangedDate: $
48  */
49 public class RelationsUtils {
50
51     private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class);
52
53     public static RelationsCommonList extractCommonPartList(ServiceContext ctx, DocumentWrapper wrapDoc,
54             String serviceContextPath)
55             throws Exception {
56         DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
57
58         RelationsCommonList relList = new RelationsCommonList();
59         List<RelationsCommonList.RelationListItem> list = relList.getRelationListItem();
60
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,
67                     serviceContextPath);
68             list.add(relationListItem);
69         }
70         return relList;
71     }
72
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));
82         
83         relationListItem.setUri(serviceContextPath + id);
84         return relationListItem;
85     }
86
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);
93     }
94
95     /**
96      * Checks if is subject of relation.
97      *
98      * @param csid the csid
99      * @param documentModel the document model
100      *
101      * @return true, if is subject of relation
102      *
103      * @throws Exception 
104      */
105     private static boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
106             throws Exception {
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);
112         }
113
114         return result;
115     }
116
117     /**
118      * Checks if is object of relation.
119      *
120      * @param csid the csid
121      * @param documentModel the document model
122      *
123      * @return true, if is object of relation
124      *
125      * @throws Exception 
126      */
127     private static boolean isObjectOfRelation(String csid, DocumentModel documentModel)
128             throws Exception {
129         boolean result = false;
130
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);
136         }
137
138         return result;
139     }
140
141     /**
142      * Checks if is predicate of relation.
143      *
144      * @param predicate the predicate
145      * @param documentModel the document model
146      *
147      * @return true, if is predicate of relation
148      *
149      * @throws Exception 
150      */
151     private static boolean isPredicateOfRelation(String predicate,
152             DocumentModel documentModel) throws Exception {
153         boolean result = false;
154
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);
160         }
161
162         return result;
163     }
164
165     /**
166      * Gets the object from subject.
167      *
168      * @param csid the csid
169      * @param documentModel the document model
170      *
171      * @return the object from subject
172      *
173      * @throws Exception 
174      */
175     private static String getObjectFromSubject(String csid, DocumentModel documentModel)
176             throws Exception {
177         String result = null;
178
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;
188                 }
189             }
190         }
191
192         return result;
193     }
194
195     /**
196      * Checks if is query match.
197      *
198      * @param documentModel the document model
199      * @param subjectCsid the subject csid
200      * @param predicate the predicate
201      * @param objectCsid the object csid
202      *
203      * @return true, if is query match
204      *
205      * @throws ClientException the client exception
206      */
207     public static boolean isQueryMatch(DocumentModel documentModel,
208             String subjectCsid,
209             String predicate,
210             String objectCsid) throws DocumentException {
211         boolean result = true;
212
213         try {
214             block:
215             {
216                 if (subjectCsid != null) {
217                     if (isSubjectOfRelation(subjectCsid, documentModel) == false) {
218                         result = false;
219                         break block;
220                     }
221                 }
222                 if (predicate != null) {
223                     if (isPredicateOfRelation(predicate, documentModel) == false) {
224                         result = false;
225                         break block;
226                     }
227                 }
228                 if (objectCsid != null) {
229                     if (isObjectOfRelation(objectCsid, documentModel) == false) {
230                         result = false;
231                         break block;
232                     }
233                 }
234             }
235         } catch (Exception e) {
236             if (logger.isDebugEnabled() == true) {
237                 e.printStackTrace();
238             }
239             throw new DocumentException(e);
240         }
241
242         return result;
243     }
244
245     /**
246      * Gets the rel url.
247      *
248      * @param repo the repo
249      * @param uuid the uuid
250      *
251      * @return the rel url
252      */
253     private static String getRelURL(String repo, String uuid) {
254         return '/' + repo + '/' + uuid;
255     }
256 }
257