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