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.AbstractMultiPartCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.authorityref.AuthorityRefList;
48 import org.collectionspace.services.common.context.ServiceContextFactory;
49 //import org.collectionspace.services.common.context.MultipartServiceContext;
50 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
51 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
52 import org.collectionspace.services.common.context.ServiceContext;
53 import org.collectionspace.services.common.document.BadRequestException;
54 import org.collectionspace.services.common.document.DocumentFilter;
55 import org.collectionspace.services.common.document.DocumentHandler;
56 import org.collectionspace.services.common.document.DocumentNotFoundException;
57 import org.collectionspace.services.common.document.DocumentWrapper;
58 import org.collectionspace.services.common.query.IQueryManager;
59 import org.collectionspace.services.common.query.QueryManager;
60 import org.collectionspace.services.common.security.UnauthorizedException;
61 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
62 import org.collectionspace.services.intake.IntakeResource;
63 import org.collectionspace.services.intake.IntakesCommonList;
64 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
65 import org.collectionspace.services.relation.NewRelationResource;
66 import org.collectionspace.services.relation.RelationsCommonList;
67 import org.collectionspace.services.relation.RelationshipType;
68 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
69 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
70 import org.jboss.resteasy.util.HttpResponseCodes;
71 import org.nuxeo.ecm.core.api.DocumentModel;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
77 * The Class CollectionObjectResource.
79 @Path("/collectionobjects")
80 @Consumes("multipart/mixed")
81 @Produces("multipart/mixed")
82 public class CollectionObjectResource
83 extends AbstractMultiPartCollectionSpaceResourceImpl {
85 /** The Constant serviceName. */
86 static final public String serviceName = "collectionobjects";
89 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
92 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
95 protected String getVersionString() {
96 /** The last change revision. */
97 final String lastChangeRevision = "$LastChangedRevision$";
98 return lastChangeRevision;
102 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
105 public String getServiceName() {
110 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
113 public Class<CollectionobjectsCommon> getCommonPartClass() {
114 return CollectionobjectsCommon.class;
118 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
121 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
122 // DocumentHandler docHandler = ctx.getDocumentHandler();
123 // if (ctx.getInput() != null) {
124 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(),
125 // CollectionobjectsCommon.class);
126 // if (obj != null) {
127 // docHandler.setCommonPart((CollectionobjectsCommon) obj);
130 // return docHandler;
134 * Creates the collection object.
136 * @param input the input
138 * @return the response
141 public Response createCollectionObject(MultipartInput input) {
143 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input); //
144 DocumentHandler handler = createDocumentHandler(ctx);
145 String csid = getRepositoryClient(ctx).create(ctx, handler);
146 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
147 path.path("" + csid);
148 Response response = Response.created(path.build()).build();
150 } catch (BadRequestException bre) {
151 Response response = Response.status(
152 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
153 throw new WebApplicationException(response);
154 } catch (UnauthorizedException ue) {
155 Response response = Response.status(
156 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
157 throw new WebApplicationException(response);
158 } catch (Exception e) {
159 if (logger.isDebugEnabled()) {
160 logger.debug("Caught exception in createCollectionObject", e);
162 Response response = Response.status(
163 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
164 throw new WebApplicationException(response);
169 * Gets the collection object.
171 * @param csid the csid
173 * @return the collection object
177 public MultipartOutput getCollectionObject(
178 @PathParam("csid") String csid) {
179 if (logger.isDebugEnabled()) {
180 logger.debug("getCollectionObject with csid=" + csid);
182 if (csid == null || "".equals(csid)) {
183 logger.error("getCollectionObject: missing csid!");
184 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
185 "get failed on CollectionObject csid=" + csid).type(
186 "text/plain").build();
187 throw new WebApplicationException(response);
189 MultipartOutput result = null;
191 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
192 DocumentHandler handler = createDocumentHandler(ctx);
193 getRepositoryClient(ctx).get(ctx, csid, handler);
194 result = (MultipartOutput) ctx.getOutput();
195 } catch (UnauthorizedException ue) {
196 Response response = Response.status(
197 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
198 throw new WebApplicationException(response);
199 } catch (DocumentNotFoundException dnfe) {
200 if (logger.isDebugEnabled()) {
201 logger.debug("getCollectionObject", dnfe);
203 Response response = Response.status(Response.Status.NOT_FOUND).entity(
204 "Get failed on CollectionObject csid=" + csid).type(
205 "text/plain").build();
206 throw new WebApplicationException(response);
207 } catch (Exception e) {
208 if (logger.isDebugEnabled()) {
209 logger.debug("getCollectionObject", e);
211 Response response = Response.status(
212 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
213 throw new WebApplicationException(response);
216 if (result == null) {
217 Response response = Response.status(Response.Status.NOT_FOUND).entity(
218 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
219 "text/plain").build();
220 throw new WebApplicationException(response);
226 * Gets the collection object list.
229 * @param keywords the keywords
231 * @return the collection object list
234 @Produces("application/xml")
235 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui,
236 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
237 CollectionobjectsCommonList result = null;
238 if (keywords != null) {
239 result = searchCollectionObjects(keywords);
241 result = getCollectionObjectList();
248 * Gets the collection object list.
250 private CollectionobjectsCommonList getCollectionObjectList() {
251 CollectionobjectsCommonList collectionObjectList;
253 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
254 DocumentHandler handler = createDocumentHandler(ctx);
255 getRepositoryClient(ctx).getAll(ctx, handler);
256 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
257 } catch (UnauthorizedException ue) {
258 Response response = Response.status(
259 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
260 throw new WebApplicationException(response);
261 } catch (Exception e) {
262 if (logger.isDebugEnabled()) {
263 logger.debug("Caught exception in getCollectionObjectList", e);
265 Response response = Response.status(
266 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
267 throw new WebApplicationException(response);
269 return collectionObjectList;
273 * Update collection object.
275 * @param csid the csid
276 * @param theUpdate the the update
278 * @return the multipart output
282 public MultipartOutput updateCollectionObject(
283 @PathParam("csid") String csid,
284 MultipartInput theUpdate) {
285 if (logger.isDebugEnabled()) {
286 logger.debug("updateCollectionObject with csid=" + csid);
288 if (csid == null || "".equals(csid)) {
289 logger.error("updateCollectionObject: missing csid!");
290 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
291 "update failed on CollectionObject csid=" + csid).type(
292 "text/plain").build();
293 throw new WebApplicationException(response);
295 MultipartOutput result = null;
297 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
298 DocumentHandler handler = createDocumentHandler(ctx);
299 getRepositoryClient(ctx).update(ctx, csid, handler);
300 result = (MultipartOutput) ctx.getOutput();
301 } catch (BadRequestException bre) {
302 Response response = Response.status(
303 Response.Status.BAD_REQUEST).entity("Update failed reason " + bre.getErrorReason()).type("text/plain").build();
304 throw new WebApplicationException(response);
305 } catch (UnauthorizedException ue) {
306 Response response = Response.status(
307 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
308 throw new WebApplicationException(response);
309 } catch (DocumentNotFoundException dnfe) {
310 if (logger.isDebugEnabled()) {
311 logger.debug("caugth exception in updateCollectionObject", dnfe);
313 Response response = Response.status(Response.Status.NOT_FOUND).entity(
314 "Update failed on CollectionObject csid=" + csid).type(
315 "text/plain").build();
316 throw new WebApplicationException(response);
317 } catch (Exception e) {
318 Response response = Response.status(
319 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
320 throw new WebApplicationException(response);
326 * Delete collection object.
328 * @param csid the csid
330 * @return the response
334 public Response deleteCollectionObject(@PathParam("csid") String csid) {
336 if (logger.isDebugEnabled()) {
337 logger.debug("deleteCollectionObject with csid=" + csid);
339 if (csid == null || "".equals(csid)) {
340 logger.error("deleteCollectionObject: missing csid!");
341 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
342 "delete failed on CollectionObject csid=" + csid).type(
343 "text/plain").build();
344 throw new WebApplicationException(response);
347 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
348 getRepositoryClient(ctx).delete(ctx, csid);
349 return Response.status(HttpResponseCodes.SC_OK).build();
350 } catch (UnauthorizedException ue) {
351 Response response = Response.status(
352 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
353 throw new WebApplicationException(response);
354 } catch (DocumentNotFoundException dnfe) {
355 if (logger.isDebugEnabled()) {
356 logger.debug("caught exception in deleteCollectionObject", dnfe);
358 Response response = Response.status(Response.Status.NOT_FOUND).entity(
359 "Delete failed on CollectionObject csid=" + csid).type(
360 "text/plain").build();
361 throw new WebApplicationException(response);
362 } catch (Exception e) {
363 Response response = Response.status(
364 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
365 throw new WebApplicationException(response);
371 * Gets the intakes common list.
374 * @param csid the csid
376 * @return the intakes common list
379 @Path("{csid}/intakes")
380 @Produces("application/xml")
381 public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
382 @PathParam("csid") String csid) {
383 IntakesCommonList result = null;
387 // Find all the intake-related relation records.
389 String subjectCsid = csid;
390 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
391 String objectCsid = null;
392 NewRelationResource relationResource = new NewRelationResource();
393 RelationsCommonList relationsCommonList = relationResource.getRelationList(subjectCsid, predicate, objectCsid);
396 // Create an array of Intake csid's
398 List<RelationsCommonList.RelationListItem> relationsListItems = relationsCommonList.getRelationListItem();
399 List<String> intakeCsidList = new ArrayList<String>();
400 for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) {
401 intakeCsidList.add(relationsListItem.getObjectCsid());
405 // Get a response list for the Intake records from the Intake resource
407 IntakeResource intakeResource = new IntakeResource();
408 result = intakeResource.getIntakeList(intakeCsidList);
409 } catch (Exception e) {
410 if (logger.isDebugEnabled()) {
411 logger.debug("Caught exception in getIntakeList", e);
413 Response response = Response.status(
414 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
415 throw new WebApplicationException(response);
422 * Gets the authority refs.
424 * @param csid the csid
427 * @return the authority refs
430 @Path("{csid}/authorityrefs")
431 @Produces("application/xml")
432 public AuthorityRefList getAuthorityRefs(
433 @PathParam("csid") String csid,
434 @Context UriInfo ui) {
435 AuthorityRefList authRefList = null;
437 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
438 DocumentWrapper<DocumentModel> docWrapper =
439 getRepositoryClient(ctx).getDoc(ctx, csid);
440 RemoteDocumentModelHandlerImpl handler
441 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
442 List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(RefNameServiceUtils.AUTH_REF_PROP);
443 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
444 } catch (UnauthorizedException ue) {
445 Response response = Response.status(
446 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
447 throw new WebApplicationException(response);
448 } catch (Exception e) {
449 if (logger.isDebugEnabled()) {
450 logger.debug("Caught exception in getAuthorityRefs", e);
452 Response response = Response.status(
453 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
454 throw new WebApplicationException(response);
462 * This is an intentionally empty method used for getting a rough time estimate
463 * of the overhead required for a client->server request/response cycle.
465 * @return the response
469 @Produces("application/xml")
470 public Response roundtrip() {
471 Response result = null;
473 if (logger.isDebugEnabled()) {
474 logger.debug("------------------------------------------------------------------------------");
475 logger.debug("Client to server roundtrip called.");
476 logger.debug("------------------------------------------------------------------------------");
479 result = Response.status(HttpResponseCodes.SC_OK).build();
485 * This method is deprecated. Use kwSearchCollectionObjects() method instead.
486 * Keywords search collection objects.
488 * @param keywords the keywords
490 * @return the collectionobjects common list
494 @Produces("application/xml")
495 public CollectionobjectsCommonList keywordsSearchCollectionObjects(
496 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
497 return searchCollectionObjects(keywords);
501 * Search collection objects.
503 * @param keywords the keywords
505 * @return the collectionobjects common list
507 private CollectionobjectsCommonList searchCollectionObjects(String keywords) {
508 CollectionobjectsCommonList collectionObjectList;
510 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
511 DocumentHandler handler = createDocumentHandler(ctx);
513 // perform a keyword search
514 if (keywords != null && !keywords.isEmpty()) {
515 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
516 //DocumentFilter documentFilter = handler.createDocumentFilter(ctx);
517 DocumentFilter documentFilter = handler.getDocumentFilter();
518 documentFilter.setWhereClause(whereClause);
519 if (logger.isDebugEnabled()) {
520 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
522 getRepositoryClient(ctx).getFiltered(ctx, handler);
524 getRepositoryClient(ctx).getAll(ctx, handler);
526 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
528 } catch (UnauthorizedException ue) {
529 Response response = Response.status(
530 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
531 throw new WebApplicationException(response);
532 } catch (Exception e) {
533 if (logger.isDebugEnabled()) {
534 logger.debug("Caught exception in getCollectionObjectList", e);
536 Response response = Response.status(
537 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
538 throw new WebApplicationException(response);
540 return collectionObjectList;