]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
db1e31fea0d4348a3a3caabf8ef4e05232c667b9
[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.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;
39
40 /**
41  * CollectionObjectDocumentModelHandler
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public class CollectionObjectRepresenationHandler
47         extends RepresentationHandler<CollectionObject, CollectionObjectList>
48 {
49
50     private final Logger logger = LoggerFactory.getLogger(CollectionObjectRepresenationHandler.class);
51     /**
52      * collectionObject is used to stash JAXB object to use when handle is called
53      * for Action.CREATE, Action.UPDATE or Action.GET
54      */
55     private CollectionObject collectionObject;
56     /**
57      * collectionObjectList is stashed when handle is called
58      * for ACTION.GET_ALL
59      */
60     private CollectionObjectList collectionObjectList;
61
62     @Override
63     public void prepare(Action action) throws Exception {
64         switch(action){
65             case CREATE:
66             case UPDATE:
67                 prepare();
68         }
69     }
70
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());
78         }
79
80         if(co.getOtherNumber() != null){
81             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
82                     ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, co.getOtherNumber());
83         }
84
85         if(co.getBriefDescription() != null){
86             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
87                     ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, co.getBriefDescription());
88         }
89
90         if(co.getComments() != null){
91             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
92                     ":" + CollectionObjectJAXBSchema.COMMENTS, co.getComments());
93         }
94
95         if(co.getDistFeatures() != null){
96             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
97                     ":" + CollectionObjectJAXBSchema.DIST_FEATURES, co.getDistFeatures());
98         }
99
100         if(co.getObjectName() != null){
101             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
102                     ":" + CollectionObjectJAXBSchema.OBJECT_NAME, co.getObjectName());
103         }
104
105         if(co.getResponsibleDept() != null){
106             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
107                     ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, co.getResponsibleDept());
108         }
109
110         if(co.getTitle() != null){
111             queryParams.put(CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME +
112                     ":" + CollectionObjectJAXBSchema.TITLE, co.getTitle());
113         }
114     }
115
116     @Override
117     public CollectionObject extractCommonObject(DocumentWrapper wrapDoc)
118             throws Exception {
119         Document document = (Document) wrapDoc.getWrappedObject();
120         CollectionObject co = new CollectionObject();
121
122         //FIXME property get should be dynamically set using schema inspection
123         //so it does not require hard coding
124         Element root = document.getRootElement();
125
126         // TODO: recognize schema thru namespace uri
127         // Namespace ns = new Namespace("collectionobject",
128         // "http://collectionspace.org/collectionobject");
129
130         Iterator<Element> siter = root.elementIterator("schema");
131         while(siter.hasNext()){
132
133             Element schemaElement = siter.next();
134             if(logger.isDebugEnabled()){
135                 logger.debug("getCommonObject() populating Common Object");
136             }
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);
140                 if(ele != null){
141                     co.setObjectNumber((String) ele.getData());
142                 }
143                 ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
144                 if(ele != null){
145                     co.setOtherNumber((String) ele.getData());
146                 }
147                 ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
148                 if(ele != null){
149                     co.setBriefDescription((String) ele.getData());
150                 }
151                 ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
152                 if(ele != null){
153                     co.setComments((String) ele.getData());
154                 }
155                 ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
156                 if(ele != null){
157                     co.setDistFeatures((String) ele.getData());
158                 }
159                 ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
160                 if(ele != null){
161                     co.setObjectName((String) ele.getData());
162                 }
163                 ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
164                 if(ele != null){
165                     co.setResponsibleDept((String) ele.getData());
166                 }
167                 ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
168                 if(ele != null){
169                     co.setTitle((String) ele.getData());
170                 }
171             }
172         }
173         return co;
174     }
175
176     @Override
177     public void fillCommonObject(CollectionObject co, DocumentWrapper wrapDoc)
178             throws Exception {
179         //Nuxeo REST takes create/update through queryParams, nothing to do here
180     }
181
182     @Override
183     public CollectionObjectList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
184         Document document = (Document) wrapDoc.getWrappedObject();
185         // debug
186         if(logger.isDebugEnabled()){
187             logger.debug(document.asXML());
188         }
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();){
193
194             Element element = (Element) i.next();
195             if(logger.isDebugEnabled()){
196                 logger.debug(element.asXML());
197             }
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);
204
205             list.add(coListItem);
206         }
207         return coList;
208     }
209
210     @Override
211     public void fillCommonObjectList(CollectionObjectList obj, DocumentWrapper wrapDoc)
212             throws Exception {
213         throw new UnsupportedOperationException();
214     }
215
216     @Override
217     public CollectionObject getCommonObject() {
218         return collectionObject;
219     }
220
221     @Override
222     public void setCommonObject(CollectionObject obj) {
223         this.collectionObject = obj;
224     }
225
226     @Override
227     public CollectionObjectList getCommonObjectList() {
228         return collectionObjectList;
229     }
230
231     @Override
232     public void setCommonObjectList(CollectionObjectList obj) {
233         this.collectionObjectList = obj;
234     }
235
236     /**
237      * getQProperty converts the given property to qualified schema property
238      * @param prop
239      * @return
240      */
241     private String getQProperty(String prop) {
242         return CollectionObjectConstants.CO_NUXEO_SCHEMA_NAME + ":" + prop;
243     }
244 }
245