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
385 @Path("{csid}/intakes")
386 @Produces("application/xml")
387 public IntakesCommonList getIntakesCommonList(@Context UriInfo ui,
388 @PathParam("csid") String csid) {
389 IntakesCommonList result = null;
390 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
394 // Find all the intake-related relation records.
396 String subjectCsid = csid;
397 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value();
398 String objectCsid = null;
399 NewRelationResource relationResource = new NewRelationResource();
400 RelationsCommonList relationsCommonList = relationResource.getRelationList(queryParams,
401 subjectCsid, predicate, objectCsid);
404 // Create an array of Intake csid's
406 List<RelationsCommonList.RelationListItem> relationsListItems = relationsCommonList.getRelationListItem();
407 List<String> intakeCsidList = new ArrayList<String>();
408 for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) {
409 intakeCsidList.add(relationsListItem.getObjectCsid());
413 // Get a response list for the Intake records from the Intake resource
415 IntakeResource intakeResource = new IntakeResource();
416 result = intakeResource.getIntakeList(intakeCsidList);
417 } catch (Exception e) {
418 if (logger.isDebugEnabled()) {
419 logger.debug("Caught exception in getIntakeList", e);
421 Response response = Response.status(
422 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
423 throw new WebApplicationException(response);
432 * Gets the authority refs.
434 * @param csid the csid
437 * @return the authority refs
440 @Path("{csid}/authorityrefs")
441 @Produces("application/xml")
442 public AuthorityRefList getAuthorityRefs(
443 @PathParam("csid") String csid,
444 @Context UriInfo ui) {
445 AuthorityRefList authRefList = null;
447 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
448 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
449 DocumentWrapper<DocumentModel> docWrapper =
450 getRepositoryClient(ctx).getDoc(ctx, csid);
451 DocumentModelHandler<MultipartInput, MultipartOutput> docHandler =
452 (DocumentModelHandler<MultipartInput, MultipartOutput>)createDocumentHandler(ctx);
453 List<String> authRefFields =
454 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
455 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
456 authRefList = docHandler.getAuthorityRefs(docWrapper, authRefFields);
457 } catch (UnauthorizedException ue) {
458 Response response = Response.status(
459 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
460 throw new WebApplicationException(response);
461 } catch (Exception e) {
462 if (logger.isDebugEnabled()) {
463 logger.debug("Caught exception in getAuthorityRefs", e);
465 Response response = Response.status(
466 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
467 throw new WebApplicationException(response);
475 * This is an intentionally empty method used for getting a rough time estimate
476 * of the overhead required for a client->server request/response cycle.
478 * @return the response
482 @Produces("application/xml")
483 public Response roundtrip() {
484 Response result = null;
486 if (logger.isDebugEnabled()) {
487 logger.debug("------------------------------------------------------------------------------");
488 logger.debug("Client to server roundtrip called.");
489 logger.debug("------------------------------------------------------------------------------");
492 result = Response.status(HttpResponseCodes.SC_OK).build();
499 @Produces("application/xml")
500 public Response createPictureDocument() {
501 Response result = null;
503 if (logger.isDebugEnabled()) {
504 logger.debug("------------------------------------------------------------------------------");
505 logger.debug("Prototype to create a Picture document in Nuxeo");
506 logger.debug("------------------------------------------------------------------------------");
510 NuxeoImageUtils.createPicture();
511 result = Response.status(HttpResponseCodes.SC_OK).build();
517 * This method is deprecated. Use kwSearchCollectionObjects() method instead.
518 * Keywords search collection objects.
521 * @param keywords the keywords
523 * @return the collectionobjects common list
527 @Produces("application/xml")
528 public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui,
529 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
530 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
531 return searchCollectionObjects(queryParams, keywords);
535 * Search collection objects.
537 * @param keywords the keywords
539 * @return the collectionobjects common list
541 private CollectionobjectsCommonList searchCollectionObjects(
542 MultivaluedMap<String, String> queryParams,
544 CollectionobjectsCommonList collectionObjectList;
546 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
547 DocumentHandler handler = createDocumentHandler(ctx);
549 // perform a keyword search
550 if (keywords != null && !keywords.isEmpty()) {
551 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
552 DocumentFilter documentFilter = handler.getDocumentFilter();
553 documentFilter.setWhereClause(whereClause);
554 if (logger.isDebugEnabled()) {
555 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
558 getRepositoryClient(ctx).getFiltered(ctx, handler);
559 collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList();
560 } catch (UnauthorizedException ue) {
561 Response response = Response.status(
562 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
563 throw new WebApplicationException(response);
564 } catch (Exception e) {
565 if (logger.isDebugEnabled()) {
566 logger.debug("Caught exception in getCollectionObjectList", e);
568 Response response = Response.status(
569 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
570 throw new WebApplicationException(response);
572 return collectionObjectList;