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