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;
45 import javax.ws.rs.core.MultivaluedMap;
47 import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
48 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
49 import org.collectionspace.services.common.authorityref.AuthorityRefList;
50 import org.collectionspace.services.common.context.ServiceContextFactory;
51 //import org.collectionspace.services.common.context.MultipartServiceContext;
52 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
53 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
54 import org.collectionspace.services.common.context.ServiceBindingUtils;
55 import org.collectionspace.services.common.context.ServiceContext;
56 import org.collectionspace.services.common.document.BadRequestException;
57 import org.collectionspace.services.common.document.DocumentFilter;
58 import org.collectionspace.services.common.document.DocumentHandler;
59 import org.collectionspace.services.common.document.DocumentNotFoundException;
60 import org.collectionspace.services.common.document.DocumentWrapper;
61 import org.collectionspace.services.common.query.IQueryManager;
62 import org.collectionspace.services.common.query.QueryManager;
63 import org.collectionspace.services.common.security.UnauthorizedException;
64 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
65 import org.collectionspace.services.intake.IntakeResource;
66 import org.collectionspace.services.intake.IntakesCommonList;
67 //import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
68 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
69 import org.collectionspace.services.relation.NewRelationResource;
70 import org.collectionspace.services.relation.RelationsCommonList;
71 import org.collectionspace.services.relation.RelationshipType;
72 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
73 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
74 import org.jboss.resteasy.util.HttpResponseCodes;
75 import org.nuxeo.ecm.core.api.DocumentModel;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
81 * The Class CollectionObjectResource.
83 @Path("/collectionobjects")
84 @Consumes("multipart/mixed")
85 @Produces("multipart/mixed")
86 public class CollectionObjectResource
87 extends AbstractMultiPartCollectionSpaceResourceImpl {
89 /** The Constant serviceName. */
90 static final public String serviceName = "collectionobjects";
93 final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
96 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
99 public String getVersionString() {
100 /** The last change revision. */
101 final String lastChangeRevision = "$LastChangedRevision$";
102 return lastChangeRevision;
106 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
109 public String getServiceName() {
114 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
117 public Class<CollectionobjectsCommon> getCommonPartClass() {
118 return CollectionobjectsCommon.class;
122 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
125 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
126 // DocumentHandler docHandler = ctx.getDocumentHandler();
127 // if (ctx.getInput() != null) {
128 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(),
129 // CollectionobjectsCommon.class);
130 // if (obj != null) {
131 // docHandler.setCommonPart((CollectionobjectsCommon) obj);
134 // return docHandler;
138 * Creates the collection object.
140 * @param input the input
142 * @return the response
145 public Response createCollectionObject(MultipartInput input) {
147 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input); //
148 DocumentHandler handler = createDocumentHandler(ctx);
149 String csid = getRepositoryClient(ctx).create(ctx, handler);
150 UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
151 path.path("" + csid);
152 Response response = Response.created(path.build()).build();
154 } catch (BadRequestException bre) {
155 Response response = Response.status(
156 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
157 throw new WebApplicationException(response);
158 } catch (UnauthorizedException ue) {
159 Response response = Response.status(
160 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
161 throw new WebApplicationException(response);
162 } catch (Exception e) {
163 if (logger.isDebugEnabled()) {
164 logger.debug("Caught exception in createCollectionObject", e);
166 Response response = Response.status(
167 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
168 throw new WebApplicationException(response);
173 * Gets the collection object.
175 * @param csid the csid
177 * @return the collection object
181 public MultipartOutput getCollectionObject(
182 @PathParam("csid") String csid) {
183 if (logger.isDebugEnabled()) {
184 logger.debug("getCollectionObject with csid=" + csid);
186 if (csid == null || "".equals(csid)) {
187 logger.error("getCollectionObject: missing csid!");
188 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
189 "get failed on CollectionObject csid=" + csid).type(
190 "text/plain").build();
191 throw new WebApplicationException(response);
193 MultipartOutput result = null;
195 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
196 DocumentHandler handler = createDocumentHandler(ctx);
197 getRepositoryClient(ctx).get(ctx, csid, handler);
198 result = (MultipartOutput) ctx.getOutput();
199 } catch (UnauthorizedException ue) {
200 Response response = Response.status(
201 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
202 throw new WebApplicationException(response);
203 } catch (DocumentNotFoundException dnfe) {
204 if (logger.isDebugEnabled()) {
205 logger.debug("getCollectionObject", dnfe);
207 Response response = Response.status(Response.Status.NOT_FOUND).entity(
208 "Get failed on CollectionObject csid=" + csid).type(
209 "text/plain").build();
210 throw new WebApplicationException(response);
211 } catch (Exception e) {
212 if (logger.isDebugEnabled()) {
213 logger.debug("getCollectionObject", e);
215 Response response = Response.status(
216 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
217 throw new WebApplicationException(response);
220 if (result == null) {
221 Response response = Response.status(Response.Status.NOT_FOUND).entity(
222 "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type(
223 "text/plain").build();
224 throw new WebApplicationException(response);
230 * Gets the collection object list.
233 * @param keywords the keywords
235 * @return the collection object list
238 @Produces("application/xml")
239 public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui,
240 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
241 CollectionobjectsCommonList result = null;
242 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
243 if (keywords != null) {
244 result = searchCollectionObjects(queryParams, keywords);
246 result = getCollectionObjectList(queryParams);
253 * Gets the collection object list.
255 private CollectionobjectsCommonList getCollectionObjectList(MultivaluedMap<String, String> queryParams) {
256 CollectionobjectsCommonList collectionObjectList;
258 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
259 DocumentHandler handler = createDocumentHandler(ctx);
260 getRepositoryClient(ctx).getFiltered(ctx, handler);
261 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
262 } catch (UnauthorizedException ue) {
263 Response response = Response.status(
264 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
265 throw new WebApplicationException(response);
266 } catch (Exception e) {
267 if (logger.isDebugEnabled()) {
268 logger.debug("Caught exception in getCollectionObjectList", e);
270 Response response = Response.status(
271 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
272 throw new WebApplicationException(response);
274 return collectionObjectList;
278 * Update collection object.
280 * @param csid the csid
281 * @param theUpdate the the update
283 * @return the multipart output
287 public MultipartOutput updateCollectionObject(
288 @PathParam("csid") String csid,
289 MultipartInput theUpdate) {
290 if (logger.isDebugEnabled()) {
291 logger.debug("updateCollectionObject with csid=" + csid);
293 if (csid == null || "".equals(csid)) {
294 logger.error("updateCollectionObject: missing csid!");
295 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
296 "update failed on CollectionObject csid=" + csid).type(
297 "text/plain").build();
298 throw new WebApplicationException(response);
300 MultipartOutput result = null;
302 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
303 DocumentHandler handler = createDocumentHandler(ctx);
304 getRepositoryClient(ctx).update(ctx, csid, handler);
305 result = (MultipartOutput) ctx.getOutput();
306 } catch (BadRequestException bre) {
307 Response response = Response.status(
308 Response.Status.BAD_REQUEST).entity("Update failed reason " + bre.getErrorReason()).type("text/plain").build();
309 throw new WebApplicationException(response);
310 } catch (UnauthorizedException ue) {
311 Response response = Response.status(
312 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
313 throw new WebApplicationException(response);
314 } catch (DocumentNotFoundException dnfe) {
315 if (logger.isDebugEnabled()) {
316 logger.debug("caugth exception in updateCollectionObject", dnfe);
318 Response response = Response.status(Response.Status.NOT_FOUND).entity(
319 "Update failed on CollectionObject csid=" + csid).type(
320 "text/plain").build();
321 throw new WebApplicationException(response);
322 } catch (Exception e) {
323 Response response = Response.status(
324 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
325 throw new WebApplicationException(response);
331 * Delete collection object.
333 * @param csid the csid
335 * @return the response
339 public Response deleteCollectionObject(@PathParam("csid") String csid) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("deleteCollectionObject with csid=" + csid);
344 if (csid == null || "".equals(csid)) {
345 logger.error("deleteCollectionObject: missing csid!");
346 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
347 "delete failed on CollectionObject csid=" + csid).type(
348 "text/plain").build();
349 throw new WebApplicationException(response);
352 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
353 getRepositoryClient(ctx).delete(ctx, csid);
354 return Response.status(HttpResponseCodes.SC_OK).build();
355 } catch (UnauthorizedException ue) {
356 Response response = Response.status(
357 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
358 throw new WebApplicationException(response);
359 } catch (DocumentNotFoundException dnfe) {
360 if (logger.isDebugEnabled()) {
361 logger.debug("caught exception in deleteCollectionObject", dnfe);
363 Response response = Response.status(Response.Status.NOT_FOUND).entity(
364 "Delete failed on CollectionObject csid=" + csid).type(
365 "text/plain").build();
366 throw new WebApplicationException(response);
367 } catch (Exception e) {
368 Response response = Response.status(
369 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
370 throw new WebApplicationException(response);
376 * Gets the intakes common list.
379 * @param csid the csid
381 * @return the intakes common list
384 @Path("{csid}/intakes")
385 @Produces("application/xml")
386 public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
387 @PathParam("csid") String csid) {
388 IntakesCommonList result = null;
389 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
393 // Find all the intake-related relation records.
395 String subjectCsid = csid;
396 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
397 String objectCsid = null;
398 NewRelationResource relationResource = new NewRelationResource();
399 RelationsCommonList relationsCommonList = relationResource.getRelationList(queryParams,
400 subjectCsid, predicate, objectCsid);
403 // Create an array of Intake csid's
405 List<RelationsCommonList.RelationListItem> relationsListItems = relationsCommonList.getRelationListItem();
406 List<String> intakeCsidList = new ArrayList<String>();
407 for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) {
408 intakeCsidList.add(relationsListItem.getObjectCsid());
412 // Get a response list for the Intake records from the Intake resource
414 IntakeResource intakeResource = new IntakeResource();
415 result = intakeResource.getIntakeList(intakeCsidList);
416 } catch (Exception e) {
417 if (logger.isDebugEnabled()) {
418 logger.debug("Caught exception in getIntakeList", e);
420 Response response = Response.status(
421 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
422 throw new WebApplicationException(response);
429 * Gets the authority refs.
431 * @param csid the csid
434 * @return the authority refs
437 @Path("{csid}/authorityrefs")
438 @Produces("application/xml")
439 public AuthorityRefList getAuthorityRefs(
440 @PathParam("csid") String csid,
441 @Context UriInfo ui) {
442 AuthorityRefList authRefList = null;
444 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
445 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
446 DocumentWrapper<DocumentModel> docWrapper =
447 getRepositoryClient(ctx).getDoc(ctx, csid);
448 DocumentModelHandler<MultipartInput, MultipartOutput> docHandler =
449 (DocumentModelHandler<MultipartInput, MultipartOutput>)createDocumentHandler(ctx);
450 List<String> authRefFields =
451 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
452 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
453 authRefList = docHandler.getAuthorityRefs(docWrapper, authRefFields);
454 } catch (UnauthorizedException ue) {
455 Response response = Response.status(
456 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
457 throw new WebApplicationException(response);
458 } catch (Exception e) {
459 if (logger.isDebugEnabled()) {
460 logger.debug("Caught exception in getAuthorityRefs", e);
462 Response response = Response.status(
463 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
464 throw new WebApplicationException(response);
472 * This is an intentionally empty method used for getting a rough time estimate
473 * of the overhead required for a client->server request/response cycle.
475 * @return the response
479 @Produces("application/xml")
480 public Response roundtrip() {
481 Response result = null;
483 if (logger.isDebugEnabled()) {
484 logger.debug("------------------------------------------------------------------------------");
485 logger.debug("Client to server roundtrip called.");
486 logger.debug("------------------------------------------------------------------------------");
489 result = Response.status(HttpResponseCodes.SC_OK).build();
496 @Produces("application/xml")
497 public Response createPictureDocument() {
498 Response result = null;
500 if (logger.isDebugEnabled()) {
501 logger.debug("------------------------------------------------------------------------------");
502 logger.debug("Prototype to create a Picture document in Nuxeo");
503 logger.debug("------------------------------------------------------------------------------");
507 NuxeoImageUtils.createPicture();
508 result = Response.status(HttpResponseCodes.SC_OK).build();
514 * This method is deprecated. Use kwSearchCollectionObjects() method instead.
515 * Keywords search collection objects.
518 * @param keywords the keywords
520 * @return the collectionobjects common list
524 @Produces("application/xml")
525 public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
526 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
527 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
528 return searchCollectionObjects(queryParams, keywords);
532 * Search collection objects.
534 * @param keywords the keywords
536 * @return the collectionobjects common list
538 private CollectionobjectsCommonList searchCollectionObjects(
539 MultivaluedMap<String, String> queryParams,
541 CollectionobjectsCommonList collectionObjectList;
543 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
544 DocumentHandler handler = createDocumentHandler(ctx);
546 // perform a keyword search
547 if (keywords != null && !keywords.isEmpty()) {
548 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
549 DocumentFilter documentFilter = handler.getDocumentFilter();
550 documentFilter.setWhereClause(whereClause);
551 if (logger.isDebugEnabled()) {
552 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
555 getRepositoryClient(ctx).getFiltered(ctx, handler);
556 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
557 } catch (UnauthorizedException ue) {
558 Response response = Response.status(
559 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
560 throw new WebApplicationException(response);
561 } catch (Exception e) {
562 if (logger.isDebugEnabled()) {
563 logger.debug("Caught exception in getCollectionObjectList", e);
565 Response response = Response.status(
566 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
567 throw new WebApplicationException(response);
569 return collectionObjectList;