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;
48 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
49 import org.collectionspace.services.common.NuxeoClientType;
50 import org.collectionspace.services.common.context.ServiceContext;
51 import org.collectionspace.services.common.relation.RelationsManager;
52 import org.collectionspace.services.common.repository.DocumentNotFoundException;
53 import org.collectionspace.services.common.repository.DocumentHandler;
54 import org.jboss.resteasy.util.HttpResponseCodes;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 @Consumes("multipart/mixed")
60 @Produces("multipart/mixed")
61 public class NewRelationResource extends AbstractCollectionSpaceResource {
63 public final static String serviceName = "relations";
64 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
68 public String getServiceName() {
73 public DocumentHandler createDocumentHandler(ServiceContext 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(), CollectionobjectsCommon.class);
80 docHandler.setCommonPart((CollectionobjectsCommon) obj);
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 * Prototype for using NXQL -will move away from here once protyping is finished.
120 @Path("query/{queryValue}")
121 public Response getQuery(@PathParam("queryValue") String queryString) {
123 Response result = null;
125 if (logger.isDebugEnabled() == true) {
126 logger.debug("Query string is: " + queryString);
129 //getDefaultClient().
131 result = Response.status(Response.Status.ACCEPTED).entity(
132 "Query performed. Look in $JBOSS_HOME/server/cspace/log/" +
133 "directory for results ").type("text/plain").build();
140 public Relation getRelation(@PathParam("csid") String csid) {
141 if (logger.isDebugEnabled()) {
142 verbose("getRelation with csid=" + csid);
144 if (csid == null || "".equals(csid)) {
145 logger.error("getRelation: missing csid!");
146 Response response = Response.status(Response.Status.BAD_REQUEST)
147 .entity("get failed on Relation csid=" + csid).type(
148 "text/plain").build();
149 throw new WebApplicationException(response);
151 Relation relation = null;
153 getDefaultClient().get(csid, getDefaultHandler());
154 relation = (Relation) getDefaultHandler().getCommonObject();
155 } catch (DocumentNotFoundException dnfe) {
156 if (logger.isDebugEnabled()) {
157 logger.debug("getRelation", dnfe);
159 Response response = Response.status(Response.Status.NOT_FOUND)
160 .entity("Get failed on Relation csid=" + csid).type(
161 "text/plain").build();
162 throw new WebApplicationException(response);
163 } catch (Exception e) {
164 if (logger.isDebugEnabled()) {
165 logger.debug("getRelation", e);
167 Response response = Response.status(
168 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
169 .type("text/plain").build();
170 throw new WebApplicationException(response);
173 if (relation == null) {
174 Response response = Response.status(Response.Status.NOT_FOUND)
176 "Get failed, the requested Relation CSID:" + csid
177 + ": was not found.").type("text/plain")
179 throw new WebApplicationException(response);
181 if (logger.isDebugEnabled()) {
182 verbose("getRelation: ", relation);
192 public RelationList getRelationList(@Context UriInfo ui) {
193 return this.getRelationListRequest(null, null, null);
197 @Path("subject/{subjectCsid}")
198 public RelationList getRelationList_S(@Context UriInfo ui,
199 @PathParam("subjectCsid") String subjectCsid) {
200 return this.getRelationListRequest(subjectCsid, null, null);
204 @Path("type/{predicate}")
205 public RelationList getRelationList_P(@Context UriInfo ui,
206 @PathParam("predicate") String predicate) {
207 return this.getRelationListRequest(null, predicate, null);
211 @Path("object/{objectCsid}")
212 public RelationList getRelationList_O(@Context UriInfo ui,
213 @PathParam("objectCsid") String objectCsid) {
214 return this.getRelationListRequest(null, null, objectCsid);
218 @Path("type/{predicate}/subject/{subjectCsid}")
219 public RelationList getRelationList_PS(@Context UriInfo ui,
220 @PathParam("predicate") String predicate,
221 @PathParam("subjectCsid") String subjectCsid) {
222 return this.getRelationListRequest(subjectCsid, predicate, null);
226 @Path("subject/{subjectCsid}/type/{predicate}")
227 public RelationList getRelationList_SP(@Context UriInfo ui,
228 @PathParam("subjectCsid") String subjectCsid,
229 @PathParam("predicate") String predicate) {
230 return this.getRelationListRequest(subjectCsid, predicate, null);
234 @Path("type/{predicate}/object/{objectCsid}")
235 public RelationList getRelationList_PO(@Context UriInfo ui,
236 @PathParam("predicate") String predicate,
237 @PathParam("objectCsid") String objectCsid) {
238 return this.getRelationListRequest(null, predicate, objectCsid);
242 @Path("object/{objectCsid}/type/{predicate}")
243 public RelationList getRelationList_OP(@Context UriInfo ui,
244 @PathParam("objectCsid") String objectCsid,
245 @PathParam("predicate") String predicate) {
246 return this.getRelationListRequest(null, predicate, objectCsid);
250 @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
251 public RelationList getRelationList_PSO(@Context UriInfo ui,
252 @PathParam("predicate") String predicate,
253 @PathParam("subjectCsid") String subjectCsid,
254 @PathParam("objectCsid") String objectCsid) {
255 return this.getRelationListRequest(subjectCsid, predicate, objectCsid);
259 @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
260 public RelationList getRelationList_SPO(@Context UriInfo ui,
261 @PathParam("subjectCsid") String subjectCsid,
262 @PathParam("predicate") String predicate,
263 @PathParam("objectCsid") String objectCsid) {
264 return this.getRelationListRequest(subjectCsid, predicate, objectCsid);
273 public Relation updateRelation(@PathParam("csid") String csid,
274 Relation theUpdate) {
275 if (logger.isDebugEnabled()) {
276 verbose("updateRelation with csid=" + csid);
278 if (csid == null || "".equals(csid)) {
279 logger.error("updateRelation: missing csid!");
280 Response response = Response.status(Response.Status.BAD_REQUEST)
281 .entity("update failed on Relation csid=" + csid).type(
282 "text/plain").build();
283 throw new WebApplicationException(response);
285 if (logger.isDebugEnabled()) {
286 verbose("updateRelation with input: ", theUpdate);
289 getDefaultHandler().setCommonObject(theUpdate);
290 getDefaultClient().update(csid, getDefaultHandler());
291 } catch (DocumentNotFoundException dnfe) {
292 if (logger.isDebugEnabled()) {
293 logger.debug("caugth exception in updateRelation", dnfe);
295 Response response = Response.status(Response.Status.NOT_FOUND)
296 .entity("Update failed on Relation csid=" + csid).type(
297 "text/plain").build();
298 throw new WebApplicationException(response);
299 } catch (Exception e) {
300 Response response = Response.status(
301 Response.Status.INTERNAL_SERVER_ERROR).entity(
302 "Update failed").type("text/plain").build();
303 throw new WebApplicationException(response);
310 public Response deleteRelation(@PathParam("csid") String csid) {
312 if (logger.isDebugEnabled()) {
313 verbose("deleteRelation with csid=" + csid);
315 if (csid == null || "".equals(csid)) {
316 logger.error("deleteRelation: missing csid!");
317 Response response = Response.status(Response.Status.BAD_REQUEST)
318 .entity("delete failed on Relation csid=" + csid).type(
319 "text/plain").build();
320 throw new WebApplicationException(response);
323 getDefaultClient().delete(csid);
324 return Response.status(HttpResponseCodes.SC_OK).build();
325 } catch (DocumentNotFoundException dnfe) {
326 if (logger.isDebugEnabled()) {
327 logger.debug("caught exception in deleteRelation", dnfe);
329 Response response = Response.status(Response.Status.NOT_FOUND)
330 .entity("Delete failed on Relation csid=" + csid).type(
331 "text/plain").build();
332 throw new WebApplicationException(response);
333 } catch (Exception e) {
334 Response response = Response.status(
335 Response.Status.INTERNAL_SERVER_ERROR).entity(
336 "Delete failed").type("text/plain").build();
337 throw new WebApplicationException(response);
347 * Gets the relation list request.
349 * @return the relation list request
351 * @throws WebApplicationException the web application exception
353 private RelationList getRelationListRequest(String subjectCsid,
356 throws WebApplicationException {
357 RelationList relationList = new RelationList();
359 Map propsFromPath = getDefaultHandler().getProperties();
360 propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
361 propsFromPath.put(RelationsManager.PREDICATE, predicate);
362 propsFromPath.put(RelationsManager.OBJECT, objectCsid);
364 getDefaultClient().getAll(SERVICE_NAME, getDefaultHandler());
365 relationList = (RelationList) getDefaultHandler().getCommonObjectList();
366 } catch (Exception e) {
367 if (logger.isDebugEnabled()) {
368 logger.debug("Caught exception in getRelationList", e);
370 Response response = Response.status(
371 Response.Status.INTERNAL_SERVER_ERROR).entity(
372 "Index failed").type("text/plain").build();
373 throw new WebApplicationException(response);
378 private void verbose(String msg, Relation relation) {
381 JAXBContext jc = JAXBContext.newInstance(Relation.class);
383 Marshaller m = jc.createMarshaller();
384 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
385 m.marshal(relation, System.out);
386 } catch (Exception e) {
392 private void verbose(String msg) {
393 System.out.println("RelationResource. " + msg);