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