]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
79efa94a2249462349b5c7d51c94f61aeb17b7a2
[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 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.query.QueryManager;
44 import org.collectionspace.services.common.query.IQueryManager;
45 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory;
46 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
47 import org.collectionspace.services.common.context.MultipartServiceContext;
48 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.document.DocumentFilter;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
56 import org.jboss.resteasy.util.HttpResponseCodes;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 @Path("/collectionobjects")
61 @Consumes("multipart/mixed")
62 @Produces("multipart/mixed")
63 public class CollectionObjectResource
64         extends AbstractCollectionSpaceResource {
65
66     final private String serviceName = "collectionobjects";
67     final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
68
69     @Override
70     protected String getVersionString() {
71         /** The last change revision. */
72         final String lastChangeRevision = "$LastChangedRevision$";
73         return lastChangeRevision;
74     }
75     
76     @Override
77     public String getServiceName() {
78         return serviceName;
79     }
80
81     @Override
82     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
83         DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler(
84                 ctx.getRepositoryClientType().toString());
85         docHandler.setServiceContext(ctx);
86         if (ctx.getInput() != null) {
87             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class);
88             if (obj != null) {
89                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
90             }
91         }
92         return docHandler;
93     }
94
95     @POST
96     public Response createCollectionObject(MultipartInput input) {
97         try {
98             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
99             DocumentHandler handler = createDocumentHandler(ctx);
100             String csid = getRepositoryClient(ctx).create(ctx, handler);
101             UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
102             path.path("" + csid);
103             Response response = Response.created(path.build()).build();
104             return response;
105         } catch (UnauthorizedException ue) {
106             Response response = Response.status(
107                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
108             throw new WebApplicationException(response);
109         } catch (Exception e) {
110             if (logger.isDebugEnabled()) {
111                 logger.debug("Caught exception in createCollectionObject", e);
112             }
113             Response response = Response.status(
114                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
115             throw new WebApplicationException(response);
116         }
117     }
118
119     @GET
120     @Path("{csid}")
121     public MultipartOutput getCollectionObject(
122             @PathParam("csid") String csid) {
123         if (logger.isDebugEnabled()) {
124             logger.debug("getCollectionObject with csid=" + csid);
125         }
126         if (csid == null || "".equals(csid)) {
127             logger.error("getCollectionObject: missing csid!");
128             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
129                     "get failed on CollectionObject csid=" + csid).type(
130                     "text/plain").build();
131             throw new WebApplicationException(response);
132         }
133         MultipartOutput result = null;
134         try {
135             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
136             DocumentHandler handler = createDocumentHandler(ctx);
137             getRepositoryClient(ctx).get(ctx, csid, handler);
138             result = (MultipartOutput) ctx.getOutput();
139         } catch (UnauthorizedException ue) {
140             Response response = Response.status(
141                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
142             throw new WebApplicationException(response);
143         } catch (DocumentNotFoundException dnfe) {
144             if (logger.isDebugEnabled()) {
145                 logger.debug("getCollectionObject", dnfe);
146             }
147             Response response = Response.status(Response.Status.NOT_FOUND).entity(
148                     "Get failed on CollectionObject csid=" + csid).type(
149                     "text/plain").build();
150             throw new WebApplicationException(response);
151         } catch (Exception e) {
152             if (logger.isDebugEnabled()) {
153                 logger.debug("getCollectionObject", e);
154             }
155             Response response = Response.status(
156                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
157             throw new WebApplicationException(response);
158         }
159
160         if (result == null) {
161             Response response = Response.status(Response.Status.NOT_FOUND).entity(
162                     "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
163                     "text/plain").build();
164             throw new WebApplicationException(response);
165         }
166         return result;
167     }
168
169     @GET
170     @Produces("application/xml")
171     public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
172         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
173         try {
174             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
175             DocumentHandler handler = createDocumentHandler(ctx);
176             getRepositoryClient(ctx).getAll(ctx, handler);
177             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
178         } catch (UnauthorizedException ue) {
179             Response response = Response.status(
180                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
181             throw new WebApplicationException(response);
182         } catch (Exception e) {
183             if (logger.isDebugEnabled()) {
184                 logger.debug("Caught exception in getCollectionObjectList", e);
185             }
186             Response response = Response.status(
187                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
188             throw new WebApplicationException(response);
189         }
190         return collectionObjectList;
191     }
192
193     @PUT
194     @Path("{csid}")
195     public MultipartOutput updateCollectionObject(
196             @PathParam("csid") String csid,
197             MultipartInput theUpdate) {
198         if (logger.isDebugEnabled()) {
199             logger.debug("updateCollectionObject with csid=" + csid);
200         }
201         if (csid == null || "".equals(csid)) {
202             logger.error("updateCollectionObject: missing csid!");
203             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
204                     "update failed on CollectionObject csid=" + csid).type(
205                     "text/plain").build();
206             throw new WebApplicationException(response);
207         }
208         MultipartOutput result = null;
209         try {
210             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
211             DocumentHandler handler = createDocumentHandler(ctx);
212             getRepositoryClient(ctx).update(ctx, csid, handler);
213             result = (MultipartOutput) ctx.getOutput();
214         } catch (UnauthorizedException ue) {
215             Response response = Response.status(
216                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
217             throw new WebApplicationException(response);
218         } catch (DocumentNotFoundException dnfe) {
219             if (logger.isDebugEnabled()) {
220                 logger.debug("caugth exception in updateCollectionObject", dnfe);
221             }
222             Response response = Response.status(Response.Status.NOT_FOUND).entity(
223                     "Update failed on CollectionObject csid=" + csid).type(
224                     "text/plain").build();
225             throw new WebApplicationException(response);
226         } catch (Exception e) {
227             Response response = Response.status(
228                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
229             throw new WebApplicationException(response);
230         }
231         return result;
232     }
233
234     @DELETE
235     @Path("{csid}")
236     public Response deleteCollectionObject(@PathParam("csid") String csid) {
237
238         if (logger.isDebugEnabled()) {
239             logger.debug("deleteCollectionObject with csid=" + csid);
240         }
241         if (csid == null || "".equals(csid)) {
242             logger.error("deleteCollectionObject: missing csid!");
243             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
244                     "delete failed on CollectionObject csid=" + csid).type(
245                     "text/plain").build();
246             throw new WebApplicationException(response);
247         }
248         try {
249             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
250             getRepositoryClient(ctx).delete(ctx, csid);
251             return Response.status(HttpResponseCodes.SC_OK).build();
252         } catch (UnauthorizedException ue) {
253             Response response = Response.status(
254                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
255             throw new WebApplicationException(response);
256         } catch (DocumentNotFoundException dnfe) {
257             if (logger.isDebugEnabled()) {
258                 logger.debug("caught exception in deleteCollectionObject", dnfe);
259             }
260             Response response = Response.status(Response.Status.NOT_FOUND).entity(
261                     "Delete failed on CollectionObject csid=" + csid).type(
262                     "text/plain").build();
263             throw new WebApplicationException(response);
264         } catch (Exception e) {
265             Response response = Response.status(
266                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
267             throw new WebApplicationException(response);
268         }
269
270     }
271     
272     @GET
273     @Path("/search")    
274     @Produces("application/xml")
275     public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
276                 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
277         CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
278         try {
279             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
280             DocumentHandler handler = createDocumentHandler(ctx);
281
282             // perform a keyword search
283             if (keywords != null && !keywords.isEmpty()) {
284                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
285                     DocumentFilter documentFilter = handler.getDocumentFilter();
286                     documentFilter.setWhereClause(whereClause);
287                     if (logger.isDebugEnabled()) {
288                         logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
289                     }
290                     getRepositoryClient(ctx).getFiltered(ctx, handler);
291             } else {
292                 getRepositoryClient(ctx).getAll(ctx, handler);
293             }            
294             collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
295             
296         } catch (UnauthorizedException ue) {
297             Response response = Response.status(
298                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
299             throw new WebApplicationException(response);
300         } catch (Exception e) {
301             if (logger.isDebugEnabled()) {
302                 logger.debug("Caught exception in getCollectionObjectList", e);
303             }
304             Response response = Response.status(
305                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
306             throw new WebApplicationException(response);
307         }
308         return collectionObjectList;
309     }    
310 }