1 package org.collectionspace.services;
3 import java.util.Iterator;
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;
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;
26 import org.dom4j.Document;
27 import org.dom4j.Element;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 @Path("/collectionobjects")
32 @Consumes("application/xml")
33 @Produces("application/xml")
34 public class CollectionObjectResource {
36 final Logger logger = LoggerFactory
37 .getLogger(CollectionObjectResource.class);
39 // This should be a DI wired by a container like Spring, Seam, or EJB3
40 final static CollectionObjectService service = new CollectionObjectServiceNuxeoImpl();
42 public CollectionObjectResource() {
47 public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
48 CollectionObjectList p = new CollectionObjectList();
50 Document document = service.getCollectionObjectList();
51 Element root = document.getRootElement();
54 System.err.println(document.asXML());
56 List<CollectionObjectList.CollectionObjectListItem> list = p
57 .getCollectionObjectListItem();
58 for (Iterator i = root.elementIterator(); i.hasNext();) {
59 Element element = (Element) i.next();
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"));
72 } catch (Exception e) {
80 public Response createCollectionObject(CollectionObject co) {
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();
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);
100 verbose("createCollectionObject: ", co);
101 UriBuilder path = UriBuilder
102 .fromResource(CollectionObjectResource.class);
103 path.path("" + csid);
104 Response response = Response.created(path.build()).build();
111 public CollectionObject getCollectionObject(@PathParam("csid") String csid) {
113 CollectionObject co = null;
115 Document document = service.getCollectionObject(csid);
116 Element root = document.getRootElement();
117 co = new CollectionObject();
119 // TODO: recognize schema thru namespace uri
120 // Namespace ns = new Namespace("collectionobject",
121 // "http://collectionspace.org/collectionobject");
123 Iterator<Element> siter = root.elementIterator("schema");
124 while (siter.hasNext()) {
126 Element schemaElement = siter.next();
128 .println("CollectionObject.getCollectionObject() called.");
130 // TODO: recognize schema thru namespace uri
131 if (CollectionObjectService.CO_SCHEMA_NAME.equals(schemaElement.attribute("name")
133 Element ele = schemaElement
134 .element(CollectionObjectJAXBSchema.OBJECT_NUMBER);
136 co.setObjectNumber((String) ele.getData());
139 .element(CollectionObjectJAXBSchema.OTHER_NUMBER);
141 co.setOtherNumber((String) ele.getData());
144 .element(CollectionObjectJAXBSchema.BRIEF_DESCRIPTION);
146 co.setBriefDescription((String) ele.getData());
149 .element(CollectionObjectJAXBSchema.COMMENTS);
151 co.setComments((String) ele.getData());
154 .element(CollectionObjectJAXBSchema.DIST_FEATURES);
156 co.setDistFeatures((String) ele.getData());
159 .element(CollectionObjectJAXBSchema.OBJECT_NAME);
161 co.setObjectName((String) ele.getData());
164 .element(CollectionObjectJAXBSchema.RESPONSIBLE_DEPT);
166 co.setResponsibleDept((String) ele.getData());
169 .element(CollectionObjectJAXBSchema.TITLE);
171 co.setTitle((String) ele.getData());
175 } catch (Exception e) {
177 Response response = Response.status(
178 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
179 .type("text/plain").build();
180 throw new WebApplicationException(response);
183 Response response = Response.status(Response.Status.NOT_FOUND)
185 "Get failed, the requested CollectionObject CSID:"
186 + csid + ": was not found.").type(
187 "text/plain").build();
188 throw new WebApplicationException(response);
190 verbose("getCollectionObject: ", co);
197 public CollectionObject updateCollectionObject(
198 @PathParam("csid") String csid, CollectionObject theUpdate) {
200 verbose("updateCollectionObject with input: ", theUpdate);
202 String status = null;
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);
214 } catch (Exception e) {
216 Response response = Response.status(Response.Status.NOT_FOUND)
217 .entity("Update failed ").type("text/plain").build();
218 throw new WebApplicationException(response);
226 public void deleteCollectionObject(@PathParam("csid") String csid) {
228 verbose("deleteCollectionObject with csid=" + csid);
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);
240 } catch (Exception e) {
242 Response response = Response.status(Response.Status.NOT_FOUND)
243 .entity("Delete failed ").type("text/plain").build();
244 throw new WebApplicationException(response);
249 private void verbose(String msg, CollectionObject co) {
252 JAXBContext jc = JAXBContext.newInstance(CollectionObject.class);
254 Marshaller m = jc.createMarshaller();
255 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
256 m.marshal(co, System.out);
257 } catch (Exception e) {
262 private void verbose(String msg) {
263 System.out.println("CollectionObjectResource. " + msg);