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