]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
175d52407ea15ce207e6ca7d511f08e6033963c9
[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 javax.ws.rs.Consumes;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
43
44 import org.collectionspace.services.common.query.IQueryManager;
45 import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;
46 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.security.UnauthorizedException;
51
52 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
53 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
54 import org.jboss.resteasy.util.HttpResponseCodes;
55
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * The Class NewRelationResource.
61  */
62 @Path("/relations")
63 @Consumes("multipart/mixed")
64 @Produces("multipart/mixed")
65 public class NewRelationResource extends
66                 AbstractMultiPartCollectionSpaceResourceImpl {
67
68         /** The Constant serviceName. */
69         public final static String serviceName = "relations";
70         
71         /** The logger. */
72         final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
73
74         /* (non-Javadoc)
75          * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
76          */
77         @Override
78         protected String getVersionString() {
79                 /** The last change revision. */
80                 final String lastChangeRevision = "$LastChangedRevision$";
81                 return lastChangeRevision;
82         }
83
84         /* (non-Javadoc)
85          * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
86          */
87         @Override
88         public String getServiceName() {
89                 return serviceName;
90         }
91
92         /* (non-Javadoc)
93          * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
94          */
95         @Override
96     public Class<RelationsCommon> getCommonPartClass() {
97         return RelationsCommon.class;
98     }
99
100         /**
101          * Creates the relation.
102          * 
103          * @param input the input
104          * 
105          * @return the response
106          */
107         @POST
108         public Response createRelation(MultipartInput input) {
109                 try {
110                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
111                         DocumentHandler handler = createDocumentHandler(ctx);
112                         String csid = getRepositoryClient(ctx).create(ctx, handler);
113                         UriBuilder path = UriBuilder
114                                         .fromResource(NewRelationResource.class);
115                         path.path("" + csid);
116                         Response response = Response.created(path.build()).build();
117                         return response;
118                 } catch (UnauthorizedException ue) {
119                         Response response = Response.status(Response.Status.UNAUTHORIZED)
120                                         .entity("Create failed reason " + ue.getErrorReason())
121                                         .type("text/plain").build();
122                         throw new WebApplicationException(response);
123                 } catch (Exception e) {
124                         if (logger.isDebugEnabled()) {
125                                 logger.debug("Caught exception in createRelation", e);
126                         }
127                         Response response = Response.status(
128                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
129                                         "Create failed").type("text/plain").build();
130                         throw new WebApplicationException(response);
131                 }
132         }
133         
134         /**
135          * Gets the relation.
136          *
137          * @param ui the ui
138          * @param csid the csid
139          * @return the relation
140          */
141         @GET
142         @Path("{csid}")
143         public MultipartOutput getRelation(@Context UriInfo ui,
144                         @PathParam("csid") String csid) {
145                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
146                 if (logger.isDebugEnabled()) {
147                         logger.debug("getRelation with csid=" + csid);
148                 }
149                 if (csid == null || "".equals(csid)) {
150                         logger.error("getRelation: missing csid!");
151                         Response response = Response.status(Response.Status.BAD_REQUEST)
152                                         .entity("get failed on Relation csid=" + csid).type(
153                                                         "text/plain").build();
154                         throw new WebApplicationException(response);
155                 }
156                 MultipartOutput result = null;
157                 try {
158                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
159                         DocumentHandler handler = createDocumentHandler(ctx);
160                         getRepositoryClient(ctx).get(ctx, csid, handler);
161                         result = (MultipartOutput) ctx.getOutput();
162                 } catch (UnauthorizedException ue) {
163                         Response response = Response.status(Response.Status.UNAUTHORIZED)
164                                         .entity("Get failed reason " + ue.getErrorReason()).type(
165                                                         "text/plain").build();
166                         throw new WebApplicationException(response);
167                 } catch (DocumentNotFoundException dnfe) {
168                         if (logger.isDebugEnabled()) {
169                                 logger.debug("getRelation", dnfe);
170                         }
171                         Response response = Response.status(Response.Status.NOT_FOUND)
172                                         .entity("Get failed on Relation csid=" + csid).type(
173                                                         "text/plain").build();
174                         throw new WebApplicationException(response);
175                 } catch (Exception e) {
176                         if (logger.isDebugEnabled()) {
177                                 logger.debug("getRelation", e);
178                         }
179                         Response response = Response.status(
180                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
181                                         .type("text/plain").build();
182                         throw new WebApplicationException(response);
183                 }
184
185                 if (result == null) {
186                         Response response = Response.status(Response.Status.NOT_FOUND)
187                                         .entity(
188                                                         "Get failed, the requested Relation CSID:" + csid
189                                                                         + ": was not found.").type("text/plain")
190                                         .build();
191                         throw new WebApplicationException(response);
192                 }
193                 return result;
194         }
195
196         /*
197          * BEGIN OF GET LIST
198          */
199         /**
200          * Gets the relation list.
201          * 
202          * @param ui the ui
203          * 
204          * @return the relation list
205          */
206         @GET
207         @Produces("application/xml")
208         public RelationsCommonList getRelationList(@Context UriInfo ui) {
209                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
210                 return this.getRelationList(queryParams, null, null, null);
211         }
212
213         /**
214          * Gets the relation list_ s.
215          * 
216          * @param ui the ui
217          * @param subjectCsid the subject csid
218          * 
219          * @return the relation list_ s
220          */
221         @GET
222         @Path("subject/{subjectCsid}")
223         @Produces("application/xml")
224         public RelationsCommonList getRelationList_S(@Context UriInfo ui,
225                         @PathParam("subjectCsid") String subjectCsid) {
226                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
227                 return this.getRelationList(queryParams, subjectCsid, null, null);
228         }
229
230         /**
231          * Gets the relation list_ p.
232          * 
233          * @param ui the ui
234          * @param predicate the predicate
235          * 
236          * @return the relation list_ p
237          */
238         @GET
239         @Path("type/{predicate}")
240         @Produces("application/xml")
241         public RelationsCommonList getRelationList_P(@Context UriInfo ui,
242                         @PathParam("predicate") String predicate) {
243                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
244                 return this.getRelationList(queryParams, null, predicate, null);
245         }
246
247         /**
248          * Gets the relation list_ o.
249          * 
250          * @param ui the ui
251          * @param objectCsid the object csid
252          * 
253          * @return the relation list_ o
254          */
255         @GET
256         @Path("object/{objectCsid}")
257         @Produces("application/xml")
258         public RelationsCommonList getRelationList_O(@Context UriInfo ui,
259                         @PathParam("objectCsid") String objectCsid) {
260                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
261                 return this.getRelationList(queryParams, null, null, objectCsid);
262         }
263
264         /**
265          * Gets the relation list_ ps.
266          * 
267          * @param ui the ui
268          * @param predicate the predicate
269          * @param subjectCsid the subject csid
270          * 
271          * @return the relation list_ ps
272          */
273         @GET
274         @Path("type/{predicate}/subject/{subjectCsid}")
275         @Produces("application/xml")
276         public RelationsCommonList getRelationList_PS(@Context UriInfo ui,
277                         @PathParam("predicate") String predicate,
278                         @PathParam("subjectCsid") String subjectCsid) {
279                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
280                 return this.getRelationList(queryParams, subjectCsid, predicate, null);
281         }
282
283         /**
284          * Gets the relation list_ sp.
285          * 
286          * @param ui the ui
287          * @param subjectCsid the subject csid
288          * @param predicate the predicate
289          * 
290          * @return the relation list_ sp
291          */
292         @GET
293         @Path("subject/{subjectCsid}/type/{predicate}")
294         @Produces("application/xml")
295         public RelationsCommonList getRelationList_SP(@Context UriInfo ui,
296                         @PathParam("subjectCsid") String subjectCsid,
297                         @PathParam("predicate") String predicate) {
298                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
299                 return this.getRelationList(queryParams, subjectCsid, predicate, null);
300         }
301
302         /**
303          * Gets the relation list_ po.
304          * 
305          * @param ui the ui
306          * @param predicate the predicate
307          * @param objectCsid the object csid
308          * 
309          * @return the relation list_ po
310          */
311         @GET
312         @Path("type/{predicate}/object/{objectCsid}")
313         @Produces("application/xml")
314         public RelationsCommonList getRelationList_PO(@Context UriInfo ui,
315                         @PathParam("predicate") String predicate,
316                         @PathParam("objectCsid") String objectCsid) {
317                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
318                 return this.getRelationList(queryParams, null, predicate, objectCsid);
319         }
320
321         /**
322          * Gets the relation list_ op.
323          * 
324          * @param ui the ui
325          * @param objectCsid the object csid
326          * @param predicate the predicate
327          * 
328          * @return the relation list_ op
329          */
330         @GET
331         @Path("object/{objectCsid}/type/{predicate}")
332         @Produces("application/xml")
333         public RelationsCommonList getRelationList_OP(@Context UriInfo ui,
334                         @PathParam("objectCsid") String objectCsid,
335                         @PathParam("predicate") String predicate) {
336                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
337                 return this.getRelationList(queryParams, null, predicate, objectCsid);
338         }
339
340         /**
341          * Gets the relation list_ pso.
342          * 
343          * @param ui the ui
344          * @param predicate the predicate
345          * @param subjectCsid the subject csid
346          * @param objectCsid the object csid
347          * 
348          * @return the relation list_ pso
349          */
350         @GET
351         @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
352         @Produces("application/xml")
353         public RelationsCommonList getRelationList_PSO(@Context UriInfo ui,
354                         @PathParam("predicate") String predicate,
355                         @PathParam("subjectCsid") String subjectCsid,
356                         @PathParam("objectCsid") String objectCsid) {
357                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
358                 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
359         }
360
361         /**
362          * Gets the relation list_ spo.
363          * 
364          * @param ui the ui
365          * @param subjectCsid the subject csid
366          * @param predicate the predicate
367          * @param objectCsid the object csid
368          * 
369          * @return the relation list_ spo
370          */
371         @GET
372         @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
373         @Produces("application/xml")
374         public RelationsCommonList getRelationList_SPO(@Context UriInfo ui,
375                         @PathParam("subjectCsid") String subjectCsid,
376                         @PathParam("predicate") String predicate,
377                         @PathParam("objectCsid") String objectCsid) {
378                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
379                 return this.getRelationList(queryParams, subjectCsid, predicate, objectCsid);
380         }
381
382         /*
383          * END OF GET LIST
384          */
385
386         /**
387          * Update relation.
388          * 
389          * @param csid the csid
390          * @param theUpdate the the update
391          * 
392          * @return the multipart output
393          */
394         @PUT
395         @Path("{csid}")
396         public MultipartOutput updateRelation(@PathParam("csid") String csid,
397                         MultipartInput theUpdate) {
398                 if (logger.isDebugEnabled()) {
399                         logger.debug("updateRelation with csid=" + csid);
400                 }
401                 if (csid == null || "".equals(csid)) {
402                         logger.error("updateRelation: missing csid!");
403                         Response response = Response.status(Response.Status.BAD_REQUEST)
404                                         .entity("update failed on Relation csid=" + csid).type(
405                                                         "text/plain").build();
406                         throw new WebApplicationException(response);
407                 }
408                 MultipartOutput result = null;
409                 try {
410                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
411                         DocumentHandler handler = createDocumentHandler(ctx);
412                         getRepositoryClient(ctx).update(ctx, csid, handler);
413                         result = (MultipartOutput) ctx.getOutput();
414                 } catch (UnauthorizedException ue) {
415                         Response response = Response.status(Response.Status.UNAUTHORIZED)
416                                         .entity("Update failed reason " + ue.getErrorReason())
417                                         .type("text/plain").build();
418                         throw new WebApplicationException(response);
419                 } catch (DocumentNotFoundException dnfe) {
420                         if (logger.isDebugEnabled()) {
421                                 logger.debug("caugth exception in updateRelation", dnfe);
422                         }
423                         Response response = Response.status(Response.Status.NOT_FOUND)
424                                         .entity("Update failed on Relation csid=" + csid).type(
425                                                         "text/plain").build();
426                         throw new WebApplicationException(response);
427                 } catch (Exception e) {
428                         Response response = Response.status(
429                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
430                                         "Update failed").type("text/plain").build();
431                         throw new WebApplicationException(response);
432                 }
433                 return result;
434         }
435
436         /**
437          * Delete relation.
438          * 
439          * @param csid the csid
440          * 
441          * @return the response
442          */
443         @DELETE
444         @Path("{csid}")
445         public Response deleteRelation(@PathParam("csid") String csid) {
446
447                 if (logger.isDebugEnabled()) {
448                         logger.debug("deleteRelation with csid=" + csid);
449                 }
450                 if (csid == null || "".equals(csid)) {
451                         logger.error("deleteRelation: missing csid!");
452                         Response response = Response.status(Response.Status.BAD_REQUEST)
453                                         .entity("delete failed on Relation csid=" + csid).type(
454                                                         "text/plain").build();
455                         throw new WebApplicationException(response);
456                 }
457                 try {
458                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
459                         getRepositoryClient(ctx).delete(ctx, csid);
460                         return Response.status(HttpResponseCodes.SC_OK).build();
461                 } catch (UnauthorizedException ue) {
462                         Response response = Response.status(Response.Status.UNAUTHORIZED)
463                                         .entity("Delete failed reason " + ue.getErrorReason())
464                                         .type("text/plain").build();
465                         throw new WebApplicationException(response);
466                 } catch (DocumentNotFoundException dnfe) {
467                         if (logger.isDebugEnabled()) {
468                                 logger.debug("caught exception in deleteRelation", dnfe);
469                         }
470                         Response response = Response.status(Response.Status.NOT_FOUND)
471                                         .entity("Delete failed on Relation csid=" + csid).type(
472                                                         "text/plain").build();
473                         throw new WebApplicationException(response);
474                 } catch (Exception e) {
475                         Response response = Response.status(
476                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
477                                         "Delete failed").type("text/plain").build();
478                         throw new WebApplicationException(response);
479                 }
480
481         }
482
483         /**
484          * Gets the relation list.
485          *
486          * @param queryParams the query params
487          * @param subjectCsid the subject csid
488          * @param predicate the predicate
489          * @param objectCsid the object csid
490          * @return the relation list
491          * @throws WebApplicationException the web application exception
492          */
493         public RelationsCommonList getRelationList(
494                         MultivaluedMap<String, String> queryParams,
495                         String subjectCsid,
496                         String predicate, 
497                         String objectCsid) throws WebApplicationException {
498                 RelationsCommonList relationList = new RelationsCommonList();
499                 try {
500                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
501                         DocumentHandler handler = createDocumentHandler(ctx);                   
502                         String relationClause = RelationsUtils.buildWhereClause(subjectCsid, predicate, objectCsid);
503                         handler.getDocumentFilter().appendWhereClause(relationClause, IQueryManager.SEARCH_QUALIFIER_AND);                      
504                         getRepositoryClient(ctx).getFiltered(ctx, handler);
505                         relationList = (RelationsCommonList)handler.getCommonPartList();
506                 } catch (UnauthorizedException ue) {
507                         Response response = Response.status(Response.Status.UNAUTHORIZED).entity(
508                                         "Get relations failed reason " +
509                                         ue.getErrorReason()).type("text/plain").build();
510                         throw new WebApplicationException(response);
511                 } catch (Exception e) {
512                         if (logger.isDebugEnabled()) {
513                                 logger.debug("Caught exception in getRelationList", e);
514                         }
515                         Response response = Response.status(
516                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
517                                         "Index failed").type("text/plain").build();
518                         throw new WebApplicationException(response);
519                 }
520                 
521                 return relationList;
522         }
523 }