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.common.CollectionSpaceResource;
50 import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants;
51 import org.collectionspace.services.relation.nuxeo.RelationHandlerFactory;
52 import org.collectionspace.services.common.CollectionSpaceHandlerFactory;
53 import org.collectionspace.services.common.NuxeoClientType;
54 import org.collectionspace.services.common.ServiceMain;
55 import org.collectionspace.services.common.relation.RelationsManager;
56 import org.collectionspace.services.common.repository.DocumentNotFoundException;
57 import org.collectionspace.services.common.repository.DocumentHandler;
58 import org.collectionspace.services.common.repository.RepositoryClient;
59 import org.collectionspace.services.common.repository.RepositoryClientFactory;
60 import org.jboss.resteasy.util.HttpResponseCodes;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 @Consumes("application/xml")
66 @Produces("application/xml")
67 public class NewRelationResource extends CollectionSpaceResource {
69 public final static String SERVICE_NAME = "relations";
70 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
71 // FIXME retrieve client type from configuration
72 // final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance()
73 // .getNuxeoClientType();
75 protected String getClientType() {
76 return ServiceMain.getInstance().getNuxeoClientType().toString();
79 protected RepositoryClientFactory getDefaultClientFactory() {
80 return RepositoryClientFactory.getInstance();
83 protected CollectionSpaceHandlerFactory getDefaultHandlerFactory() {
84 return RelationHandlerFactory.getInstance();
87 // public NewRelationResource() {
91 public Response createRelation(Relation relation) {
95 getDefaultHandler().setCommonObject(relation);
96 csid = getDefaultClient().create(SERVICE_NAME, getDefaultHandler());
97 relation.setCsid(csid);
98 if (logger.isDebugEnabled()) {
99 verbose("createRelation: ", relation);
101 UriBuilder path = UriBuilder.fromResource(RelationResource.class);
102 path.path("" + csid);
103 Response response = Response.created(path.build()).build();
105 } catch (Exception e) {
106 if (logger.isDebugEnabled()) {
107 logger.debug("Caught exception in createRelation", e);
109 Response response = Response.status(
110 Response.Status.INTERNAL_SERVER_ERROR).entity(
111 "Create failed").type("text/plain").build();
112 throw new WebApplicationException(response);
117 @Path("query/{queryValue}")
118 public Response getQuery(@PathParam("queryValue") String queryString) {
120 Response result = null;
122 if (logger.isDebugEnabled() == true) {
123 logger.debug("Query string is: " + queryString);
126 result = Response.status(Response.Status.ACCEPTED).entity(
127 "Query performed. Look in $JBOSS_HOME/server/cspace/log/" +
128 "directory for results ").type("text/plain").build();
135 public Relation getRelation(@PathParam("csid") String csid) {
136 if (logger.isDebugEnabled()) {
137 verbose("getRelation with csid=" + csid);
139 if (csid == null || "".equals(csid)) {
140 logger.error("getRelation: missing csid!");
141 Response response = Response.status(Response.Status.BAD_REQUEST)
142 .entity("get failed on Relation csid=" + csid).type(
143 "text/plain").build();
144 throw new WebApplicationException(response);
146 Relation relation = null;
148 getDefaultClient().get(csid, getDefaultHandler());
149 relation = (Relation) getDefaultHandler().getCommonObject();
150 } catch (DocumentNotFoundException dnfe) {
151 if (logger.isDebugEnabled()) {
152 logger.debug("getRelation", dnfe);
154 Response response = Response.status(Response.Status.NOT_FOUND)
155 .entity("Get failed on Relation csid=" + csid).type(
156 "text/plain").build();
157 throw new WebApplicationException(response);
158 } catch (Exception e) {
159 if (logger.isDebugEnabled()) {
160 logger.debug("getRelation", e);
162 Response response = Response.status(
163 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
164 .type("text/plain").build();
165 throw new WebApplicationException(response);
168 if (relation == null) {
169 Response response = Response.status(Response.Status.NOT_FOUND)
171 "Get failed, the requested Relation CSID:" + csid
172 + ": was not found.").type("text/plain")
174 throw new WebApplicationException(response);
176 if (logger.isDebugEnabled()) {
177 verbose("getRelation: ", relation);
187 public RelationList getRelationList(@Context UriInfo ui) {
188 return this.getRelationListRequest(null, null, null);
192 @Path("subject/{subjectCsid}")
193 public RelationList getRelationList_S(@Context UriInfo ui,
194 @PathParam("subjectCsid") String subjectCsid) {
195 return this.getRelationListRequest(subjectCsid, null, null);
199 @Path("type/{predicate}")
200 public RelationList getRelationList_P(@Context UriInfo ui,
201 @PathParam("predicate") String predicate) {
202 return this.getRelationListRequest(null, predicate, null);
206 @Path("object/{objectCsid}")
207 public RelationList getRelationList_O(@Context UriInfo ui,
208 @PathParam("objectCsid") String objectCsid) {
209 return this.getRelationListRequest(null, null, objectCsid);
213 @Path("type/{predicate}/subject/{subjectCsid}")
214 public RelationList getRelationList_PS(@Context UriInfo ui,
215 @PathParam("predicate") String predicate,
216 @PathParam("subjectCsid") String subjectCsid) {
217 return this.getRelationListRequest(subjectCsid, predicate, null);
221 @Path("subject/{subjectCsid}/type/{predicate}")
222 public RelationList getRelationList_SP(@Context UriInfo ui,
223 @PathParam("subjectCsid") String subjectCsid,
224 @PathParam("predicate") String predicate) {
225 return this.getRelationListRequest(subjectCsid, predicate, null);
229 @Path("type/{predicate}/object/{objectCsid}")
230 public RelationList getRelationList_PO(@Context UriInfo ui,
231 @PathParam("predicate") String predicate,
232 @PathParam("objectCsid") String objectCsid) {
233 return this.getRelationListRequest(null, predicate, objectCsid);
237 @Path("object/{objectCsid}/type/{predicate}")
238 public RelationList getRelationList_OP(@Context UriInfo ui,
239 @PathParam("objectCsid") String objectCsid,
240 @PathParam("predicate") String predicate) {
241 return this.getRelationListRequest(null, predicate, objectCsid);
245 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
246 public RelationList getRelationList_PSO(@Context UriInfo ui,
247 @PathParam("predicate") String predicate,
248 @PathParam("subjectCsid") String subjectCsid,
249 @PathParam("objectCsid") String objectCsid) {
250 return this.getRelationListRequest(subjectCsid, predicate, objectCsid);
254 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
255 public RelationList getRelationList_SPO(@Context UriInfo ui,
256 @PathParam("subjectCsid") String subjectCsid,
257 @PathParam("predicate") String predicate,
258 @PathParam("objectCsid") String objectCsid) {
259 return this.getRelationListRequest(subjectCsid, predicate, objectCsid);
268 public Relation updateRelation(@PathParam("csid") String csid,
269 Relation theUpdate) {
270 if (logger.isDebugEnabled()) {
271 verbose("updateRelation with csid=" + csid);
273 if (csid == null || "".equals(csid)) {
274 logger.error("updateRelation: missing csid!");
275 Response response = Response.status(Response.Status.BAD_REQUEST)
276 .entity("update failed on Relation csid=" + csid).type(
277 "text/plain").build();
278 throw new WebApplicationException(response);
280 if (logger.isDebugEnabled()) {
281 verbose("updateRelation with input: ", theUpdate);
284 getDefaultHandler().setCommonObject(theUpdate);
285 getDefaultClient().update(csid, getDefaultHandler());
286 } catch (DocumentNotFoundException dnfe) {
287 if (logger.isDebugEnabled()) {
288 logger.debug("caugth exception in updateRelation", dnfe);
290 Response response = Response.status(Response.Status.NOT_FOUND)
291 .entity("Update failed on Relation csid=" + csid).type(
292 "text/plain").build();
293 throw new WebApplicationException(response);
294 } catch (Exception e) {
295 Response response = Response.status(
296 Response.Status.INTERNAL_SERVER_ERROR).entity(
297 "Update failed").type("text/plain").build();
298 throw new WebApplicationException(response);
305 public Response deleteRelation(@PathParam("csid") String csid) {
307 if (logger.isDebugEnabled()) {
308 verbose("deleteRelation with csid=" + csid);
310 if (csid == null || "".equals(csid)) {
311 logger.error("deleteRelation: missing csid!");
312 Response response = Response.status(Response.Status.BAD_REQUEST)
313 .entity("delete failed on Relation csid=" + csid).type(
314 "text/plain").build();
315 throw new WebApplicationException(response);
318 getDefaultClient().delete(csid);
319 return Response.status(HttpResponseCodes.SC_OK).build();
320 } catch (DocumentNotFoundException dnfe) {
321 if (logger.isDebugEnabled()) {
322 logger.debug("caught exception in deleteRelation", dnfe);
324 Response response = Response.status(Response.Status.NOT_FOUND)
325 .entity("Delete failed on Relation csid=" + csid).type(
326 "text/plain").build();
327 throw new WebApplicationException(response);
328 } catch (Exception e) {
329 Response response = Response.status(
330 Response.Status.INTERNAL_SERVER_ERROR).entity(
331 "Delete failed").type("text/plain").build();
332 throw new WebApplicationException(response);
342 * Gets the relation list request.
344 * @return the relation list request
346 * @throws WebApplicationException the web application exception
348 private RelationList getRelationListRequest(String subjectCsid,
351 throws WebApplicationException {
352 RelationList relationList = new RelationList();
354 Map propsFromPath = getDefaultHandler().getProperties();
355 propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
356 propsFromPath.put(RelationsManager.PREDICATE, predicate);
357 propsFromPath.put(RelationsManager.OBJECT, objectCsid);
359 getDefaultClient().getAll(SERVICE_NAME, getDefaultHandler());
360 relationList = (RelationList) getDefaultHandler().getCommonObjectList();
361 } catch (Exception e) {
362 if (logger.isDebugEnabled()) {
363 logger.debug("Caught exception in getRelationList", e);
365 Response response = Response.status(
366 Response.Status.INTERNAL_SERVER_ERROR).entity(
367 "Index failed").type("text/plain").build();
368 throw new WebApplicationException(response);
373 private void verbose(String msg, Relation relation) {
376 JAXBContext jc = JAXBContext.newInstance(Relation.class);
378 Marshaller m = jc.createMarshaller();
379 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
380 m.marshal(relation, System.out);
381 } catch (Exception e) {
387 private void verbose(String msg) {
388 System.out.println("RelationResource. " + msg);