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