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;
44 import javax.xml.bind.JAXBContext;
45 import javax.xml.bind.Marshaller;
47 import org.collectionspace.services.relation.RelationList.RelationListItem;
49 import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants;
50 import org.collectionspace.services.relation.nuxeo.RelationHandlerFactory;
51 import org.collectionspace.services.common.NuxeoClientType;
52 import org.collectionspace.services.common.ServiceMain;
53 import org.collectionspace.services.common.relation.RelationsManager;
54 import org.collectionspace.services.common.repository.DocumentNotFoundException;
55 import org.collectionspace.services.common.repository.DocumentHandler;
56 import org.collectionspace.services.common.repository.RepositoryClient;
57 import org.collectionspace.services.common.repository.RepositoryClientFactory;
58 import org.jboss.resteasy.util.HttpResponseCodes;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 @Consumes("application/xml")
64 @Produces("application/xml")
65 public class NewRelationResource {
67 public final static String SERVICE_NAME = "relations";
68 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
69 // FIXME retrieve client type from configuration
70 final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance()
71 .getNuxeoClientType();
73 public NewRelationResource() {
78 public Response createRelation(Relation relation) {
82 RepositoryClientFactory clientFactory = RepositoryClientFactory
84 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
86 RelationHandlerFactory handlerFactory = RelationHandlerFactory
88 DocumentHandler handler = (DocumentHandler) handlerFactory
89 .getHandler(CLIENT_TYPE.toString());
90 handler.setCommonObject(relation);
91 csid = client.create(SERVICE_NAME, handler);
92 relation.setCsid(csid);
93 if (logger.isDebugEnabled()) {
94 verbose("createRelation: ", relation);
96 UriBuilder path = UriBuilder.fromResource(RelationResource.class);
98 Response response = Response.created(path.build()).build();
100 } catch (Exception e) {
101 if (logger.isDebugEnabled()) {
102 logger.debug("Caught exception in createRelation", e);
104 Response response = Response.status(
105 Response.Status.INTERNAL_SERVER_ERROR).entity(
106 "Create failed").type("text/plain").build();
107 throw new WebApplicationException(response);
113 public Relation getRelation(@PathParam("csid") String csid) {
114 if (logger.isDebugEnabled()) {
115 verbose("getRelation with csid=" + csid);
117 if (csid == null || "".equals(csid)) {
118 logger.error("getRelation: missing csid!");
119 Response response = Response.status(Response.Status.BAD_REQUEST)
120 .entity("get failed on Relation csid=" + csid).type(
121 "text/plain").build();
122 throw new WebApplicationException(response);
124 Relation relation = null;
126 RepositoryClientFactory clientFactory = RepositoryClientFactory
128 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
130 RelationHandlerFactory handlerFactory = RelationHandlerFactory
132 DocumentHandler handler = (DocumentHandler) handlerFactory
133 .getHandler(CLIENT_TYPE.toString());
134 client.get(csid, handler);
135 relation = (Relation) handler.getCommonObject();
136 } catch (DocumentNotFoundException dnfe) {
137 if (logger.isDebugEnabled()) {
138 logger.debug("getRelation", dnfe);
140 Response response = Response.status(Response.Status.NOT_FOUND)
141 .entity("Get failed on Relation csid=" + csid).type(
142 "text/plain").build();
143 throw new WebApplicationException(response);
144 } catch (Exception e) {
145 if (logger.isDebugEnabled()) {
146 logger.debug("getRelation", e);
148 Response response = Response.status(
149 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
150 .type("text/plain").build();
151 throw new WebApplicationException(response);
154 if (relation == null) {
155 Response response = Response.status(Response.Status.NOT_FOUND)
157 "Get failed, the requested Relation CSID:" + csid
158 + ": was not found.").type("text/plain")
160 throw new WebApplicationException(response);
162 if (logger.isDebugEnabled()) {
163 verbose("getRelation: ", relation);
173 public RelationList getRelationList(@Context UriInfo ui) {
174 return this.getRelationListRequest(null, null, null);
178 @Path("subject/{subjectCsid}")
179 public RelationList getRelationList_S(@Context UriInfo ui,
180 @PathParam("subjectCsid") String subjectCsid) {
181 return this.getRelationListRequest(subjectCsid, null, null);
185 @Path("type/{predicate}")
186 public RelationList getRelationList_P(@Context UriInfo ui,
187 @PathParam("predicate") String predicate) {
188 return this.getRelationListRequest(null, predicate, null);
192 @Path("object/{objectCsid}")
193 public RelationList getRelationList_O(@Context UriInfo ui,
194 @PathParam("objectCsid") String objectCsid) {
195 return this.getRelationListRequest(null, null, objectCsid);
199 @Path("type/{predicate}/subject/{subjectCsid}")
200 public RelationList getRelationList_PS(@Context UriInfo ui,
201 @PathParam("predicate") String predicate,
202 @PathParam("subjectCsid") String subjectCsid) {
203 return this.getRelationListRequest(subjectCsid, predicate, null);
207 @Path("subject/{subjectCsid}/type/{predicate}")
208 public RelationList getRelationList_SP(@Context UriInfo ui,
209 @PathParam("subjectCsid") String subjectCsid,
210 @PathParam("predicate") String predicate) {
211 return this.getRelationListRequest(subjectCsid, predicate, null);
215 @Path("type/{predicate}/object/{objectCsid}")
216 public RelationList getRelationList_PO(@Context UriInfo ui,
217 @PathParam("predicate") String predicate,
218 @PathParam("objectCsid") String objectCsid) {
219 return this.getRelationListRequest(null, predicate, objectCsid);
223 @Path("object/{objectCsid}/type/{predicate}")
224 public RelationList getRelationList_OP(@Context UriInfo ui,
225 @PathParam("objectCsid") String objectCsid,
226 @PathParam("predicate") String predicate) {
227 return this.getRelationListRequest(null, predicate, objectCsid);
231 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
232 public RelationList getRelationList_PSO(@Context UriInfo ui,
233 @PathParam("predicate") String predicate,
234 @PathParam("subjectCsid") String subjectCsid,
235 @PathParam("objectCsid") String objectCsid) {
236 return this.getRelationListRequest(subjectCsid, predicate, objectCsid);
240 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
241 public RelationList getRelationList_SPO(@Context UriInfo ui,
242 @PathParam("subjectCsid") String subjectCsid,
243 @PathParam("predicate") String predicate,
244 @PathParam("objectCsid") String objectCsid) {
245 return this.getRelationListRequest(subjectCsid, predicate, objectCsid);
254 public Relation updateRelation(@PathParam("csid") String csid,
255 Relation theUpdate) {
256 if (logger.isDebugEnabled()) {
257 verbose("updateRelation with csid=" + csid);
259 if (csid == null || "".equals(csid)) {
260 logger.error("updateRelation: missing csid!");
261 Response response = Response.status(Response.Status.BAD_REQUEST)
262 .entity("update failed on Relation csid=" + csid).type(
263 "text/plain").build();
264 throw new WebApplicationException(response);
266 if (logger.isDebugEnabled()) {
267 verbose("updateRelation with input: ", theUpdate);
270 RepositoryClientFactory clientFactory = RepositoryClientFactory
272 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
274 RelationHandlerFactory handlerFactory = RelationHandlerFactory
276 DocumentHandler handler = (DocumentHandler) handlerFactory
277 .getHandler(CLIENT_TYPE.toString());
278 handler.setCommonObject(theUpdate);
279 client.update(csid, handler);
280 } catch (DocumentNotFoundException dnfe) {
281 if (logger.isDebugEnabled()) {
282 logger.debug("caugth exception in updateRelation", dnfe);
284 Response response = Response.status(Response.Status.NOT_FOUND)
285 .entity("Update failed on Relation csid=" + csid).type(
286 "text/plain").build();
287 throw new WebApplicationException(response);
288 } catch (Exception e) {
289 Response response = Response.status(
290 Response.Status.INTERNAL_SERVER_ERROR).entity(
291 "Update failed").type("text/plain").build();
292 throw new WebApplicationException(response);
299 public Response deleteRelation(@PathParam("csid") String csid) {
301 if (logger.isDebugEnabled()) {
302 verbose("deleteRelation with csid=" + csid);
304 if (csid == null || "".equals(csid)) {
305 logger.error("deleteRelation: missing csid!");
306 Response response = Response.status(Response.Status.BAD_REQUEST)
307 .entity("delete failed on Relation csid=" + csid).type(
308 "text/plain").build();
309 throw new WebApplicationException(response);
312 RepositoryClientFactory clientFactory = RepositoryClientFactory
314 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
317 return Response.status(HttpResponseCodes.SC_OK).build();
318 } catch (DocumentNotFoundException dnfe) {
319 if (logger.isDebugEnabled()) {
320 logger.debug("caught exception in deleteRelation", dnfe);
322 Response response = Response.status(Response.Status.NOT_FOUND)
323 .entity("Delete failed on Relation csid=" + csid).type(
324 "text/plain").build();
325 throw new WebApplicationException(response);
326 } catch (Exception e) {
327 Response response = Response.status(
328 Response.Status.INTERNAL_SERVER_ERROR).entity(
329 "Delete failed").type("text/plain").build();
330 throw new WebApplicationException(response);
340 * Gets the relation list request.
342 * @return the relation list request
344 * @throws WebApplicationException the web application exception
346 private RelationList getRelationListRequest(String subjectCsid,
349 throws WebApplicationException {
350 RelationList relationList = new RelationList();
352 RepositoryClientFactory clientFactory = RepositoryClientFactory
354 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
356 RelationHandlerFactory handlerFactory = RelationHandlerFactory
358 DocumentHandler handler = (DocumentHandler) handlerFactory
359 .getHandler(CLIENT_TYPE.toString());
361 Map propsFromPath = handler.getProperties();
362 propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
363 propsFromPath.put(RelationsManager.PREDICATE, predicate);
364 propsFromPath.put(RelationsManager.OBJECT, objectCsid);
366 client.getAll(SERVICE_NAME, handler);
367 relationList = (RelationList) handler.getCommonObjectList();
368 } catch (Exception e) {
369 if (logger.isDebugEnabled()) {
370 logger.debug("Caught exception in getRelationList", e);
372 Response response = Response.status(
373 Response.Status.INTERNAL_SERVER_ERROR).entity(
374 "Index failed").type("text/plain").build();
375 throw new WebApplicationException(response);
380 private void verbose(String msg, Relation relation) {
383 JAXBContext jc = JAXBContext.newInstance(Relation.class);
385 Marshaller m = jc.createMarshaller();
386 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
387 m.marshal(relation, System.out);
388 } catch (Exception e) {
394 private void verbose(String msg) {
395 System.out.println("RelationResource. " + msg);