]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8e606825f5b830a622a1e911e23aca3678c91d75
[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.RemoteServiceContext;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.document.DocumentNotFoundException;
45 import org.collectionspace.services.common.document.DocumentHandler;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.util.HttpResponseCodes;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 @Path("/acquisitions")
53 @Consumes("multipart/mixed")
54 @Produces("multipart/mixed")
55 public class AcquisitionResource
56         extends AbstractCollectionSpaceResource {
57
58     final private String serviceName = "acquisitions";
59     final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
60
61     @Override
62     public String getServiceName() {
63         return serviceName;
64     }
65
66     @Override
67     public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
68         DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler(
69                 ctx.getRepositoryClientType().toString());
70         docHandler.setServiceContext(ctx);
71         if(ctx.getInput() != null){
72             Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
73             if(obj != null){
74                 docHandler.setCommonPart((AcquisitionsCommon) obj);
75             }
76         }
77         return docHandler;
78     }
79
80     public AcquisitionResource() {
81         // do nothing
82     }
83
84     @POST
85     public Response createAcquisition(MultipartInput input) {
86
87         try{
88             RemoteServiceContext ctx = createServiceContext(input);
89             DocumentHandler handler = createDocumentHandler(ctx);
90             String csid = getRepositoryClient(ctx).create(ctx, handler);
91             UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
92             path.path("" + csid);
93             Response response = Response.created(path.build()).build();
94             return response;
95         }catch(Exception e){
96             if(logger.isDebugEnabled()){
97                 logger.debug("Caught exception in createAcquisition", e);
98             }
99             Response response = Response.status(
100                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
101             throw new WebApplicationException(response);
102         }
103     }
104
105     @GET
106     @Path("{csid}")
107     public MultipartOutput getAcquisition(
108             @PathParam("csid") String csid) {
109         if(logger.isDebugEnabled()){
110             logger.debug("getAcquisition with csid=" + csid);
111         }
112         if(csid == null || "".equals(csid)){
113             logger.error("getAcquisition: missing csid!");
114             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
115                     "get failed on Acquisition csid=" + csid).type(
116                     "text/plain").build();
117             throw new WebApplicationException(response);
118         }
119         MultipartOutput result = null;
120         try{
121             RemoteServiceContext ctx = createServiceContext(null);
122             DocumentHandler handler = createDocumentHandler(ctx);
123             getRepositoryClient(ctx).get(ctx, csid, handler);
124             result = ctx.getOutput();
125         }catch(DocumentNotFoundException dnfe){
126             if(logger.isDebugEnabled()){
127                 logger.debug("getAcquisition", dnfe);
128             }
129             Response response = Response.status(Response.Status.NOT_FOUND).entity(
130                     "Get failed on Acquisition csid=" + csid).type(
131                     "text/plain").build();
132             throw new WebApplicationException(response);
133         }catch(Exception e){
134             if(logger.isDebugEnabled()){
135                 logger.debug("getAcquisition", e);
136             }
137             Response response = Response.status(
138                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
139             throw new WebApplicationException(response);
140         }
141
142         if(result == null){
143             Response response = Response.status(Response.Status.NOT_FOUND).entity(
144                     "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
145                     "text/plain").build();
146             throw new WebApplicationException(response);
147         }
148         return result;
149     }
150
151     @GET
152     @Produces("application/xml")
153     public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
154         AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
155         try{
156             RemoteServiceContext ctx = createServiceContext(null);
157             DocumentHandler handler = createDocumentHandler(ctx);
158             getRepositoryClient(ctx).getAll(ctx, handler);
159             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
160         }catch(Exception e){
161             if(logger.isDebugEnabled()){
162                 logger.debug("Caught exception in getAcquisitionList", e);
163             }
164             Response response = Response.status(
165                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
166             throw new WebApplicationException(response);
167         }
168         return acquisitionObjectList;
169     }
170
171     @PUT
172     @Path("{csid}")
173     public MultipartOutput updateAcquisition(
174             @PathParam("csid") String csid,
175             MultipartInput theUpdate) {
176         if(logger.isDebugEnabled()){
177             logger.debug("updateAcquisition with csid=" + csid);
178         }
179         if(csid == null || "".equals(csid)){
180             logger.error("updateAcquisition: missing csid!");
181             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
182                     "update failed on Acquisition csid=" + csid).type(
183                     "text/plain").build();
184             throw new WebApplicationException(response);
185         }
186         if(logger.isDebugEnabled()){
187             logger.debug("updateAcquisition with input: ", theUpdate);
188         }
189         MultipartOutput result = null;
190         try{
191             RemoteServiceContext ctx = createServiceContext(theUpdate);
192             DocumentHandler handler = createDocumentHandler(ctx);
193             getRepositoryClient(ctx).update(ctx, csid, handler);
194             result = ctx.getOutput();
195         }catch(DocumentNotFoundException dnfe){
196             if(logger.isDebugEnabled()){
197                 logger.debug("caugth exception in updateAcquisition", dnfe);
198             }
199             Response response = Response.status(Response.Status.NOT_FOUND).entity(
200                     "Update failed on Acquisition csid=" + csid).type(
201                     "text/plain").build();
202             throw new WebApplicationException(response);
203         }catch(Exception e){
204             Response response = Response.status(
205                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
206             throw new WebApplicationException(response);
207         }
208         return result;
209     }
210
211     @DELETE
212     @Path("{csid}")
213     public Response deleteAcquisition(@PathParam("csid") String csid) {
214
215         if(logger.isDebugEnabled()){
216             logger.debug("deleteAcquisition with csid=" + csid);
217         }
218         if(csid == null || "".equals(csid)){
219             logger.error("deleteAcquisition: missing csid!");
220             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
221                     "delete failed on Acquisition csid=" + csid).type(
222                     "text/plain").build();
223             throw new WebApplicationException(response);
224         }
225         try{
226             ServiceContext ctx = createServiceContext(null);
227             getRepositoryClient(ctx).delete(ctx, csid);
228             return Response.status(HttpResponseCodes.SC_OK).build();
229         }catch(DocumentNotFoundException dnfe){
230             if(logger.isDebugEnabled()){
231                 logger.debug("caught exception in deleteAcquisition", dnfe);
232             }
233             Response response = Response.status(Response.Status.NOT_FOUND).entity(
234                     "Delete failed on Acquisition csid=" + csid).type(
235                     "text/plain").build();
236             throw new WebApplicationException(response);
237         }catch(Exception e){
238             Response response = Response.status(
239                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
240             throw new WebApplicationException(response);
241         }
242
243     }
244
245 }