]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2f4f0096243229de39ca7a7b2e5b847a8f5ca885
[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.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;
63
64 @Path("/relations")
65 @Consumes("application/xml")
66 @Produces("application/xml")
67 public class NewRelationResource extends CollectionSpaceResource {
68
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();
74         
75         protected String getClientType() {
76                 return ServiceMain.getInstance().getNuxeoClientType().toString();
77         }
78         
79         protected RepositoryClientFactory getDefaultClientFactory() {
80                 return RepositoryClientFactory.getInstance();
81         }
82         
83         protected CollectionSpaceHandlerFactory getDefaultHandlerFactory() {
84                 return RelationHandlerFactory.getInstance();
85         }
86         
87 //      public NewRelationResource() {
88 //      }
89
90         @POST
91         public Response createRelation(Relation relation) {
92
93                 String csid = null;
94                 try {
95                         getDefaultHandler().setCommonObject(relation);
96                         csid = getDefaultClient().create(SERVICE_NAME, getDefaultHandler());
97                         relation.setCsid(csid);
98                         if (logger.isDebugEnabled()) {
99                                 verbose("createRelation: ", relation);
100                         }
101                         UriBuilder path = UriBuilder.fromResource(RelationResource.class);
102                         path.path("" + csid);
103                         Response response = Response.created(path.build()).build();
104                         return response;
105                 } catch (Exception e) {
106                         if (logger.isDebugEnabled()) {
107                                 logger.debug("Caught exception in createRelation", e);
108                         }
109                         Response response = Response.status(
110                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
111                                         "Create failed").type("text/plain").build();
112                         throw new WebApplicationException(response);
113                 }
114         }
115         
116         @GET
117         @Path("query/{queryValue}")
118         public Response getQuery(@PathParam("queryValue") String queryString) {
119                 
120                 Response result = null;
121                 
122                 if (logger.isDebugEnabled() == true) {
123                         logger.debug("Query string is: " + queryString);
124                 }
125                                 
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();
129                 
130                 return result;
131         }
132
133         @GET
134         @Path("{csid}")
135         public Relation getRelation(@PathParam("csid") String csid) {
136                 if (logger.isDebugEnabled()) {
137                         verbose("getRelation with csid=" + csid);
138                 }
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);
145                 }
146                 Relation relation = null;
147                 try {
148                         getDefaultClient().get(csid, getDefaultHandler());
149                         relation = (Relation) getDefaultHandler().getCommonObject();
150                 } catch (DocumentNotFoundException dnfe) {
151                         if (logger.isDebugEnabled()) {
152                                 logger.debug("getRelation", dnfe);
153                         }
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);
161                         }
162                         Response response = Response.status(
163                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
164                                         .type("text/plain").build();
165                         throw new WebApplicationException(response);
166                 }
167
168                 if (relation == null) {
169                         Response response = Response.status(Response.Status.NOT_FOUND)
170                                         .entity(
171                                                         "Get failed, the requested Relation CSID:" + csid
172                                                                         + ": was not found.").type("text/plain")
173                                         .build();
174                         throw new WebApplicationException(response);
175                 }
176                 if (logger.isDebugEnabled()) {
177                         verbose("getRelation: ", relation);
178                 }
179                 return relation;
180         }
181         
182         /*
183          * BEGIN OF GET LIST
184          */
185
186         @GET
187         public RelationList getRelationList(@Context UriInfo ui) {
188                 return this.getRelationListRequest(null, null, null);
189         }
190
191         @GET
192         @Path("subject/{subjectCsid}")
193         public RelationList getRelationList_S(@Context UriInfo ui,
194                         @PathParam("subjectCsid") String subjectCsid) {
195                 return this.getRelationListRequest(subjectCsid, null, null);
196         }
197         
198         @GET
199         @Path("type/{predicate}")
200         public RelationList getRelationList_P(@Context UriInfo ui,
201                         @PathParam("predicate") String predicate) {
202                 return this.getRelationListRequest(null, predicate, null);
203         }
204
205         @GET
206         @Path("object/{objectCsid}")
207         public RelationList getRelationList_O(@Context UriInfo ui,
208                         @PathParam("objectCsid") String objectCsid) {
209                 return this.getRelationListRequest(null, null, objectCsid);
210         }
211
212         @GET
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);
218         }
219         
220         @GET
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);
226         }
227         
228         @GET
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);
234         }       
235         
236         @GET
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);
242         }
243         
244         @GET
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);
251         }
252
253         @GET
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);
260         }
261         
262         /*
263          * END OF GET LIST
264          */
265         
266         @PUT
267         @Path("{csid}")
268         public Relation updateRelation(@PathParam("csid") String csid,
269                         Relation theUpdate) {
270                 if (logger.isDebugEnabled()) {
271                         verbose("updateRelation with csid=" + csid);
272                 }
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);
279                 }
280                 if (logger.isDebugEnabled()) {
281                         verbose("updateRelation with input: ", theUpdate);
282                 }
283                 try {
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);
289                         }
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);
299                 }
300                 return theUpdate;
301         }
302
303         @DELETE
304         @Path("{csid}")
305         public Response deleteRelation(@PathParam("csid") String csid) {
306
307                 if (logger.isDebugEnabled()) {
308                         verbose("deleteRelation with csid=" + csid);
309                 }
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);
316                 }
317                 try {
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);
323                         }
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);
333                 }
334
335         }
336
337         /*
338          * Private Methods
339          */
340
341         /**
342          * Gets the relation list request.
343          * 
344          * @return the relation list request
345          * 
346          * @throws WebApplicationException the web application exception
347          */
348         private RelationList getRelationListRequest(String subjectCsid,
349                         String predicate,
350                         String objectCsid)
351                                 throws WebApplicationException {
352                 RelationList relationList = new RelationList();
353                 try {
354                         Map propsFromPath = getDefaultHandler().getProperties();
355                         propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
356                         propsFromPath.put(RelationsManager.PREDICATE, predicate);
357                         propsFromPath.put(RelationsManager.OBJECT, objectCsid);
358
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);
364                         }
365                         Response response = Response.status(
366                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
367                                         "Index failed").type("text/plain").build();
368                         throw new WebApplicationException(response);
369                 }
370                 return relationList;
371         }
372
373         private void verbose(String msg, Relation relation) {
374                 try {
375                         verbose(msg);
376                         JAXBContext jc = JAXBContext.newInstance(Relation.class);
377
378                         Marshaller m = jc.createMarshaller();
379                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
380                         m.marshal(relation, System.out);
381                 } catch (Exception e) {
382                         e.printStackTrace();
383                 }
384
385         }
386
387         private void verbose(String msg) {
388                 System.out.println("RelationResource. " + msg);
389         }
390 }