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