]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
634fb006eb301645dc57df364323fb5bad15179b
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
3 import java.io.ByteArrayInputStream;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import javax.ws.rs.Consumes;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.Produces;
13 import java.util.Map;
14 import javax.ws.rs.DELETE;
15 import javax.ws.rs.POST;
16 import javax.ws.rs.PUT;
17 import javax.ws.rs.PathParam;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.Context;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.UriBuilder;
22 import javax.ws.rs.core.UriInfo;
23 import javax.xml.bind.JAXBContext;
24 import javax.xml.bind.Marshaller;
25 import org.collectionspace.hello.*;
26
27
28 import org.collectionspace.hello.services.nuxeo.NuxeoRESTClient;
29 import org.collectionspace.hello.CollectionObjectList.CollectionObjectListItem;
30 import org.collectionspace.hello.services.CollectionObjectJAXBSchema;
31 import org.collectionspace.hello.services.CollectionObjectListItemJAXBSchema;
32
33 import org.dom4j.Document;
34 import org.dom4j.Element;
35 import org.dom4j.Namespace;
36 import org.dom4j.io.SAXReader;
37 import org.restlet.resource.Representation;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @Path("/collectionobjects")
42 @Consumes("application/xml")
43 @Produces("application/xml")
44 public class CollectionObjectResource implements CollectionSpaceResource {
45
46         final static String CO_NUXEO_DOCTYPE = "CollectionObject";
47         final static String CO_NUXEO_SCHEMA_NAME = "collectionobject";
48         final static String CO_NUXEO_DC_TITLE = "CollectionSpace-CollectionObject";
49         
50     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
51
52     public CollectionObjectResource() {
53         // do nothing
54     }
55
56     @GET
57     public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
58         CollectionObjectList p = new CollectionObjectList();
59         try{
60             NuxeoRESTClient nxClient = getClient();
61
62             List<String> pathParams = new ArrayList<String>();
63             Map<String, String> queryParams = new HashMap<String, String>();
64             pathParams = Arrays.asList("default", CS_NUXEO_WORKSPACE_UID, "browse");
65             Representation res = nxClient.get(pathParams, queryParams);
66             SAXReader reader = new SAXReader();
67             Document document = reader.read(res.getStream());
68             Element root = document.getRootElement();
69             
70             //debug
71             System.err.println(res.toString());
72             System.err.println(document.asXML());
73
74             List<CollectionObjectList.CollectionObjectListItem> list = p.getCollectionObjectListItem();
75             for(Iterator i = root.elementIterator(); i.hasNext();){
76                 Element element = (Element) i.next();
77                 //debug
78                 System.err.println();element.asXML();
79
80                 // set the CollectionObject list item entity elements                
81                 CollectionObjectListItem pli = new CollectionObjectListItem();
82                 pli.setObjectNumber(element.attributeValue("title"));
83                 pli.setUri(element.attributeValue("url"));
84                 pli.setCsid(element.attributeValue("id"));
85                 list.add(pli);
86             }
87
88         }catch(Exception e){
89             e.printStackTrace();
90         }
91         
92         return p;
93     }
94
95     @POST
96     public Response createCollectionObject(CollectionObject co) {
97
98         NuxeoRESTClient nxClient = getClient();
99
100         List<String> pathParams = new ArrayList<String>();
101         Map<String, String> queryParams = new HashMap<String, String>();
102         pathParams.add("default");
103         pathParams.add(CS_NUXEO_WORKSPACE_UID);
104         pathParams.add("createDocument");
105         queryParams.put("docType", CO_NUXEO_DOCTYPE);
106         
107         // a default title for the Dublin Core schema
108         queryParams.put("dublincore:title", CO_NUXEO_DC_TITLE);
109         
110         // CollectionObject core values
111         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER, 
112                         co.getObjectNumber());
113         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, 
114                         co.getOtherNumber());
115         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION,
116                         co.getBriefDescription());
117         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.COMMENTS,
118                         co.getComments());
119         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.DIST_FEATURES,
120                         co.getDistFeatures());
121         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NAME,
122                         co.getObjectName());
123         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT,
124                         co.getResponsibleDept());
125         queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.TITLE,
126                         co.getTitle());
127         
128         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
129         Representation res = nxClient.post(pathParams, queryParams, bais);
130
131         String csid = null;
132         SAXReader reader = new SAXReader();
133         try {
134             Document document = reader.read(res.getStream());
135             Element root = document.getRootElement();
136             for (Iterator i = root.elementIterator(); i.hasNext();){
137                 Element element = (Element) i.next();
138                 if ("docRef".equals(element.getName())){
139                     csid = (String) element.getData();
140                     co.setCsid(csid);
141                 }
142             }
143         } catch(Exception e){
144             Response response = Response.status(Response.Status.NOT_FOUND).entity(
145                     "Create failed").type("text/plain").build();
146             throw new WebApplicationException(response);
147         }
148
149         
150         verbose("createCollectionObject: ", co);
151         UriBuilder path = UriBuilder.fromResource(PersonNuxeoResource.class);
152         path.path("" + csid);
153         Response response = Response.created(path.build()).build();
154         
155         return response;
156     }
157
158     @GET
159     @Path("{csid}")
160     public CollectionObject getCollectionObject(@PathParam("csid") String csid) {
161
162         CollectionObject co = null;
163         try {
164             List<String> pathParams = new ArrayList<String>();
165             Map<String, String> queryParams = new HashMap<String, String>();
166
167             pathParams.add("default");
168             pathParams.add(csid);
169             pathParams.add("export");
170             queryParams.put("format", "XML");
171
172             NuxeoRESTClient nxClient = getClient();
173             Representation res = nxClient.get(pathParams, queryParams);
174
175             SAXReader reader = new SAXReader();
176             Document document = reader.read(res.getStream());
177             Element root = document.getRootElement();
178             co = new CollectionObject();
179
180  //                     TODO: recognize schema thru namespace uri
181 //          Namespace ns = new Namespace("collectionobject", "http://collectionspace.org/collectionobject");
182
183             Iterator<Element> siter = root.elementIterator("schema");
184             while (siter.hasNext()) {
185
186                 Element schemaElement = siter.next();
187                 System.err.println("CollectionObject.getCollectionObject() called.");
188
189                 //TODO: recognize schema thru namespace uri
190                 if (CO_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
191                     Element ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
192                     if(ele != null){
193                         co.setObjectNumber((String) ele.getData());
194                     }
195                     ele = schemaElement.element(CollectionObjectJAXBSchema.OTHER_NUMBER);
196                     if(ele != null){
197                         co.setOtherNumber((String) ele.getData());
198                     }
199                     ele = schemaElement.element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
200                     if(ele != null){
201                         co.setBriefDescription((String) ele.getData());
202                     }
203                     ele = schemaElement.element(CollectionObjectJAXBSchema.COMMENTS);
204                     if(ele != null){
205                         co.setComments((String) ele.getData());
206                     }
207                     ele = schemaElement.element(CollectionObjectJAXBSchema.DIST_FEATURES);
208                     if(ele != null){
209                         co.setDistFeatures((String) ele.getData());
210                     }
211                     ele = schemaElement.element(CollectionObjectJAXBSchema.OBJECT_NAME);
212                     if(ele != null){
213                         co.setObjectName((String) ele.getData());
214                     }
215                     ele = schemaElement.element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
216                     if(ele != null){
217                         co.setResponsibleDept((String) ele.getData());
218                     }
219                     ele = schemaElement.element(CollectionObjectJAXBSchema.TITLE);
220                     if(ele != null){
221                         co.setTitle((String) ele.getData());
222                     }
223                 }
224             }
225         } catch(Exception e){
226             e.printStackTrace();
227             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
228                     "Get failed").type("text/plain").build();
229             throw new WebApplicationException(response);
230         }
231         if (co == null) {
232             Response response = Response.status(Response.Status.NOT_FOUND).entity(
233                     "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type("text/plain").build();
234             throw new WebApplicationException(response);
235         }
236         verbose("getCollectionObject: ", co);
237         
238         return co;
239     }
240
241     @PUT
242     @Path("{csid}")
243     public CollectionObject updateCollectionObject(
244             @PathParam("csid") String csid,
245             CollectionObject theUpdate) {
246
247         verbose("updateCollectionObject with input: ", theUpdate);
248
249         List<String> pathParams = new ArrayList<String>();
250         Map<String, String> queryParams = new HashMap<String, String>();
251         pathParams.add("default");
252         pathParams.add(csid);
253         pathParams.add("updateDocumentRestlet");
254         
255         //todo: intelligent merge needed
256         if(theUpdate.getObjectNumber() != null){
257             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NUMBER, 
258                         theUpdate.getObjectNumber());
259         }
260
261         if(theUpdate.getOtherNumber() != null){
262             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OTHER_NUMBER, 
263                         theUpdate.getOtherNumber());
264         }
265
266         if(theUpdate.getBriefDescription()!= null){
267             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.BRIEF_DESCRIPTION, 
268                         theUpdate.getBriefDescription());
269         }
270
271         if(theUpdate.getComments() != null){
272             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.COMMENTS, 
273                         theUpdate.getComments());
274         }
275
276         if(theUpdate.getDistFeatures() != null){
277             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.DIST_FEATURES, 
278                         theUpdate.getDistFeatures());
279         }
280
281         if(theUpdate.getObjectName() != null){
282             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.OBJECT_NAME, 
283                         theUpdate.getObjectName());
284         }
285
286         if(theUpdate.getResponsibleDept() != null){
287             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.RESPONSIBLE_DEPT, 
288                         theUpdate.getResponsibleDept());
289         }
290
291         if(theUpdate.getTitle() != null){
292             queryParams.put(CO_NUXEO_SCHEMA_NAME + ":" + CollectionObjectJAXBSchema.TITLE, 
293                         theUpdate.getTitle());
294         }
295
296         NuxeoRESTClient nxClient = getClient();
297         Representation res = nxClient.get(pathParams, queryParams);
298         SAXReader reader = new SAXReader();
299         String status = null;
300         try {
301             Document document = reader.read(res.getStream());
302             Element root = document.getRootElement();
303             for(Iterator i = root.elementIterator(); i.hasNext();){
304                 Element element = (Element) i.next();
305                 if("docRef".equals(element.getName())){
306                     status = (String) element.getData();
307                     verbose("updateCollectionObject response: " + status);
308                 }
309             }
310         } catch(Exception e) {
311             //FIXME: NOT_FOUND?
312             Response response = Response.status(Response.Status.NOT_FOUND).entity(
313                     "Update failed ").type("text/plain").build();
314             throw new WebApplicationException(response);
315         }
316         
317         return theUpdate;
318     }
319
320     @DELETE
321     @Path("{csid}")
322     public void deleteCollectionObject(@PathParam("csid") String csid) {
323
324         verbose("deleteCollectionObject with csid=" + csid);
325         
326         NuxeoRESTClient nxClient = getClient();
327         List<String> pathParams = new ArrayList<String>();
328         Map<String, String> queryParams = new HashMap<String, String>();
329
330         pathParams.add("default");
331         pathParams.add(csid);
332         pathParams.add("deleteDocumentRestlet");
333         Representation res = nxClient.get(pathParams, queryParams);
334         SAXReader reader = new SAXReader();
335         String status = "";
336         
337         try {
338             Document document = reader.read(res.getStream());
339             Element root = document.getRootElement();
340             for(Iterator i = root.elementIterator(); i.hasNext();){
341                 Element element = (Element) i.next();
342                 if("docRef".equals(element.getName())){
343                     status = (String) element.getData();
344                     verbose("deleteCollectionObjectt response: " + status);
345                 }
346             }
347         }catch(Exception e){
348             //FIXME: NOT_FOUND?
349             Response response = Response.status(Response.Status.NOT_FOUND).entity(
350                     "Delete failed ").type("text/plain").build();
351             throw new WebApplicationException(response);
352         }
353
354     }
355
356     private void verbose(String msg, CollectionObject co) {
357         try {
358             verbose(msg);
359             JAXBContext jc = JAXBContext.newInstance(
360                     CollectionObject.class);
361
362             Marshaller m = jc.createMarshaller();
363             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
364                     Boolean.TRUE);
365             m.marshal(co, System.out);
366         } catch(Exception e){
367             e.printStackTrace();
368         }
369     }
370
371     private NuxeoRESTClient getClient() {
372         NuxeoRESTClient nxClient = new NuxeoRESTClient("http://127.0.0.1:8080/nuxeo");
373         nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
374         nxClient.setBasicAuthentication("Administrator", "Administrator");
375         return nxClient;
376     }
377
378     private void verbose(String msg) {
379         System.out.println("CollectionObjectResource. " + msg);
380     }
381 }