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.collectionobject.nuxeo;
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;
40 * CollectionObjectDocumentModelHandler
42 * $LastChangedRevision: $
45 public class CollectionObjectDocumentModelHandler
46 extends DocumentModelHandler<CollectionObject, CollectionObjectList> {
48 private final Logger logger = LoggerFactory.getLogger(CollectionObjectDocumentModelHandler.class);
50 * collectionObject is used to stash JAXB object to use when handle is called
51 * for Action.CREATE, Action.UPDATE or Action.GET
53 private CollectionObject collectionObject;
55 * collectionObjectList is stashed when handle is called
58 private CollectionObjectList collectionObjectList;
61 public void prepare(Action action) throws Exception {
62 //no specific action needed
66 public void handle(Action action, DocumentWrapper wrapDoc) throws Exception {
69 handleCreate(wrapDoc);
72 handleUpdate(wrapDoc);
78 handleGetAll(wrapDoc);
84 * handleCreate processes create operation response
88 public void handleCreate(DocumentWrapper wrapDoc) throws Exception {
89 CollectionObject co = getCommonObject();
91 String msg = "Error creating document: Missing input data";
93 throw new IllegalStateException(msg);
95 //FIXME set other parts as well
96 fillCommonObject(co, wrapDoc);
100 * handleUpdate processes update operation response
104 public void handleUpdate(DocumentWrapper wrapDoc) throws Exception {
105 CollectionObject co = getCommonObject();
107 String msg = "Error updating document: Missing input data";
109 throw new IllegalStateException(msg);
111 //FIXME set other parts as well
112 fillCommonObject(co, wrapDoc);
116 * handleGet processes get operation response
120 public void handleGet(DocumentWrapper wrapDoc) throws Exception {
121 CollectionObject co = extractCommonObject(wrapDoc);
124 //FIXME retrive other parts as well
128 * handleGetAll processes index operation response
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);
139 * getCommonObject get associated CollectionObject
143 public CollectionObject getCommonObject() {
144 return collectionObject;
148 * setCommonObject set associated collectionobject
149 * @param collectionObject
152 public void setCommonObject(CollectionObject collectionObject) {
153 this.collectionObject = collectionObject;
157 * getCollectionObjectList get associated CollectionObject (for index/GET_ALL)
161 public CollectionObjectList getCommonObjectList() {
162 return collectionObjectList;
166 public void setCommonObjectList(CollectionObjectList collectionObjectList) {
167 this.collectionObjectList = collectionObjectList;
171 public CollectionObject extractCommonObject(DocumentWrapper wrapDoc)
173 DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
174 CollectionObject co = new CollectionObject();
176 //FIXME property get should be dynamically set using schema inspection
177 //so it does not require hard coding
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)));
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
206 // a default title for the Dublin Core schema
207 docModel.setPropertyValue("dublincore:title", CollectionObjectConstants.CO_NUXEO_DC_TITLE);
209 // CollectionObject core values
210 if(co.getObjectNumber() != null){
211 docModel.setPropertyValue(
212 getQProperty(CollectionObjectJAXBSchema.OBJECT_NUMBER),
213 co.getObjectNumber());
215 if(co.getOtherNumber() != null){
216 docModel.setPropertyValue(
217 getQProperty(CollectionObjectJAXBSchema.OTHER_NUMBER),
218 co.getOtherNumber());
220 if(co.getBriefDescription() != null){
221 docModel.setPropertyValue(
222 getQProperty(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION),
223 co.getBriefDescription());
225 if(co.getComments() != null){
226 docModel.setPropertyValue(
227 getQProperty(CollectionObjectJAXBSchema.COMMENTS),
230 if(co.getDistFeatures() != null){
231 docModel.setPropertyValue(
232 getQProperty(CollectionObjectJAXBSchema.DIST_FEATURES),
233 co.getDistFeatures());
235 if(co.getObjectName() != null){
236 docModel.setPropertyValue(
237 getQProperty(CollectionObjectJAXBSchema.OBJECT_NAME),
240 if(co.getResponsibleDept() != null){
241 docModel.setPropertyValue(
242 getQProperty(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT),
243 co.getResponsibleDept());
245 if(co.getTitle() != null){
246 docModel.setPropertyValue(
247 getQProperty(CollectionObjectJAXBSchema.TITLE),
253 public CollectionObjectList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
254 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
256 CollectionObjectList coList = new CollectionObjectList();
257 List<CollectionObjectList.CollectionObjectListItem> list = coList.getCollectionObjectListItem();
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);
277 public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc) throws Exception {
278 throw new UnsupportedOperationException();
282 * getQProperty converts the given property to qualified schema property
286 private String getQProperty(String prop) {
287 return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop;