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.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
44 import org.collectionspace.services.common.query.IQueryManager;
45 import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;
46 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.security.UnauthorizedException;
52 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
53 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
54 import org.jboss.resteasy.util.HttpResponseCodes;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * The Class NewRelationResource.
63 @Consumes("multipart/mixed")
64 @Produces("multipart/mixed")
65 public class NewRelationResource extends
66 AbstractMultiPartCollectionSpaceResourceImpl {
68 /** The Constant serviceName. */
69 public final static String serviceName = "relations";
72 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
75 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
78 protected String getVersionString() {
79 /** The last change revision. */
80 final String lastChangeRevision = "$LastChangedRevision$";
81 return lastChangeRevision;
85 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
88 public String getServiceName() {
93 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
96 public Class<RelationsCommon> getCommonPartClass() {
97 return RelationsCommon.class;
101 * Creates the relation.
103 * @param input the input
105 * @return the response
108 public Response createRelation(MultipartInput input) {
110 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
111 DocumentHandler handler = createDocumentHandler(ctx);
112 String csid = getRepositoryClient(ctx).create(ctx, handler);
113 UriBuilder path = UriBuilder
114 .fromResource(NewRelationResource.class);
115 path.path("" + csid);
116 Response response = Response.created(path.build()).build();
118 } catch (UnauthorizedException ue) {
119 Response response = Response.status(Response.Status.UNAUTHORIZED)
120 .entity("Create failed reason " + ue.getErrorReason())
121 .type("text/plain").build();
122 throw new WebApplicationException(response);
123 } catch (Exception e) {
124 if (logger.isDebugEnabled()) {
125 logger.debug("Caught exception in createRelation", e);
127 Response response = Response.status(
128 Response.Status.INTERNAL_SERVER_ERROR).entity(
129 "Create failed").type("text/plain").build();
130 throw new WebApplicationException(response);
138 * @param csid the csid
139 * @return the relation
143 public MultipartOutput getRelation(@Context UriInfo ui,
144 @PathParam("csid") String csid) {
145 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
146 if (logger.isDebugEnabled()) {
147 logger.debug("getRelation with csid=" + csid);
149 if (csid == null || "".equals(csid)) {
150 logger.error("getRelation: missing csid!");
151 Response response = Response.status(Response.Status.BAD_REQUEST)
152 .entity("get failed on Relation csid=" + csid).type(
153 "text/plain").build();
154 throw new WebApplicationException(response);
156 MultipartOutput result = null;
158 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
159 DocumentHandler handler = createDocumentHandler(ctx);
160 getRepositoryClient(ctx).get(ctx, csid, handler);
161 result = (MultipartOutput) ctx.getOutput();
162 } catch (UnauthorizedException ue) {
163 Response response = Response.status(Response.Status.UNAUTHORIZED)
164 .entity("Get failed reason " + ue.getErrorReason()).type(
165 "text/plain").build();
166 throw new WebApplicationException(response);
167 } catch (DocumentNotFoundException dnfe) {
168 if (logger.isDebugEnabled()) {
169 logger.debug("getRelation", dnfe);
171 Response response = Response.status(Response.Status.NOT_FOUND)
172 .entity("Get failed on Relation csid=" + csid).type(
173 "text/plain").build();
174 throw new WebApplicationException(response);
175 } catch (Exception e) {
176 if (logger.isDebugEnabled()) {
177 logger.debug("getRelation", e);
179 Response response = Response.status(
180 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
181 .type("text/plain").build();
182 throw new WebApplicationException(response);
185 if (result == null) {
186 Response response = Response.status(Response.Status.NOT_FOUND)
188 "Get failed, the requested Relation CSID:" + csid
189 + ": was not found.").type("text/plain")
191 throw new WebApplicationException(response);
200 * Gets the relation list.
204 * @return the relation list
207 @Produces("application/xml")
208 public RelationsCommonList getRelationList(@Context UriInfo ui) {
209 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
210 return this.getRelationList(queryParams, null, null, null);
214 * Gets the relation list_ s.
217 * @param subjectCsid the subject csid
219 * @return the relation list_ s
222 @Path("subject/{subjectCsid}")
223 @Produces("application/xml")
224 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
225 @PathParam("subjectCsid") String subjectCsid) {
226 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
227 return this.getRelationList(queryParams, subjectCsid, null, null);
231 * Gets the relation list_ p.
234 * @param predicate the predicate
236 * @return the relation list_ p
239 @Path("type/{predicate}")
240 @Produces("application/xml")
241 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
242 @PathParam("predicate") String predicate) {
243 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
244 return this.getRelationList(queryParams, null, predicate, null);
248 * Gets the relation list_ o.
251 * @param objectCsid the object csid
253 * @return the relation list_ o
256 @Path("object/{objectCsid}")
257 @Produces("application/xml")
258 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
259 @PathParam("objectCsid") String objectCsid) {
260 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
261 return this.getRelationList(queryParams, null, null, objectCsid);
265 * Gets the relation list_ ps.
268 * @param predicate the predicate
269 * @param subjectCsid the subject csid
271 * @return the relation list_ ps
274 @Path("type/{predicate}/subject/{subjectCsid}")
275 @Produces("application/xml")
276 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
277 @PathParam("predicate") String predicate,
278 @PathParam("subjectCsid") String subjectCsid) {
279 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
280 return this.getRelationList(queryParams, subjectCsid, predicate, null);
284 * Gets the relation list_ sp.
287 * @param subjectCsid the subject csid
288 * @param predicate the predicate
290 * @return the relation list_ sp
293 @Path("subject/{subjectCsid}/type/{predicate}")
294 @Produces("application/xml")
295 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
296 @PathParam("subjectCsid") String subjectCsid,
297 @PathParam("predicate") String predicate) {
298 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
299 return this.getRelationList(queryParams, subjectCsid, predicate, null);
303 * Gets the relation list_ po.
306 * @param predicate the predicate
307 * @param objectCsid the object csid
309 * @return the relation list_ po
312 @Path("type/{predicate}/object/{objectCsid}")
313 @Produces("application/xml")
314 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
315 @PathParam("predicate") String predicate,
316 @PathParam("objectCsid") String objectCsid) {
317 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
318 return this.getRelationList(queryParams, null, predicate, objectCsid);
322 * Gets the relation list_ op.
325 * @param objectCsid the object csid
326 * @param predicate the predicate
328 * @return the relation list_ op
331 @Path("object/{objectCsid}/type/{predicate}")
332 @Produces("application/xml")
333 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
334 @PathParam("objectCsid") String objectCsid,
335 @PathParam("predicate") String predicate) {
336 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
337 return this.getRelationList(queryParams, null, predicate, objectCsid);
341 * Gets the relation list_ pso.
344 * @param predicate the predicate
345 * @param subjectCsid the subject csid
346 * @param objectCsid the object csid
348 * @return the relation list_ pso
351 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
352 @Produces("application/xml")
353 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
354 @PathParam("predicate") String predicate,
355 @PathParam("subjectCsid") String subjectCsid,
356 @PathParam("objectCsid") String objectCsid) {
357 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
358 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
362 * Gets the relation list_ spo.
365 * @param subjectCsid the subject csid
366 * @param predicate the predicate
367 * @param objectCsid the object csid
369 * @return the relation list_ spo
372 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
373 @Produces("application/xml")
374 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
375 @PathParam("subjectCsid") String subjectCsid,
376 @PathParam("predicate") String predicate,
377 @PathParam("objectCsid") String objectCsid) {
378 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
379 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
389 * @param csid the csid
390 * @param theUpdate the the update
392 * @return the multipart output
396 public MultipartOutput updateRelation(@PathParam("csid") String csid,
397 MultipartInput theUpdate) {
398 if (logger.isDebugEnabled()) {
399 logger.debug("updateRelation with csid=" + csid);
401 if (csid == null || "".equals(csid)) {
402 logger.error("updateRelation: missing csid!");
403 Response response = Response.status(Response.Status.BAD_REQUEST)
404 .entity("update failed on Relation csid=" + csid).type(
405 "text/plain").build();
406 throw new WebApplicationException(response);
408 MultipartOutput result = null;
410 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
411 DocumentHandler handler = createDocumentHandler(ctx);
412 getRepositoryClient(ctx).update(ctx, csid, handler);
413 result = (MultipartOutput) ctx.getOutput();
414 } catch (UnauthorizedException ue) {
415 Response response = Response.status(Response.Status.UNAUTHORIZED)
416 .entity("Update failed reason " + ue.getErrorReason())
417 .type("text/plain").build();
418 throw new WebApplicationException(response);
419 } catch (DocumentNotFoundException dnfe) {
420 if (logger.isDebugEnabled()) {
421 logger.debug("caugth exception in updateRelation", dnfe);
423 Response response = Response.status(Response.Status.NOT_FOUND)
424 .entity("Update failed on Relation csid=" + csid).type(
425 "text/plain").build();
426 throw new WebApplicationException(response);
427 } catch (Exception e) {
428 Response response = Response.status(
429 Response.Status.INTERNAL_SERVER_ERROR).entity(
430 "Update failed").type("text/plain").build();
431 throw new WebApplicationException(response);
439 * @param csid the csid
441 * @return the response
445 public Response deleteRelation(@PathParam("csid") String csid) {
447 if (logger.isDebugEnabled()) {
448 logger.debug("deleteRelation with csid=" + csid);
450 if (csid == null || "".equals(csid)) {
451 logger.error("deleteRelation: missing csid!");
452 Response response = Response.status(Response.Status.BAD_REQUEST)
453 .entity("delete failed on Relation csid=" + csid).type(
454 "text/plain").build();
455 throw new WebApplicationException(response);
458 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
459 getRepositoryClient(ctx).delete(ctx, csid);
460 return Response.status(HttpResponseCodes.SC_OK).build();
461 } catch (UnauthorizedException ue) {
462 Response response = Response.status(Response.Status.UNAUTHORIZED)
463 .entity("Delete failed reason " + ue.getErrorReason())
464 .type("text/plain").build();
465 throw new WebApplicationException(response);
466 } catch (DocumentNotFoundException dnfe) {
467 if (logger.isDebugEnabled()) {
468 logger.debug("caught exception in deleteRelation", dnfe);
470 Response response = Response.status(Response.Status.NOT_FOUND)
471 .entity("Delete failed on Relation csid=" + csid).type(
472 "text/plain").build();
473 throw new WebApplicationException(response);
474 } catch (Exception e) {
475 Response response = Response.status(
476 Response.Status.INTERNAL_SERVER_ERROR).entity(
477 "Delete failed").type("text/plain").build();
478 throw new WebApplicationException(response);
484 * Gets the relation list.
486 * @param queryParams the query params
487 * @param subjectCsid the subject csid
488 * @param predicate the predicate
489 * @param objectCsid the object csid
490 * @return the relation list
491 * @throws WebApplicationException the web application exception
493 public RelationsCommonList getRelationList(
494 MultivaluedMap<String, String> queryParams,
497 String objectCsid) throws WebApplicationException {
498 RelationsCommonList relationList = new RelationsCommonList();
500 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
501 DocumentHandler handler = createDocumentHandler(ctx);
502 String relationClause = RelationsUtils.buildWhereClause(subjectCsid, predicate, objectCsid);
503 handler.getDocumentFilter().appendWhereClause(relationClause, IQueryManager.SEARCH_QUALIFIER_AND);
504 getRepositoryClient(ctx).getFiltered(ctx, handler);
505 relationList = (RelationsCommonList)handler.getCommonPartList();
506 } catch (UnauthorizedException ue) {
507 Response response = Response.status(Response.Status.UNAUTHORIZED).entity(
508 "Get relations failed reason " +
509 ue.getErrorReason()).type("text/plain").build();
510 throw new WebApplicationException(response);
511 } catch (Exception e) {
512 if (logger.isDebugEnabled()) {
513 logger.debug("Caught exception in getRelationList", e);
515 Response response = Response.status(
516 Response.Status.INTERNAL_SERVER_ERROR).entity(
517 "Index failed").type("text/plain").build();
518 throw new WebApplicationException(response);