]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9a2684e3f95097037dc9ea2b6af41c482bb0b522
[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.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;
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         relationListItem.setUri(serviceContextPath + docModel.getId());
74         relationListItem.setCsid(docModel.getId());
75         return relationListItem;
76     }
77
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);
84     }
85
86     /**
87      * Checks if is subject of relation.
88      *
89      * @param csid the csid
90      * @param documentModel the document model
91      *
92      * @return true, if is subject of relation
93      *
94      * @throws Exception 
95      */
96     private static boolean isSubjectOfRelation(String csid, DocumentModel documentModel)
97             throws Exception {
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);
103         }
104
105         return result;
106     }
107
108     /**
109      * Checks if is object of relation.
110      *
111      * @param csid the csid
112      * @param documentModel the document model
113      *
114      * @return true, if is object of relation
115      *
116      * @throws Exception 
117      */
118     private static boolean isObjectOfRelation(String csid, DocumentModel documentModel)
119             throws Exception {
120         boolean result = false;
121
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);
127         }
128
129         return result;
130     }
131
132     /**
133      * Checks if is predicate of relation.
134      *
135      * @param predicate the predicate
136      * @param documentModel the document model
137      *
138      * @return true, if is predicate of relation
139      *
140      * @throws Exception 
141      */
142     private static boolean isPredicateOfRelation(String predicate,
143             DocumentModel documentModel) throws Exception {
144         boolean result = false;
145
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);
151         }
152
153         return result;
154     }
155
156     /**
157      * Gets the object from subject.
158      *
159      * @param csid the csid
160      * @param documentModel the document model
161      *
162      * @return the object from subject
163      *
164      * @throws Exception 
165      */
166     private static String getObjectFromSubject(String csid, DocumentModel documentModel)
167             throws Exception {
168         String result = null;
169
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;
179                 }
180             }
181         }
182
183         return result;
184     }
185
186     /**
187      * Checks if is query match.
188      *
189      * @param documentModel the document model
190      * @param subjectCsid the subject csid
191      * @param predicate the predicate
192      * @param objectCsid the object csid
193      *
194      * @return true, if is query match
195      *
196      * @throws ClientException the client exception
197      */
198     public static boolean isQueryMatch(DocumentModel documentModel,
199             String subjectCsid,
200             String predicate,
201             String objectCsid) throws DocumentException {
202         boolean result = true;
203
204         try{
205             block:
206             {
207                 if(subjectCsid != null){
208                     if(isSubjectOfRelation(subjectCsid, documentModel) == false){
209                         result = false;
210                         break block;
211                     }
212                 }
213                 if(predicate != null){
214                     if(isPredicateOfRelation(predicate, documentModel) == false){
215                         result = false;
216                         break block;
217                     }
218                 }
219                 if(objectCsid != null){
220                     if(isObjectOfRelation(objectCsid, documentModel) == false){
221                         result = false;
222                         break block;
223                     }
224                 }
225             }
226         }catch(Exception e){
227             if(logger.isDebugEnabled() == true){
228                 e.printStackTrace();
229             }
230             throw new DocumentException(e);
231         }
232
233         return result;
234     }
235
236     /**
237      * Gets the rel url.
238      *
239      * @param repo the repo
240      * @param uuid the uuid
241      *
242      * @return the rel url
243      */
244     private static String getRelURL(String repo, String uuid) {
245         return '/' + repo + '/' + uuid;
246     }
247 }
248