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.AbstractCollectionSpaceResource;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.relation.IRelationsManager;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 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;
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 AbstractCollectionSpaceResource {
67 /** The Constant serviceName. */
68 public final static String serviceName = "relations";
71 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
74 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
77 protected String getVersionString() {
78 /** The last change revision. */
79 final String lastChangeRevision = "$LastChangedRevision$";
80 return lastChangeRevision;
84 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
87 public String getServiceName() {
92 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
95 public DocumentHandler createDocumentHandler(ServiceContext ctx)
97 DocumentHandler docHandler = ctx.getDocumentHandler();
98 if (ctx.getInput() != null) {
99 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx
100 .getCommonPartLabel(), RelationsCommon.class);
102 docHandler.setCommonPart((RelationsCommon) obj);
109 * Creates the relation.
111 * @param input the input
113 * @return the response
116 public Response createRelation(MultipartInput input) {
119 ServiceContext ctx = MultipartServiceContextFactory.get()
120 .createServiceContext(input, getServiceName());
121 DocumentHandler handler = createDocumentHandler(ctx);
122 String csid = getRepositoryClient(ctx).create(ctx, handler);
123 UriBuilder path = UriBuilder
124 .fromResource(NewRelationResource.class);
125 path.path("" + csid);
126 Response response = Response.created(path.build()).build();
128 } catch (UnauthorizedException ue) {
129 Response response = Response.status(Response.Status.UNAUTHORIZED)
130 .entity("Create failed reason " + ue.getErrorReason())
131 .type("text/plain").build();
132 throw new WebApplicationException(response);
133 } catch (Exception e) {
134 if (logger.isDebugEnabled()) {
135 logger.debug("Caught exception in createRelation", e);
137 Response response = Response.status(
138 Response.Status.INTERNAL_SERVER_ERROR).entity(
139 "Create failed").type("text/plain").build();
140 throw new WebApplicationException(response);
147 * @param csid the csid
149 * @return the relation
153 public MultipartOutput getRelation(@PathParam("csid") String csid) {
154 if (logger.isDebugEnabled()) {
155 logger.debug("getRelation with csid=" + csid);
157 if (csid == null || "".equals(csid)) {
158 logger.error("getRelation: missing csid!");
159 Response response = Response.status(Response.Status.BAD_REQUEST)
160 .entity("get failed on Relation csid=" + csid).type(
161 "text/plain").build();
162 throw new WebApplicationException(response);
164 MultipartOutput result = null;
166 ServiceContext ctx = MultipartServiceContextFactory.get()
167 .createServiceContext(null, getServiceName());
168 DocumentHandler handler = createDocumentHandler(ctx);
169 getRepositoryClient(ctx).get(ctx, csid, handler);
170 result = (MultipartOutput) ctx.getOutput();
171 } catch (UnauthorizedException ue) {
172 Response response = Response.status(Response.Status.UNAUTHORIZED)
173 .entity("Get failed reason " + ue.getErrorReason()).type(
174 "text/plain").build();
175 throw new WebApplicationException(response);
176 } catch (DocumentNotFoundException dnfe) {
177 if (logger.isDebugEnabled()) {
178 logger.debug("getRelation", dnfe);
180 Response response = Response.status(Response.Status.NOT_FOUND)
181 .entity("Get failed on Relation csid=" + csid).type(
182 "text/plain").build();
183 throw new WebApplicationException(response);
184 } catch (Exception e) {
185 if (logger.isDebugEnabled()) {
186 logger.debug("getRelation", e);
188 Response response = Response.status(
189 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
190 .type("text/plain").build();
191 throw new WebApplicationException(response);
194 if (result == null) {
195 Response response = Response.status(Response.Status.NOT_FOUND)
197 "Get failed, the requested Relation CSID:" + csid
198 + ": was not found.").type("text/plain")
200 throw new WebApplicationException(response);
209 * Gets the relation list.
213 * @return the relation list
216 @Produces("application/xml")
217 public RelationsCommonList getRelationList(@Context UriInfo ui) {
218 return this.getRelationList(null, null, null);
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 return this.getRelationList(subjectCsid, null, null);
238 * Gets the relation list_ p.
241 * @param predicate the predicate
243 * @return the relation list_ p
246 @Path("type/{predicate}")
247 @Produces("application/xml")
248 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
249 @PathParam("predicate") String predicate) {
250 return this.getRelationList(null, predicate, null);
254 * Gets the relation list_ o.
257 * @param objectCsid the object csid
259 * @return the relation list_ o
262 @Path("object/{objectCsid}")
263 @Produces("application/xml")
264 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
265 @PathParam("objectCsid") String objectCsid) {
266 return this.getRelationList(null, null, objectCsid);
270 * Gets the relation list_ ps.
273 * @param predicate the predicate
274 * @param subjectCsid the subject csid
276 * @return the relation list_ ps
279 @Path("type/{predicate}/subject/{subjectCsid}")
280 @Produces("application/xml")
281 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
282 @PathParam("predicate") String predicate,
283 @PathParam("subjectCsid") String subjectCsid) {
284 return this.getRelationList(subjectCsid, predicate, null);
288 * Gets the relation list_ sp.
291 * @param subjectCsid the subject csid
292 * @param predicate the predicate
294 * @return the relation list_ sp
297 @Path("subject/{subjectCsid}/type/{predicate}")
298 @Produces("application/xml")
299 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
300 @PathParam("subjectCsid") String subjectCsid,
301 @PathParam("predicate") String predicate) {
302 return this.getRelationList(subjectCsid, predicate, null);
306 * Gets the relation list_ po.
309 * @param predicate the predicate
310 * @param objectCsid the object csid
312 * @return the relation list_ po
315 @Path("type/{predicate}/object/{objectCsid}")
316 @Produces("application/xml")
317 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
318 @PathParam("predicate") String predicate,
319 @PathParam("objectCsid") String objectCsid) {
320 return this.getRelationList(null, predicate, objectCsid);
324 * Gets the relation list_ op.
327 * @param objectCsid the object csid
328 * @param predicate the predicate
330 * @return the relation list_ op
333 @Path("object/{objectCsid}/type/{predicate}")
334 @Produces("application/xml")
335 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
336 @PathParam("objectCsid") String objectCsid,
337 @PathParam("predicate") String predicate) {
338 return this.getRelationList(null, predicate, objectCsid);
342 * Gets the relation list_ pso.
345 * @param predicate the predicate
346 * @param subjectCsid the subject csid
347 * @param objectCsid the object csid
349 * @return the relation list_ pso
352 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
353 @Produces("application/xml")
354 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
355 @PathParam("predicate") String predicate,
356 @PathParam("subjectCsid") String subjectCsid,
357 @PathParam("objectCsid") String objectCsid) {
358 return this.getRelationList(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 return this.getRelationList(subjectCsid, predicate, objectCsid);
388 * @param csid the csid
389 * @param theUpdate the the update
391 * @return the multipart output
395 public MultipartOutput updateRelation(@PathParam("csid") String csid,
396 MultipartInput theUpdate) {
397 if (logger.isDebugEnabled()) {
398 logger.debug("updateRelation with csid=" + csid);
400 if (csid == null || "".equals(csid)) {
401 logger.error("updateRelation: missing csid!");
402 Response response = Response.status(Response.Status.BAD_REQUEST)
403 .entity("update failed on Relation csid=" + csid).type(
404 "text/plain").build();
405 throw new WebApplicationException(response);
407 MultipartOutput result = null;
409 ServiceContext ctx = MultipartServiceContextFactory.get()
410 .createServiceContext(theUpdate, getServiceName());
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 ctx = MultipartServiceContextFactory.get()
459 .createServiceContext(null, getServiceName());
460 getRepositoryClient(ctx).delete(ctx, csid);
461 return Response.status(HttpResponseCodes.SC_OK).build();
462 } catch (UnauthorizedException ue) {
463 Response response = Response.status(Response.Status.UNAUTHORIZED)
464 .entity("Delete failed reason " + ue.getErrorReason())
465 .type("text/plain").build();
466 throw new WebApplicationException(response);
467 } catch (DocumentNotFoundException dnfe) {
468 if (logger.isDebugEnabled()) {
469 logger.debug("caught exception in deleteRelation", dnfe);
471 Response response = Response.status(Response.Status.NOT_FOUND)
472 .entity("Delete failed on Relation csid=" + csid).type(
473 "text/plain").build();
474 throw new WebApplicationException(response);
475 } catch (Exception e) {
476 Response response = Response.status(
477 Response.Status.INTERNAL_SERVER_ERROR).entity(
478 "Delete failed").type("text/plain").build();
479 throw new WebApplicationException(response);
485 * Gets the relation list request.
487 * @return the relation list request
489 * @throws WebApplicationException
490 * the web application exception
492 public RelationsCommonList getRelationList(String subjectCsid,
493 String predicate, String objectCsid) throws WebApplicationException {
494 RelationsCommonList relationList = new RelationsCommonList();
496 ServiceContext ctx = MultipartServiceContextFactory.get()
497 .createServiceContext(null, getServiceName());
498 DocumentHandler handler = createDocumentHandler(ctx);
499 Map propsFromPath = handler.getProperties();
500 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
501 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
502 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
503 getRepositoryClient(ctx).getAll(ctx, handler);
504 relationList = (RelationsCommonList) handler.getCommonPartList();
505 } catch (UnauthorizedException ue) {
506 Response response = Response.status(Response.Status.UNAUTHORIZED)
508 "Get relations failed reason "
509 + ue.getErrorReason()).type("text/plain")
511 throw new WebApplicationException(response);
512 } catch (Exception e) {
513 if (logger.isDebugEnabled()) {
514 logger.debug("Caught exception in getRelationList", e);
516 Response response = Response.status(
517 Response.Status.INTERNAL_SERVER_ERROR).entity(
518 "Index failed").type("text/plain").build();
519 throw new WebApplicationException(response);