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;
30 import java.util.StringTokenizer;
31 import org.collectionspace.services.CollectionObjectJAXBSchema;
32 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
33 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
34 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
35 import org.collectionspace.services.common.repository.DocumentWrapper;
36 import org.collectionspace.services.nuxeo.client.rest.RepresentationHandler;
38 import org.dom4j.Document;
39 import org.dom4j.Element;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * CollectionObjectDocumentModelHandler
46 * $LastChangedRevision: $
49 public class CollectionObjectRepresenationHandler
50 extends RepresentationHandler<CollectionobjectsCommon, CollectionobjectsCommonList>
53 private final Logger logger = LoggerFactory.getLogger(CollectionObjectRepresenationHandler.class);
55 * collectionObject is used to stash JAXB object to use when handle is called
56 * for Action.CREATE, Action.UPDATE or Action.GET
58 private CollectionobjectsCommon collectionObject;
60 * collectionObjectList is stashed when handle is called
63 private CollectionobjectsCommonList collectionObjectList;
66 public void prepare(Action action) throws Exception {
74 private void prepare() {
75 Map<String, String> queryParams = getQueryParams();
76 CollectionobjectsCommon co = getCommonPart();
77 // todo: intelligent merge needed
78 if(co.getObjectNumber() != null){
79 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
80 ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER, co.getObjectNumber());
83 if(co.getOtherNumber() != null){
84 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
85 ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, co.getOtherNumber());
88 if(co.getBriefDescription() != null){
89 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
90 ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, co.getBriefDescription());
93 if(co.getComments() != null){
94 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
95 ":" + CollectionObjectJAXBSchema.COMMENTS, co.getComments());
98 if(co.getDistFeatures() != null){
99 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
100 ":" + CollectionObjectJAXBSchema.DIST_FEATURES, co.getDistFeatures());
103 if(co.getObjectName() != null){
104 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
105 ":" + CollectionObjectJAXBSchema.OBJECT_NAME, co.getObjectName());
108 if(co.getResponsibleDept() != null){
109 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
110 ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, co.getResponsibleDept());
113 if(co.getTitle() != null){
114 queryParams.put(CollectionObjectConstants.NUXEO_SCHEMA_NAME +
115 ":" + CollectionObjectJAXBSchema.TITLE, co.getTitle());
120 public CollectionobjectsCommon extractCommonPart(DocumentWrapper wrapDoc)
122 Document document = (Document) wrapDoc.getWrappedObject();
123 CollectionobjectsCommon co = new CollectionobjectsCommon();
125 //FIXME property get should be dynamically set using schema inspection
126 //so it does not require hard coding
127 Element root = document.getRootElement();
129 // TODO: recognize schema thru namespace uri
130 // Namespace ns = new Namespace("collectionobject",
131 // "http://collectionspace.org/collectionobject");
133 Iterator<Element> siter = root.elementIterator("schema");
134 while(siter.hasNext()){
136 Element schemaElement = siter.next();
137 if(logger.isDebugEnabled()){
138 logger.debug("getCommonObject() populating Common Object");
140 // TODO: recognize schema thru namespace uri
141 if(CollectionObjectConstants.NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
142 Element ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
144 co.setObjectNumber((String) ele.getData());
146 ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
148 co.setOtherNumber((String) ele.getData());
150 ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
152 co.setBriefDescription((String) ele.getData());
154 ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
156 co.setComments((String) ele.getData());
158 ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
160 co.setDistFeatures((String) ele.getData());
162 ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
164 co.setObjectName((String) ele.getData());
166 ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
168 co.setResponsibleDept((String) ele.getData());
170 ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
172 co.setTitle((String) ele.getData());
180 public void fillCommonPart(CollectionobjectsCommon co, DocumentWrapper wrapDoc)
182 //Nuxeo REST takes create/update through queryParams, nothing to do here
186 public CollectionobjectsCommonList extractCommonPartList(DocumentWrapper wrapDoc) throws Exception {
187 Document document = (Document) wrapDoc.getWrappedObject();
189 if(logger.isDebugEnabled()){
190 logger.debug(document.asXML());
192 CollectionobjectsCommonList coList = new CollectionobjectsCommonList();
193 List<CollectionobjectsCommonList.CollectionObjectListItem> list = coList.getCollectionObjectListItem();
194 Element root = document.getRootElement();
195 for(Iterator i = root.elementIterator(); i.hasNext();){
197 Element element = (Element) i.next();
198 if(logger.isDebugEnabled()){
199 logger.debug(element.asXML());
201 // set the CollectionObject list item entity elements
202 CollectionObjectListItem coListItem = new CollectionObjectListItem();
203 coListItem.setObjectNumber(element.attributeValue("title"));
204 String id = element.attributeValue("id");
205 coListItem.setCsid(id);
206 coListItem.setUri("/collectionobjects/" + id);
208 list.add(coListItem);
215 public CollectionobjectsCommon getCommonPart() {
216 return collectionObject;
220 public void setCommonPart(CollectionobjectsCommon obj) {
221 this.collectionObject = obj;
225 public CollectionobjectsCommonList getCommonPartList() {
226 return collectionObjectList;
230 public void setCommonPartList(CollectionobjectsCommonList obj) {
231 this.collectionObjectList = obj;
234 public String getDocumentType() {
235 return CollectionObjectConstants.NUXEO_DOCTYPE;
240 public String getQProperty(String prop) {
241 return CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + prop;