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.ArrayList;
29 import java.util.List;
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;
46 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.authorityref.AuthorityRefList;
48 import org.collectionspace.services.common.context.MultipartServiceContext;
49 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
50 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
51 import org.collectionspace.services.common.context.ServiceContext;
52 import org.collectionspace.services.common.document.BadRequestException;
53 import org.collectionspace.services.common.document.DocumentFilter;
54 import org.collectionspace.services.common.document.DocumentHandler;
55 import org.collectionspace.services.common.document.DocumentNotFoundException;
56 import org.collectionspace.services.common.document.DocumentWrapper;
57 import org.collectionspace.services.common.query.IQueryManager;
58 import org.collectionspace.services.common.query.QueryManager;
59 import org.collectionspace.services.common.security.UnauthorizedException;
60 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
61 import org.collectionspace.services.intake.IntakeResource;
62 import org.collectionspace.services.intake.IntakesCommonList;
63 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
64 import org.collectionspace.services.relation.NewRelationResource;
65 import org.collectionspace.services.relation.RelationsCommonList;
66 import org.collectionspace.services.relation.RelationshipType;
67 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
68 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
69 import org.jboss.resteasy.util.HttpResponseCodes;
70 import org.nuxeo.ecm.core.api.DocumentModel;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
76 * The Class CollectionObjectResource.
78 @Path("/collectionobjects")
79 @Consumes("multipart/mixed")
80 @Produces("multipart/mixed")
81 public class CollectionObjectResource
82 extends AbstractCollectionSpaceResourceImpl {
84 /** The Constant serviceName. */
85 static final public String serviceName = "collectionobjects";
88 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
91 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
94 protected String getVersionString() {
95 /** The last change revision. */
96 final String lastChangeRevision = "$LastChangedRevision$";
97 return lastChangeRevision;
101 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
104 public String getServiceName() {
109 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
112 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
113 DocumentHandler docHandler = ctx.getDocumentHandler();
114 if (ctx.getInput() != null) {
115 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(),
116 CollectionobjectsCommon.class);
118 docHandler.setCommonPart((CollectionobjectsCommon) obj);
125 * Creates the collection object.
127 * @param input the input
129 * @return the response
132 public Response createCollectionObject(MultipartInput input) {
134 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
135 DocumentHandler handler = createDocumentHandler(ctx);
136 String csid = getRepositoryClient(ctx).create(ctx, handler);
137 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
138 path.path("" + csid);
139 Response response = Response.created(path.build()).build();
141 } catch (BadRequestException bre) {
142 Response response = Response.status(
143 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
144 throw new WebApplicationException(response);
145 } catch (UnauthorizedException ue) {
146 Response response = Response.status(
147 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
148 throw new WebApplicationException(response);
149 } catch (Exception e) {
150 if (logger.isDebugEnabled()) {
151 logger.debug("Caught exception in createCollectionObject", e);
153 Response response = Response.status(
154 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
155 throw new WebApplicationException(response);
160 * Gets the collection object.
162 * @param csid the csid
164 * @return the collection object
168 public MultipartOutput getCollectionObject(
169 @PathParam("csid") String csid) {
170 if (logger.isDebugEnabled()) {
171 logger.debug("getCollectionObject with csid=" + csid);
173 if (csid == null || "".equals(csid)) {
174 logger.error("getCollectionObject: missing csid!");
175 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
176 "get failed on CollectionObject csid=" + csid).type(
177 "text/plain").build();
178 throw new WebApplicationException(response);
180 MultipartOutput result = null;
182 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
183 DocumentHandler handler = createDocumentHandler(ctx);
184 getRepositoryClient(ctx).get(ctx, csid, handler);
185 result = (MultipartOutput) ctx.getOutput();
186 } catch (UnauthorizedException ue) {
187 Response response = Response.status(
188 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
189 throw new WebApplicationException(response);
190 } catch (DocumentNotFoundException dnfe) {
191 if (logger.isDebugEnabled()) {
192 logger.debug("getCollectionObject", dnfe);
194 Response response = Response.status(Response.Status.NOT_FOUND).entity(
195 "Get failed on CollectionObject csid=" + csid).type(
196 "text/plain").build();
197 throw new WebApplicationException(response);
198 } catch (Exception e) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("getCollectionObject", e);
202 Response response = Response.status(
203 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
204 throw new WebApplicationException(response);
207 if (result == null) {
208 Response response = Response.status(Response.Status.NOT_FOUND).entity(
209 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
210 "text/plain").build();
211 throw new WebApplicationException(response);
217 * Gets the collection object list.
221 * @return the collection object list
224 @Produces("application/xml")
225 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) {
226 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
228 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
229 DocumentHandler handler = createDocumentHandler(ctx);
230 getRepositoryClient(ctx).getAll(ctx, handler);
231 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
232 } catch (UnauthorizedException ue) {
233 Response response = Response.status(
234 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
235 throw new WebApplicationException(response);
236 } catch (Exception e) {
237 if (logger.isDebugEnabled()) {
238 logger.debug("Caught exception in getCollectionObjectList", e);
240 Response response = Response.status(
241 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
242 throw new WebApplicationException(response);
244 return collectionObjectList;
248 * Update collection object.
250 * @param csid the csid
251 * @param theUpdate the the update
253 * @return the multipart output
257 public MultipartOutput updateCollectionObject(
258 @PathParam("csid") String csid,
259 MultipartInput theUpdate) {
260 if (logger.isDebugEnabled()) {
261 logger.debug("updateCollectionObject with csid=" + csid);
263 if (csid == null || "".equals(csid)) {
264 logger.error("updateCollectionObject: missing csid!");
265 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
266 "update failed on CollectionObject csid=" + csid).type(
267 "text/plain").build();
268 throw new WebApplicationException(response);
270 MultipartOutput result = null;
272 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
273 DocumentHandler handler = createDocumentHandler(ctx);
274 getRepositoryClient(ctx).update(ctx, csid, handler);
275 result = (MultipartOutput) ctx.getOutput();
276 } catch (BadRequestException bre) {
277 Response response = Response.status(
278 Response.Status.BAD_REQUEST).entity("Update failed reason " + bre.getErrorReason()).type("text/plain").build();
279 throw new WebApplicationException(response);
280 } catch (UnauthorizedException ue) {
281 Response response = Response.status(
282 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
283 throw new WebApplicationException(response);
284 } catch (DocumentNotFoundException dnfe) {
285 if (logger.isDebugEnabled()) {
286 logger.debug("caugth exception in updateCollectionObject", dnfe);
288 Response response = Response.status(Response.Status.NOT_FOUND).entity(
289 "Update failed on CollectionObject csid=" + csid).type(
290 "text/plain").build();
291 throw new WebApplicationException(response);
292 } catch (Exception e) {
293 Response response = Response.status(
294 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
295 throw new WebApplicationException(response);
301 * Delete collection object.
303 * @param csid the csid
305 * @return the response
309 public Response deleteCollectionObject(@PathParam("csid") String csid) {
311 if (logger.isDebugEnabled()) {
312 logger.debug("deleteCollectionObject with csid=" + csid);
314 if (csid == null || "".equals(csid)) {
315 logger.error("deleteCollectionObject: missing csid!");
316 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
317 "delete failed on CollectionObject csid=" + csid).type(
318 "text/plain").build();
319 throw new WebApplicationException(response);
322 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
323 getRepositoryClient(ctx).delete(ctx, csid);
324 return Response.status(HttpResponseCodes.SC_OK).build();
325 } catch (UnauthorizedException ue) {
326 Response response = Response.status(
327 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
328 throw new WebApplicationException(response);
329 } catch (DocumentNotFoundException dnfe) {
330 if (logger.isDebugEnabled()) {
331 logger.debug("caught exception in deleteCollectionObject", dnfe);
333 Response response = Response.status(Response.Status.NOT_FOUND).entity(
334 "Delete failed on CollectionObject csid=" + csid).type(
335 "text/plain").build();
336 throw new WebApplicationException(response);
337 } catch (Exception e) {
338 Response response = Response.status(
339 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
340 throw new WebApplicationException(response);
346 * Gets the intakes common list.
349 * @param csid the csid
351 * @return the intakes common list
354 @Path("{csid}/intakes")
355 @Produces("application/xml")
356 public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
357 @PathParam("csid") String csid) {
358 IntakesCommonList result = null;
362 // Find all the intake-related relation records.
364 String subjectCsid = csid;
365 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
366 String objectCsid = null;
367 NewRelationResource relationResource = new NewRelationResource();
368 RelationsCommonList relationsCommonList = relationResource.getRelationList(subjectCsid, predicate, objectCsid);
371 // Create an array of Intake csid's
373 List<RelationsCommonList.RelationListItem> relationsListItems = relationsCommonList.getRelationListItem();
374 List<String> intakeCsidList = new ArrayList<String>();
375 for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) {
376 intakeCsidList.add(relationsListItem.getObjectCsid());
380 // Get a response list for the Intake records from the Intake resource
382 IntakeResource intakeResource = new IntakeResource();
383 result = intakeResource.getIntakeList(intakeCsidList);
384 } catch (Exception e) {
385 if (logger.isDebugEnabled()) {
386 logger.debug("Caught exception in getIntakeList", e);
388 Response response = Response.status(
389 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
390 throw new WebApplicationException(response);
397 @Path("{csid}/authorityrefs")
398 @Produces("application/xml")
399 public AuthorityRefList getAuthorityRefs(
400 @PathParam("csid") String csid,
401 @Context UriInfo ui) {
402 AuthorityRefList authRefList = null;
404 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
405 DocumentWrapper<DocumentModel> docWrapper =
406 getRepositoryClient(ctx).getDoc(ctx, csid);
407 RemoteDocumentModelHandlerImpl handler
408 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
409 List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(RefNameServiceUtils.AUTH_REF_PROP);
410 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
411 } catch (UnauthorizedException ue) {
412 Response response = Response.status(
413 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
414 throw new WebApplicationException(response);
415 } catch (Exception e) {
416 if (logger.isDebugEnabled()) {
417 logger.debug("Caught exception in getAuthorityRefs", e);
419 Response response = Response.status(
420 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
421 throw new WebApplicationException(response);
429 * This is an intentionally empty method used for getting a rough time estimate
430 * of the overhead required for a client->server request/response cycle.
432 * @return the response
436 @Produces("application/xml")
437 public Response roundtrip() {
438 Response result = null;
440 if (logger.isDebugEnabled()) {
441 logger.debug("------------------------------------------------------------------------------");
442 logger.debug("Client to server roundtrip called.");
443 logger.debug("------------------------------------------------------------------------------");
446 result = Response.status(HttpResponseCodes.SC_OK).build();
452 //FIXME: Replace this "search" resource with a keyword "kw" query parameter
454 * Keywords search collection objects.
456 * @param keywords the keywords
458 * @return the collectionobjects common list
462 @Produces("application/xml")
463 public CollectionobjectsCommonList keywordsSearchCollectionObjects(
464 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
465 CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList();
467 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
468 DocumentHandler handler = createDocumentHandler(ctx);
470 // perform a keyword search
471 if (keywords != null && !keywords.isEmpty()) {
472 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
473 DocumentFilter documentFilter = handler.getDocumentFilter();
474 documentFilter.setWhereClause(whereClause);
475 if (logger.isDebugEnabled()) {
476 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
478 getRepositoryClient(ctx).getFiltered(ctx, handler);
480 getRepositoryClient(ctx).getAll(ctx, handler);
482 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
484 } catch (UnauthorizedException ue) {
485 Response response = Response.status(
486 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
487 throw new WebApplicationException(response);
488 } catch (Exception e) {
489 if (logger.isDebugEnabled()) {
490 logger.debug("Caught exception in getCollectionObjectList", e);
492 Response response = Response.status(
493 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
494 throw new WebApplicationException(response);
496 return collectionObjectList;