1 package org.collectionspace.services.relation;
3 import javax.ws.rs.Consumes;
4 import javax.ws.rs.GET;
5 import javax.ws.rs.Path;
6 import javax.ws.rs.Produces;
7 import javax.ws.rs.DELETE;
8 import javax.ws.rs.POST;
9 import javax.ws.rs.PUT;
10 import javax.ws.rs.PathParam;
11 import javax.ws.rs.WebApplicationException;
12 import javax.ws.rs.core.Context;
13 import javax.ws.rs.core.Response;
14 import javax.ws.rs.core.UriBuilder;
15 import javax.ws.rs.core.UriInfo;
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.Marshaller;
19 import org.collectionspace.services.relation.RelationList.RelationListItem;
21 import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants;
22 import org.collectionspace.services.relation.nuxeo.RelationHandlerFactory;
23 import org.collectionspace.services.common.NuxeoClientType;
24 import org.collectionspace.services.common.ServiceMain;
25 import org.collectionspace.services.common.repository.DocumentNotFoundException;
26 import org.collectionspace.services.common.repository.DocumentHandler;
27 import org.collectionspace.services.common.repository.RepositoryClient;
28 import org.collectionspace.services.common.repository.RepositoryClientFactory;
29 import org.jboss.resteasy.util.HttpResponseCodes;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 @Consumes("application/xml")
35 @Produces("application/xml")
36 public class NewRelationResource {
38 public final static String SERVICE_NAME = "relations";
39 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
40 //FIXME retrieve client type from configuration
41 final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance().getNuxeoClientType();
43 public NewRelationResource() {
48 public Response createRelation(
53 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
54 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
55 RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance();
56 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
57 handler.setCommonObject(relation);
58 csid = client.create(SERVICE_NAME, handler);
59 relation.setCsid(csid);
60 if(logger.isDebugEnabled()){
61 verbose("createRelation: ", relation);
63 UriBuilder path = UriBuilder.fromResource(RelationResource.class);
65 Response response = Response.created(path.build()).build();
68 if(logger.isDebugEnabled()){
69 logger.debug("Caught exception in createRelation", e);
71 Response response = Response.status(
72 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
73 throw new WebApplicationException(response);
79 public Relation getRelation(
80 @PathParam("csid") String csid) {
81 if(logger.isDebugEnabled()){
82 verbose("getRelation with csid=" + csid);
84 if(csid == null || "".equals(csid)){
85 logger.error("getRelation: missing csid!");
86 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
87 "get failed on Relation csid=" + csid).type(
88 "text/plain").build();
89 throw new WebApplicationException(response);
91 Relation relation = null;
93 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
94 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
95 RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance();
96 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
97 client.get(csid, handler);
98 relation = (Relation) handler.getCommonObject();
99 }catch(DocumentNotFoundException dnfe){
100 if(logger.isDebugEnabled()){
101 logger.debug("getRelation", dnfe);
103 Response response = Response.status(Response.Status.NOT_FOUND).entity(
104 "Get failed on Relation csid=" + csid).type(
105 "text/plain").build();
106 throw new WebApplicationException(response);
108 if(logger.isDebugEnabled()){
109 logger.debug("getRelation", e);
111 Response response = Response.status(
112 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
113 throw new WebApplicationException(response);
116 if(relation == null){
117 Response response = Response.status(Response.Status.NOT_FOUND).entity(
118 "Get failed, the requested Relation CSID:" + csid + ": was not found.").type(
119 "text/plain").build();
120 throw new WebApplicationException(response);
122 if(logger.isDebugEnabled()){
123 verbose("getRelation: ", relation);
129 public RelationList getRelationList(@Context UriInfo ui) {
130 RelationList relationList = new RelationList();
132 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
133 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
134 RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance();
135 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
136 client.getAll(SERVICE_NAME, handler);
137 relationList = (RelationList) handler.getCommonObjectList();
139 if(logger.isDebugEnabled()){
140 logger.debug("Caught exception in getRelationList", e);
142 Response response = Response.status(
143 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
144 throw new WebApplicationException(response);
151 public Relation updateRelation(
152 @PathParam("csid") String csid,
153 Relation theUpdate) {
154 if(logger.isDebugEnabled()){
155 verbose("updateRelation with csid=" + csid);
157 if(csid == null || "".equals(csid)){
158 logger.error("updateRelation: missing csid!");
159 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
160 "update failed on Relation csid=" + csid).type(
161 "text/plain").build();
162 throw new WebApplicationException(response);
164 if(logger.isDebugEnabled()){
165 verbose("updateRelation with input: ", theUpdate);
168 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
169 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
170 RelationHandlerFactory handlerFactory = RelationHandlerFactory.getInstance();
171 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
172 handler.setCommonObject(theUpdate);
173 client.update(csid, handler);
174 }catch(DocumentNotFoundException dnfe){
175 if(logger.isDebugEnabled()){
176 logger.debug("caugth exception in updateRelation", dnfe);
178 Response response = Response.status(Response.Status.NOT_FOUND).entity(
179 "Update failed on Relation csid=" + csid).type(
180 "text/plain").build();
181 throw new WebApplicationException(response);
183 Response response = Response.status(
184 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
185 throw new WebApplicationException(response);
192 public Response deleteRelation(@PathParam("csid") String csid) {
194 if(logger.isDebugEnabled()){
195 verbose("deleteRelation with csid=" + csid);
197 if(csid == null || "".equals(csid)){
198 logger.error("deleteRelation: missing csid!");
199 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
200 "delete failed on Relation csid=" + csid).type(
201 "text/plain").build();
202 throw new WebApplicationException(response);
205 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
206 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
208 return Response.status(HttpResponseCodes.SC_OK).build();
209 }catch(DocumentNotFoundException dnfe){
210 if(logger.isDebugEnabled()){
211 logger.debug("caught exception in deleteRelation", dnfe);
213 Response response = Response.status(Response.Status.NOT_FOUND).entity(
214 "Delete failed on Relation csid=" + csid).type(
215 "text/plain").build();
216 throw new WebApplicationException(response);
218 Response response = Response.status(
219 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
220 throw new WebApplicationException(response);
225 private void verbose(String msg, Relation relation) {
228 JAXBContext jc = JAXBContext.newInstance(
231 Marshaller m = jc.createMarshaller();
232 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
233 m.marshal(relation, System.out);
240 private void verbose(String msg) {
241 System.out.println("RelationResource. " + msg);