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