]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
28b6ea95f0e54c1159e5db9c6c7f52c1204620fe
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services;
2
3 import java.util.Iterator;
4 import java.util.List;
5 import javax.ws.rs.Consumes;
6 import javax.ws.rs.GET;
7 import javax.ws.rs.Path;
8 import javax.ws.rs.Produces;
9 import javax.ws.rs.DELETE;
10 import javax.ws.rs.POST;
11 import javax.ws.rs.PUT;
12 import javax.ws.rs.PathParam;
13 import javax.ws.rs.WebApplicationException;
14 import javax.ws.rs.core.Context;
15 import javax.ws.rs.core.Response;
16 import javax.ws.rs.core.UriBuilder;
17 import javax.ws.rs.core.UriInfo;
18 import javax.xml.bind.JAXBContext;
19 import javax.xml.bind.Marshaller;
20
21 import org.collectionspace.services.CollectionObjectService;
22 import org.collectionspace.services.collectionobject.*;
23 import org.collectionspace.services.collectionobject.CollectionObjectList.*;
24 import org.collectionspace.services.CollectionObjectJAXBSchema;
25
26 import org.dom4j.Document;
27 import org.dom4j.Element;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 @Path("/collectionobjects")
32 @Consumes("application/xml")
33 @Produces("application/xml")
34 public class CollectionObjectResource {
35
36         final Logger logger = LoggerFactory
37                         .getLogger(CollectionObjectResource.class);
38
39         // This should be a DI wired by a container like Spring, Seam, or EJB3
40         final static CollectionObjectService service = new CollectionObjectServiceNuxeoImpl();
41
42         public CollectionObjectResource() {
43                 // do nothing
44         }
45
46         @GET
47         public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
48                 CollectionObjectList p = new CollectionObjectList();
49                 try {
50                         Document document = service.getCollectionObjectList();
51                         Element root = document.getRootElement();
52
53                         // debug
54                         System.err.println(document.asXML());
55
56                         List<CollectionObjectList.CollectionObjectListItem> list = p
57                                         .getCollectionObjectListItem();
58                         for (Iterator i = root.elementIterator(); i.hasNext();) {
59                                 Element element = (Element) i.next();
60                                 // debug
61                                 System.err.println();
62                                 element.asXML();
63
64                                 // set the CollectionObject list item entity elements
65                                 CollectionObjectListItem pli = new CollectionObjectListItem();
66                                 pli.setObjectNumber(element.attributeValue("title"));
67                                 pli.setUri(element.attributeValue("url"));
68                                 pli.setCsid(element.attributeValue("id"));
69                                 list.add(pli);
70                         }
71
72                 } catch (Exception e) {
73                         e.printStackTrace();
74                 }
75
76                 return p;
77         }
78
79         @POST
80         public Response createCollectionObject(CollectionObject co) {
81                 
82                 String csid = null;
83                 try {
84
85                         Document document = service.postCollectionObject(co);
86                         Element root = document.getRootElement();
87                         for (Iterator i = root.elementIterator(); i.hasNext();) {
88                                 Element element = (Element) i.next();
89                                 if ("docRef".equals(element.getName())) {
90                                         csid = (String) element.getData();
91                                         co.setCsid(csid);
92                                 }
93                         }
94                 } catch (Exception e) {
95                         Response response = Response.status(Response.Status.NOT_FOUND)
96                                         .entity("Create failed").type("text/plain").build();
97                         throw new WebApplicationException(response);
98                 }
99
100                 verbose("createCollectionObject: ", co);
101                 UriBuilder path = UriBuilder
102                                 .fromResource(CollectionObjectResource.class);
103                 path.path("" + csid);
104                 Response response = Response.created(path.build()).build();
105
106                 return response;
107         }
108
109         @GET
110         @Path("{csid}")
111         public CollectionObject getCollectionObject(@PathParam("csid") String csid) {
112
113                 CollectionObject co = null;
114                 try {
115                         Document document = service.getCollectionObject(csid);
116                         Element root = document.getRootElement();
117                         co = new CollectionObject();
118
119                         // TODO: recognize schema thru namespace uri
120                         // Namespace ns = new Namespace("collectionobject",
121                         // "http://collectionspace.org/collectionobject");
122
123                         Iterator<Element> siter = root.elementIterator("schema");
124                         while (siter.hasNext()) {
125
126                                 Element schemaElement = siter.next();
127                                 System.err
128                                                 .println("CollectionObject.getCollectionObject() called.");
129
130                                 // TODO: recognize schema thru namespace uri
131                                 if (CollectionObjectService.CO_SCHEMA_NAME.equals(schemaElement.attribute("name")
132                                                 .getValue())) {
133                                         Element ele = schemaElement
134                                                         .element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
135                                         if (ele != null) {
136                                                 co.setObjectNumber((String) ele.getData());
137                                         }
138                                         ele = schemaElement
139                                                         .element(CollectionObjectJAXBSchema.OTHER_NUMBER);
140                                         if (ele != null) {
141                                                 co.setOtherNumber((String) ele.getData());
142                                         }
143                                         ele = schemaElement
144                                                         .element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
145                                         if (ele != null) {
146                                                 co.setBriefDescription((String) ele.getData());
147                                         }
148                                         ele = schemaElement
149                                                         .element(CollectionObjectJAXBSchema.COMMENTS);
150                                         if (ele != null) {
151                                                 co.setComments((String) ele.getData());
152                                         }
153                                         ele = schemaElement
154                                                         .element(CollectionObjectJAXBSchema.DIST_FEATURES);
155                                         if (ele != null) {
156                                                 co.setDistFeatures((String) ele.getData());
157                                         }
158                                         ele = schemaElement
159                                                         .element(CollectionObjectJAXBSchema.OBJECT_NAME);
160                                         if (ele != null) {
161                                                 co.setObjectName((String) ele.getData());
162                                         }
163                                         ele = schemaElement
164                                                         .element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
165                                         if (ele != null) {
166                                                 co.setResponsibleDept((String) ele.getData());
167                                         }
168                                         ele = schemaElement
169                                                         .element(CollectionObjectJAXBSchema.TITLE);
170                                         if (ele != null) {
171                                                 co.setTitle((String) ele.getData());
172                                         }
173                                 }
174                         }
175                 } catch (Exception e) {
176                         e.printStackTrace();
177                         Response response = Response.status(
178                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
179                                         .type("text/plain").build();
180                         throw new WebApplicationException(response);
181                 }
182                 if (co == null) {
183                         Response response = Response.status(Response.Status.NOT_FOUND)
184                                         .entity(
185                                                         "Get failed, the requested CollectionObject CSID:"
186                                                                         + csid + ": was not found.").type(
187                                                         "text/plain").build();
188                         throw new WebApplicationException(response);
189                 }
190                 verbose("getCollectionObject: ", co);
191
192                 return co;
193         }
194
195         @PUT
196         @Path("{csid}")
197         public CollectionObject updateCollectionObject(
198                         @PathParam("csid") String csid, CollectionObject theUpdate) {
199
200                 verbose("updateCollectionObject with input: ", theUpdate);
201
202                 String status = null;
203                 try {
204
205                         Document document = service.putCollectionObject(csid, theUpdate);
206                         Element root = document.getRootElement();
207                         for (Iterator i = root.elementIterator(); i.hasNext();) {
208                                 Element element = (Element) i.next();
209                                 if ("docRef".equals(element.getName())) {
210                                         status = (String) element.getData();
211                                         verbose("updateCollectionObject response: " + status);
212                                 }
213                         }
214                 } catch (Exception e) {
215                         // FIXME: NOT_FOUND?
216                         Response response = Response.status(Response.Status.NOT_FOUND)
217                                         .entity("Update failed ").type("text/plain").build();
218                         throw new WebApplicationException(response);
219                 }
220
221                 return theUpdate;
222         }
223
224         @DELETE
225         @Path("{csid}")
226         public void deleteCollectionObject(@PathParam("csid") String csid) {
227
228                 verbose("deleteCollectionObject with csid=" + csid);
229                 try {
230                         
231                         Document document = service.deleteCollectionObject(csid);
232                         Element root = document.getRootElement();
233                         for (Iterator i = root.elementIterator(); i.hasNext();) {
234                                 Element element = (Element) i.next();
235                                 if ("docRef".equals(element.getName())) {
236                                         String status = (String) element.getData();
237                                         verbose("deleteCollectionObjectt response: " + status);
238                                 }
239                         }
240                 } catch (Exception e) {
241                         // FIXME: NOT_FOUND?
242                         Response response = Response.status(Response.Status.NOT_FOUND)
243                                         .entity("Delete failed ").type("text/plain").build();
244                         throw new WebApplicationException(response);
245                 }
246
247         }
248
249         private void verbose(String msg, CollectionObject co) {
250                 try {
251                         verbose(msg);
252                         JAXBContext jc = JAXBContext.newInstance(CollectionObject.class);
253
254                         Marshaller m = jc.createMarshaller();
255                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
256                         m.marshal(co, System.out);
257                 } catch (Exception e) {
258                         e.printStackTrace();
259                 }
260         }
261
262         private void verbose(String msg) {
263                 System.out.println("CollectionObjectResource. " + msg);
264         }
265
266 }