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;
29 import org.collectionspace.services.CollectionObjectJAXBSchema;
30 import org.collectionspace.services.collectionobject.CollectionObject;
31 import org.collectionspace.services.collectionobject.CollectionObjectList;
32 import org.collectionspace.services.collectionobject.CollectionObjectList.CollectionObjectListItem;
33 import org.collectionspace.services.collectionobject.CollectionObjectService;
34 import org.collectionspace.services.common.repository.DocumentWrapper;
35 import org.collectionspace.services.nuxeo.client.rest.RepresentationHandler;
36 import org.dom4j.Document;
37 import org.dom4j.Element;
38 import org.nuxeo.ecm.core.api.DocumentModel;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * CollectionObjectDocumentModelHandler
45 * $LastChangedRevision: $
48 public class CollectionObjectRepresenationHandler
49 extends RepresentationHandler<CollectionObject, CollectionObjectList>
52 private final Logger logger = LoggerFactory.getLogger(CollectionObjectRepresenationHandler.class);
54 * collectionObject is used to stash JAXB object to use when handle is called
55 * for Action.CREATE, Action.UPDATE or Action.GET
57 private CollectionObject collectionObject;
59 * collectionObjectList is stashed when handle is called
62 private CollectionObjectList collectionObjectList;
65 public void prepare(Action action) throws Exception {
74 public void handle(Action action, DocumentWrapper wrapDoc)
78 handleCreate(wrapDoc);
81 handleUpdate(wrapDoc);
87 handleGetAll(wrapDoc);
93 * handleCreate processes create operation response
97 public void handleCreate(DocumentWrapper wrapDoc) throws Exception {
98 CollectionObject co = getCommonObject();
100 String msg = "Error creating document: Missing input data";
102 throw new IllegalStateException(msg);
104 //FIXME set other parts as well
105 fillCommonObject(co, wrapDoc);
109 * handleUpdate processes update operation response
113 public void handleUpdate(DocumentWrapper wrapDoc) throws Exception {
114 CollectionObject co = getCommonObject();
116 String msg = "Error updating document: Missing input data";
118 throw new IllegalStateException(msg);
120 //FIXME set other parts as well
121 fillCommonObject(co, wrapDoc);
125 * handleGet processes get operation response
129 public void handleGet(DocumentWrapper wrapDoc) throws Exception {
130 CollectionObject co = extractCommonObject(wrapDoc);
132 //FIXME retrive other parts as well
136 * handleGetAll processes index operation response
140 public void handleGetAll(DocumentWrapper wrapDoc) throws Exception {
141 CollectionObjectList coList = extractCommonObjectList(wrapDoc);
142 setCommonObjectList(coList);
145 private void prepare() {
146 Map<String, String> queryParams = getQueryParams();
147 CollectionObject co = getCommonObject();
148 // todo: intelligent merge needed
149 if(co.getObjectNumber() != null){
150 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
151 ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER, co.getObjectNumber());
154 if(co.getOtherNumber() != null){
155 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
156 ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, co.getOtherNumber());
159 if(co.getBriefDescription() != null){
160 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
161 ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, co.getBriefDescription());
164 if(co.getComments() != null){
165 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
166 ":" + CollectionObjectJAXBSchema.COMMENTS, co.getComments());
169 if(co.getDistFeatures() != null){
170 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
171 ":" + CollectionObjectJAXBSchema.DIST_FEATURES, co.getDistFeatures());
174 if(co.getObjectName() != null){
175 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
176 ":" + CollectionObjectJAXBSchema.OBJECT_NAME, co.getObjectName());
179 if(co.getResponsibleDept() != null){
180 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
181 ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, co.getResponsibleDept());
184 if(co.getTitle() != null){
185 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
186 ":" + CollectionObjectJAXBSchema.TITLE, co.getTitle());
191 public CollectionObject extractCommonObject(DocumentWrapper wrapDoc)
193 Document document = (Document) wrapDoc.getWrappedObject();
194 CollectionObject co = new CollectionObject();
196 //FIXME property get should be dynamically set using schema inspection
197 //so it does not require hard coding
198 Element root = document.getRootElement();
200 // TODO: recognize schema thru namespace uri
201 // Namespace ns = new Namespace("collectionobject",
202 // "http://collectionspace.org/collectionobject");
204 Iterator<Element> siter = root.elementIterator("schema");
205 while(siter.hasNext()){
207 Element schemaElement = siter.next();
208 if(logger.isDebugEnabled()){
209 logger.debug("getCommonObject() populating Common Object");
211 // TODO: recognize schema thru namespace uri
212 if(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
213 Element ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
215 co.setObjectNumber((String) ele.getData());
217 ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
219 co.setOtherNumber((String) ele.getData());
221 ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
223 co.setBriefDescription((String) ele.getData());
225 ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
227 co.setComments((String) ele.getData());
229 ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
231 co.setDistFeatures((String) ele.getData());
233 ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
235 co.setObjectName((String) ele.getData());
237 ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
239 co.setResponsibleDept((String) ele.getData());
241 ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
243 co.setTitle((String) ele.getData());
251 public void fillCommonObject(CollectionObject co, DocumentWrapper wrapDoc)
253 //Nuxeo REST takes create/update through queryParams, nothing to do here
257 public CollectionObjectList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
258 Document document = (Document) wrapDoc.getWrappedObject();
260 if(logger.isDebugEnabled()){
261 logger.debug(document.asXML());
263 CollectionObjectList coList = new CollectionObjectList();
264 List<CollectionObjectList.CollectionObjectListItem> list = coList.getCollectionObjectListItem();
265 Element root = document.getRootElement();
266 for(Iterator i = root.elementIterator(); i.hasNext();){
268 Element element = (Element) i.next();
269 if(logger.isDebugEnabled()){
270 logger.debug(element.asXML());
272 // set the CollectionObject list item entity elements
273 CollectionObjectListItem coListItem = new CollectionObjectListItem();
274 coListItem.setObjectNumber(element.attributeValue("title"));
275 String id = element.attributeValue("id");
276 coListItem.setCsid(id);
277 coListItem.setUri("/collectionobjects/" + id);
279 list.add(coListItem);
285 public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc)
287 throw new UnsupportedOperationException();
291 public CollectionObject getCommonObject() {
292 return collectionObject;
296 public void setCommonObject(CollectionObject obj) {
297 this.collectionObject = obj;
301 public CollectionObjectList getCommonObjectList() {
302 return collectionObjectList;
306 public void setCommonObjectList(CollectionObjectList obj) {
307 this.collectionObjectList = obj;
311 * getQProperty converts the given property to qualified schema property
315 private String getQProperty(String prop) {
316 return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop;