]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f77645fb725283087affec7c5087d0ea9ce9ab40
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.relation;
2
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;
18
19 import org.collectionspace.services.relation.RelationList.RelationListItem;
20
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;
32
33 @Path("/relations")
34 @Consumes("application/xml")
35 @Produces("application/xml")
36 public class NewRelationResource {
37
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();
42
43     public NewRelationResource() {
44         // do nothing
45     }
46
47     @POST
48     public Response createRelation(
49             Relation relation) {
50
51         String csid = null;
52         try{
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);
62             }
63             UriBuilder path = UriBuilder.fromResource(RelationResource.class);
64             path.path("" + csid);
65             Response response = Response.created(path.build()).build();
66             return response;
67         }catch(Exception e){
68             if(logger.isDebugEnabled()){
69                 logger.debug("Caught exception in createRelation", e);
70             }
71             Response response = Response.status(
72                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
73             throw new WebApplicationException(response);
74         }
75     }
76
77     @GET
78     @Path("{csid}")
79     public Relation getRelation(
80             @PathParam("csid") String csid) {
81         if(logger.isDebugEnabled()){
82             verbose("getRelation with csid=" + csid);
83         }
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);
90         }
91         Relation relation = null;
92         try{
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);
102             }
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);
107         }catch(Exception e){
108             if(logger.isDebugEnabled()){
109                 logger.debug("getRelation", e);
110             }
111             Response response = Response.status(
112                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
113             throw new WebApplicationException(response);
114         }
115
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);
121         }
122         if(logger.isDebugEnabled()){
123             verbose("getRelation: ", relation);
124         }
125         return relation;
126     }
127
128     @GET
129     public RelationList getRelationList(@Context UriInfo ui) {
130         RelationList relationList = new RelationList();
131         try{
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();
138         }catch(Exception e){
139             if(logger.isDebugEnabled()){
140                 logger.debug("Caught exception in getRelationList", e);
141             }
142             Response response = Response.status(
143                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
144             throw new WebApplicationException(response);
145         }
146         return relationList;
147     }
148
149     @PUT
150     @Path("{csid}")
151     public Relation updateRelation(
152             @PathParam("csid") String csid,
153             Relation theUpdate) {
154         if(logger.isDebugEnabled()){
155             verbose("updateRelation with csid=" + csid);
156         }
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);
163         }
164         if(logger.isDebugEnabled()){
165             verbose("updateRelation with input: ", theUpdate);
166         }
167         try{
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);
177             }
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);
182         }catch(Exception e){
183             Response response = Response.status(
184                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
185             throw new WebApplicationException(response);
186         }
187         return theUpdate;
188     }
189
190     @DELETE
191     @Path("{csid}")
192     public Response deleteRelation(@PathParam("csid") String csid) {
193
194         if(logger.isDebugEnabled()){
195             verbose("deleteRelation with csid=" + csid);
196         }
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);
203         }
204         try{
205             RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
206             RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
207             client.delete(csid);
208             return Response.status(HttpResponseCodes.SC_OK).build();
209         }catch(DocumentNotFoundException dnfe){
210             if(logger.isDebugEnabled()){
211                 logger.debug("caught exception in deleteRelation", dnfe);
212             }
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);
217         }catch(Exception e){
218             Response response = Response.status(
219                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
220             throw new WebApplicationException(response);
221         }
222
223     }
224
225     private void verbose(String msg, Relation relation) {
226         try{
227             verbose(msg);
228             JAXBContext jc = JAXBContext.newInstance(
229                     Relation.class);
230
231             Marshaller m = jc.createMarshaller();
232             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
233             m.marshal(relation, System.out);
234         }catch(Exception e){
235             e.printStackTrace();
236         }
237
238     }
239
240     private void verbose(String msg) {
241         System.out.println("RelationResource. " + msg);
242     }
243 }