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