]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
80d6314086e88280d57b4f9929758d17f9cdf541
[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.collectionobject.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import org.collectionspace.services.CollectionObjectJAXBSchema;
29 import org.collectionspace.services.collectionobject.CollectionObject;
30 import org.collectionspace.services.collectionobject.CollectionObjectList;
31 import org.collectionspace.services.collectionobject.CollectionObjectList.CollectionObjectListItem;
32 import org.collectionspace.services.common.repository.DocumentWrapper;
33 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
34 import org.nuxeo.ecm.core.api.DocumentModel;
35 import org.nuxeo.ecm.core.api.DocumentModelList;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * CollectionObjectDocumentModelHandler
41  *
42  * $LastChangedRevision: $
43  * $LastChangedDate: $
44  */
45 public class CollectionObjectDocumentModelHandler
46         extends DocumentModelHandler<CollectionObject, CollectionObjectList> {
47
48     private final Logger logger = LoggerFactory.getLogger(CollectionObjectDocumentModelHandler.class);
49     /**
50      * collectionObject is used to stash JAXB object to use when handle is called
51      * for Action.CREATE, Action.UPDATE or Action.GET
52      */
53     private CollectionObject collectionObject;
54     /**
55      * collectionObjectList is stashed when handle is called
56      * for ACTION.GET_ALL
57      */
58     private CollectionObjectList collectionObjectList;
59
60     @Override
61     public void prepare(Action action) throws Exception {
62         //no specific action needed
63     }
64
65     @Override
66     public void handle(Action action, DocumentWrapper wrapDoc) throws Exception {
67         switch(action){
68             case CREATE:
69                 handleCreate(wrapDoc);
70                 break;
71             case UPDATE:
72                 handleUpdate(wrapDoc);
73                 break;
74             case GET:
75                 handleGet(wrapDoc);
76                 break;
77             case GET_ALL:
78                 handleGetAll(wrapDoc);
79                 break;
80         }
81     }
82
83     /**
84      * handleCreate processes create operation response
85      * @param wrapDoc
86      * @throws Exception
87      */
88     public void handleCreate(DocumentWrapper wrapDoc) throws Exception {
89         CollectionObject co = getCommonObject();
90         if(co == null){
91             String msg = "Error creating document: Missing input data";
92             logger.error(msg);
93             throw new IllegalStateException(msg);
94         }
95         //FIXME set other parts as well
96         fillCommonObject(co, wrapDoc);
97     }
98
99     /**
100      * handleUpdate processes update operation response
101      * @param wrapDoc
102      * @throws Exception
103      */
104     public void handleUpdate(DocumentWrapper wrapDoc) throws Exception {
105         CollectionObject co = getCommonObject();
106         if(co == null){
107             String msg = "Error updating document: Missing input data";
108             logger.error(msg);
109             throw new IllegalStateException(msg);
110         }
111         //FIXME set other parts as well
112         fillCommonObject(co, wrapDoc);
113     }
114
115     /**
116      * handleGet processes get operation response
117      * @param wrapDoc
118      * @throws Exception
119      */
120     public void handleGet(DocumentWrapper wrapDoc) throws Exception {
121         CollectionObject co = extractCommonObject(wrapDoc);
122         setCommonObject(co);
123
124         //FIXME retrive other parts as well
125     }
126
127     /**
128      * handleGetAll processes index operation response
129      * @param wrapDoc
130      * @throws Exception
131      */
132     public void handleGetAll(DocumentWrapper wrapDoc) throws Exception {
133         CollectionObjectList coList = extractCommonObjectList(wrapDoc);
134         //FIXME, this is unncessarily called on each call from client
135         setCommonObjectList(coList);
136     }
137
138     /**
139      * getCommonObject get associated CollectionObject
140      * @return
141      */
142     @Override
143     public CollectionObject getCommonObject() {
144         return collectionObject;
145     }
146
147     /**
148      * setCommonObject set associated collectionobject
149      * @param collectionObject
150      */
151     @Override
152     public void setCommonObject(CollectionObject collectionObject) {
153         this.collectionObject = collectionObject;
154     }
155
156     /**
157      * getCollectionObjectList get associated CollectionObject (for index/GET_ALL)
158      * @return
159      */
160     @Override
161     public CollectionObjectList getCommonObjectList() {
162         return collectionObjectList;
163     }
164
165     @Override
166     public void setCommonObjectList(CollectionObjectList collectionObjectList) {
167         this.collectionObjectList = collectionObjectList;
168     }
169
170     @Override
171     public CollectionObject extractCommonObject(DocumentWrapper wrapDoc)
172             throws Exception {
173         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
174         CollectionObject co = new CollectionObject();
175
176         //FIXME property get should be dynamically set using schema inspection
177         //so it does not require hard coding
178
179         // CollectionObject core values
180         co.setObjectNumber((String) docModel.getPropertyValue(
181                 getQProperty(CollectionObjectJAXBSchema.OBJECT_NUMBER)));
182         co.setOtherNumber((String) docModel.getPropertyValue(
183                 getQProperty(CollectionObjectJAXBSchema.OTHER_NUMBER)));
184         co.setBriefDescription((String) docModel.getPropertyValue(
185                 getQProperty(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION)));
186         co.setComments((String) docModel.getPropertyValue(
187                 getQProperty(CollectionObjectJAXBSchema.COMMENTS)));
188         co.setDistFeatures((String) docModel.getPropertyValue(
189                 getQProperty(CollectionObjectJAXBSchema.DIST_FEATURES)));
190         co.setObjectName((String) docModel.getPropertyValue(
191                 getQProperty(CollectionObjectJAXBSchema.OBJECT_NAME)));
192         co.setResponsibleDept((String) docModel.getPropertyValue(
193                 getQProperty(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT)));
194         co.setTitle((String) docModel.getPropertyValue(
195                 getQProperty(CollectionObjectJAXBSchema.TITLE)));
196
197         return co;
198     }
199
200     @Override
201     public void fillCommonObject(CollectionObject co, DocumentWrapper wrapDoc) throws Exception {
202         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
203         //FIXME property setter should be dynamically set using schema inspection
204         //so it does not require hard coding
205
206         // a default title for the Dublin Core schema
207         docModel.setPropertyValue("dublincore:title", CollectionObjectConstants.CO_NUXEO_DC_TITLE);
208
209         // CollectionObject core values
210         if(co.getObjectNumber() != null){
211             docModel.setPropertyValue(
212                     getQProperty(CollectionObjectJAXBSchema.OBJECT_NUMBER),
213                     co.getObjectNumber());
214         }
215         if(co.getOtherNumber() != null){
216             docModel.setPropertyValue(
217                     getQProperty(CollectionObjectJAXBSchema.OTHER_NUMBER),
218                     co.getOtherNumber());
219         }
220         if(co.getBriefDescription() != null){
221             docModel.setPropertyValue(
222                     getQProperty(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION),
223                     co.getBriefDescription());
224         }
225         if(co.getComments() != null){
226             docModel.setPropertyValue(
227                     getQProperty(CollectionObjectJAXBSchema.COMMENTS),
228                     co.getComments());
229         }
230         if(co.getDistFeatures() != null){
231             docModel.setPropertyValue(
232                     getQProperty(CollectionObjectJAXBSchema.DIST_FEATURES),
233                     co.getDistFeatures());
234         }
235         if(co.getObjectName() != null){
236             docModel.setPropertyValue(
237                     getQProperty(CollectionObjectJAXBSchema.OBJECT_NAME),
238                     co.getObjectName());
239         }
240         if(co.getResponsibleDept() != null){
241             docModel.setPropertyValue(
242                     getQProperty(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT),
243                     co.getResponsibleDept());
244         }
245         if(co.getTitle() != null){
246             docModel.setPropertyValue(
247                     getQProperty(CollectionObjectJAXBSchema.TITLE),
248                     co.getTitle());
249         }
250     }
251
252     @Override
253     public CollectionObjectList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
254         DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
255
256         CollectionObjectList coList = new CollectionObjectList();
257         List<CollectionObjectList.CollectionObjectListItem> list = coList.getCollectionObjectListItem();
258
259         //FIXME: iterating over a long list of documents is not a long term
260         //strategy...need to change to more efficient iterating in future
261         Iterator<DocumentModel> iter = docList.iterator();
262         while(iter.hasNext()){
263             DocumentModel docModel = iter.next();
264             CollectionObjectListItem coListItem = new CollectionObjectListItem();
265             coListItem.setObjectNumber((String) docModel.getPropertyValue(
266                     getQProperty(CollectionObjectJAXBSchema.OBJECT_NUMBER)));
267             //need fully qualified context for URI
268             coListItem.setUri("/collectionobjects/" + docModel.getId());
269             coListItem.setCsid(docModel.getId());
270             list.add(coListItem);
271         }
272
273         return coList;
274     }
275
276     @Override
277     public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc) throws Exception {
278         throw new UnsupportedOperationException();
279     }
280
281     /**
282      * getQProperty converts the given property to qualified schema property
283      * @param prop
284      * @return
285      */
286     private String getQProperty(String prop) {
287         return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop;
288     }
289 }
290