]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dd58dfcf2ac387855b42f9ad42079049cfb345c7
[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 java.util.Map;
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;
41
42 /**
43  * CollectionObjectDocumentModelHandler
44  *
45  * $LastChangedRevision: $
46  * $LastChangedDate: $
47  */
48 public class CollectionObjectRepresenationHandler
49         extends RepresentationHandler<CollectionObject, CollectionObjectList>
50 {
51
52     private final Logger logger = LoggerFactory.getLogger(CollectionObjectRepresenationHandler.class);
53     /**
54      * collectionObject is used to stash JAXB object to use when handle is called
55      * for Action.CREATE, Action.UPDATE or Action.GET
56      */
57     private CollectionObject collectionObject;
58     /**
59      * collectionObjectList is stashed when handle is called
60      * for ACTION.GET_ALL
61      */
62     private CollectionObjectList collectionObjectList;
63
64     @Override
65     public void prepare(Action action) throws Exception {
66         switch(action){
67             case CREATE:
68             case UPDATE:
69                 prepare();
70         }
71     }
72
73     @Override
74     public void handle(Action action, DocumentWrapper wrapDoc)
75             throws Exception {
76         switch(action){
77             case CREATE:
78                 handleCreate(wrapDoc);
79                 break;
80             case UPDATE:
81                 handleUpdate(wrapDoc);
82                 break;
83             case GET:
84                 handleGet(wrapDoc);
85                 break;
86             case GET_ALL:
87                 handleGetAll(wrapDoc);
88                 break;
89         }
90     }
91
92     /**
93      * handleCreate processes create operation response
94      * @param wrapDoc
95      * @throws Exception
96      */
97     public void handleCreate(DocumentWrapper wrapDoc) throws Exception {
98         CollectionObject co = getCommonObject();
99         if(co == null){
100             String msg = "Error creating document: Missing input data";
101             logger.error(msg);
102             throw new IllegalStateException(msg);
103         }
104         //FIXME set other parts as well
105         fillCommonObject(co, wrapDoc);
106     }
107
108     /**
109      * handleUpdate processes update operation response
110      * @param wrapDoc
111      * @throws Exception
112      */
113     public void handleUpdate(DocumentWrapper wrapDoc) throws Exception {
114         CollectionObject co = getCommonObject();
115         if(co == null){
116             String msg = "Error updating document: Missing input data";
117             logger.error(msg);
118             throw new IllegalStateException(msg);
119         }
120         //FIXME set other parts as well
121         fillCommonObject(co, wrapDoc);
122     }
123
124     /**
125      * handleGet processes get operation response
126      * @param wrapDoc
127      * @throws Exception
128      */
129     public void handleGet(DocumentWrapper wrapDoc) throws Exception {
130         CollectionObject co = extractCommonObject(wrapDoc);
131         setCommonObject(co);
132         //FIXME retrive other parts as well
133     }
134
135     /**
136      * handleGetAll processes index operation response
137      * @param wrapDoc
138      * @throws Exception
139      */
140     public void handleGetAll(DocumentWrapper wrapDoc) throws Exception {
141         CollectionObjectList coList = extractCommonObjectList(wrapDoc);
142         setCommonObjectList(coList);
143     }
144
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());
152         }
153
154         if(co.getOtherNumber() != null){
155             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
156                     ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, co.getOtherNumber());
157         }
158
159         if(co.getBriefDescription() != null){
160             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
161                     ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, co.getBriefDescription());
162         }
163
164         if(co.getComments() != null){
165             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
166                     ":" + CollectionObjectJAXBSchema.COMMENTS, co.getComments());
167         }
168
169         if(co.getDistFeatures() != null){
170             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
171                     ":" + CollectionObjectJAXBSchema.DIST_FEATURES, co.getDistFeatures());
172         }
173
174         if(co.getObjectName() != null){
175             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
176                     ":" + CollectionObjectJAXBSchema.OBJECT_NAME, co.getObjectName());
177         }
178
179         if(co.getResponsibleDept() != null){
180             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
181                     ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, co.getResponsibleDept());
182         }
183
184         if(co.getTitle() != null){
185             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
186                     ":" + CollectionObjectJAXBSchema.TITLE, co.getTitle());
187         }
188     }
189
190     @Override
191     public CollectionObject extractCommonObject(DocumentWrapper wrapDoc)
192             throws Exception {
193         Document document = (Document) wrapDoc.getWrappedObject();
194         CollectionObject co = new CollectionObject();
195
196         //FIXME property get should be dynamically set using schema inspection
197         //so it does not require hard coding
198         Element root = document.getRootElement();
199
200         // TODO: recognize schema thru namespace uri
201         // Namespace ns = new Namespace("collectionobject",
202         // "http://collectionspace.org/collectionobject");
203
204         Iterator<Element> siter = root.elementIterator("schema");
205         while(siter.hasNext()){
206
207             Element schemaElement = siter.next();
208             if(logger.isDebugEnabled()){
209                 logger.debug("getCommonObject() populating Common Object");
210             }
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);
214                 if(ele != null){
215                     co.setObjectNumber((String) ele.getData());
216                 }
217                 ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
218                 if(ele != null){
219                     co.setOtherNumber((String) ele.getData());
220                 }
221                 ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
222                 if(ele != null){
223                     co.setBriefDescription((String) ele.getData());
224                 }
225                 ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
226                 if(ele != null){
227                     co.setComments((String) ele.getData());
228                 }
229                 ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
230                 if(ele != null){
231                     co.setDistFeatures((String) ele.getData());
232                 }
233                 ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
234                 if(ele != null){
235                     co.setObjectName((String) ele.getData());
236                 }
237                 ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
238                 if(ele != null){
239                     co.setResponsibleDept((String) ele.getData());
240                 }
241                 ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
242                 if(ele != null){
243                     co.setTitle((String) ele.getData());
244                 }
245             }
246         }
247         return co;
248     }
249
250     @Override
251     public void fillCommonObject(CollectionObject co, DocumentWrapper wrapDoc)
252             throws Exception {
253         //Nuxeo REST takes create/update through queryParams, nothing to do here
254     }
255
256     @Override
257     public CollectionObjectList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
258         Document document = (Document) wrapDoc.getWrappedObject();
259         // debug
260         if(logger.isDebugEnabled()){
261             logger.debug(document.asXML());
262         }
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();){
267
268             Element element = (Element) i.next();
269             if(logger.isDebugEnabled()){
270                 logger.debug(element.asXML());
271             }
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);
278
279             list.add(coListItem);
280         }
281         return coList;
282     }
283
284     @Override
285     public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc)
286             throws Exception {
287         throw new UnsupportedOperationException();
288     }
289
290     @Override
291     public CollectionObject getCommonObject() {
292         return collectionObject;
293     }
294
295     @Override
296     public void setCommonObject(CollectionObject obj) {
297         this.collectionObject = obj;
298     }
299
300     @Override
301     public CollectionObjectList getCommonObjectList() {
302         return collectionObjectList;
303     }
304
305     @Override
306     public void setCommonObjectList(CollectionObjectList obj) {
307         this.collectionObjectList = obj;
308     }
309
310     /**
311      * getQProperty converts the given property to qualified schema property
312      * @param prop
313      * @return
314      */
315     private String getQProperty(String prop) {
316         return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop;
317     }
318 }
319