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