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