]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6763ff0bb25f3f5fc4f8652d30b5d648810b7e94
[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
45
46 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
47 import org.collectionspace.services.common.context.MultipartServiceContext;
48 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.relation.IRelationsManager;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
56 import org.jboss.resteasy.util.HttpResponseCodes;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 @Path("/relations")
61 @Consumes("multipart/mixed")
62 @Produces("multipart/mixed")
63 public class NewRelationResource extends AbstractCollectionSpaceResource {
64
65     public final static String serviceName = "relations";
66     final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
67
68     @Override
69     protected String getVersionString() {
70         /** The last change revision. */
71         final String lastChangeRevision = "$LastChangedRevision$";
72         return lastChangeRevision;
73     }
74     
75     @Override
76     public String getServiceName() {
77         return serviceName;
78     }
79
80     @Override
81     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
82         DocumentHandler docHandler = ctx.getDocumentHandler();
83         if (ctx.getInput() != null) {
84             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class);
85             if (obj != null) {
86                 docHandler.setCommonPart((RelationsCommon) obj);
87             }
88         }
89         return docHandler;
90     }
91
92     @POST
93     public Response createRelation(MultipartInput input) {
94
95         try {
96             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
97             DocumentHandler handler = createDocumentHandler(ctx);
98             String csid = getRepositoryClient(ctx).create(ctx, handler);
99             UriBuilder path = UriBuilder.fromResource(NewRelationResource.class);
100             path.path("" + csid);
101             Response response = Response.created(path.build()).build();
102             return response;
103         } catch (UnauthorizedException ue) {
104             Response response = Response.status(
105                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
106             throw new WebApplicationException(response);
107         } catch (Exception e) {
108             if (logger.isDebugEnabled()) {
109                 logger.debug("Caught exception in createRelation", e);
110             }
111             Response response = Response.status(
112                     Response.Status.INTERNAL_SERVER_ERROR).entity(
113                     "Create failed").type("text/plain").build();
114             throw new WebApplicationException(response);
115         }
116     }
117
118     @GET
119     @Path("{csid}")
120     public MultipartOutput getRelation(@PathParam("csid") String csid) {
121         if (logger.isDebugEnabled()) {
122             logger.debug("getRelation with csid=" + csid);
123         }
124         if (csid == null || "".equals(csid)) {
125             logger.error("getRelation: missing csid!");
126             Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type(
127                     "text/plain").build();
128             throw new WebApplicationException(response);
129         }
130         MultipartOutput result = null;
131         try {
132             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
133             DocumentHandler handler = createDocumentHandler(ctx);
134             getRepositoryClient(ctx).get(ctx, csid, handler);
135             result = (MultipartOutput) ctx.getOutput();
136         } catch (UnauthorizedException ue) {
137             Response response = Response.status(
138                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
139             throw new WebApplicationException(response);
140         } catch (DocumentNotFoundException dnfe) {
141             if (logger.isDebugEnabled()) {
142                 logger.debug("getRelation", dnfe);
143             }
144             Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type(
145                     "text/plain").build();
146             throw new WebApplicationException(response);
147         } catch (Exception e) {
148             if (logger.isDebugEnabled()) {
149                 logger.debug("getRelation", e);
150             }
151             Response response = Response.status(
152                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
153             throw new WebApplicationException(response);
154         }
155
156         if (result == null) {
157             Response response = Response.status(Response.Status.NOT_FOUND).entity(
158                     "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build();
159             throw new WebApplicationException(response);
160         }
161         return result;
162     }
163
164     /*
165      * BEGIN OF GET LIST
166      */
167     @GET
168     @Produces("application/xml")
169     public RelationsCommonList getRelationList(@Context UriInfo ui) {
170         return this.getRelationList(null, null, null);
171     }
172
173     @GET
174     @Path("subject/{subjectCsid}")
175     @Produces("application/xml")
176     public RelationsCommonList getRelationList_S(@Context UriInfo ui,
177             @PathParam("subjectCsid") String subjectCsid) {
178         return this.getRelationList(subjectCsid, null, null);
179     }
180
181     @GET
182     @Path("type/{predicate}")
183     @Produces("application/xml")
184     public RelationsCommonList getRelationList_P(@Context UriInfo ui,
185             @PathParam("predicate") String predicate) {
186         return this.getRelationList(null, predicate, null);
187     }
188
189     @GET
190     @Path("object/{objectCsid}")
191     @Produces("application/xml")
192     public RelationsCommonList getRelationList_O(@Context UriInfo ui,
193             @PathParam("objectCsid") String objectCsid) {
194         return this.getRelationList(null, null, objectCsid);
195     }
196
197     @GET
198     @Path("type/{predicate}/subject/{subjectCsid}")
199     @Produces("application/xml")
200     public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
201             @PathParam("predicate") String predicate,
202             @PathParam("subjectCsid") String subjectCsid) {
203         return this.getRelationList(subjectCsid, predicate, null);
204     }
205
206     @GET
207     @Path("subject/{subjectCsid}/type/{predicate}")
208     @Produces("application/xml")
209     public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
210             @PathParam("subjectCsid") String subjectCsid,
211             @PathParam("predicate") String predicate) {
212         return this.getRelationList(subjectCsid, predicate, null);
213     }
214
215     @GET
216     @Path("type/{predicate}/object/{objectCsid}")
217     @Produces("application/xml")
218     public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
219             @PathParam("predicate") String predicate,
220             @PathParam("objectCsid") String objectCsid) {
221         return this.getRelationList(null, predicate, objectCsid);
222     }
223
224     @GET
225     @Path("object/{objectCsid}/type/{predicate}")
226     @Produces("application/xml")
227     public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
228             @PathParam("objectCsid") String objectCsid,
229             @PathParam("predicate") String predicate) {
230         return this.getRelationList(null, predicate, objectCsid);
231     }
232
233     @GET
234     @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
235     @Produces("application/xml")
236     public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
237             @PathParam("predicate") String predicate,
238             @PathParam("subjectCsid") String subjectCsid,
239             @PathParam("objectCsid") String objectCsid) {
240         return this.getRelationList(subjectCsid, predicate, objectCsid);
241     }
242
243     @GET
244     @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
245     @Produces("application/xml")
246     public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
247             @PathParam("subjectCsid") String subjectCsid,
248             @PathParam("predicate") String predicate,
249             @PathParam("objectCsid") String objectCsid) {
250         return this.getRelationList(subjectCsid, predicate, objectCsid);
251     }
252     /*
253      * END OF GET LIST
254      */
255
256     @PUT
257     @Path("{csid}")
258     public MultipartOutput updateRelation(@PathParam("csid") String csid,
259             MultipartInput theUpdate) {
260         if (logger.isDebugEnabled()) {
261             logger.debug("updateRelation with csid=" + csid);
262         }
263         if (csid == null || "".equals(csid)) {
264             logger.error("updateRelation: missing csid!");
265             Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type(
266                     "text/plain").build();
267             throw new WebApplicationException(response);
268         }
269         MultipartOutput result = null;
270         try {
271             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
272             DocumentHandler handler = createDocumentHandler(ctx);
273             getRepositoryClient(ctx).update(ctx, csid, handler);
274             result = (MultipartOutput) ctx.getOutput();
275         } catch (UnauthorizedException ue) {
276             Response response = Response.status(
277                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
278             throw new WebApplicationException(response);
279         } catch (DocumentNotFoundException dnfe) {
280             if (logger.isDebugEnabled()) {
281                 logger.debug("caugth exception in updateRelation", dnfe);
282             }
283             Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type(
284                     "text/plain").build();
285             throw new WebApplicationException(response);
286         } catch (Exception e) {
287             Response response = Response.status(
288                     Response.Status.INTERNAL_SERVER_ERROR).entity(
289                     "Update failed").type("text/plain").build();
290             throw new WebApplicationException(response);
291         }
292         return result;
293     }
294
295     @DELETE
296     @Path("{csid}")
297     public Response deleteRelation(@PathParam("csid") String csid) {
298
299         if (logger.isDebugEnabled()) {
300             logger.debug("deleteRelation with csid=" + csid);
301         }
302         if (csid == null || "".equals(csid)) {
303             logger.error("deleteRelation: missing csid!");
304             Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type(
305                     "text/plain").build();
306             throw new WebApplicationException(response);
307         }
308         try {
309             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
310             getRepositoryClient(ctx).delete(ctx, csid);
311             return Response.status(HttpResponseCodes.SC_OK).build();
312         } catch (UnauthorizedException ue) {
313             Response response = Response.status(
314                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
315             throw new WebApplicationException(response);
316         } catch (DocumentNotFoundException dnfe) {
317             if (logger.isDebugEnabled()) {
318                 logger.debug("caught exception in deleteRelation", dnfe);
319             }
320             Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type(
321                     "text/plain").build();
322             throw new WebApplicationException(response);
323         } catch (Exception e) {
324             Response response = Response.status(
325                     Response.Status.INTERNAL_SERVER_ERROR).entity(
326                     "Delete failed").type("text/plain").build();
327             throw new WebApplicationException(response);
328         }
329
330     }
331
332     /*
333      * Private Methods
334      */
335     /**
336      * Gets the relation list request.
337      *
338      * @return the relation list request
339      *
340      * @throws WebApplicationException the web application exception
341      */
342     private RelationsCommonList getRelationList(String subjectCsid,
343             String predicate,
344             String objectCsid)
345             throws WebApplicationException {
346         RelationsCommonList relationList = new RelationsCommonList();
347         try {
348             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
349             DocumentHandler handler = createDocumentHandler(ctx);
350             Map propsFromPath = handler.getProperties();
351             propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid);
352             propsFromPath.put(IRelationsManager.PREDICATE, predicate);
353             propsFromPath.put(IRelationsManager.OBJECT, objectCsid);
354             getRepositoryClient(ctx).getAll(ctx, handler);
355             relationList = (RelationsCommonList) handler.getCommonPartList();
356         } catch (UnauthorizedException ue) {
357             Response response = Response.status(
358                     Response.Status.UNAUTHORIZED).entity("Get relations failed reason " + ue.getErrorReason()).type("text/plain").build();
359             throw new WebApplicationException(response);
360         } catch (Exception e) {
361             if (logger.isDebugEnabled()) {
362                 logger.debug("Caught exception in getRelationList", e);
363             }
364             Response response = Response.status(
365                     Response.Status.INTERNAL_SERVER_ERROR).entity(
366                     "Index failed").type("text/plain").build();
367             throw new WebApplicationException(response);
368         }
369         return relationList;
370     }
371 }