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