]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4dfc50536132fb682c4edba817837d7d2aa6211f
[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
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;
57
58 @Path("/relations")
59 @Consumes("multipart/mixed")
60 @Produces("multipart/mixed")
61 public class NewRelationResource extends AbstractCollectionSpaceResource {
62
63         public final static String serviceName = "relations";
64         final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
65         
66
67     @Override
68     public String getServiceName() {
69         return serviceName;
70     }
71
72     @Override
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);
79             if(obj != null){
80                 docHandler.setCommonPart((CollectionobjectsCommon) obj);
81             }
82         }
83         return docHandler;
84     }
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         /*
117          * Prototype for using NXQL -will move away from here once protyping is finished.
118          */
119         @GET
120         @Path("query/{queryValue}")
121         public Response getQuery(@PathParam("queryValue") String queryString) {
122                 
123                 Response result = null;
124                 
125                 if (logger.isDebugEnabled() == true) {
126                         logger.debug("Query string is: " + queryString);
127                 }
128                 
129                 //getDefaultClient().
130                                 
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();
134                 
135                 return result;
136         }
137
138         @GET
139         @Path("{csid}")
140         public Relation getRelation(@PathParam("csid") String csid) {
141                 if (logger.isDebugEnabled()) {
142                         verbose("getRelation with csid=" + csid);
143                 }
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);
150                 }
151                 Relation relation = null;
152                 try {
153                         getDefaultClient().get(csid, getDefaultHandler());
154                         relation = (Relation) getDefaultHandler().getCommonObject();
155                 } catch (DocumentNotFoundException dnfe) {
156                         if (logger.isDebugEnabled()) {
157                                 logger.debug("getRelation", dnfe);
158                         }
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);
166                         }
167                         Response response = Response.status(
168                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
169                                         .type("text/plain").build();
170                         throw new WebApplicationException(response);
171                 }
172
173                 if (relation == null) {
174                         Response response = Response.status(Response.Status.NOT_FOUND)
175                                         .entity(
176                                                         "Get failed, the requested Relation CSID:" + csid
177                                                                         + ": was not found.").type("text/plain")
178                                         .build();
179                         throw new WebApplicationException(response);
180                 }
181                 if (logger.isDebugEnabled()) {
182                         verbose("getRelation: ", relation);
183                 }
184                 return relation;
185         }
186         
187         /*
188          * BEGIN OF GET LIST
189          */
190
191         @GET
192         public RelationList getRelationList(@Context UriInfo ui) {
193                 return this.getRelationListRequest(null, null, null);
194         }
195
196         @GET
197         @Path("subject/{subjectCsid}")
198         public RelationList getRelationList_S(@Context UriInfo ui,
199                         @PathParam("subjectCsid") String subjectCsid) {
200                 return this.getRelationListRequest(subjectCsid, null, null);
201         }
202         
203         @GET
204         @Path("type/{predicate}")
205         public RelationList getRelationList_P(@Context UriInfo ui,
206                         @PathParam("predicate") String predicate) {
207                 return this.getRelationListRequest(null, predicate, null);
208         }
209
210         @GET
211         @Path("object/{objectCsid}")
212         public RelationList getRelationList_O(@Context UriInfo ui,
213                         @PathParam("objectCsid") String objectCsid) {
214                 return this.getRelationListRequest(null, null, objectCsid);
215         }
216
217         @GET
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);
223         }
224         
225         @GET
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);
231         }
232         
233         @GET
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);
239         }       
240         
241         @GET
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);
247         }
248         
249         @GET
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);
256         }
257
258         @GET
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);
265         }
266         
267         /*
268          * END OF GET LIST
269          */
270         
271         @PUT
272         @Path("{csid}")
273         public Relation updateRelation(@PathParam("csid") String csid,
274                         Relation theUpdate) {
275                 if (logger.isDebugEnabled()) {
276                         verbose("updateRelation with csid=" + csid);
277                 }
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);
284                 }
285                 if (logger.isDebugEnabled()) {
286                         verbose("updateRelation with input: ", theUpdate);
287                 }
288                 try {
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);
294                         }
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);
304                 }
305                 return theUpdate;
306         }
307
308         @DELETE
309         @Path("{csid}")
310         public Response deleteRelation(@PathParam("csid") String csid) {
311
312                 if (logger.isDebugEnabled()) {
313                         verbose("deleteRelation with csid=" + csid);
314                 }
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);
321                 }
322                 try {
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);
328                         }
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);
338                 }
339
340         }
341
342         /*
343          * Private Methods
344          */
345
346         /**
347          * Gets the relation list request.
348          * 
349          * @return the relation list request
350          * 
351          * @throws WebApplicationException the web application exception
352          */
353         private RelationList getRelationListRequest(String subjectCsid,
354                         String predicate,
355                         String objectCsid)
356                                 throws WebApplicationException {
357                 RelationList relationList = new RelationList();
358                 try {
359                         Map propsFromPath = getDefaultHandler().getProperties();
360                         propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
361                         propsFromPath.put(RelationsManager.PREDICATE, predicate);
362                         propsFromPath.put(RelationsManager.OBJECT, objectCsid);
363
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);
369                         }
370                         Response response = Response.status(
371                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
372                                         "Index failed").type("text/plain").build();
373                         throw new WebApplicationException(response);
374                 }
375                 return relationList;
376         }
377
378         private void verbose(String msg, Relation relation) {
379                 try {
380                         verbose(msg);
381                         JAXBContext jc = JAXBContext.newInstance(Relation.class);
382
383                         Marshaller m = jc.createMarshaller();
384                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
385                         m.marshal(relation, System.out);
386                 } catch (Exception e) {
387                         e.printStackTrace();
388                 }
389
390         }
391
392         private void verbose(String msg) {
393                 System.out.println("RelationResource. " + msg);
394         }
395 }