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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 * $LastChangedRevision$
26 package org.collectionspace.services.collectionobject;
28 import java.util.List;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.lang.reflect.Type;
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;
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;
67 import org.collectionspace.services.intake.IntakesCommonList;
68 import org.collectionspace.services.intake.IntakeResource;
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;
76 @Path("/collectionobjects")
77 @Consumes("multipart/mixed")
78 @Produces("multipart/mixed")
79 public class CollectionObjectResource
80 extends AbstractCollectionSpaceResource {
82 static final public String serviceName = "collectionobjects";
83 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
86 protected String getVersionString() {
87 /** The last change revision. */
88 final String lastChangeRevision = "$LastChangedRevision$";
89 return lastChangeRevision;
93 public String getServiceName() {
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);
104 docHandler.setCommonPart((CollectionobjectsCommon) obj);
111 public Response createCollectionObject(MultipartInput input) {
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();
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);
132 Response response = Response.status(
133 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
134 throw new WebApplicationException(response);
140 public MultipartOutput getCollectionObject(
141 @PathParam("csid") String csid) {
142 if (logger.isDebugEnabled()) {
143 logger.debug("getCollectionObject with csid=" + csid);
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);
152 MultipartOutput result = null;
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);
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);
174 Response response = Response.status(
175 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
176 throw new WebApplicationException(response);
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);
189 @Produces("application/xml")
190 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
191 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
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);
205 Response response = Response.status(
206 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
207 throw new WebApplicationException(response);
209 return collectionObjectList;
214 public MultipartOutput updateCollectionObject(
215 @PathParam("csid") String csid,
216 MultipartInput theUpdate) {
217 if (logger.isDebugEnabled()) {
218 logger.debug("updateCollectionObject with csid=" + csid);
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);
227 MultipartOutput result = null;
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);
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);
259 public Response deleteCollectionObject(@PathParam("csid") String csid) {
261 if (logger.isDebugEnabled()) {
262 logger.debug("deleteCollectionObject with csid=" + csid);
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);
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);
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);
296 @Path("{csid}/intakes")
297 @Produces("application/xml")
298 public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
299 @PathParam("csid") String csid) {
300 IntakesCommonList result = null;
304 // Find all the intake-related relation records.
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);
313 // Create an array of Intake csid's
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());
322 // Get a response list for the Intake records from the Intake resource
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);
330 Response response = Response.status(
331 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
332 throw new WebApplicationException(response);
338 //FIXME: Replace this "search" resource with a keyword "kw" query parameter
341 @Produces("application/xml")
342 public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
343 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
344 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
346 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
347 DocumentHandler handler = createDocumentHandler(ctx);
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());
357 getRepositoryClient(ctx).getFiltered(ctx, handler);
359 getRepositoryClient(ctx).getAll(ctx, handler);
361 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
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);
371 Response response = Response.status(
372 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
373 throw new WebApplicationException(response);
375 return collectionObjectList;