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