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;
46 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
47 import org.collectionspace.services.common.context.RemoteServiceContext;
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.relation.nuxeo.RelationHandlerFactory;
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 @Consumes("multipart/mixed")
61 @Produces("multipart/mixed")
62 public class NewRelationResource extends AbstractCollectionSpaceResource {
64 public final static String serviceName = "relations";
65 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
68 public String getServiceName() {
73 public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
74 DocumentHandler docHandler = RelationHandlerFactory.getInstance().getHandler(
75 ctx.getRepositoryClientType().toString());
76 docHandler.setServiceContext(ctx);
77 if(ctx.getInput() != null){
78 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class);
80 docHandler.setCommonPart((RelationsCommon) obj);
87 public Response createRelation(MultipartInput input) {
90 RemoteServiceContext ctx = createServiceContext(input);
91 DocumentHandler handler = createDocumentHandler(ctx);
92 String csid = getRepositoryClient(ctx).create(ctx, handler);
93 UriBuilder path = UriBuilder.fromResource(NewRelationResource.class);
95 Response response = Response.created(path.build()).build();
98 if(logger.isDebugEnabled()){
99 logger.debug("Caught exception in createRelation", e);
101 Response response = Response.status(
102 Response.Status.INTERNAL_SERVER_ERROR).entity(
103 "Create failed").type("text/plain").build();
104 throw new WebApplicationException(response);
110 public MultipartOutput getRelation(@PathParam("csid") String csid) {
111 if(logger.isDebugEnabled()){
112 logger.debug("getRelation with csid=" + csid);
114 if(csid == null || "".equals(csid)){
115 logger.error("getRelation: missing csid!");
116 Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type(
117 "text/plain").build();
118 throw new WebApplicationException(response);
120 MultipartOutput result = null;
122 RemoteServiceContext ctx = createServiceContext(null);
123 DocumentHandler handler = createDocumentHandler(ctx);
124 getRepositoryClient(ctx).get(ctx, csid, handler);
125 result = ctx.getOutput();
126 }catch(DocumentNotFoundException dnfe){
127 if(logger.isDebugEnabled()){
128 logger.debug("getRelation", dnfe);
130 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type(
131 "text/plain").build();
132 throw new WebApplicationException(response);
134 if(logger.isDebugEnabled()){
135 logger.debug("getRelation", e);
137 Response response = Response.status(
138 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
139 throw new WebApplicationException(response);
143 Response response = Response.status(Response.Status.NOT_FOUND).entity(
144 "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build();
145 throw new WebApplicationException(response);
154 @Produces("application/xml")
155 public RelationsCommonList getRelationList(@Context UriInfo ui) {
156 return this.getRelationList(null, null, null);
160 @Path("subject/{subjectCsid}")
161 @Produces("application/xml")
162 public RelationsCommonList getRelationList_S(@Context UriInfo ui,
163 @PathParam("subjectCsid") String subjectCsid) {
164 return this.getRelationList(subjectCsid, null, null);
168 @Path("type/{predicate}")
169 @Produces("application/xml")
170 public RelationsCommonList getRelationList_P(@Context UriInfo ui,
171 @PathParam("predicate") String predicate) {
172 return this.getRelationList(null, predicate, null);
176 @Path("object/{objectCsid}")
177 @Produces("application/xml")
178 public RelationsCommonList getRelationList_O(@Context UriInfo ui,
179 @PathParam("objectCsid") String objectCsid) {
180 return this.getRelationList(null, null, objectCsid);
184 @Path("type/{predicate}/subject/{subjectCsid}")
185 @Produces("application/xml")
186 public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
187 @PathParam("predicate") String predicate,
188 @PathParam("subjectCsid") String subjectCsid) {
189 return this.getRelationList(subjectCsid, predicate, null);
193 @Path("subject/{subjectCsid}/type/{predicate}")
194 @Produces("application/xml")
195 public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
196 @PathParam("subjectCsid") String subjectCsid,
197 @PathParam("predicate") String predicate) {
198 return this.getRelationList(subjectCsid, predicate, null);
202 @Path("type/{predicate}/object/{objectCsid}")
203 @Produces("application/xml")
204 public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
205 @PathParam("predicate") String predicate,
206 @PathParam("objectCsid") String objectCsid) {
207 return this.getRelationList(null, predicate, objectCsid);
211 @Path("object/{objectCsid}/type/{predicate}")
212 @Produces("application/xml")
213 public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
214 @PathParam("objectCsid") String objectCsid,
215 @PathParam("predicate") String predicate) {
216 return this.getRelationList(null, predicate, objectCsid);
220 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
221 @Produces("application/xml")
222 public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
223 @PathParam("predicate") String predicate,
224 @PathParam("subjectCsid") String subjectCsid,
225 @PathParam("objectCsid") String objectCsid) {
226 return this.getRelationList(subjectCsid, predicate, objectCsid);
230 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
231 @Produces("application/xml")
232 public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
233 @PathParam("subjectCsid") String subjectCsid,
234 @PathParam("predicate") String predicate,
235 @PathParam("objectCsid") String objectCsid) {
236 return this.getRelationList(subjectCsid, predicate, objectCsid);
244 public MultipartOutput updateRelation(@PathParam("csid") String csid,
245 MultipartInput theUpdate) {
246 if(logger.isDebugEnabled()){
247 logger.debug("updateRelation with csid=" + csid);
249 if(csid == null || "".equals(csid)){
250 logger.error("updateRelation: missing csid!");
251 Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type(
252 "text/plain").build();
253 throw new WebApplicationException(response);
255 MultipartOutput result = null;
257 RemoteServiceContext ctx = createServiceContext(theUpdate);
258 DocumentHandler handler = createDocumentHandler(ctx);
259 getRepositoryClient(ctx).update(ctx, csid, handler);
260 result = ctx.getOutput();
261 }catch(DocumentNotFoundException dnfe){
262 if(logger.isDebugEnabled()){
263 logger.debug("caugth exception in updateRelation", dnfe);
265 Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type(
266 "text/plain").build();
267 throw new WebApplicationException(response);
269 Response response = Response.status(
270 Response.Status.INTERNAL_SERVER_ERROR).entity(
271 "Update failed").type("text/plain").build();
272 throw new WebApplicationException(response);
279 public Response deleteRelation(@PathParam("csid") String csid) {
281 if(logger.isDebugEnabled()){
282 logger.debug("deleteRelation with csid=" + csid);
284 if(csid == null || "".equals(csid)){
285 logger.error("deleteRelation: missing csid!");
286 Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type(
287 "text/plain").build();
288 throw new WebApplicationException(response);
291 ServiceContext ctx = createServiceContext(null);
292 getRepositoryClient(ctx).delete(ctx, csid);
293 return Response.status(HttpResponseCodes.SC_OK).build();
294 }catch(DocumentNotFoundException dnfe){
295 if(logger.isDebugEnabled()){
296 logger.debug("caught exception in deleteRelation", dnfe);
298 Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type(
299 "text/plain").build();
300 throw new WebApplicationException(response);
302 Response response = Response.status(
303 Response.Status.INTERNAL_SERVER_ERROR).entity(
304 "Delete failed").type("text/plain").build();
305 throw new WebApplicationException(response);
314 * Gets the relation list request.
316 * @return the relation list request
318 * @throws WebApplicationException the web application exception
320 private RelationsCommonList getRelationList(String subjectCsid,
323 throws WebApplicationException {
324 RelationsCommonList relationList = new RelationsCommonList();
326 RemoteServiceContext ctx = createServiceContext(null);
327 DocumentHandler handler = createDocumentHandler(ctx);
328 Map propsFromPath = handler.getProperties();
329 propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
330 propsFromPath.put(IRelationsManager.PREDICATE, predicate);
331 propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
332 getRepositoryClient(ctx).getAll(ctx, handler);
333 relationList = (RelationsCommonList) handler.getCommonPartList();
335 if(logger.isDebugEnabled()){
336 logger.debug("Caught exception in getRelationList", e);
338 Response response = Response.status(
339 Response.Status.INTERNAL_SERVER_ERROR).entity(
340 "Index failed").type("text/plain").build();
341 throw new WebApplicationException(response);