]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c4874470ab091502320f5dc80956a625b06d0699
[tmp/jakarta-migration.git] /
1 /**     
2  * NewRelationResource.java
3  *
4  * {Purpose of This Class}
5  *
6  * {Other Notes Relating to This Class (Optional)}
7  *
8  * $LastChangedBy: $
9  * $LastChangedRevision: $
10  * $LastChangedDate: $
11  *
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:
15  *
16  * http://www.collectionspace.org
17  * http://wiki.collectionspace.org
18  *
19  * Copyright © 2009 {Contributing Institution}
20  *
21  * Licensed under the Educational Community License (ECL), Version 2.0.
22  * You may not use this file except in compliance with this License.
23  *
24  * You may obtain a copy of the ECL 2.0 License at
25  * https://source.collectionspace.org/collection-space/LICENSE.txt
26  */
27 package org.collectionspace.services.relation;
28
29 import java.util.Map;
30
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;
46
47 import org.collectionspace.services.relation.RelationList.RelationListItem;
48
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;
61
62 @Path("/relations")
63 @Consumes("application/xml")
64 @Produces("application/xml")
65 public class NewRelationResource {
66
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();
72
73         public NewRelationResource() {
74                 // do nothing
75         }
76
77         @POST
78         public Response createRelation(Relation relation) {
79
80                 String csid = null;
81                 try {
82                         RepositoryClientFactory clientFactory = RepositoryClientFactory
83                                         .getInstance();
84                         RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
85                                         .toString());
86                         RelationHandlerFactory handlerFactory = RelationHandlerFactory
87                                         .getInstance();
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);
95                         }
96                         UriBuilder path = UriBuilder.fromResource(RelationResource.class);
97                         path.path("" + csid);
98                         Response response = Response.created(path.build()).build();
99                         return response;
100                 } catch (Exception e) {
101                         if (logger.isDebugEnabled()) {
102                                 logger.debug("Caught exception in createRelation", e);
103                         }
104                         Response response = Response.status(
105                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
106                                         "Create failed").type("text/plain").build();
107                         throw new WebApplicationException(response);
108                 }
109         }
110
111         @GET
112         @Path("{csid}")
113         public Relation getRelation(@PathParam("csid") String csid) {
114                 if (logger.isDebugEnabled()) {
115                         verbose("getRelation with csid=" + csid);
116                 }
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);
123                 }
124                 Relation relation = null;
125                 try {
126                         RepositoryClientFactory clientFactory = RepositoryClientFactory
127                                         .getInstance();
128                         RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
129                                         .toString());
130                         RelationHandlerFactory handlerFactory = RelationHandlerFactory
131                                         .getInstance();
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);
139                         }
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);
147                         }
148                         Response response = Response.status(
149                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
150                                         .type("text/plain").build();
151                         throw new WebApplicationException(response);
152                 }
153
154                 if (relation == null) {
155                         Response response = Response.status(Response.Status.NOT_FOUND)
156                                         .entity(
157                                                         "Get failed, the requested Relation CSID:" + csid
158                                                                         + ": was not found.").type("text/plain")
159                                         .build();
160                         throw new WebApplicationException(response);
161                 }
162                 if (logger.isDebugEnabled()) {
163                         verbose("getRelation: ", relation);
164                 }
165                 return relation;
166         }
167         
168         /*
169          * BEGIN OF GET LIST
170          */
171
172         @GET
173         public RelationList getRelationList(@Context UriInfo ui) {
174                 return this.getRelationListRequest(null, null, null);
175         }
176
177         @GET
178         @Path("subject/{subjectCsid}")
179         public RelationList getRelationList_S(@Context UriInfo ui,
180                         @PathParam("subjectCsid") String subjectCsid) {
181                 return this.getRelationListRequest(subjectCsid, null, null);
182         }
183         
184         @GET
185         @Path("type/{predicate}")
186         public RelationList getRelationList_P(@Context UriInfo ui,
187                         @PathParam("predicate") String predicate) {
188                 return this.getRelationListRequest(null, predicate, null);
189         }
190
191         @GET
192         @Path("object/{objectCsid}")
193         public RelationList getRelationList_O(@Context UriInfo ui,
194                         @PathParam("objectCsid") String objectCsid) {
195                 return this.getRelationListRequest(null, null, objectCsid);
196         }
197
198         @GET
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);
204         }
205         
206         @GET
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);
212         }
213         
214         @GET
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);
220         }       
221         
222         @GET
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);
228         }
229         
230         @GET
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);
237         }
238
239         @GET
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);
246         }
247         
248         /*
249          * END OF GET LIST
250          */
251         
252         @PUT
253         @Path("{csid}")
254         public Relation updateRelation(@PathParam("csid") String csid,
255                         Relation theUpdate) {
256                 if (logger.isDebugEnabled()) {
257                         verbose("updateRelation with csid=" + csid);
258                 }
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);
265                 }
266                 if (logger.isDebugEnabled()) {
267                         verbose("updateRelation with input: ", theUpdate);
268                 }
269                 try {
270                         RepositoryClientFactory clientFactory = RepositoryClientFactory
271                                         .getInstance();
272                         RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
273                                         .toString());
274                         RelationHandlerFactory handlerFactory = RelationHandlerFactory
275                                         .getInstance();
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);
283                         }
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);
293                 }
294                 return theUpdate;
295         }
296
297         @DELETE
298         @Path("{csid}")
299         public Response deleteRelation(@PathParam("csid") String csid) {
300
301                 if (logger.isDebugEnabled()) {
302                         verbose("deleteRelation with csid=" + csid);
303                 }
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);
310                 }
311                 try {
312                         RepositoryClientFactory clientFactory = RepositoryClientFactory
313                                         .getInstance();
314                         RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
315                                         .toString());
316                         client.delete(csid);
317                         return Response.status(HttpResponseCodes.SC_OK).build();
318                 } catch (DocumentNotFoundException dnfe) {
319                         if (logger.isDebugEnabled()) {
320                                 logger.debug("caught exception in deleteRelation", dnfe);
321                         }
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);
331                 }
332
333         }
334
335         /*
336          * Private Methods
337          */
338
339         /**
340          * Gets the relation list request.
341          * 
342          * @return the relation list request
343          * 
344          * @throws WebApplicationException the web application exception
345          */
346         private RelationList getRelationListRequest(String subjectCsid,
347                         String predicate,
348                         String objectCsid)
349                                 throws WebApplicationException {
350                 RelationList relationList = new RelationList();
351                 try {
352                         RepositoryClientFactory clientFactory = RepositoryClientFactory
353                                         .getInstance();
354                         RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
355                                         .toString());
356                         RelationHandlerFactory handlerFactory = RelationHandlerFactory
357                                         .getInstance();
358                         DocumentHandler handler = (DocumentHandler) handlerFactory
359                                         .getHandler(CLIENT_TYPE.toString());
360
361                         Map propsFromPath = handler.getProperties();
362                         propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
363                         propsFromPath.put(RelationsManager.PREDICATE, predicate);
364                         propsFromPath.put(RelationsManager.OBJECT, objectCsid);
365
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);
371                         }
372                         Response response = Response.status(
373                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
374                                         "Index failed").type("text/plain").build();
375                         throw new WebApplicationException(response);
376                 }
377                 return relationList;
378         }
379
380         private void verbose(String msg, Relation relation) {
381                 try {
382                         verbose(msg);
383                         JAXBContext jc = JAXBContext.newInstance(Relation.class);
384
385                         Marshaller m = jc.createMarshaller();
386                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
387                         m.marshal(relation, System.out);
388                 } catch (Exception e) {
389                         e.printStackTrace();
390                 }
391
392         }
393
394         private void verbose(String msg) {
395                 System.out.println("RelationResource. " + msg);
396         }
397 }