2 * NewRelationResource.java
4 * {Purpose of This Class}
6 * {Other Notes Relating to This Class (Optional)}
9 * $LastChangedRevision$
12 * This document is a part of the source code and related artifacts
13 * for CollectionSpace, an open source collections management system
14 * for museums and related institutions:
16 * http://www.collectionspace.org
17 * http://wiki.collectionspace.org
19 * Copyright � 2009 {Contributing Institution}
21 * Licensed under the Educational Community License (ECL), Version 2.0.
22 * You may not use this file except in compliance with this License.
24 * You may obtain a copy of the ECL 2.0 License at
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
27 package org.collectionspace.services.relation;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriBuilder;
43 import javax.ws.rs.core.UriInfo;
45 import org.collectionspace.services.common.query.IQueryManager;
46 import org.collectionspace.services.common.relation.IRelationsManager;
47 import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;
48 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
56 import org.jboss.resteasy.util.HttpResponseCodes;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * The Class NewRelationResource.
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class NewRelationResource extends
68 AbstractMultiPartCollectionSpaceResourceImpl {
70 /** The Constant serviceName. */
71 public final static String serviceName = "relations";
74 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
77 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
80 protected String getVersionString() {
81 /** The last change revision. */
82 final String lastChangeRevision = "$LastChangedRevision$";
83 return lastChangeRevision;
87 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
90 public String getServiceName() {
95 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
98 public Class<RelationsCommon> getCommonPartClass() {
99 return RelationsCommon.class;
103 * Creates the relation.
105 * @param input the input
107 * @return the response
110 public Response createRelation(MultipartInput input) {
112 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
113 DocumentHandler handler = createDocumentHandler(ctx);
114 String csid = getRepositoryClient(ctx).create(ctx, handler);
115 UriBuilder path = UriBuilder
116 .fromResource(NewRelationResource.class);
117 path.path("" + csid);
118 Response response = Response.created(path.build()).build();
120 } catch (UnauthorizedException ue) {
121 Response response = Response.status(Response.Status.UNAUTHORIZED)
122 .entity("Create failed reason " + ue.getErrorReason())
123 .type("text/plain").build();
124 throw new WebApplicationException(response);
125 } catch (Exception e) {
126 if (logger.isDebugEnabled()) {
127 logger.debug("Caught exception in createRelation", e);
129 Response response = Response.status(
130 Response.Status.INTERNAL_SERVER_ERROR).entity(
131 "Create failed").type("text/plain").build();
132 throw new WebApplicationException(response);
140 * @param csid the csid
141 * @return the relation
145 public MultipartOutput getRelation(@Context UriInfo ui,
146 @PathParam("csid") String csid) {
147 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
148 if (logger.isDebugEnabled()) {
149 logger.debug("getRelation with csid=" + csid);
151 if (csid == null || "".equals(csid)) {
152 logger.error("getRelation: missing csid!");
153 Response response = Response.status(Response.Status.BAD_REQUEST)
154 .entity("get failed on Relation csid=" + csid).type(
155 "text/plain").build();
156 throw new WebApplicationException(response);
158 MultipartOutput result = null;
160 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
161 DocumentHandler handler = createDocumentHandler(ctx);
162 getRepositoryClient(ctx).get(ctx, csid, handler);
163 result = (MultipartOutput) ctx.getOutput();
164 } catch (UnauthorizedException ue) {
165 Response response = Response.status(Response.Status.UNAUTHORIZED)
166 .entity("Get failed reason " + ue.getErrorReason()).type(
167 "text/plain").build();
168 throw new WebApplicationException(response);
169 } catch (DocumentNotFoundException dnfe) {
170 if (logger.isDebugEnabled()) {
171 logger.debug("getRelation", dnfe);
173 Response response = Response.status(Response.Status.NOT_FOUND)
174 .entity("Get failed on Relation csid=" + csid).type(
175 "text/plain").build();
176 throw new WebApplicationException(response);
177 } catch (Exception e) {
178 if (logger.isDebugEnabled()) {
179 logger.debug("getRelation", e);
181 Response response = Response.status(
182 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
183 .type("text/plain").build();
184 throw new WebApplicationException(response);
187 if (result == null) {
188 Response response = Response.status(Response.Status.NOT_FOUND)
190 "Get failed, the requested Relation CSID:" + csid
191 + ": was not found.").type("text/plain")
193 throw new WebApplicationException(response);
202 * Gets the relation list.
209 * @return the relation list
212 @Produces("application/xml")
213 public RelationsCommonList getRelationList(@Context UriInfo ui,
214 @QueryParam(IRelationsManager.SUBJECT_QP) String subjectCsid,
215 @QueryParam(IRelationsManager.PREDICATE_QP) String predicate,
216 @QueryParam(IRelationsManager.OBJECT_QP) String objectCsid) {
217 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
218 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
222 * Gets the relation list_ s.
225 * @param subjectCsid the subject csid
227 * @return the relation list_ s
230 @Path("subject/{subjectCsid}")
231 @Produces("application/xml")
232 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
233 @PathParam("subjectCsid") String subjectCsid) {
234 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
235 return this.getRelationList(queryParams, subjectCsid, null, null);
239 * Gets the relation list_ p.
242 * @param predicate the predicate
244 * @return the relation list_ p
247 @Path("type/{predicate}")
248 @Produces("application/xml")
249 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
250 @PathParam("predicate") String predicate) {
251 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
252 return this.getRelationList(queryParams, null, predicate, null);
256 * Gets the relation list_ o.
259 * @param objectCsid the object csid
261 * @return the relation list_ o
264 @Path("object/{objectCsid}")
265 @Produces("application/xml")
266 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
267 @PathParam("objectCsid") String objectCsid) {
268 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
269 return this.getRelationList(queryParams, null, null, objectCsid);
273 * Gets the relation list_ ps.
276 * @param predicate the predicate
277 * @param subjectCsid the subject csid
279 * @return the relation list_ ps
282 @Path("type/{predicate}/subject/{subjectCsid}")
283 @Produces("application/xml")
284 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
285 @PathParam("predicate") String predicate,
286 @PathParam("subjectCsid") String subjectCsid) {
287 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
288 return this.getRelationList(queryParams, subjectCsid, predicate, null);
292 * Gets the relation list_ sp.
295 * @param subjectCsid the subject csid
296 * @param predicate the predicate
298 * @return the relation list_ sp
301 @Path("subject/{subjectCsid}/type/{predicate}")
302 @Produces("application/xml")
303 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
304 @PathParam("subjectCsid") String subjectCsid,
305 @PathParam("predicate") String predicate) {
306 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
307 return this.getRelationList(queryParams, subjectCsid, predicate, null);
311 * Gets the relation list_ po.
314 * @param predicate the predicate
315 * @param objectCsid the object csid
317 * @return the relation list_ po
320 @Path("type/{predicate}/object/{objectCsid}")
321 @Produces("application/xml")
322 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
323 @PathParam("predicate") String predicate,
324 @PathParam("objectCsid") String objectCsid) {
325 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
326 return this.getRelationList(queryParams, null, predicate, objectCsid);
330 * Gets the relation list_ op.
333 * @param objectCsid the object csid
334 * @param predicate the predicate
336 * @return the relation list_ op
339 @Path("object/{objectCsid}/type/{predicate}")
340 @Produces("application/xml")
341 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
342 @PathParam("objectCsid") String objectCsid,
343 @PathParam("predicate") String predicate) {
344 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
345 return this.getRelationList(queryParams, null, predicate, objectCsid);
349 * Gets the relation list_ pso.
352 * @param predicate the predicate
353 * @param subjectCsid the subject csid
354 * @param objectCsid the object csid
356 * @return the relation list_ pso
359 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
360 @Produces("application/xml")
361 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
362 @PathParam("predicate") String predicate,
363 @PathParam("subjectCsid") String subjectCsid,
364 @PathParam("objectCsid") String objectCsid) {
365 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
366 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
370 * Gets the relation list_ spo.
373 * @param subjectCsid the subject csid
374 * @param predicate the predicate
375 * @param objectCsid the object csid
377 * @return the relation list_ spo
380 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
381 @Produces("application/xml")
382 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
383 @PathParam("subjectCsid") String subjectCsid,
384 @PathParam("predicate") String predicate,
385 @PathParam("objectCsid") String objectCsid) {
386 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
387 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
397 * @param csid the csid
398 * @param theUpdate the the update
400 * @return the multipart output
404 public MultipartOutput updateRelation(@PathParam("csid") String csid,
405 MultipartInput theUpdate) {
406 if (logger.isDebugEnabled()) {
407 logger.debug("updateRelation with csid=" + csid);
409 if (csid == null || "".equals(csid)) {
410 logger.error("updateRelation: missing csid!");
411 Response response = Response.status(Response.Status.BAD_REQUEST)
412 .entity("update failed on Relation csid=" + csid).type(
413 "text/plain").build();
414 throw new WebApplicationException(response);
416 MultipartOutput result = null;
418 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
419 DocumentHandler handler = createDocumentHandler(ctx);
420 getRepositoryClient(ctx).update(ctx, csid, handler);
421 result = (MultipartOutput) ctx.getOutput();
422 } catch (UnauthorizedException ue) {
423 Response response = Response.status(Response.Status.UNAUTHORIZED)
424 .entity("Update failed reason " + ue.getErrorReason())
425 .type("text/plain").build();
426 throw new WebApplicationException(response);
427 } catch (DocumentNotFoundException dnfe) {
428 if (logger.isDebugEnabled()) {
429 logger.debug("caugth exception in updateRelation", dnfe);
431 Response response = Response.status(Response.Status.NOT_FOUND)
432 .entity("Update failed on Relation csid=" + csid).type(
433 "text/plain").build();
434 throw new WebApplicationException(response);
435 } catch (Exception e) {
436 Response response = Response.status(
437 Response.Status.INTERNAL_SERVER_ERROR).entity(
438 "Update failed").type("text/plain").build();
439 throw new WebApplicationException(response);
447 * @param csid the csid
449 * @return the response
453 public Response deleteRelation(@PathParam("csid") String csid) {
455 if (logger.isDebugEnabled()) {
456 logger.debug("deleteRelation with csid=" + csid);
458 if (csid == null || "".equals(csid)) {
459 logger.error("deleteRelation: missing csid!");
460 Response response = Response.status(Response.Status.BAD_REQUEST)
461 .entity("delete failed on Relation csid=" + csid).type(
462 "text/plain").build();
463 throw new WebApplicationException(response);
466 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
467 getRepositoryClient(ctx).delete(ctx, csid);
468 return Response.status(HttpResponseCodes.SC_OK).build();
469 } catch (UnauthorizedException ue) {
470 Response response = Response.status(Response.Status.UNAUTHORIZED)
471 .entity("Delete failed reason " + ue.getErrorReason())
472 .type("text/plain").build();
473 throw new WebApplicationException(response);
474 } catch (DocumentNotFoundException dnfe) {
475 if (logger.isDebugEnabled()) {
476 logger.debug("caught exception in deleteRelation", dnfe);
478 Response response = Response.status(Response.Status.NOT_FOUND)
479 .entity("Delete failed on Relation csid=" + csid).type(
480 "text/plain").build();
481 throw new WebApplicationException(response);
482 } catch (Exception e) {
483 Response response = Response.status(
484 Response.Status.INTERNAL_SERVER_ERROR).entity(
485 "Delete failed").type("text/plain").build();
486 throw new WebApplicationException(response);
492 * Gets the relation list.
494 * @param queryParams the query params
495 * @param subjectCsid the subject csid
496 * @param predicate the predicate
497 * @param objectCsid the object csid
498 * @return the relation list
499 * @throws WebApplicationException the web application exception
501 public RelationsCommonList getRelationList(
502 MultivaluedMap<String, String> queryParams,
505 String objectCsid) throws WebApplicationException {
506 RelationsCommonList relationList = new RelationsCommonList();
508 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
509 DocumentHandler handler = createDocumentHandler(ctx);
510 String relationClause = RelationsUtils.buildWhereClause(subjectCsid, predicate, objectCsid);
511 handler.getDocumentFilter().appendWhereClause(relationClause, IQueryManager.SEARCH_QUALIFIER_AND);
512 getRepositoryClient(ctx).getFiltered(ctx, handler);
513 relationList = (RelationsCommonList)handler.getCommonPartList();
514 } catch (UnauthorizedException ue) {
515 Response response = Response.status(Response.Status.UNAUTHORIZED).entity(
516 "Get relations failed reason " +
517 ue.getErrorReason()).type("text/plain").build();
518 throw new WebApplicationException(response);
519 } catch (Exception e) {
520 if (logger.isDebugEnabled()) {
521 logger.debug("Caught exception in getRelationList", e);
523 Response response = Response.status(
524 Response.Status.INTERNAL_SERVER_ERROR).entity(
525 "Index failed").type("text/plain").build();
526 throw new WebApplicationException(response);