]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1197cc7cf163672dec80e4fe817838bc0ba435a9
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
3 import java.net.URI;
4 import java.util.List;
5 import javax.ws.rs.Consumes;
6 import javax.ws.rs.GET;
7 import javax.ws.rs.POST;
8 import javax.ws.rs.PUT;
9 import javax.ws.rs.Path;
10 import javax.ws.rs.PathParam;
11 import javax.ws.rs.Produces;
12 import javax.ws.rs.WebApplicationException;
13 import javax.ws.rs.core.Response;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.AtomicLong;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.core.Context;
19 import javax.ws.rs.core.UriBuilder;
20 import javax.ws.rs.core.UriInfo;
21 import javax.xml.bind.JAXBContext;
22 import javax.xml.bind.Marshaller;
23 import org.collectionspace.hello.CollectionObject;
24 import org.collectionspace.hello.CollectionObjectList;
25 import org.collectionspace.hello.CollectionObjectListItem;
26 import org.collectionspace.hello.DefaultCollectionObject;
27 import org.collectionspace.hello.ServiceMetadata;
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.getLogger(CollectionObjectResource.class);
37     private Map<String, CollectionObject> CollectionObjectDB =
38       new ConcurrentHashMap<String, CollectionObject>();
39     private AtomicLong idCounter = new AtomicLong();
40
41     public CollectionObjectResource() {
42     }
43
44     @POST
45     public Response createCollectionObject(CollectionObject c) {
46       if (c == null) {
47         Response response = Response.status(Response.Status.BAD_REQUEST).entity(
48           "Add failed, the CollectionObject provided was empty.").type("text/plain").build();
49         throw new WebApplicationException(response);
50       }
51       Long id = idCounter.incrementAndGet();
52       // c.getServiceMetadata().setCollectionSpaceId(id.toString());
53       c.setServiceMetadata( new ServiceMetadata() );
54       c.getServiceMetadata().setCollectionSpaceId("100");
55       // c.setVersion(1);
56       CollectionObjectDB.put(c.getServiceMetadata().getCollectionSpaceId(), c);
57       verbose("created CollectionObject", c);
58       UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
59       path.path("" + c.getServiceMetadata().getCollectionSpaceId());
60       Response response = Response.created(path.build()).build();
61       return response;
62     }
63
64     @GET
65     @Path("{id}")
66     public CollectionObject getCollectionObject(@PathParam("id") String id) {
67       CollectionObject c = CollectionObjectDB.get(id);
68       if (c == null) {
69         Response response = Response.status(Response.Status.NOT_FOUND).entity(
70           "Get failed, the requested CollectionObject ID:" + id + ": was not found.").type("text/plain").build();
71         throw new WebApplicationException(response);
72       }
73       verbose("get CollectionObject", c);
74       return c;
75     }
76
77     @PUT
78     @Path("{id}")
79     public CollectionObject updateCollectionObject(@PathParam("id") String id, CollectionObject update) {
80       CollectionObject current = CollectionObjectDB.get(id);
81       if (current == null) {
82         Response response = Response.status(Response.Status.NOT_FOUND).entity(
83           "Update failed, the CollectionObject ID:" + id + ": was not found.").type("text/plain").build();
84         throw new WebApplicationException(response);
85       }
86       verbose("update CollectionObject input", update);
87       //todo: intelligent merge needed
88       // current.getServiceMetadata().setLastUpdated( [current date/time here] );
89       current.getDefaultCollectionObject().setObjectNumber(
90         update.getDefaultCollectionObject().getObjectNumber());
91       current.getDefaultCollectionObject().setOtherNumber(
92         update.getDefaultCollectionObject().getOtherNumber());
93       current.getDefaultCollectionObject().setBriefDescription(
94         update.getDefaultCollectionObject().getBriefDescription());
95       current.getDefaultCollectionObject().setComments(
96         update.getDefaultCollectionObject().getComments());
97       current.getDefaultCollectionObject().setDistinguishingFeatures(
98         update.getDefaultCollectionObject().getDistinguishingFeatures());
99       current.getDefaultCollectionObject().setObjectName(
100         update.getDefaultCollectionObject().getObjectName());
101       current.getDefaultCollectionObject().setResponsibleDepartment(
102         update.getDefaultCollectionObject().getResponsibleDepartment());
103         verbose("update CollectionObject output", current);
104       return current;
105     }
106
107     // Get a list
108     @GET
109     public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
110       CollectionObjectList CollectionObjectList = new CollectionObjectList();
111       // The auto-generated method called here has a potentially misleading name; it returns a List.
112       List<CollectionObjectListItem> list =
113         CollectionObjectList.getCollectionObjectListItem();
114       // builder starts with current URI and has appended path of getCollectionObject method
115       UriBuilder ub = ui.getAbsolutePathBuilder().path(this.getClass(), "getCollectionObject");
116       for (CollectionObject c : CollectionObjectDB.values()) {
117         CollectionObjectListItem cli = new CollectionObjectListItem();
118         cli.setCollectionSpaceId(c.getServiceMetadata().getCollectionSpaceId());
119         cli.setObjectNumber(c.getDefaultCollectionObject().getObjectNumber());
120         cli.setObjectName(c.getDefaultCollectionObject().getObjectName());
121         // builder has {id} variable that must be filled in for each customer
122         URI uri = ub.build(c.getServiceMetadata().getCollectionSpaceId());
123         cli.setUri(uri.toString());
124         list.add(cli);
125       }
126       return CollectionObjectList;
127     }
128
129     @DELETE
130     @Path("{id}")
131     public void deleteCollectionObject(@PathParam("id") String id) {
132       CollectionObject removed = CollectionObjectDB.remove(id);
133       if (removed == null) {
134         Response response = Response.status(Response.Status.NOT_FOUND).entity(
135           "Delete failed, the CollectionObject ID:" + id + ": was not found.").type("text/plain").build();
136         throw new WebApplicationException(response);
137       }
138       verbose("deleted CollectionObject", removed);
139     }
140
141     private void verbose(String msg, CollectionObject c) {
142       try {
143         System.out.println("CollectionObjectResource : " + msg);
144         JAXBContext jc = JAXBContext.newInstance(CollectionObject.class);
145         Marshaller m = jc.createMarshaller();
146         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
147         m.marshal(c, System.out);
148       } catch (Exception e) {
149         e.printStackTrace();
150       }
151     }
152
153 //    @POST
154 //    @Consumes("application/xml")
155 //    public Response createCollectionObject(InputStream is) {
156 //        CollectionObject c = readCollectionObject(is);
157 //        c.setId(idCounter.incrementAndGet());
158 //        c.setVersion(1);
159 //        CollectionObjectDB.put(c.getId(), c);
160 //        try {
161 //            System.out.println("Created CollectionObject " + c.getId());
162 //            outputCollectionObject(System.out, c);
163 //        } catch (IOException ioe) {
164 //            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
165 //        }
166 //        return Response.created(URI.create("/CollectionObjects/" + c.getId())).build();
167 //
168 //    }
169 //
170 //    @GET
171 //    @Path("{id}")
172 //    @Produces("application/xml")
173 //    public StreamingOutput getCollectionObject(@PathParam("id") int id) {
174 //        final CollectionObject c = CollectionObjectDB.get(id);
175 //        if (c == null) {
176 //            throw new WebApplicationException(Response.Status.NOT_FOUND);
177 //        }
178 //        return new StreamingOutput() {
179 //
180 //            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
181 //                outputCollectionObject(outputStream, c);
182 //            }
183 //        };
184 //    }
185 //
186 //    @PUT
187 //    @Path("{id}")
188 //    @Consumes("application/xml")
189 //    @Produces("application/xml")
190 //    public StreamingOutput updateCollectionObject(@PathParam("id") int id, InputStream is) {
191 //        CollectionObject update = readCollectionObject(is);
192 //        CollectionObject current = CollectionObjectDB.get(id);
193 //        if (current == null) {
194 //            throw new WebApplicationException(Response.Status.NOT_FOUND);
195 //        }
196 //
197 //        current.setFirstName(update.getFirstName());
198 //        current.setLastName(update.getLastName());
199 //        current.setStreet(update.getStreet());
200 //        current.setState(update.getState());
201 //        current.setZip(update.getZip());
202 //        current.setCountry(update.getCountry());
203 //        current.setVersion(current.getVersion() + 1);
204 //        final CollectionObject scurrent = current;
205 //        return new StreamingOutput() {
206 //
207 //            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
208 //                outputCollectionObject(outputStream, scurrent);
209 //            }
210 //        };
211 //    }
212 //
213 //    protected void outputCollectionObject(OutputStream os, CollectionObject c) throws IOException {
214 //        PrintStream writer = new PrintStream(os);
215 //        writer.println("<CollectionObject id=\"" + c.getId() + "\" version=\"" + c.getVersion() + "\">");
216 //        writer.println("   <first-name>" + c.getFirstName() + "</first-name>");
217 //        writer.println("   <last-name>" + c.getLastName() + "</last-name>");
218 //        writer.println("   <street>" + c.getStreet() + "</street>");
219 //        writer.println("   <city>" + c.getCity() + "</city>");
220 //        writer.println("   <state>" + c.getState() + "</state>");
221 //        writer.println("   <zip>" + c.getZip() + "</zip>");
222 //        writer.println("   <country>" + c.getCountry() + "</country>");
223 //        writer.println("</CollectionObject>");
224 //    }
225 //
226 //    protected CollectionObject readCollectionObject(InputStream is) {
227 //        try {
228 //            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
229 //            Document doc = builder.parse(is);
230 //            Element root = doc.getDocumentElement();
231 //            CollectionObject c = new CollectionObject();
232 //            if (root.getAttribute("id") != null && !root.getAttribute("id").trim().equals("")) {
233 //                c.setId(Integer.valueOf(root.getAttribute("id")));
234 //            }
235 //            if (root.getAttribute("version") != null && !root.getAttribute("version").trim().equals("")) {
236 //                c.setVersion(Integer.valueOf(root.getAttribute("version")));
237 //            }
238 //            NodeList nodes = root.getChildNodes();
239 //            for (int i = 0; i < nodes.getLength(); i++) {
240 //                Element element = (Element) nodes.item(i);
241 //                if (element.getTagName().equals("first-name")) {
242 //                    c.setFirstName(element.getTextContent());
243 //                } else if (element.getTagName().equals("last-name")) {
244 //                    c.setLastName(element.getTextContent());
245 //                } else if (element.getTagName().equals("street")) {
246 //                    c.setStreet(element.getTextContent());
247 //                } else if (element.getTagName().equals("city")) {
248 //                    c.setCity(element.getTextContent());
249 //                } else if (element.getTagName().equals("state")) {
250 //                    c.setState(element.getTextContent());
251 //                } else if (element.getTagName().equals("zip")) {
252 //                    c.setZip(element.getTextContent());
253 //                } else if (element.getTagName().equals("country")) {
254 //                    c.setCountry(element.getTextContent());
255 //                }
256 //            }
257 //            return c;
258 //        } catch (Exception e) {
259 //            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
260 //        }
261 //    }
262 }