]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
50149d8ce86abd138d8d883b83c3fbe3efeada64
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
3 import java.io.ByteArrayInputStream;
4 import org.collectionspace.hello.services.nuxeo.NuxeoRESTClient;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.List;
10 import javax.ws.rs.Consumes;
11 import javax.ws.rs.GET;
12 import javax.ws.rs.Path;
13 import javax.ws.rs.Produces;
14 import java.util.Map;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.POST;
17 import javax.ws.rs.PUT;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.Context;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24 import javax.xml.bind.JAXBContext;
25 import javax.xml.bind.Marshaller;
26 import org.collectionspace.hello.*;
27
28
29 import org.collectionspace.hello.People.PeopleItem;
30 import org.dom4j.Document;
31 import org.dom4j.Element;
32 import org.dom4j.Namespace;
33 import org.dom4j.io.SAXReader;
34 import org.restlet.resource.Representation;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @Path("/persons")
39 @Consumes("application/xml")
40 @Produces("application/xml")
41 public class PersonNuxeoResource extends CollectionSpaceResource {
42
43     final Logger logger = LoggerFactory.getLogger(PersonNuxeoResource.class);
44
45     public PersonNuxeoResource() {
46     }
47
48     @GET
49     public People getPeople(@Context UriInfo ui) {
50         People p = new People();
51         try{
52             List<People.PeopleItem> list = p.getPeopleItem();
53             NuxeoRESTClient nxClient = getClient();
54
55             List<String> pathParams = new ArrayList<String>();
56             Map<String, String> queryParams = new HashMap<String, String>();
57
58             pathParams = Arrays.asList("default", CS_PERSON_WORKSPACE_UID, "browse");
59             Representation res = nxClient.get(pathParams, queryParams);
60             SAXReader reader = new SAXReader();
61             Document document = reader.read(res.getStream());
62             Element root = document.getRootElement();
63             for(Iterator i = root.elementIterator(); i.hasNext();){
64                 Element element = (Element) i.next();
65                 PeopleItem pli = new PeopleItem();
66                 pli.setTitle(element.attributeValue("title"));
67                 pli.setUri(element.attributeValue("url"));
68                 pli.setId(element.attributeValue("id"));
69                 list.add(pli);
70             }
71
72         }catch(Exception e){
73             e.printStackTrace();
74         }
75         return p;
76     }
77
78     @POST
79     public Response createPerson(PersonNuxeo p) {
80
81         NuxeoRESTClient nxClient = getClient();
82
83         List<String> pathParams = new ArrayList<String>();
84         Map<String, String> queryParams = new HashMap<String, String>();
85         pathParams.add("default");
86         pathParams.add(CS_PERSON_WORKSPACE_UID);
87         pathParams.add("createDocument");
88         queryParams.put("docType", "Hello");
89         queryParams.put("dublincore:title", p.getFirstName() + " " + p.getLastName());
90         queryParams.put("hello:cversion", Integer.valueOf(1).toString());
91         queryParams.put("hello:firstName", p.getFirstName());
92         queryParams.put("hello:lastName", p.getLastName());
93         queryParams.put("hello:street", p.getStreet());
94         queryParams.put("hello:city", p.getCity());
95         queryParams.put("hello:state", p.getState());
96         queryParams.put("hello:zip", p.getZip());
97         queryParams.put("hello:country", p.getCountry());
98         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
99         Representation res = nxClient.post(pathParams, queryParams,bais);
100
101         SAXReader reader = new SAXReader();
102         try{
103             Document document = reader.read(res.getStream());
104             Element root = document.getRootElement();
105             for(Iterator i = root.elementIterator(); i.hasNext();){
106                 Element element = (Element) i.next();
107                 if("docRef".equals(element.getName())){
108                     String id = (String) element.getData();
109                     p.setId(id);
110                 }
111             }
112         }catch(Exception e){
113             Response response = Response.status(Response.Status.NOT_FOUND).entity(
114                     "Create failed").type("text/plain").build();
115             throw new WebApplicationException(response);
116         }
117
118         verbose("created person", p);
119         UriBuilder path = UriBuilder.fromResource(PersonNuxeoResource.class);
120         path.path("" + p.getId());
121         Response response = Response.created(path.build()).build();
122         return response;
123     }
124
125     @GET
126     @Path("{id}")
127     public PersonNuxeo getPerson(@PathParam("id") String id) {
128
129         PersonNuxeo p = null;
130         try{
131
132             NuxeoRESTClient nxClient = getClient();
133
134             List<String> pathParams = new ArrayList<String>();
135             Map<String, String> queryParams = new HashMap<String, String>();
136
137             pathParams.add("default");
138             pathParams.add(id);
139             pathParams.add("export");
140             queryParams.put("format", "XML");
141
142             Representation res = nxClient.get(pathParams, queryParams);
143             SAXReader reader = new SAXReader();
144
145             Document document = reader.read(res.getStream());
146             Element root = document.getRootElement();
147             p = new PersonNuxeo();
148             //TODO: recognize schema thru namespace uri
149 //            Namespace ns = new Namespace("hello", "http://collectionspace.org/hello");
150             Iterator<Element> siter = root.elementIterator("schema");
151             while(siter.hasNext()){
152
153                 Element s = siter.next();
154
155                 //TODO: recognize schema thru namespace uri
156                 if("hello".equals(s.attribute("name").getValue())){
157                     p.setId(id);
158                     Element ele = s.element("cversion");
159                     if(ele != null){
160                         p.setVersion((String) ele.getData());
161                     }
162                     ele = s.element("firstName");
163                     if(ele != null){
164                         p.setFirstName((String) ele.getData());
165                     }
166                     ele = s.element("lastName");
167                     if(ele != null){
168                         p.setLastName((String) ele.getData());
169                     }
170                     ele = s.element("city");
171                     if(ele != null){
172                         p.setCity((String) ele.getData());
173                     }
174                     ele = s.element("state");
175                     if(ele != null){
176                         p.setState((String) ele.getData());
177                     }
178                     ele = s.element("zip");
179                     if(ele != null){
180                         p.setZip((String) ele.getData());
181                     }
182                     ele = s.element("country");
183                     if(ele != null){
184                         p.setCountry((String) ele.getData());
185                     }
186                 }
187             }
188
189         }catch(Exception e){
190             e.printStackTrace();
191             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
192                     "Get failed").type("text/plain").build();
193             throw new WebApplicationException(response);
194         }
195         if(p == null){
196             Response response = Response.status(Response.Status.NOT_FOUND).entity(
197                     "Get failed, the requested person ID:" + id + ": was not found.").type("text/plain").build();
198             throw new WebApplicationException(response);
199         }
200         verbose("get person", p);
201         return p;
202     }
203
204     @PUT
205     @Path("{id}")
206     public PersonNuxeo updatePerson(
207             @PathParam("id") String id,
208             PersonNuxeo update) {
209
210         verbose("updating person input", update);
211
212         NuxeoRESTClient nxClient = getClient();
213
214         List<String> pathParams = new ArrayList<String>();
215         Map<String, String> queryParams = new HashMap<String, String>();
216         pathParams.add("default");
217         pathParams.add(update.getId());
218         pathParams.add("updateDocumentRestlet");
219         queryParams.put("dublincore:title", "change title");
220         //todo: intelligent merge needed
221         if(update.getFirstName() != null){
222             queryParams.put("hello:firstName", update.getFirstName());
223         }
224
225         if(update.getLastName() != null){
226             queryParams.put("hello:lastName", update.getLastName());
227         }
228
229         if(update.getFirstName() != null && update.getLastName() != null){
230             queryParams.put("dublincore:title", update.getFirstName() + " " + update.getLastName());
231         }
232
233         if(update.getStreet() != null){
234             queryParams.put("hello:street", update.getStreet());
235         }
236
237         if(update.getCity() != null){
238             queryParams.put("hello:city", update.getCity());
239         }
240
241         if(update.getState() != null){
242             queryParams.put("hello:state", update.getState());
243         }
244
245         if(update.getZip() != null){
246             queryParams.put("hello:zip", update.getZip());
247         }
248
249         if(update.getCountry() != null){
250             queryParams.put("hello:country", update.getCountry());
251         }
252
253         Representation res = nxClient.get(pathParams, queryParams);
254         SAXReader reader = new SAXReader();
255         String status = "";
256         try{
257             Document document = reader.read(res.getStream());
258             Element root = document.getRootElement();
259             for(Iterator i = root.elementIterator(); i.hasNext();){
260                 Element element = (Element) i.next();
261                 if("docRef".equals(element.getName())){
262                     status = (String) element.getData();
263                     verbose("updatePerson: response=" + status);
264                 }
265
266             }
267         }catch(Exception e){
268             //FIXME: NOT_FOUND?
269             Response response = Response.status(Response.Status.NOT_FOUND).entity(
270                     "Update failed ").type("text/plain").build();
271             throw new WebApplicationException(response);
272         }
273         return update;
274     }
275
276     @DELETE
277     @Path("{id}")
278     public void deletePerson(@PathParam("id") String id) {
279         verbose("deleting person with id=" + id);
280         NuxeoRESTClient nxClient = getClient();
281         List<String> pathParams = new ArrayList<String>();
282         Map<String, String> queryParams = new HashMap<String, String>();
283         pathParams.add("default");
284         pathParams.add(id);
285         pathParams.add("deleteDocumentRestlet");
286         Representation res = nxClient.get(pathParams, queryParams);
287         SAXReader reader = new SAXReader();
288         String status = "";
289         try{
290             Document document = reader.read(res.getStream());
291             Element root = document.getRootElement();
292             for(Iterator i = root.elementIterator(); i.hasNext();){
293                 Element element = (Element) i.next();
294                 if("docRef".equals(element.getName())){
295                     status = (String) element.getData();
296                     verbose("deletePerson: response=" + status);
297                 }
298
299             }
300         }catch(Exception e){
301             //FIXME: NOT_FOUND?
302             Response response = Response.status(Response.Status.NOT_FOUND).entity(
303                     "Delete failed ").type("text/plain").build();
304             throw new WebApplicationException(response);
305         }
306
307     }
308
309     private void verbose(String msg, PersonNuxeo p) {
310         try{
311             verbose(msg);
312             JAXBContext jc = JAXBContext.newInstance(
313                     PersonNuxeo.class);
314
315             Marshaller m = jc.createMarshaller();
316             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
317                     Boolean.TRUE);
318             m.marshal(p, System.out);
319         } catch(Exception e){
320             e.printStackTrace();
321         }
322
323     }
324
325 //    private void getQueryModel() throws IOException {
326 //        NuxeoRESTClient nxClient = getClient();
327 //
328 //        List<String> pathParams = new ArrayList<String>();
329 //        Map<String, String> queryParams = new HashMap<String, String>();
330 //
331 //        //query model for user documents
332 //        pathParams = Arrays.asList("execQueryModel", "USER_DOCUMENTS");
333 //        queryParams.put("QP1", "Administrator");
334 //        queryParams.put("format", "XML");
335 //
336 //
337 //        Representation res = nxClient.get(pathParams, queryParams);
338 //        String resStr = res.getText();
339 //        verbose("getQueryModel:" + resStr);
340 //
341 //    }
342 //
343 //    private void getVocabulary() throws IOException {
344 //        NuxeoRESTClient nxClient = getClient();
345 //
346 //        List<String> pathParams = new ArrayList<String>();
347 //        Map<String, String> queryParams = new HashMap<String, String>();
348 //        //get vocabulary
349 //        pathParams = Arrays.asList("vocabulary", "continent_country");
350 //        queryParams.put("lang", "en");
351 //
352 //        Representation res = nxClient.get(pathParams, queryParams);
353 //        String resStr = res.getText();
354 //        verbose("getVocabulary:" + resStr);
355 //
356 //    }
357
358     private void verbose(String msg) {
359         System.out.println("PersonNuxeoResource: " + msg);
360     }
361 }