]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
eaab128527eb1ff3b9d779443836c10960b83526
[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.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
39 import javax.ws.rs.core.UriInfo;
40
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.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.collectionspace.services.common.document.DocumentHandlerFactory;
49 import org.collectionspace.services.common.query.IQueryManager;
50 import org.collectionspace.services.common.query.QueryManager;
51 import org.collectionspace.services.common.security.UnauthorizedException;
52 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
53 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
54 import org.jboss.resteasy.util.HttpResponseCodes;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 @Path("/acquisitions")
59 @Consumes("multipart/mixed")
60 @Produces("multipart/mixed")
61 public class AcquisitionResource
62         extends AbstractCollectionSpaceResource {
63
64     final private String serviceName = "acquisitions";
65     final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
66
67     @Override
68     protected String getVersionString() {
69         /** The last change revision. */
70         final String lastChangeRevision = "$LastChangedRevision$";
71         return lastChangeRevision;
72     }
73     
74     @Override
75     public String getServiceName() {
76         return serviceName;
77     }
78
79     @Override
80     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
81         DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
82                 ctx.getDocumentHandlerClass());
83         docHandler.setServiceContext(ctx);
84         if (ctx.getInput() != null) {
85             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
86             if (obj != null) {
87                 docHandler.setCommonPart((AcquisitionsCommon) obj);
88             }
89         }
90         return docHandler;
91     }
92
93     public AcquisitionResource() {
94         // do nothing
95     }
96
97     @POST
98     public Response createAcquisition(MultipartInput input) {
99
100         try {
101             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
102             DocumentHandler handler = createDocumentHandler(ctx);
103             String csid = getRepositoryClient(ctx).create(ctx, handler);
104             UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
105             path.path("" + csid);
106             Response response = Response.created(path.build()).build();
107             return response;
108         } catch (UnauthorizedException ue) {
109             Response response = Response.status(
110                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
111             throw new WebApplicationException(response);
112         } catch (Exception e) {
113             if (logger.isDebugEnabled()) {
114                 logger.debug("Caught exception in createAcquisition", e);
115             }
116             Response response = Response.status(
117                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
118             throw new WebApplicationException(response);
119         }
120     }
121
122     @GET
123     @Path("{csid}")
124     public MultipartOutput getAcquisition(
125             @PathParam("csid") String csid) {
126         if (logger.isDebugEnabled()) {
127             logger.debug("getAcquisition with csid=" + csid);
128         }
129         if (csid == null || "".equals(csid)) {
130             logger.error("getAcquisition: missing csid!");
131             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
132                     "get failed on Acquisition csid=" + csid).type(
133                     "text/plain").build();
134             throw new WebApplicationException(response);
135         }
136         MultipartOutput result = null;
137         try {
138             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
139             DocumentHandler handler = createDocumentHandler(ctx);
140             getRepositoryClient(ctx).get(ctx, csid, handler);
141             result = (MultipartOutput) ctx.getOutput();
142         } catch (UnauthorizedException ue) {
143             Response response = Response.status(
144                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
145             throw new WebApplicationException(response);
146         } catch (DocumentNotFoundException dnfe) {
147             if (logger.isDebugEnabled()) {
148                 logger.debug("getAcquisition", dnfe);
149             }
150             Response response = Response.status(Response.Status.NOT_FOUND).entity(
151                     "Get failed on Acquisition csid=" + csid).type(
152                     "text/plain").build();
153             throw new WebApplicationException(response);
154         } catch (Exception e) {
155             if (logger.isDebugEnabled()) {
156                 logger.debug("getAcquisition", e);
157             }
158             Response response = Response.status(
159                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
160             throw new WebApplicationException(response);
161         }
162
163         if (result == null) {
164             Response response = Response.status(Response.Status.NOT_FOUND).entity(
165                     "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
166                     "text/plain").build();
167             throw new WebApplicationException(response);
168         }
169         return result;
170     }
171
172     @GET
173     @Produces("application/xml")
174     public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
175         AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
176         try {
177             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
178             DocumentHandler handler = createDocumentHandler(ctx);
179             getRepositoryClient(ctx).getAll(ctx, handler);
180             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
181         } catch (UnauthorizedException ue) {
182             Response response = Response.status(
183                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
184             throw new WebApplicationException(response);
185         } catch (Exception e) {
186             if (logger.isDebugEnabled()) {
187                 logger.debug("Caught exception in getAcquisitionList", e);
188             }
189             Response response = Response.status(
190                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
191             throw new WebApplicationException(response);
192         }
193         return acquisitionObjectList;
194     }
195
196     @PUT
197     @Path("{csid}")
198     public MultipartOutput updateAcquisition(
199             @PathParam("csid") String csid,
200             MultipartInput theUpdate) {
201         if (logger.isDebugEnabled()) {
202             logger.debug("updateAcquisition with csid=" + csid);
203         }
204         if (csid == null || "".equals(csid)) {
205             logger.error("updateAcquisition: missing csid!");
206             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
207                     "update failed on Acquisition csid=" + csid).type(
208                     "text/plain").build();
209             throw new WebApplicationException(response);
210         }
211         if (logger.isDebugEnabled()) {
212             logger.debug("updateAcquisition with input: ", theUpdate);
213         }
214         MultipartOutput result = null;
215         try {
216             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
217             DocumentHandler handler = createDocumentHandler(ctx);
218             getRepositoryClient(ctx).update(ctx, csid, handler);
219             result = (MultipartOutput) ctx.getOutput();
220         } catch (UnauthorizedException ue) {
221             Response response = Response.status(
222                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
223             throw new WebApplicationException(response);
224         } catch (DocumentNotFoundException dnfe) {
225             if (logger.isDebugEnabled()) {
226                 logger.debug("caugth exception in updateAcquisition", dnfe);
227             }
228             Response response = Response.status(Response.Status.NOT_FOUND).entity(
229                     "Update failed on Acquisition csid=" + csid).type(
230                     "text/plain").build();
231             throw new WebApplicationException(response);
232         } catch (Exception e) {
233             Response response = Response.status(
234                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
235             throw new WebApplicationException(response);
236         }
237         return result;
238     }
239
240     @DELETE
241     @Path("{csid}")
242     public Response deleteAcquisition(@PathParam("csid") String csid) {
243
244         if (logger.isDebugEnabled()) {
245             logger.debug("deleteAcquisition with csid=" + csid);
246         }
247         if (csid == null || "".equals(csid)) {
248             logger.error("deleteAcquisition: missing csid!");
249             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
250                     "delete failed on Acquisition csid=" + csid).type(
251                     "text/plain").build();
252             throw new WebApplicationException(response);
253         }
254         try {
255             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
256             getRepositoryClient(ctx).delete(ctx, csid);
257             return Response.status(HttpResponseCodes.SC_OK).build();
258         } catch (UnauthorizedException ue) {
259             Response response = Response.status(
260                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
261             throw new WebApplicationException(response);
262         } catch (DocumentNotFoundException dnfe) {
263             if (logger.isDebugEnabled()) {
264                 logger.debug("caught exception in deleteAcquisition", dnfe);
265             }
266             Response response = Response.status(Response.Status.NOT_FOUND).entity(
267                     "Delete failed on Acquisition csid=" + csid).type(
268                     "text/plain").build();
269             throw new WebApplicationException(response);
270         } catch (Exception e) {
271             Response response = Response.status(
272                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
273             throw new WebApplicationException(response);
274         }
275     }
276     
277     @GET
278     @Path("/search")    
279     @Produces("application/xml")
280     public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
281                 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
282         AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
283         try {
284             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
285             DocumentHandler handler = createDocumentHandler(ctx);
286
287             // perform a keyword search
288             if (keywords != null && !keywords.isEmpty()) {
289                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
290                     DocumentFilter documentFilter = handler.getDocumentFilter();
291                     documentFilter.setWhereClause(whereClause);
292                     if (logger.isDebugEnabled()) {
293                         logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
294                     }
295                     getRepositoryClient(ctx).getFiltered(ctx, handler);
296             } else {
297                 getRepositoryClient(ctx).getAll(ctx, handler);
298             }            
299             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
300             
301         } catch (UnauthorizedException ue) {
302             Response response = Response.status(
303                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
304             throw new WebApplicationException(response);
305         } catch (Exception e) {
306             if (logger.isDebugEnabled()) {
307                 logger.debug("Caught exception in search for Acquisitions", e);
308             }
309             Response response = Response.status(
310                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
311             throw new WebApplicationException(response);
312         }
313         return acquisitionObjectList;
314     }    
315     
316 }