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.common.repository.DocumentWrapper;
34 import org.collectionspace.services.nuxeo.client.rest.RepresentationHandler;
35 import org.dom4j.Document;
36 import org.dom4j.Element;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * CollectionObjectDocumentModelHandler
43 * $LastChangedRevision: $
46 public class CollectionObjectRepresenationHandler
47 extends RepresentationHandler<CollectionObject, CollectionObjectList>
50 private final Logger logger = LoggerFactory.getLogger(CollectionObjectRepresenationHandler.class);
52 * collectionObject is used to stash JAXB object to use when handle is called
53 * for Action.CREATE, Action.UPDATE or Action.GET
55 private CollectionObject collectionObject;
57 * collectionObjectList is stashed when handle is called
60 private CollectionObjectList collectionObjectList;
63 public void prepare(Action action) throws Exception {
71 private void prepare() {
72 Map<String, String> queryParams = getQueryParams();
73 CollectionObject co = getCommonObject();
74 // todo: intelligent merge needed
75 if(co.getObjectNumber() != null){
76 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
77 ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER, co.getObjectNumber());
80 if(co.getOtherNumber() != null){
81 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
82 ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, co.getOtherNumber());
85 if(co.getBriefDescription() != null){
86 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
87 ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, co.getBriefDescription());
90 if(co.getComments() != null){
91 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
92 ":" + CollectionObjectJAXBSchema.COMMENTS, co.getComments());
95 if(co.getDistFeatures() != null){
96 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
97 ":" + CollectionObjectJAXBSchema.DIST_FEATURES, co.getDistFeatures());
100 if(co.getObjectName() != null){
101 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
102 ":" + CollectionObjectJAXBSchema.OBJECT_NAME, co.getObjectName());
105 if(co.getResponsibleDept() != null){
106 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
107 ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, co.getResponsibleDept());
110 if(co.getTitle() != null){
111 queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
112 ":" + CollectionObjectJAXBSchema.TITLE, co.getTitle());
117 public CollectionObject extractCommonObject(DocumentWrapper wrapDoc)
119 Document document = (Document) wrapDoc.getWrappedObject();
120 CollectionObject co = new CollectionObject();
122 //FIXME property get should be dynamically set using schema inspection
123 //so it does not require hard coding
124 Element root = document.getRootElement();
126 // TODO: recognize schema thru namespace uri
127 // Namespace ns = new Namespace("collectionobject",
128 // "http://collectionspace.org/collectionobject");
130 Iterator<Element> siter = root.elementIterator("schema");
131 while(siter.hasNext()){
133 Element schemaElement = siter.next();
134 if(logger.isDebugEnabled()){
135 logger.debug("getCommonObject() populating Common Object");
137 // TODO: recognize schema thru namespace uri
138 if(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
139 Element ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
141 co.setObjectNumber((String) ele.getData());
143 ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
145 co.setOtherNumber((String) ele.getData());
147 ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
149 co.setBriefDescription((String) ele.getData());
151 ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
153 co.setComments((String) ele.getData());
155 ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
157 co.setDistFeatures((String) ele.getData());
159 ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
161 co.setObjectName((String) ele.getData());
163 ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
165 co.setResponsibleDept((String) ele.getData());
167 ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
169 co.setTitle((String) ele.getData());
177 public void fillCommonObject(CollectionObject co, DocumentWrapper wrapDoc)
179 //Nuxeo REST takes create/update through queryParams, nothing to do here
183 public CollectionObjectList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
184 Document document = (Document) wrapDoc.getWrappedObject();
186 if(logger.isDebugEnabled()){
187 logger.debug(document.asXML());
189 CollectionObjectList coList = new CollectionObjectList();
190 List<CollectionObjectList.CollectionObjectListItem> list = coList.getCollectionObjectListItem();
191 Element root = document.getRootElement();
192 for(Iterator i = root.elementIterator(); i.hasNext();){
194 Element element = (Element) i.next();
195 if(logger.isDebugEnabled()){
196 logger.debug(element.asXML());
198 // set the CollectionObject list item entity elements
199 CollectionObjectListItem coListItem = new CollectionObjectListItem();
200 coListItem.setObjectNumber(element.attributeValue("title"));
201 String id = element.attributeValue("id");
202 coListItem.setCsid(id);
203 coListItem.setUri("/collectionobjects/" + id);
205 list.add(coListItem);
211 public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc)
213 throw new UnsupportedOperationException();
217 public CollectionObject getCommonObject() {
218 return collectionObject;
222 public void setCommonObject(CollectionObject obj) {
223 this.collectionObject = obj;
227 public CollectionObjectList getCommonObjectList() {
228 return collectionObjectList;
232 public void setCommonObjectList(CollectionObjectList obj) {
233 this.collectionObjectList = obj;
237 * getQProperty converts the given property to qualified schema property
241 private String getQProperty(String prop) {
242 return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop;