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.MultivaluedMap;
42 import javax.ws.rs.core.Response;
43 import javax.ws.rs.core.UriBuilder;
44 import javax.ws.rs.core.UriInfo;
46 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
47 //import org.collectionspace.services.common.context.MultipartServiceContext;
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;
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 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
106 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx)
107 // throws Exception {
108 // DocumentHandler docHandler = ctx.getDocumentHandler();
109 // if (ctx.getInput() != null) {
110 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx
111 // .getCommonPartLabel(), RelationsCommon.class);
112 // if (obj != null) {
113 // docHandler.setCommonPart((RelationsCommon) obj);
116 // return docHandler;
120 * Creates the relation.
122 * @param input the input
124 * @return the response
127 public Response createRelation(MultipartInput input) {
129 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
130 DocumentHandler handler = createDocumentHandler(ctx);
131 String csid = getRepositoryClient(ctx).create(ctx, handler);
132 UriBuilder path = UriBuilder
133 .fromResource(NewRelationResource.class);
134 path.path("" + csid);
135 Response response = Response.created(path.build()).build();
137 } catch (UnauthorizedException ue) {
138 Response response = Response.status(Response.Status.UNAUTHORIZED)
139 .entity("Create failed reason " + ue.getErrorReason())
140 .type("text/plain").build();
141 throw new WebApplicationException(response);
142 } catch (Exception e) {
143 if (logger.isDebugEnabled()) {
144 logger.debug("Caught exception in createRelation", e);
146 Response response = Response.status(
147 Response.Status.INTERNAL_SERVER_ERROR).entity(
148 "Create failed").type("text/plain").build();
149 throw new WebApplicationException(response);
157 * @param csid the csid
158 * @return the relation
162 public MultipartOutput getRelation(@Context UriInfo ui,
163 @PathParam("csid") String csid) {
164 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
165 if (logger.isDebugEnabled()) {
166 logger.debug("getRelation with csid=" + csid);
168 if (csid == null || "".equals(csid)) {
169 logger.error("getRelation: missing csid!");
170 Response response = Response.status(Response.Status.BAD_REQUEST)
171 .entity("get failed on Relation csid=" + csid).type(
172 "text/plain").build();
173 throw new WebApplicationException(response);
175 MultipartOutput result = null;
177 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
178 DocumentHandler handler = createDocumentHandler(ctx);
179 getRepositoryClient(ctx).get(ctx, csid, handler);
180 result = (MultipartOutput) ctx.getOutput();
181 } catch (UnauthorizedException ue) {
182 Response response = Response.status(Response.Status.UNAUTHORIZED)
183 .entity("Get failed reason " + ue.getErrorReason()).type(
184 "text/plain").build();
185 throw new WebApplicationException(response);
186 } catch (DocumentNotFoundException dnfe) {
187 if (logger.isDebugEnabled()) {
188 logger.debug("getRelation", dnfe);
190 Response response = Response.status(Response.Status.NOT_FOUND)
191 .entity("Get failed on Relation csid=" + csid).type(
192 "text/plain").build();
193 throw new WebApplicationException(response);
194 } catch (Exception e) {
195 if (logger.isDebugEnabled()) {
196 logger.debug("getRelation", e);
198 Response response = Response.status(
199 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
200 .type("text/plain").build();
201 throw new WebApplicationException(response);
204 if (result == null) {
205 Response response = Response.status(Response.Status.NOT_FOUND)
207 "Get failed, the requested Relation CSID:" + csid
208 + ": was not found.").type("text/plain")
210 throw new WebApplicationException(response);
219 * Gets the relation list.
223 * @return the relation list
226 @Produces("application/xml")
227 public RelationsCommonList getRelationList(@Context UriInfo ui) {
228 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
229 return this.getRelationList(queryParams, null, null, null);
233 * Gets the relation list_ s.
236 * @param subjectCsid the subject csid
238 * @return the relation list_ s
241 @Path("subject/{subjectCsid}")
242 @Produces("application/xml")
243 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
244 @PathParam("subjectCsid") String subjectCsid) {
245 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
246 return this.getRelationList(queryParams, subjectCsid, null, null);
250 * Gets the relation list_ p.
253 * @param predicate the predicate
255 * @return the relation list_ p
258 @Path("type/{predicate}")
259 @Produces("application/xml")
260 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
261 @PathParam("predicate") String predicate) {
262 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
263 return this.getRelationList(queryParams, null, predicate, null);
267 * Gets the relation list_ o.
270 * @param objectCsid the object csid
272 * @return the relation list_ o
275 @Path("object/{objectCsid}")
276 @Produces("application/xml")
277 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
278 @PathParam("objectCsid") String objectCsid) {
279 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
280 return this.getRelationList(queryParams, null, null, objectCsid);
284 * Gets the relation list_ ps.
287 * @param predicate the predicate
288 * @param subjectCsid the subject csid
290 * @return the relation list_ ps
293 @Path("type/{predicate}/subject/{subjectCsid}")
294 @Produces("application/xml")
295 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
296 @PathParam("predicate") String predicate,
297 @PathParam("subjectCsid") String subjectCsid) {
298 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
299 return this.getRelationList(queryParams, subjectCsid, predicate, null);
303 * Gets the relation list_ sp.
306 * @param subjectCsid the subject csid
307 * @param predicate the predicate
309 * @return the relation list_ sp
312 @Path("subject/{subjectCsid}/type/{predicate}")
313 @Produces("application/xml")
314 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
315 @PathParam("subjectCsid") String subjectCsid,
316 @PathParam("predicate") String predicate) {
317 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
318 return this.getRelationList(queryParams, subjectCsid, predicate, null);
322 * Gets the relation list_ po.
325 * @param predicate the predicate
326 * @param objectCsid the object csid
328 * @return the relation list_ po
331 @Path("type/{predicate}/object/{objectCsid}")
332 @Produces("application/xml")
333 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
334 @PathParam("predicate") String predicate,
335 @PathParam("objectCsid") String objectCsid) {
336 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
337 return this.getRelationList(queryParams, null, predicate, objectCsid);
341 * Gets the relation list_ op.
344 * @param objectCsid the object csid
345 * @param predicate the predicate
347 * @return the relation list_ op
350 @Path("object/{objectCsid}/type/{predicate}")
351 @Produces("application/xml")
352 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
353 @PathParam("objectCsid") String objectCsid,
354 @PathParam("predicate") String predicate) {
355 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
356 return this.getRelationList(queryParams, null, predicate, objectCsid);
360 * Gets the relation list_ pso.
363 * @param predicate the predicate
364 * @param subjectCsid the subject csid
365 * @param objectCsid the object csid
367 * @return the relation list_ pso
370 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
371 @Produces("application/xml")
372 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
373 @PathParam("predicate") String predicate,
374 @PathParam("subjectCsid") String subjectCsid,
375 @PathParam("objectCsid") String objectCsid) {
376 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
377 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
381 * Gets the relation list_ spo.
384 * @param subjectCsid the subject csid
385 * @param predicate the predicate
386 * @param objectCsid the object csid
388 * @return the relation list_ spo
391 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
392 @Produces("application/xml")
393 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
394 @PathParam("subjectCsid") String subjectCsid,
395 @PathParam("predicate") String predicate,
396 @PathParam("objectCsid") String objectCsid) {
397 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
398 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
408 * @param csid the csid
409 * @param theUpdate the the update
411 * @return the multipart output
415 public MultipartOutput updateRelation(@PathParam("csid") String csid,
416 MultipartInput theUpdate) {
417 if (logger.isDebugEnabled()) {
418 logger.debug("updateRelation with csid=" + csid);
420 if (csid == null || "".equals(csid)) {
421 logger.error("updateRelation: missing csid!");
422 Response response = Response.status(Response.Status.BAD_REQUEST)
423 .entity("update failed on Relation csid=" + csid).type(
424 "text/plain").build();
425 throw new WebApplicationException(response);
427 MultipartOutput result = null;
429 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
430 DocumentHandler handler = createDocumentHandler(ctx);
431 getRepositoryClient(ctx).update(ctx, csid, handler);
432 result = (MultipartOutput) ctx.getOutput();
433 } catch (UnauthorizedException ue) {
434 Response response = Response.status(Response.Status.UNAUTHORIZED)
435 .entity("Update failed reason " + ue.getErrorReason())
436 .type("text/plain").build();
437 throw new WebApplicationException(response);
438 } catch (DocumentNotFoundException dnfe) {
439 if (logger.isDebugEnabled()) {
440 logger.debug("caugth exception in updateRelation", dnfe);
442 Response response = Response.status(Response.Status.NOT_FOUND)
443 .entity("Update failed on Relation csid=" + csid).type(
444 "text/plain").build();
445 throw new WebApplicationException(response);
446 } catch (Exception e) {
447 Response response = Response.status(
448 Response.Status.INTERNAL_SERVER_ERROR).entity(
449 "Update failed").type("text/plain").build();
450 throw new WebApplicationException(response);
458 * @param csid the csid
460 * @return the response
464 public Response deleteRelation(@PathParam("csid") String csid) {
466 if (logger.isDebugEnabled()) {
467 logger.debug("deleteRelation with csid=" + csid);
469 if (csid == null || "".equals(csid)) {
470 logger.error("deleteRelation: missing csid!");
471 Response response = Response.status(Response.Status.BAD_REQUEST)
472 .entity("delete failed on Relation csid=" + csid).type(
473 "text/plain").build();
474 throw new WebApplicationException(response);
477 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
478 getRepositoryClient(ctx).delete(ctx, csid);
479 return Response.status(HttpResponseCodes.SC_OK).build();
480 } catch (UnauthorizedException ue) {
481 Response response = Response.status(Response.Status.UNAUTHORIZED)
482 .entity("Delete failed reason " + ue.getErrorReason())
483 .type("text/plain").build();
484 throw new WebApplicationException(response);
485 } catch (DocumentNotFoundException dnfe) {
486 if (logger.isDebugEnabled()) {
487 logger.debug("caught exception in deleteRelation", dnfe);
489 Response response = Response.status(Response.Status.NOT_FOUND)
490 .entity("Delete failed on Relation csid=" + csid).type(
491 "text/plain").build();
492 throw new WebApplicationException(response);
493 } catch (Exception e) {
494 Response response = Response.status(
495 Response.Status.INTERNAL_SERVER_ERROR).entity(
496 "Delete failed").type("text/plain").build();
497 throw new WebApplicationException(response);
503 * Gets the relation list.
505 * @param queryParams the query params
506 * @param subjectCsid the subject csid
507 * @param predicate the predicate
508 * @param objectCsid the object csid
509 * @return the relation list
510 * @throws WebApplicationException the web application exception
512 public RelationsCommonList getRelationList(
513 MultivaluedMap<String, String> queryParams,
516 String objectCsid) throws WebApplicationException {
517 RelationsCommonList relationList = new RelationsCommonList();
519 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
520 DocumentHandler handler = createDocumentHandler(ctx);
521 Map<String, Object> propsFromPath = handler.getProperties();
522 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
523 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
524 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
525 // Until we replace this with a search, "getAll()" is better then "getFiltered"
526 getRepositoryClient(ctx).getAll(ctx, handler);
527 relationList = (RelationsCommonList) handler.getCommonPartList();
528 } catch (UnauthorizedException ue) {
529 Response response = Response.status(Response.Status.UNAUTHORIZED)
531 "Get relations failed reason "
532 + ue.getErrorReason()).type("text/plain")
534 throw new WebApplicationException(response);
535 } catch (Exception e) {
536 if (logger.isDebugEnabled()) {
537 logger.debug("Caught exception in getRelationList", e);
539 Response response = Response.status(
540 Response.Status.INTERNAL_SERVER_ERROR).entity(
541 "Index failed").type("text/plain").build();
542 throw new WebApplicationException(response);