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