]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
189f6612c105b186a10e1a5605f143485833df3c
[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.AbstractCollectionSpaceResource;
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 @Path("/collectionobjects")
77 @Consumes("multipart/mixed")
78 @Produces("multipart/mixed")
79 public class CollectionObjectResource
80         extends AbstractCollectionSpaceResource {
81
82     static final public String serviceName = "collectionobjects";
83     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
84
85     @Override
86     protected String getVersionString() {
87         /** The last change revision. */
88         final String lastChangeRevision = "$LastChangedRevision$";
89         return lastChangeRevision;
90     }
91
92     @Override
93     public String getServiceName() {
94         return serviceName;
95     }
96
97     @Override
98     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
99         DocumentHandler docHandler = ctx.getDocumentHandler();
100         if (ctx.getInput() != null) {
101             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(),
102                     CollectionobjectsCommon.class);
103             if (obj != null) {
104                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
105             }
106         }
107         return docHandler;
108     }
109
110     @POST
111     public Response createCollectionObject(MultipartInput input) {
112         try {
113             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
114             DocumentHandler handler = createDocumentHandler(ctx);
115             String csid = getRepositoryClient(ctx).create(ctx, handler);
116             UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
117             path.path("" + csid);
118             Response response = Response.created(path.build()).build();
119             return response;
120         } catch (BadRequestException bre) {
121             Response response = Response.status(
122                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
123             throw new WebApplicationException(response);
124         } catch (UnauthorizedException ue) {
125             Response response = Response.status(
126                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
127             throw new WebApplicationException(response);
128         } catch (Exception e) {
129             if (logger.isDebugEnabled()) {
130                 logger.debug("Caught exception in createCollectionObject", e);
131             }
132             Response response = Response.status(
133                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
134             throw new WebApplicationException(response);
135         }
136     }
137
138     @GET
139     @Path("{csid}")
140     public MultipartOutput getCollectionObject(
141             @PathParam("csid") String csid) {
142         if (logger.isDebugEnabled()) {
143             logger.debug("getCollectionObject with csid=" + csid);
144         }
145         if (csid == null || "".equals(csid)) {
146             logger.error("getCollectionObject: missing csid!");
147             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
148                     "get failed on CollectionObject csid=" + csid).type(
149                     "text/plain").build();
150             throw new WebApplicationException(response);
151         }
152         MultipartOutput result = null;
153         try {
154             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
155             DocumentHandler handler = createDocumentHandler(ctx);
156             getRepositoryClient(ctx).get(ctx, csid, handler);
157             result = (MultipartOutput) ctx.getOutput();
158         } catch (UnauthorizedException ue) {
159             Response response = Response.status(
160                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
161             throw new WebApplicationException(response);
162         } catch (DocumentNotFoundException dnfe) {
163             if (logger.isDebugEnabled()) {
164                 logger.debug("getCollectionObject", dnfe);
165             }
166             Response response = Response.status(Response.Status.NOT_FOUND).entity(
167                     "Get failed on CollectionObject csid=" + csid).type(
168                     "text/plain").build();
169             throw new WebApplicationException(response);
170         } catch (Exception e) {
171             if (logger.isDebugEnabled()) {
172                 logger.debug("getCollectionObject", e);
173             }
174             Response response = Response.status(
175                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
176             throw new WebApplicationException(response);
177         }
178
179         if (result == null) {
180             Response response = Response.status(Response.Status.NOT_FOUND).entity(
181                     "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
182                     "text/plain").build();
183             throw new WebApplicationException(response);
184         }
185         return result;
186     }
187
188     @GET
189     @Produces("application/xml")
190     public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
191         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
192         try {
193             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
194             DocumentHandler handler = createDocumentHandler(ctx);
195             getRepositoryClient(ctx).getAll(ctx, handler);
196             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
197         } catch (UnauthorizedException ue) {
198             Response response = Response.status(
199                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
200             throw new WebApplicationException(response);
201         } catch (Exception e) {
202             if (logger.isDebugEnabled()) {
203                 logger.debug("Caught exception in getCollectionObjectList", e);
204             }
205             Response response = Response.status(
206                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
207             throw new WebApplicationException(response);
208         }
209         return collectionObjectList;
210     }
211
212     @PUT
213     @Path("{csid}")
214     public MultipartOutput updateCollectionObject(
215             @PathParam("csid") String csid,
216             MultipartInput theUpdate) {
217         if (logger.isDebugEnabled()) {
218             logger.debug("updateCollectionObject with csid=" + csid);
219         }
220         if (csid == null || "".equals(csid)) {
221             logger.error("updateCollectionObject: missing csid!");
222             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
223                     "update failed on CollectionObject csid=" + csid).type(
224                     "text/plain").build();
225             throw new WebApplicationException(response);
226         }
227         MultipartOutput result = null;
228         try {
229             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
230             DocumentHandler handler = createDocumentHandler(ctx);
231             getRepositoryClient(ctx).update(ctx, csid, handler);
232             result = (MultipartOutput) ctx.getOutput();
233         } catch (BadRequestException bre) {
234             Response response = Response.status(
235                     Response.Status.BAD_REQUEST).entity("Update failed reason " + bre.getErrorReason()).type("text/plain").build();
236             throw new WebApplicationException(response);
237         } catch (UnauthorizedException ue) {
238             Response response = Response.status(
239                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
240             throw new WebApplicationException(response);
241         } catch (DocumentNotFoundException dnfe) {
242             if (logger.isDebugEnabled()) {
243                 logger.debug("caugth exception in updateCollectionObject", dnfe);
244             }
245             Response response = Response.status(Response.Status.NOT_FOUND).entity(
246                     "Update failed on CollectionObject csid=" + csid).type(
247                     "text/plain").build();
248             throw new WebApplicationException(response);
249         } catch (Exception e) {
250             Response response = Response.status(
251                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
252             throw new WebApplicationException(response);
253         }
254         return result;
255     }
256
257     @DELETE
258     @Path("{csid}")
259     public Response deleteCollectionObject(@PathParam("csid") String csid) {
260
261         if (logger.isDebugEnabled()) {
262             logger.debug("deleteCollectionObject with csid=" + csid);
263         }
264         if (csid == null || "".equals(csid)) {
265             logger.error("deleteCollectionObject: missing csid!");
266             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
267                     "delete failed on CollectionObject csid=" + csid).type(
268                     "text/plain").build();
269             throw new WebApplicationException(response);
270         }
271         try {
272             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
273             getRepositoryClient(ctx).delete(ctx, csid);
274             return Response.status(HttpResponseCodes.SC_OK).build();
275         } catch (UnauthorizedException ue) {
276             Response response = Response.status(
277                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
278             throw new WebApplicationException(response);
279         } catch (DocumentNotFoundException dnfe) {
280             if (logger.isDebugEnabled()) {
281                 logger.debug("caught exception in deleteCollectionObject", dnfe);
282             }
283             Response response = Response.status(Response.Status.NOT_FOUND).entity(
284                     "Delete failed on CollectionObject csid=" + csid).type(
285                     "text/plain").build();
286             throw new WebApplicationException(response);
287         } catch (Exception e) {
288             Response response = Response.status(
289                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
290             throw new WebApplicationException(response);
291         }
292
293     }
294
295     @GET
296     @Path("{csid}/intakes")
297     @Produces("application/xml")
298     public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
299                 @PathParam("csid") String csid) {
300         IntakesCommonList result = null;        
301         
302         try {
303                 //
304                 // Find all the intake-related relation records.
305                 //
306                 String subjectCsid = csid;
307                 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
308                 String objectCsid = null;
309                 NewRelationResource relationResource = new NewRelationResource();               
310                 RelationsCommonList relationsCommonList = relationResource.getRelationList(subjectCsid, predicate, objectCsid);
311                 
312                 //
313                 // Create an array of Intake csid's
314                 //
315                 List<RelationsCommonList.RelationListItem> relationsListItems = relationsCommonList.getRelationListItem();
316                 List<String> intakeCsidList = new ArrayList<String>();
317             for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) {
318                 intakeCsidList.add(relationsListItem.getObjectCsid());
319                 }
320             
321             //
322             // Get a response list for the Intake records from the Intake resource
323             //
324                 IntakeResource intakeResource = new IntakeResource();
325                 result = intakeResource.getIntakeList(intakeCsidList);
326         } catch (Exception e) {
327             if (logger.isDebugEnabled()) {
328                 logger.debug("Caught exception in getIntakeList", e);
329             }
330             Response response = Response.status(
331                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
332             throw new WebApplicationException(response);
333         }
334         
335         return result;
336     }    
337
338     //FIXME: Replace this "search" resource with a keyword "kw" query parameter
339     @GET
340     @Path("/search")
341     @Produces("application/xml")
342     public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
343             @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
344         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
345         try {
346             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
347             DocumentHandler handler = createDocumentHandler(ctx);
348
349             // perform a keyword search
350             if (keywords != null && !keywords.isEmpty()) {
351                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
352                 DocumentFilter documentFilter = handler.getDocumentFilter();
353                 documentFilter.setWhereClause(whereClause);
354                 if (logger.isDebugEnabled()) {
355                     logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
356                 }
357                 getRepositoryClient(ctx).getFiltered(ctx, handler);
358             } else {
359                 getRepositoryClient(ctx).getAll(ctx, handler);
360             }
361             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
362
363         } catch (UnauthorizedException ue) {
364             Response response = Response.status(
365                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
366             throw new WebApplicationException(response);
367         } catch (Exception e) {
368             if (logger.isDebugEnabled()) {
369                 logger.debug("Caught exception in getCollectionObjectList", e);
370             }
371             Response response = Response.status(
372                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
373             throw new WebApplicationException(response);
374         }
375         return collectionObjectList;
376     }
377         
378 }