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