]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dad878dc131fc801eb8db210b835f5a22b82f367
[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  *  $LastChangedRevision$
25  */
26 package org.collectionspace.services.collectionobject;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.QueryParam;
40 import javax.ws.rs.WebApplicationException;
41 import javax.ws.rs.core.Context;
42 import javax.ws.rs.core.Response;
43 import javax.ws.rs.core.UriBuilder;
44 import javax.ws.rs.core.UriInfo;
45
46 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.authorityref.AuthorityRefList;
48 import org.collectionspace.services.common.context.MultipartServiceContext;
49 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
50 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
51 import org.collectionspace.services.common.context.ServiceContext;
52 import org.collectionspace.services.common.document.BadRequestException;
53 import org.collectionspace.services.common.document.DocumentFilter;
54 import org.collectionspace.services.common.document.DocumentHandler;
55 import org.collectionspace.services.common.document.DocumentNotFoundException;
56 import org.collectionspace.services.common.document.DocumentWrapper;
57 import org.collectionspace.services.common.query.IQueryManager;
58 import org.collectionspace.services.common.query.QueryManager;
59 import org.collectionspace.services.common.security.UnauthorizedException;
60 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
61 import org.collectionspace.services.intake.IntakeResource;
62 import org.collectionspace.services.intake.IntakesCommonList;
63 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
64 import org.collectionspace.services.relation.NewRelationResource;
65 import org.collectionspace.services.relation.RelationsCommonList;
66 import org.collectionspace.services.relation.RelationshipType;
67 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
68 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
69 import org.jboss.resteasy.util.HttpResponseCodes;
70 import org.nuxeo.ecm.core.api.DocumentModel;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74
75 /**
76  * The Class CollectionObjectResource.
77  */
78 @Path("/collectionobjects")
79 @Consumes("multipart/mixed")
80 @Produces("multipart/mixed")
81 public class CollectionObjectResource
82         extends AbstractCollectionSpaceResourceImpl {
83
84     /** The Constant serviceName. */
85     static final public String serviceName = "collectionobjects";
86     
87     /** The logger. */
88     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
89
90     /* (non-Javadoc)
91      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
92      */
93     @Override
94     protected String getVersionString() {
95         /** The last change revision. */
96         final String lastChangeRevision = "$LastChangedRevision$";
97         return lastChangeRevision;
98     }
99
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
102      */
103     @Override
104     public String getServiceName() {
105         return serviceName;
106     }
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
110      */
111     @Override
112     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
113         DocumentHandler docHandler = ctx.getDocumentHandler();
114         if (ctx.getInput() != null) {
115             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(),
116                     CollectionobjectsCommon.class);
117             if (obj != null) {
118                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
119             }
120         }
121         return docHandler;
122     }
123
124     /**
125      * Creates the collection object.
126      * 
127      * @param input the input
128      * 
129      * @return the response
130      */
131     @POST
132     public Response createCollectionObject(MultipartInput input) {
133         try {
134             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
135             DocumentHandler handler = createDocumentHandler(ctx);
136             String csid = getRepositoryClient(ctx).create(ctx, handler);
137             UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
138             path.path("" + csid);
139             Response response = Response.created(path.build()).build();
140             return response;
141         } catch (BadRequestException bre) {
142             Response response = Response.status(
143                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
144             throw new WebApplicationException(response);
145         } catch (UnauthorizedException ue) {
146             Response response = Response.status(
147                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + 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 createCollectionObject", e);
152             }
153             Response response = Response.status(
154                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
155             throw new WebApplicationException(response);
156         }
157     }
158
159     /**
160      * Gets the collection object.
161      * 
162      * @param csid the csid
163      * 
164      * @return the collection object
165      */
166     @GET
167     @Path("{csid}")
168     public MultipartOutput getCollectionObject(
169             @PathParam("csid") String csid) {
170         if (logger.isDebugEnabled()) {
171             logger.debug("getCollectionObject with csid=" + csid);
172         }
173         if (csid == null || "".equals(csid)) {
174             logger.error("getCollectionObject: missing csid!");
175             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
176                     "get failed on CollectionObject csid=" + csid).type(
177                     "text/plain").build();
178             throw new WebApplicationException(response);
179         }
180         MultipartOutput result = null;
181         try {
182             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
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("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
189             throw new WebApplicationException(response);
190         } catch (DocumentNotFoundException dnfe) {
191             if (logger.isDebugEnabled()) {
192                 logger.debug("getCollectionObject", dnfe);
193             }
194             Response response = Response.status(Response.Status.NOT_FOUND).entity(
195                     "Get failed on CollectionObject csid=" + csid).type(
196                     "text/plain").build();
197             throw new WebApplicationException(response);
198         } catch (Exception e) {
199             if (logger.isDebugEnabled()) {
200                 logger.debug("getCollectionObject", e);
201             }
202             Response response = Response.status(
203                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
204             throw new WebApplicationException(response);
205         }
206
207         if (result == null) {
208             Response response = Response.status(Response.Status.NOT_FOUND).entity(
209                     "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
210                     "text/plain").build();
211             throw new WebApplicationException(response);
212         }
213         return result;
214     }
215
216     /**
217      * Gets the collection object list.
218      * 
219      * @param ui the ui
220      * 
221      * @return the collection object list
222      */
223     @GET
224     @Produces("application/xml")
225     public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
226         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
227         try {
228             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
229             DocumentHandler handler = createDocumentHandler(ctx);
230             getRepositoryClient(ctx).getAll(ctx, handler);
231             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
232         } catch (UnauthorizedException ue) {
233             Response response = Response.status(
234                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
235             throw new WebApplicationException(response);
236         } catch (Exception e) {
237             if (logger.isDebugEnabled()) {
238                 logger.debug("Caught exception in getCollectionObjectList", e);
239             }
240             Response response = Response.status(
241                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
242             throw new WebApplicationException(response);
243         }
244         return collectionObjectList;
245     }
246
247     /**
248      * Update collection object.
249      * 
250      * @param csid the csid
251      * @param theUpdate the the update
252      * 
253      * @return the multipart output
254      */
255     @PUT
256     @Path("{csid}")
257     public MultipartOutput updateCollectionObject(
258             @PathParam("csid") String csid,
259             MultipartInput theUpdate) {
260         if (logger.isDebugEnabled()) {
261             logger.debug("updateCollectionObject with csid=" + csid);
262         }
263         if (csid == null || "".equals(csid)) {
264             logger.error("updateCollectionObject: missing csid!");
265             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
266                     "update failed on CollectionObject csid=" + csid).type(
267                     "text/plain").build();
268             throw new WebApplicationException(response);
269         }
270         MultipartOutput result = null;
271         try {
272             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
273             DocumentHandler handler = createDocumentHandler(ctx);
274             getRepositoryClient(ctx).update(ctx, csid, handler);
275             result = (MultipartOutput) ctx.getOutput();
276         } catch (BadRequestException bre) {
277             Response response = Response.status(
278                     Response.Status.BAD_REQUEST).entity("Update failed reason " + bre.getErrorReason()).type("text/plain").build();
279             throw new WebApplicationException(response);
280         } catch (UnauthorizedException ue) {
281             Response response = Response.status(
282                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
283             throw new WebApplicationException(response);
284         } catch (DocumentNotFoundException dnfe) {
285             if (logger.isDebugEnabled()) {
286                 logger.debug("caugth exception in updateCollectionObject", dnfe);
287             }
288             Response response = Response.status(Response.Status.NOT_FOUND).entity(
289                     "Update failed on CollectionObject csid=" + csid).type(
290                     "text/plain").build();
291             throw new WebApplicationException(response);
292         } catch (Exception e) {
293             Response response = Response.status(
294                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
295             throw new WebApplicationException(response);
296         }
297         return result;
298     }
299
300     /**
301      * Delete collection object.
302      * 
303      * @param csid the csid
304      * 
305      * @return the response
306      */
307     @DELETE
308     @Path("{csid}")
309     public Response deleteCollectionObject(@PathParam("csid") String csid) {
310
311         if (logger.isDebugEnabled()) {
312             logger.debug("deleteCollectionObject with csid=" + csid);
313         }
314         if (csid == null || "".equals(csid)) {
315             logger.error("deleteCollectionObject: missing csid!");
316             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
317                     "delete failed on CollectionObject csid=" + csid).type(
318                     "text/plain").build();
319             throw new WebApplicationException(response);
320         }
321         try {
322             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
323             getRepositoryClient(ctx).delete(ctx, csid);
324             return Response.status(HttpResponseCodes.SC_OK).build();
325         } catch (UnauthorizedException ue) {
326             Response response = Response.status(
327                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
328             throw new WebApplicationException(response);
329         } catch (DocumentNotFoundException dnfe) {
330             if (logger.isDebugEnabled()) {
331                 logger.debug("caught exception in deleteCollectionObject", dnfe);
332             }
333             Response response = Response.status(Response.Status.NOT_FOUND).entity(
334                     "Delete failed on CollectionObject csid=" + csid).type(
335                     "text/plain").build();
336             throw new WebApplicationException(response);
337         } catch (Exception e) {
338             Response response = Response.status(
339                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
340             throw new WebApplicationException(response);
341         }
342
343     }
344
345     /**
346      * Gets the intakes common list.
347      * 
348      * @param ui the ui
349      * @param csid the csid
350      * 
351      * @return the intakes common list
352      */
353     @GET
354     @Path("{csid}/intakes")
355     @Produces("application/xml")
356     public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
357                 @PathParam("csid") String csid) {
358         IntakesCommonList result = null;        
359         
360         try {
361                 //
362                 // Find all the intake-related relation records.
363                 //
364                 String subjectCsid = csid;
365                 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
366                 String objectCsid = null;
367                 NewRelationResource relationResource = new NewRelationResource();               
368                 RelationsCommonList relationsCommonList = relationResource.getRelationList(subjectCsid, predicate, objectCsid);
369                 
370                 //
371                 // Create an array of Intake csid's
372                 //
373                 List<RelationsCommonList.RelationListItem> relationsListItems = relationsCommonList.getRelationListItem();
374                 List<String> intakeCsidList = new ArrayList<String>();
375             for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) {
376                 intakeCsidList.add(relationsListItem.getObjectCsid());
377                 }
378             
379             //
380             // Get a response list for the Intake records from the Intake resource
381             //
382                 IntakeResource intakeResource = new IntakeResource();
383                 result = intakeResource.getIntakeList(intakeCsidList);
384         } catch (Exception e) {
385             if (logger.isDebugEnabled()) {
386                 logger.debug("Caught exception in getIntakeList", e);
387             }
388             Response response = Response.status(
389                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
390             throw new WebApplicationException(response);
391         }
392         
393         return result;
394     }
395
396     @GET
397     @Path("{csid}/authorityrefs")
398     @Produces("application/xml")
399     public AuthorityRefList getAuthorityRefs(
400                 @PathParam("csid") String csid, 
401                 @Context UriInfo ui) {
402         AuthorityRefList authRefList = null;
403         try {
404             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
405             DocumentWrapper<DocumentModel> docWrapper = 
406                 getRepositoryClient(ctx).getDoc(ctx, csid);
407             RemoteDocumentModelHandlerImpl handler 
408                 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
409             List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(RefNameServiceUtils.AUTH_REF_PROP);
410             authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
411         } catch (UnauthorizedException ue) {
412             Response response = Response.status(
413                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
414             throw new WebApplicationException(response);
415         } catch (Exception e) {
416             if (logger.isDebugEnabled()) {
417                 logger.debug("Caught exception in getAuthorityRefs", e);
418             }
419             Response response = Response.status(
420                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
421             throw new WebApplicationException(response);
422         }
423         return authRefList;
424     }
425     
426     /**
427      * Roundtrip.
428      * 
429      * This is an intentionally empty method used for getting a rough time estimate
430      * of the overhead required for a client->server request/response cycle.
431      * 
432      * @return the response
433      */
434     @GET
435     @Path("/roundtrip")
436     @Produces("application/xml")
437     public Response roundtrip() {
438         Response result = null;
439         
440                 if (logger.isDebugEnabled()) {
441                         logger.debug("------------------------------------------------------------------------------");
442                         logger.debug("Client to server roundtrip called.");
443                         logger.debug("------------------------------------------------------------------------------");
444                         logger.debug("");
445                 }
446                 result = Response.status(HttpResponseCodes.SC_OK).build();
447                 
448                 return result;
449     }
450
451
452     //FIXME: Replace this "search" resource with a keyword "kw" query parameter
453     /**
454      * Keywords search collection objects.
455      * 
456      * @param keywords the keywords
457      * 
458      * @return the collectionobjects common list
459      */
460     @GET
461     @Path("/search")
462     @Produces("application/xml")
463     public CollectionobjectsCommonList keywordsSearchCollectionObjects(
464             @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
465         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
466         try {
467             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
468             DocumentHandler handler = createDocumentHandler(ctx);
469
470             // perform a keyword search
471             if (keywords != null && !keywords.isEmpty()) {
472                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
473                 DocumentFilter documentFilter = handler.getDocumentFilter();
474                 documentFilter.setWhereClause(whereClause);
475                 if (logger.isDebugEnabled()) {
476                     logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
477                 }
478                 getRepositoryClient(ctx).getFiltered(ctx, handler);
479             } else {
480                 getRepositoryClient(ctx).getAll(ctx, handler);
481             }
482             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
483
484         } catch (UnauthorizedException ue) {
485             Response response = Response.status(
486                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
487             throw new WebApplicationException(response);
488         } catch (Exception e) {
489             if (logger.isDebugEnabled()) {
490                 logger.debug("Caught exception in getCollectionObjectList", e);
491             }
492             Response response = Response.status(
493                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
494             throw new WebApplicationException(response);
495         }
496         return collectionObjectList;
497     }
498         
499 }