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