]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
26ef178a1363ab9a84adfe7aedf45a4ddadbc908
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.acquisition;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39
40 import org.collectionspace.services.acquisition.nuxeo.AcquisitionHandlerFactory;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
42 import org.collectionspace.services.common.context.MultipartServiceContext;
43 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.document.DocumentNotFoundException;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
49 import org.jboss.resteasy.util.HttpResponseCodes;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 @Path("/acquisitions")
54 @Consumes("multipart/mixed")
55 @Produces("multipart/mixed")
56 public class AcquisitionResource
57         extends AbstractCollectionSpaceResource {
58
59     final private String serviceName = "acquisitions";
60     final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
61
62     @Override
63     public String getServiceName() {
64         return serviceName;
65     }
66
67     @Override
68     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
69         DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler(
70                 ctx.getRepositoryClientType().toString());
71         docHandler.setServiceContext(ctx);
72         if (ctx.getInput() != null) {
73             Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
74             if (obj != null) {
75                 docHandler.setCommonPart((AcquisitionsCommon) obj);
76             }
77         }
78         return docHandler;
79     }
80
81     public AcquisitionResource() {
82         // do nothing
83     }
84
85     @POST
86     public Response createAcquisition(MultipartInput input) {
87
88         try {
89             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
90             DocumentHandler handler = createDocumentHandler(ctx);
91             String csid = getRepositoryClient(ctx).create(ctx, handler);
92             UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
93             path.path("" + csid);
94             Response response = Response.created(path.build()).build();
95             return response;
96         } catch (Exception e) {
97             if (logger.isDebugEnabled()) {
98                 logger.debug("Caught exception in createAcquisition", e);
99             }
100             Response response = Response.status(
101                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
102             throw new WebApplicationException(response);
103         }
104     }
105
106     @GET
107     @Path("{csid}")
108     public MultipartOutput getAcquisition(
109             @PathParam("csid") String csid) {
110         if (logger.isDebugEnabled()) {
111             logger.debug("getAcquisition with csid=" + csid);
112         }
113         if (csid == null || "".equals(csid)) {
114             logger.error("getAcquisition: missing csid!");
115             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
116                     "get failed on Acquisition csid=" + csid).type(
117                     "text/plain").build();
118             throw new WebApplicationException(response);
119         }
120         MultipartOutput result = null;
121         try {
122             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
123             DocumentHandler handler = createDocumentHandler(ctx);
124             getRepositoryClient(ctx).get(ctx, csid, handler);
125             result = (MultipartOutput) ctx.getOutput();
126         } catch (DocumentNotFoundException dnfe) {
127             if (logger.isDebugEnabled()) {
128                 logger.debug("getAcquisition", dnfe);
129             }
130             Response response = Response.status(Response.Status.NOT_FOUND).entity(
131                     "Get failed on Acquisition csid=" + csid).type(
132                     "text/plain").build();
133             throw new WebApplicationException(response);
134         } catch (Exception e) {
135             if (logger.isDebugEnabled()) {
136                 logger.debug("getAcquisition", e);
137             }
138             Response response = Response.status(
139                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
140             throw new WebApplicationException(response);
141         }
142
143         if (result == null) {
144             Response response = Response.status(Response.Status.NOT_FOUND).entity(
145                     "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
146                     "text/plain").build();
147             throw new WebApplicationException(response);
148         }
149         return result;
150     }
151
152     @GET
153     @Produces("application/xml")
154     public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
155         AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
156         try {
157             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
158             DocumentHandler handler = createDocumentHandler(ctx);
159             getRepositoryClient(ctx).getAll(ctx, handler);
160             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
161         } catch (Exception e) {
162             if (logger.isDebugEnabled()) {
163                 logger.debug("Caught exception in getAcquisitionList", e);
164             }
165             Response response = Response.status(
166                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
167             throw new WebApplicationException(response);
168         }
169         return acquisitionObjectList;
170     }
171
172     @PUT
173     @Path("{csid}")
174     public MultipartOutput updateAcquisition(
175             @PathParam("csid") String csid,
176             MultipartInput theUpdate) {
177         if (logger.isDebugEnabled()) {
178             logger.debug("updateAcquisition with csid=" + csid);
179         }
180         if (csid == null || "".equals(csid)) {
181             logger.error("updateAcquisition: missing csid!");
182             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
183                     "update failed on Acquisition csid=" + csid).type(
184                     "text/plain").build();
185             throw new WebApplicationException(response);
186         }
187         if (logger.isDebugEnabled()) {
188             logger.debug("updateAcquisition with input: ", theUpdate);
189         }
190         MultipartOutput result = null;
191         try {
192             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
193             DocumentHandler handler = createDocumentHandler(ctx);
194             getRepositoryClient(ctx).update(ctx, csid, handler);
195             result = (MultipartOutput) ctx.getOutput();
196         } catch (DocumentNotFoundException dnfe) {
197             if (logger.isDebugEnabled()) {
198                 logger.debug("caugth exception in updateAcquisition", dnfe);
199             }
200             Response response = Response.status(Response.Status.NOT_FOUND).entity(
201                     "Update failed on Acquisition csid=" + csid).type(
202                     "text/plain").build();
203             throw new WebApplicationException(response);
204         } catch (Exception e) {
205             Response response = Response.status(
206                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
207             throw new WebApplicationException(response);
208         }
209         return result;
210     }
211
212     @DELETE
213     @Path("{csid}")
214     public Response deleteAcquisition(@PathParam("csid") String csid) {
215
216         if (logger.isDebugEnabled()) {
217             logger.debug("deleteAcquisition with csid=" + csid);
218         }
219         if (csid == null || "".equals(csid)) {
220             logger.error("deleteAcquisition: missing csid!");
221             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
222                     "delete failed on Acquisition csid=" + csid).type(
223                     "text/plain").build();
224             throw new WebApplicationException(response);
225         }
226         try {
227             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
228             getRepositoryClient(ctx).delete(ctx, csid);
229             return Response.status(HttpResponseCodes.SC_OK).build();
230         } catch (DocumentNotFoundException dnfe) {
231             if (logger.isDebugEnabled()) {
232                 logger.debug("caught exception in deleteAcquisition", dnfe);
233             }
234             Response response = Response.status(Response.Status.NOT_FOUND).entity(
235                     "Delete failed on Acquisition csid=" + csid).type(
236                     "text/plain").build();
237             throw new WebApplicationException(response);
238         } catch (Exception e) {
239             Response response = Response.status(
240                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
241             throw new WebApplicationException(response);
242         }
243
244     }
245 }