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