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