]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a4f3974a6ef26b7c532ec41369da9a7af3123b00
[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.io.SAXReader;
33 import org.restlet.resource.Representation;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Path("/persons")
38 @Consumes("application/xml")
39 @Produces("application/xml")
40 public class PersonNuxeoResource {
41
42     final Logger logger = LoggerFactory.getLogger(PersonNuxeoResource.class);
43
44     public PersonNuxeoResource() {
45     }
46
47     @GET
48     public People getPeople(@Context UriInfo ui) {
49         People p = new People();
50         try{
51             List<People.PeopleItem> list = p.getPeopleItem();
52             NuxeoRESTClient nxClient = getClient();
53
54             List<String> pathParams = new ArrayList<String>();
55             Map<String, String> queryParams = new HashMap<String, String>();
56             //browse default repository for People
57             //For sanjay, People repository id is f084243e-4b81-42a1-9a05-518e974facbd
58             pathParams = Arrays.asList("default", "f084243e-4b81-42a1-9a05-518e974facbd", "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("f084243e-4b81-42a1-9a05-518e974facbd");
87         pathParams.add("createDocument");
88         queryParams.put("docType", "Hello");
89         queryParams.put("dublincore:title", p.getFirstName() + " " + p.getLastName());
90         queryParams.put("hello:cversion", p.getVersion());
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         PersonNuxeo p = null;
129         if(p == null){
130             Response response = Response.status(Response.Status.NOT_FOUND).entity(
131                     "Get failed, the requested person ID:" + id + ": was not found.").type("text/plain").build();
132             throw new WebApplicationException(response);
133         }
134         verbose("get person", p);
135         return p;
136     }
137
138     @PUT
139     @Path("{id}")
140     public PersonNuxeo updatePerson(@PathParam("id") String id, PersonNuxeo update) {
141
142         verbose("updating person input", update);
143
144         NuxeoRESTClient nxClient = getClient();
145
146         List<String> pathParams = new ArrayList<String>();
147         Map<String, String> queryParams = new HashMap<String, String>();
148         pathParams.add("default");
149         pathParams.add(update.getId());
150         pathParams.add("updateDocumentRestlet");
151         queryParams.put("dublincore:title", "change title");
152         //todo: intelligent merge needed
153         if(update.getFirstName() != null){
154             queryParams.put("hello:firstName", update.getFirstName());
155         }
156         if(update.getLastName() != null){
157             queryParams.put("hello:lastName", update.getLastName());
158         }
159         if(update.getFirstName() != null && update.getLastName() != null){
160             queryParams.put("dublincore:title", update.getFirstName() + " " + update.getLastName());
161         }
162         if(update.getStreet() != null){
163             queryParams.put("hello:street", update.getStreet());
164         }
165         if(update.getCity() != null){
166             queryParams.put("hello:city", update.getCity());
167         }
168         if(update.getState() != null){
169             queryParams.put("hello:state", update.getState());
170         }
171         if(update.getZip() != null){
172             queryParams.put("hello:zip", update.getZip());
173         }
174         if(update.getCountry() != null){
175             queryParams.put("hello:country", update.getCountry());
176         }
177
178         Representation res = nxClient.get(pathParams, queryParams);
179         SAXReader reader = new SAXReader();
180         String status = "";
181         try{
182             Document document = reader.read(res.getStream());
183             Element root = document.getRootElement();
184             for(Iterator i = root.elementIterator(); i.hasNext();){
185                 Element element = (Element) i.next();
186                 if("docRef".equals(element.getName())){
187                     status = (String) element.getData();
188                     verbose("updatePerson: response=" + status);
189                 }
190
191             }
192         }catch(Exception e){
193             //FIXME: NOT_FOUND?
194             Response response = Response.status(Response.Status.NOT_FOUND).entity(
195                     "Update failed ").type("text/plain").build();
196             throw new WebApplicationException(response);
197         }
198         //returning the same? ... for now
199         return update;
200     }
201
202     @DELETE
203     @Path("{id}")
204     public void deletePerson(@PathParam("id") String id) {
205         verbose("deleting person with id=" + id);
206         NuxeoRESTClient nxClient = getClient();
207         List<String> pathParams = new ArrayList<String>();
208         Map<String, String> queryParams = new HashMap<String, String>();
209         pathParams.add("default");
210         pathParams.add(id);
211         pathParams.add("deleteDocumentRestlet");
212         Representation res = nxClient.get(pathParams, queryParams);
213         SAXReader reader = new SAXReader();
214         String status = "";
215         try{
216             Document document = reader.read(res.getStream());
217             Element root = document.getRootElement();
218             for(Iterator i = root.elementIterator(); i.hasNext();){
219                 Element element = (Element) i.next();
220                 if("docRef".equals(element.getName())){
221                     status = (String) element.getData();
222                     verbose("deletePerson: response=" + status);
223                 }
224
225             }
226         }catch(Exception e){
227             //FIXME: NOT_FOUND?
228             Response response = Response.status(Response.Status.NOT_FOUND).entity(
229                     "Delete failed ").type("text/plain").build();
230             throw new WebApplicationException(response);
231         }
232     }
233
234     private void verbose(String msg, PersonNuxeo p) {
235         try{
236             verbose(msg);
237             JAXBContext jc = JAXBContext.newInstance(
238                     PersonNuxeo.class);
239
240             Marshaller m = jc.createMarshaller();
241             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
242                     Boolean.TRUE);
243             m.marshal(p, System.out);
244         }catch(Exception e){
245             e.printStackTrace();
246         }
247
248     }
249
250 //    private void getQueryModel() throws IOException {
251 //        NuxeoRESTClient nxClient = getClient();
252 //
253 //        List<String> pathParams = new ArrayList<String>();
254 //        Map<String, String> queryParams = new HashMap<String, String>();
255 //
256 //        //query model for user documents
257 //        pathParams = Arrays.asList("execQueryModel", "USER_DOCUMENTS");
258 //        queryParams.put("QP1", "Administrator");
259 //        queryParams.put("format", "XML");
260 //
261 //
262 //        Representation res = nxClient.get(pathParams, queryParams);
263 //        String resStr = res.getText();
264 //        verbose("getQueryModel:" + resStr);
265 //
266 //    }
267 //
268 //    private void getVocabulary() throws IOException {
269 //        NuxeoRESTClient nxClient = getClient();
270 //
271 //        List<String> pathParams = new ArrayList<String>();
272 //        Map<String, String> queryParams = new HashMap<String, String>();
273 //        //get vocabulary
274 //        pathParams = Arrays.asList("vocabulary", "continent_country");
275 //        queryParams.put("lang", "en");
276 //
277 //        Representation res = nxClient.get(pathParams, queryParams);
278 //        String resStr = res.getText();
279 //        verbose("getVocabulary:" + resStr);
280 //
281 //    }
282     private NuxeoRESTClient getClient() {
283         NuxeoRESTClient nxClient = new NuxeoRESTClient("http://127.0.0.1:8080/nuxeo");
284         nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
285         nxClient.setBasicAuthentication("Administrator", "Administrator");
286         return nxClient;
287     }
288
289     private void verbose(String msg) {
290         System.out.println("PersonNuxeoResource: " + msg);
291     }
292 }