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;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.DELETE;
36 import javax.ws.rs.POST;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.WebApplicationException;
40 import javax.ws.rs.core.Context;
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.AbstractMultiPartCollectionSpaceResourceImpl;
46 //import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.relation.IRelationsManager;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.security.UnauthorizedException;
53 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
55 import org.jboss.resteasy.util.HttpResponseCodes;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * The Class NewRelationResource.
64 @Consumes("multipart/mixed")
65 @Produces("multipart/mixed")
66 public class NewRelationResource extends
67 AbstractMultiPartCollectionSpaceResourceImpl {
69 /** The Constant serviceName. */
70 public final static String serviceName = "relations";
73 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
76 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
79 protected String getVersionString() {
80 /** The last change revision. */
81 final String lastChangeRevision = "$LastChangedRevision$";
82 return lastChangeRevision;
86 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
89 public String getServiceName() {
94 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
97 public Class<RelationsCommon> getCommonPartClass() {
98 return RelationsCommon.class;
102 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
105 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx)
106 // throws Exception {
107 // DocumentHandler docHandler = ctx.getDocumentHandler();
108 // if (ctx.getInput() != null) {
109 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx
110 // .getCommonPartLabel(), RelationsCommon.class);
111 // if (obj != null) {
112 // docHandler.setCommonPart((RelationsCommon) obj);
115 // return docHandler;
119 * Creates the relation.
121 * @param input the input
123 * @return the response
126 public Response createRelation(MultipartInput input) {
128 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
129 DocumentHandler handler = createDocumentHandler(ctx);
130 String csid = getRepositoryClient(ctx).create(ctx, handler);
131 UriBuilder path = UriBuilder
132 .fromResource(NewRelationResource.class);
133 path.path("" + csid);
134 Response response = Response.created(path.build()).build();
136 } catch (UnauthorizedException ue) {
137 Response response = Response.status(Response.Status.UNAUTHORIZED)
138 .entity("Create failed reason " + ue.getErrorReason())
139 .type("text/plain").build();
140 throw new WebApplicationException(response);
141 } catch (Exception e) {
142 if (logger.isDebugEnabled()) {
143 logger.debug("Caught exception in createRelation", e);
145 Response response = Response.status(
146 Response.Status.INTERNAL_SERVER_ERROR).entity(
147 "Create failed").type("text/plain").build();
148 throw new WebApplicationException(response);
155 * @param csid the csid
157 * @return the relation
161 public MultipartOutput getRelation(@PathParam("csid") String csid) {
162 if (logger.isDebugEnabled()) {
163 logger.debug("getRelation with csid=" + csid);
165 if (csid == null || "".equals(csid)) {
166 logger.error("getRelation: missing csid!");
167 Response response = Response.status(Response.Status.BAD_REQUEST)
168 .entity("get failed on Relation csid=" + csid).type(
169 "text/plain").build();
170 throw new WebApplicationException(response);
172 MultipartOutput result = null;
174 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
175 DocumentHandler handler = createDocumentHandler(ctx);
176 getRepositoryClient(ctx).get(ctx, csid, handler);
177 result = (MultipartOutput) ctx.getOutput();
178 } catch (UnauthorizedException ue) {
179 Response response = Response.status(Response.Status.UNAUTHORIZED)
180 .entity("Get failed reason " + ue.getErrorReason()).type(
181 "text/plain").build();
182 throw new WebApplicationException(response);
183 } catch (DocumentNotFoundException dnfe) {
184 if (logger.isDebugEnabled()) {
185 logger.debug("getRelation", dnfe);
187 Response response = Response.status(Response.Status.NOT_FOUND)
188 .entity("Get failed on Relation csid=" + csid).type(
189 "text/plain").build();
190 throw new WebApplicationException(response);
191 } catch (Exception e) {
192 if (logger.isDebugEnabled()) {
193 logger.debug("getRelation", e);
195 Response response = Response.status(
196 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
197 .type("text/plain").build();
198 throw new WebApplicationException(response);
201 if (result == null) {
202 Response response = Response.status(Response.Status.NOT_FOUND)
204 "Get failed, the requested Relation CSID:" + csid
205 + ": was not found.").type("text/plain")
207 throw new WebApplicationException(response);
216 * Gets the relation list.
220 * @return the relation list
223 @Produces("application/xml")
224 public RelationsCommonList getRelationList(@Context UriInfo ui) {
225 return this.getRelationList(null, null, null);
229 * Gets the relation list_ s.
232 * @param subjectCsid the subject csid
234 * @return the relation list_ s
237 @Path("subject/{subjectCsid}")
238 @Produces("application/xml")
239 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
240 @PathParam("subjectCsid") String subjectCsid) {
241 return this.getRelationList(subjectCsid, null, null);
245 * Gets the relation list_ p.
248 * @param predicate the predicate
250 * @return the relation list_ p
253 @Path("type/{predicate}")
254 @Produces("application/xml")
255 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
256 @PathParam("predicate") String predicate) {
257 return this.getRelationList(null, predicate, null);
261 * Gets the relation list_ o.
264 * @param objectCsid the object csid
266 * @return the relation list_ o
269 @Path("object/{objectCsid}")
270 @Produces("application/xml")
271 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
272 @PathParam("objectCsid") String objectCsid) {
273 return this.getRelationList(null, null, objectCsid);
277 * Gets the relation list_ ps.
280 * @param predicate the predicate
281 * @param subjectCsid the subject csid
283 * @return the relation list_ ps
286 @Path("type/{predicate}/subject/{subjectCsid}")
287 @Produces("application/xml")
288 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
289 @PathParam("predicate") String predicate,
290 @PathParam("subjectCsid") String subjectCsid) {
291 return this.getRelationList(subjectCsid, predicate, null);
295 * Gets the relation list_ sp.
298 * @param subjectCsid the subject csid
299 * @param predicate the predicate
301 * @return the relation list_ sp
304 @Path("subject/{subjectCsid}/type/{predicate}")
305 @Produces("application/xml")
306 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
307 @PathParam("subjectCsid") String subjectCsid,
308 @PathParam("predicate") String predicate) {
309 return this.getRelationList(subjectCsid, predicate, null);
313 * Gets the relation list_ po.
316 * @param predicate the predicate
317 * @param objectCsid the object csid
319 * @return the relation list_ po
322 @Path("type/{predicate}/object/{objectCsid}")
323 @Produces("application/xml")
324 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
325 @PathParam("predicate") String predicate,
326 @PathParam("objectCsid") String objectCsid) {
327 return this.getRelationList(null, predicate, objectCsid);
331 * Gets the relation list_ op.
334 * @param objectCsid the object csid
335 * @param predicate the predicate
337 * @return the relation list_ op
340 @Path("object/{objectCsid}/type/{predicate}")
341 @Produces("application/xml")
342 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
343 @PathParam("objectCsid") String objectCsid,
344 @PathParam("predicate") String predicate) {
345 return this.getRelationList(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 return this.getRelationList(subjectCsid, predicate, objectCsid);
369 * Gets the relation list_ spo.
372 * @param subjectCsid the subject csid
373 * @param predicate the predicate
374 * @param objectCsid the object csid
376 * @return the relation list_ spo
379 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
380 @Produces("application/xml")
381 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
382 @PathParam("subjectCsid") String subjectCsid,
383 @PathParam("predicate") String predicate,
384 @PathParam("objectCsid") String objectCsid) {
385 return this.getRelationList(subjectCsid, predicate, objectCsid);
395 * @param csid the csid
396 * @param theUpdate the the update
398 * @return the multipart output
402 public MultipartOutput updateRelation(@PathParam("csid") String csid,
403 MultipartInput theUpdate) {
404 if (logger.isDebugEnabled()) {
405 logger.debug("updateRelation with csid=" + csid);
407 if (csid == null || "".equals(csid)) {
408 logger.error("updateRelation: missing csid!");
409 Response response = Response.status(Response.Status.BAD_REQUEST)
410 .entity("update failed on Relation csid=" + csid).type(
411 "text/plain").build();
412 throw new WebApplicationException(response);
414 MultipartOutput result = null;
416 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
417 DocumentHandler handler = createDocumentHandler(ctx);
418 getRepositoryClient(ctx).update(ctx, csid, handler);
419 result = (MultipartOutput) ctx.getOutput();
420 } catch (UnauthorizedException ue) {
421 Response response = Response.status(Response.Status.UNAUTHORIZED)
422 .entity("Update failed reason " + ue.getErrorReason())
423 .type("text/plain").build();
424 throw new WebApplicationException(response);
425 } catch (DocumentNotFoundException dnfe) {
426 if (logger.isDebugEnabled()) {
427 logger.debug("caugth exception in updateRelation", dnfe);
429 Response response = Response.status(Response.Status.NOT_FOUND)
430 .entity("Update failed on Relation csid=" + csid).type(
431 "text/plain").build();
432 throw new WebApplicationException(response);
433 } catch (Exception e) {
434 Response response = Response.status(
435 Response.Status.INTERNAL_SERVER_ERROR).entity(
436 "Update failed").type("text/plain").build();
437 throw new WebApplicationException(response);
445 * @param csid the csid
447 * @return the response
451 public Response deleteRelation(@PathParam("csid") String csid) {
453 if (logger.isDebugEnabled()) {
454 logger.debug("deleteRelation with csid=" + csid);
456 if (csid == null || "".equals(csid)) {
457 logger.error("deleteRelation: missing csid!");
458 Response response = Response.status(Response.Status.BAD_REQUEST)
459 .entity("delete failed on Relation csid=" + csid).type(
460 "text/plain").build();
461 throw new WebApplicationException(response);
464 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
465 getRepositoryClient(ctx).delete(ctx, csid);
466 return Response.status(HttpResponseCodes.SC_OK).build();
467 } catch (UnauthorizedException ue) {
468 Response response = Response.status(Response.Status.UNAUTHORIZED)
469 .entity("Delete failed reason " + ue.getErrorReason())
470 .type("text/plain").build();
471 throw new WebApplicationException(response);
472 } catch (DocumentNotFoundException dnfe) {
473 if (logger.isDebugEnabled()) {
474 logger.debug("caught exception in deleteRelation", dnfe);
476 Response response = Response.status(Response.Status.NOT_FOUND)
477 .entity("Delete failed on Relation csid=" + csid).type(
478 "text/plain").build();
479 throw new WebApplicationException(response);
480 } catch (Exception e) {
481 Response response = Response.status(
482 Response.Status.INTERNAL_SERVER_ERROR).entity(
483 "Delete failed").type("text/plain").build();
484 throw new WebApplicationException(response);
490 * Gets the relation list request.
492 * @return the relation list request
494 * @throws WebApplicationException
495 * the web application exception
497 public RelationsCommonList getRelationList(String subjectCsid,
498 String predicate, String objectCsid) throws WebApplicationException {
499 RelationsCommonList relationList = new RelationsCommonList();
501 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
502 DocumentHandler handler = createDocumentHandler(ctx);
503 Map<String, Object> propsFromPath = handler.getProperties();
504 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
505 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
506 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
507 getRepositoryClient(ctx).getAll(ctx, handler);
508 relationList = (RelationsCommonList) handler.getCommonPartList();
509 } catch (UnauthorizedException ue) {
510 Response response = Response.status(Response.Status.UNAUTHORIZED)
512 "Get relations failed reason "
513 + ue.getErrorReason()).type("text/plain")
515 throw new WebApplicationException(response);
516 } catch (Exception e) {
517 if (logger.isDebugEnabled()) {
518 logger.debug("Caught exception in getRelationList", e);
520 Response response = Response.status(
521 Response.Status.INTERNAL_SERVER_ERROR).entity(
522 "Index failed").type("text/plain").build();
523 throw new WebApplicationException(response);