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