]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a8113cf5e13612aa831da548659a54d8223dca96
[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 Regents of the University of California
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.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
43
44 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
45 import org.collectionspace.services.common.authorityref.AuthorityRefList;
46 import org.collectionspace.services.common.ServiceMessages;
47 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentWrapper;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.common.query.QueryManager;
56 import org.collectionspace.services.common.security.UnauthorizedException;
57 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
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 /**
66  * AcquisitionResource.java
67  *
68  * Handles requests to the Acquisition service, orchestrates the retrieval
69  * of relevant resources, and returns responses to the client.
70  */
71 @Path("/acquisitions")
72 @Consumes("multipart/mixed")
73 @Produces("multipart/mixed")
74 public class AcquisitionResource
75         extends AbstractMultiPartCollectionSpaceResourceImpl {
76
77     /** The service name. */
78     final private String serviceName = "acquisitions";
79     /** The logger. */
80     final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
81
82     /* (non-Javadoc)
83      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
84      */
85     @Override
86     protected String getVersionString() {
87         /** The last change revision. */
88         final String lastChangeRevision = "$LastChangedRevision$";
89         return lastChangeRevision;
90     }
91
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
94      */
95     @Override
96     public String getServiceName() {
97         return serviceName;
98     }
99
100     @Override
101     public Class<AcquisitionsCommon> getCommonPartClass() {
102         return AcquisitionsCommon.class;
103     }
104
105     /* (non-Javadoc)
106      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
107      */
108 //    @Override
109 //    public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
110 //        DocumentHandler docHandler = ctx.getDocumentHandler();
111 //        if (ctx.getInput() != null) {
112 //            Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
113 //            if (obj != null) {
114 //                docHandler.setCommonPart((AcquisitionsCommon) obj);
115 //            }
116 //        }
117 //        return docHandler;
118 //    }
119     /**
120      * Instantiates a new acquisition resource.
121      */
122     public AcquisitionResource() {
123         // do nothing
124     }
125
126     /**
127      * Creates the acquisition.
128      * 
129      * @param input the input
130      * 
131      * @return the response
132      */
133     @POST
134     public Response createAcquisition(MultipartInput input) {
135
136         try {
137             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
138             DocumentHandler handler = createDocumentHandler(ctx);
139             String csid = getRepositoryClient(ctx).create(ctx, handler);
140             UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
141             path.path("" + csid);
142             Response response = Response.created(path.build()).build();
143             return response;
144         } catch (UnauthorizedException ue) {
145             Response response = Response.status(
146                     Response.Status.UNAUTHORIZED).entity(
147                     ServiceMessages.CREATE_FAILED + ue.getErrorReason()).type("text/plain").build();
148             throw new WebApplicationException(response);
149         } catch (Exception e) {
150             if (logger.isDebugEnabled()) {
151                 logger.debug("Caught exception in createAcquisition", e);
152             }
153             Response response = Response.status(
154                     Response.Status.INTERNAL_SERVER_ERROR).entity(
155                     ServiceMessages.CREATE_FAILED + e.getMessage()).type("text/plain").build();
156             throw new WebApplicationException(response);
157         }
158     }
159
160     /**
161      * Gets the acquisition.
162      * 
163      * @param csid the csid
164      * 
165      * @return the acquisition
166      */
167     @GET
168     @Path("{csid}")
169     public MultipartOutput getAcquisition(
170             @PathParam("csid") String csid) {
171         if (logger.isDebugEnabled()) {
172             logger.debug("getAcquisition with csid=" + csid);
173         }
174         if (csid == null || "".equals(csid)) {
175             logger.error("getAcquisition:" + ServiceMessages.MISSING_CSID);
176             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
177                     ServiceMessages.READ_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
178             throw new WebApplicationException(response);
179         }
180         MultipartOutput result = null;
181         try {
182             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
183             DocumentHandler handler = createDocumentHandler(ctx);
184             getRepositoryClient(ctx).get(ctx, csid, handler);
185             result = (MultipartOutput) ctx.getOutput();
186         } catch (UnauthorizedException ue) {
187             Response response = Response.status(
188                     Response.Status.UNAUTHORIZED).entity(
189                     ServiceMessages.READ_FAILED + ue.getErrorReason()).type("text/plain").build();
190             throw new WebApplicationException(response);
191         } catch (DocumentNotFoundException dnfe) {
192             if (logger.isDebugEnabled()) {
193                 logger.debug("getAcquisition", dnfe);
194             }
195             Response response = Response.status(Response.Status.NOT_FOUND).entity(
196                     ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
197             throw new WebApplicationException(response);
198         } catch (Exception e) {
199             if (logger.isDebugEnabled()) {
200                 logger.debug("getAcquisition", e);
201             }
202             Response response = Response.status(
203                     Response.Status.INTERNAL_SERVER_ERROR).entity(
204                     ServiceMessages.READ_FAILED + e.getMessage()).type("text/plain").build();
205             throw new WebApplicationException(response);
206         }
207
208         if (result == null) {
209             Response response = Response.status(Response.Status.NOT_FOUND).entity(
210                     ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
211             throw new WebApplicationException(response);
212         }
213         return result;
214     }
215
216     /**
217      * Gets the acquisition list.
218      * 
219      * @param ui the ui
220      * @param keywords the keywords
221      * 
222      * @return the acquisition list
223      */
224     @GET
225     @Produces("application/xml")
226     public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui,
227             @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
228         AcquisitionsCommonList result = null;
229         MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
230         if (keywords != null) {
231             result = searchAcquisitions(queryParams, keywords);
232         } else {
233             result = getAcquisitionsList(queryParams);
234         }
235
236         return result;
237     }
238
239     /**
240      * Gets the acquisitions list.
241      * 
242      * @return the acquisitions list
243      */
244     private AcquisitionsCommonList getAcquisitionsList(MultivaluedMap<String, String> queryParams) {
245         AcquisitionsCommonList acquisitionObjectList;
246         try {
247             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
248             DocumentHandler handler = createDocumentHandler(ctx);
249             getRepositoryClient(ctx).getFiltered(ctx, handler);
250             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
251         } catch (UnauthorizedException ue) {
252             Response response = Response.status(
253                     Response.Status.UNAUTHORIZED).entity(
254                     ServiceMessages.LIST_FAILED + ue.getErrorReason()).type("text/plain").build();
255             throw new WebApplicationException(response);
256         } catch (Exception e) {
257             if (logger.isDebugEnabled()) {
258                 logger.debug("Caught exception in getAcquisitionList", e);
259             }
260             Response response = Response.status(
261                     Response.Status.INTERNAL_SERVER_ERROR).entity(
262                     ServiceMessages.LIST_FAILED + e.getMessage()).type("text/plain").build();
263             throw new WebApplicationException(response);
264         }
265         return acquisitionObjectList;
266     }
267
268     /**
269      * Update acquisition.
270      * 
271      * @param csid the csid
272      * @param theUpdate the the update
273      * 
274      * @return the multipart output
275      */
276     @PUT
277     @Path("{csid}")
278     public MultipartOutput updateAcquisition(
279             @PathParam("csid") String csid,
280             MultipartInput theUpdate) {
281         if (logger.isDebugEnabled()) {
282             logger.debug("updateAcquisition with csid=" + csid);
283         }
284         if (csid == null || "".equals(csid)) {
285             logger.error("updateAcquisition: missing csid!");
286             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
287                     ServiceMessages.UPDATE_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
288             throw new WebApplicationException(response);
289         }
290         if (logger.isDebugEnabled()) {
291             logger.debug("updateAcquisition with input: ", theUpdate);
292         }
293         MultipartOutput result = null;
294         try {
295             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
296             DocumentHandler handler = createDocumentHandler(ctx);
297             getRepositoryClient(ctx).update(ctx, csid, handler);
298             result = (MultipartOutput) ctx.getOutput();
299         } catch (UnauthorizedException ue) {
300             Response response = Response.status(
301                     Response.Status.UNAUTHORIZED).entity(
302                     ServiceMessages.UPDATE_FAILED + ue.getErrorReason()).type("text/plain").build();
303             throw new WebApplicationException(response);
304         } catch (DocumentNotFoundException dnfe) {
305             if (logger.isDebugEnabled()) {
306                 logger.debug("caught exception in updateAcquisition", dnfe);
307             }
308             Response response = Response.status(Response.Status.NOT_FOUND).entity(
309                     ServiceMessages.UPDATE_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
310             throw new WebApplicationException(response);
311         } catch (Exception e) {
312             Response response = Response.status(
313                     Response.Status.INTERNAL_SERVER_ERROR).entity(
314                     ServiceMessages.UPDATE_FAILED + e.getMessage()).type("text/plain").build();
315             throw new WebApplicationException(response);
316         }
317         return result;
318     }
319
320     /**
321      * Delete acquisition.
322      * 
323      * @param csid the csid
324      * 
325      * @return the response
326      */
327     @DELETE
328     @Path("{csid}")
329     public Response deleteAcquisition(@PathParam("csid") String csid) {
330
331         if (logger.isDebugEnabled()) {
332             logger.debug("deleteAcquisition with csid=" + csid);
333         }
334         if (csid == null || "".equals(csid)) {
335             logger.error("deleteAcquisition: missing csid!");
336             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
337                     ServiceMessages.DELETE_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
338             throw new WebApplicationException(response);
339         }
340         try {
341             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
342             getRepositoryClient(ctx).delete(ctx, csid);
343             return Response.status(HttpResponseCodes.SC_OK).build();
344         } catch (UnauthorizedException ue) {
345             Response response = Response.status(
346                     Response.Status.UNAUTHORIZED).entity(
347                     ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
348             throw new WebApplicationException(response);
349         } catch (DocumentNotFoundException dnfe) {
350             if (logger.isDebugEnabled()) {
351                 logger.debug("caught exception in deleteAcquisition", dnfe);
352             }
353             Response response = Response.status(Response.Status.NOT_FOUND).entity(
354                     ServiceMessages.DELETE_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
355             throw new WebApplicationException(response);
356         } catch (Exception e) {
357             Response response = Response.status(
358                     Response.Status.INTERNAL_SERVER_ERROR).entity(
359                     ServiceMessages.DELETE_FAILED + e.getMessage()).type("text/plain").build();
360             throw new WebApplicationException(response);
361         }
362     }
363
364     /**
365      * Keywords search acquisitions.
366      * 
367      * @param ui the ui
368      * @param keywords the keywords
369      * 
370      * @return the acquisitions common list
371      */
372     @GET
373     @Path("/search")
374     @Produces("application/xml")
375     @Deprecated
376     public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
377             @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
378         MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
379         return searchAcquisitions(queryParams, keywords);
380     }
381
382     /**
383      * Search acquisitions.
384      * 
385      * @param keywords the keywords
386      * 
387      * @return the acquisitions common list
388      */
389     private AcquisitionsCommonList searchAcquisitions(
390             MultivaluedMap<String, String> queryParams,
391             String keywords) {
392         AcquisitionsCommonList acquisitionObjectList;
393         try {
394             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
395             DocumentHandler handler = createDocumentHandler(ctx);
396
397             // perform a keyword search
398             if (keywords != null && !keywords.isEmpty()) {
399                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
400                 DocumentFilter documentFilter = handler.getDocumentFilter();
401                 documentFilter.setWhereClause(whereClause);
402                 if (logger.isDebugEnabled()) {
403                     logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
404                 }
405             }
406             getRepositoryClient(ctx).getFiltered(ctx, handler);
407             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
408         } catch (UnauthorizedException ue) {
409             Response response = Response.status(
410                     Response.Status.UNAUTHORIZED).entity(
411                     ServiceMessages.SEARCH_FAILED + ue.getErrorReason()).type("text/plain").build();
412             throw new WebApplicationException(response);
413         } catch (Exception e) {
414             if (logger.isDebugEnabled()) {
415                 logger.debug("Caught exception in search for Acquisitions", e);
416             }
417             Response response = Response.status(
418                     Response.Status.INTERNAL_SERVER_ERROR).entity(
419                     ServiceMessages.SEARCH_FAILED + e.getMessage()).type("text/plain").build();
420             throw new WebApplicationException(response);
421         }
422         return acquisitionObjectList;
423     }
424
425     /**
426      * Gets the authority refs.
427      * 
428      * @param csid the csid
429      * @param ui the ui
430      * 
431      * @return the authority refs
432      */
433     @GET
434     @Path("{csid}/authorityrefs")
435     @Produces("application/xml")
436     public AuthorityRefList getAuthorityRefs(
437             @PathParam("csid") String csid,
438             @Context UriInfo ui) {
439         AuthorityRefList authRefList = null;
440         try {
441             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
442             DocumentWrapper<DocumentModel> docWrapper =
443                     getRepositoryClient(ctx).getDoc(ctx, csid);
444             DocumentModelHandler<MultipartInput, MultipartOutput> handler = (DocumentModelHandler<MultipartInput, MultipartOutput>) createDocumentHandler(ctx);
445             List<String> authRefFields =
446                     ((MultipartServiceContextImpl) ctx).getCommonPartPropertyValues(
447                     ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
448             authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
449         } catch (UnauthorizedException ue) {
450             Response response = Response.status(
451                     Response.Status.UNAUTHORIZED).entity(
452                     ServiceMessages.AUTH_REFS_FAILED + ue.getErrorReason()).type("text/plain").build();
453             throw new WebApplicationException(response);
454         } catch (Exception e) {
455             if (logger.isDebugEnabled()) {
456                 logger.debug("Caught exception in getAuthorityRefs", e);
457             }
458             Response response = Response.status(
459                     Response.Status.INTERNAL_SERVER_ERROR).entity(
460                     ServiceMessages.AUTH_REFS_FAILED + e.getMessage()).type("text/plain").build();
461             throw new WebApplicationException(response);
462         }
463         return authRefList;
464     }
465 }