]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f822ad6941331323b9ca0182465d83380bc13a63
[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.BadRequestException;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
57 import org.jboss.resteasy.util.HttpResponseCodes;
58
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * The Class NewRelationResource.
64  */
65 @Path("/relations")
66 @Consumes("multipart/mixed")
67 @Produces("multipart/mixed")
68 public class NewRelationResource extends
69                 AbstractMultiPartCollectionSpaceResourceImpl {
70
71         /** The Constant serviceName. */
72         public final static String serviceName = "relations";
73         
74         /** The logger. */
75         final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
76
77         /* (non-Javadoc)
78          * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
79          */
80         @Override
81         protected String getVersionString() {
82                 /** The last change revision. */
83                 final String lastChangeRevision = "$LastChangedRevision$";
84                 return lastChangeRevision;
85         }
86
87         /* (non-Javadoc)
88          * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
89          */
90         @Override
91         public String getServiceName() {
92                 return serviceName;
93         }
94
95         /* (non-Javadoc)
96          * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
97          */
98         @Override
99     public Class<RelationsCommon> getCommonPartClass() {
100         return RelationsCommon.class;
101     }
102
103         /**
104          * Creates the relation.
105          * 
106          * @param input the input
107          * 
108          * @return the response
109          */
110         @POST
111         public Response createRelation(MultipartInput input) {
112                 try {
113                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
114                         DocumentHandler handler = createDocumentHandler(ctx);
115                         String csid = getRepositoryClient(ctx).create(ctx, handler);
116                         UriBuilder path = UriBuilder
117                                         .fromResource(NewRelationResource.class);
118                         path.path("" + csid);
119                         Response response = Response.created(path.build()).build();
120                         return response;
121                 } catch (UnauthorizedException ue) {
122                         Response response = Response.status(Response.Status.UNAUTHORIZED)
123                                         .entity("Create failed reason " + ue.getErrorReason())
124                                         .type("text/plain").build();
125                         throw new WebApplicationException(response);
126                 } catch (BadRequestException bre) {
127                         Response response = Response.status(Response.Status.BAD_REQUEST)
128                                         .entity("Create failed reason " + bre.getErrorReason())
129                                         .type("text/plain").build();
130                         throw new WebApplicationException(response);
131                 } catch (Exception e) {
132                         if (logger.isDebugEnabled()) {
133                                 logger.debug("Caught exception in createRelation", e);
134                         }
135                         Response response = Response.status(
136                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
137                                         "Create failed").type("text/plain").build();
138                         throw new WebApplicationException(response);
139                 }
140         }
141         
142         /**
143          * Gets the relation.
144          *
145          * @param ui the ui
146          * @param csid the csid
147          * @return the relation
148          */
149         @GET
150         @Path("{csid}")
151         public MultipartOutput getRelation(@Context UriInfo ui,
152                         @PathParam("csid") String csid) {
153                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
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<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
167                         DocumentHandler handler = createDocumentHandler(ctx);
168                         getRepositoryClient(ctx).get(ctx, csid, handler);
169                         result = (MultipartOutput) ctx.getOutput();
170                 } catch (UnauthorizedException ue) {
171                         Response response = Response.status(Response.Status.UNAUTHORIZED)
172                                         .entity("Get failed reason " + ue.getErrorReason()).type(
173                                                         "text/plain").build();
174                         throw new WebApplicationException(response);
175                 } catch (DocumentNotFoundException dnfe) {
176                         if (logger.isDebugEnabled()) {
177                                 logger.debug("getRelation", dnfe);
178                         }
179                         Response response = Response.status(Response.Status.NOT_FOUND)
180                                         .entity("Get failed on Relation csid=" + csid).type(
181                                                         "text/plain").build();
182                         throw new WebApplicationException(response);
183                 } catch (Exception e) {
184                         if (logger.isDebugEnabled()) {
185                                 logger.debug("getRelation", e);
186                         }
187                         Response response = Response.status(
188                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
189                                         .type("text/plain").build();
190                         throw new WebApplicationException(response);
191                 }
192
193                 if (result == null) {
194                         Response response = Response.status(Response.Status.NOT_FOUND)
195                                         .entity(
196                                                         "Get failed, the requested Relation CSID:" + csid
197                                                                         + ": was not found.").type("text/plain")
198                                         .build();
199                         throw new WebApplicationException(response);
200                 }
201                 return result;
202         }
203
204         /**
205          * Gets the relation list.
206          * 
207          * @param ui the ui
208          * @param subjectCsid 
209          * @param predicate 
210          * @param objectCsid 
211          * 
212          * @return the relation list
213          */
214         @GET
215         @Produces("application/xml")
216         public RelationsCommonList getRelationList(@Context UriInfo ui,
217                         @QueryParam(IRelationsManager.SUBJECT_QP) String subjectCsid,
218                         @QueryParam(IRelationsManager.SUBJECT_TYPE_QP) String subjectType,
219                         @QueryParam(IRelationsManager.PREDICATE_QP) String predicate,
220                         @QueryParam(IRelationsManager.OBJECT_QP) String objectCsid,
221                         @QueryParam(IRelationsManager.OBJECT_TYPE_QP) String objectType) {
222                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
223                 return this.getRelationList(queryParams, subjectCsid, subjectType,
224                                 predicate,
225                                 objectCsid, objectType);
226         }
227
228         /**
229          * Update relation.
230          * 
231          * @param csid the csid
232          * @param theUpdate the the update
233          * 
234          * @return the multipart output
235          */
236         @PUT
237         @Path("{csid}")
238         public MultipartOutput updateRelation(@PathParam("csid") String csid,
239                         MultipartInput theUpdate) {
240                 if (logger.isDebugEnabled()) {
241                         logger.debug("updateRelation with csid=" + csid);
242                 }
243                 if (csid == null || "".equals(csid)) {
244                         logger.error("updateRelation: missing csid!");
245                         Response response = Response.status(Response.Status.BAD_REQUEST)
246                                         .entity("update failed on Relation csid=" + csid).type(
247                                                         "text/plain").build();
248                         throw new WebApplicationException(response);
249                 }
250                 MultipartOutput result = null;
251                 try {
252                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
253                         DocumentHandler handler = createDocumentHandler(ctx);
254                         getRepositoryClient(ctx).update(ctx, csid, handler);
255                         result = (MultipartOutput) ctx.getOutput();
256                 } catch (UnauthorizedException ue) {
257                         Response response = Response.status(Response.Status.UNAUTHORIZED)
258                                         .entity("Update failed reason " + ue.getErrorReason())
259                                         .type("text/plain").build();
260                         throw new WebApplicationException(response);
261                 } catch (DocumentNotFoundException dnfe) {
262                         if (logger.isDebugEnabled()) {
263                                 logger.debug("caugth exception in updateRelation", dnfe);
264                         }
265                         Response response = Response.status(Response.Status.NOT_FOUND)
266                                         .entity("Update failed on Relation csid=" + csid).type(
267                                                         "text/plain").build();
268                         throw new WebApplicationException(response);
269                 } catch (Exception e) {
270                         Response response = Response.status(
271                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
272                                         "Update failed").type("text/plain").build();
273                         throw new WebApplicationException(response);
274                 }
275                 return result;
276         }
277
278         /**
279          * Delete relation.
280          * 
281          * @param csid the csid
282          * 
283          * @return the response
284          */
285         @DELETE
286         @Path("{csid}")
287         public Response deleteRelation(@PathParam("csid") String csid) {
288
289                 if (logger.isDebugEnabled()) {
290                         logger.debug("deleteRelation with csid=" + csid);
291                 }
292                 if (csid == null || "".equals(csid)) {
293                         logger.error("deleteRelation: missing csid!");
294                         Response response = Response.status(Response.Status.BAD_REQUEST)
295                                         .entity("delete failed on Relation csid=" + csid).type(
296                                                         "text/plain").build();
297                         throw new WebApplicationException(response);
298                 }
299                 try {
300                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
301                         getRepositoryClient(ctx).delete(ctx, csid);
302                         return Response.status(HttpResponseCodes.SC_OK).build();
303                 } catch (UnauthorizedException ue) {
304                         Response response = Response.status(Response.Status.UNAUTHORIZED)
305                                         .entity("Delete failed reason " + ue.getErrorReason())
306                                         .type("text/plain").build();
307                         throw new WebApplicationException(response);
308                 } catch (DocumentNotFoundException dnfe) {
309                         if (logger.isDebugEnabled()) {
310                                 logger.debug("caught exception in deleteRelation", dnfe);
311                         }
312                         Response response = Response.status(Response.Status.NOT_FOUND)
313                                         .entity("Delete failed on Relation csid=" + csid).type(
314                                                         "text/plain").build();
315                         throw new WebApplicationException(response);
316                 } catch (Exception e) {
317                         Response response = Response.status(
318                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
319                                         "Delete failed").type("text/plain").build();
320                         throw new WebApplicationException(response);
321                 }
322
323         }
324
325         /**
326          * Gets the relation list.
327          *
328          * @param queryParams the query params
329          * @param subjectCsid the subject csid
330          * @param subjectType 
331          * @param predicate the predicate
332          * @param objectCsid the object csid
333          * @param objectType 
334          * @return the relation list
335          * @throws WebApplicationException the web application exception
336          */
337         public RelationsCommonList getRelationList(
338                         MultivaluedMap<String, String> queryParams,
339                         String subjectCsid,
340                         String subjectType,
341                         String predicate, 
342                         String objectCsid,
343                         String objectType) throws WebApplicationException {
344                 RelationsCommonList relationList = new RelationsCommonList();
345                 try {
346                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
347                         DocumentHandler handler = createDocumentHandler(ctx);
348                         String relationClause = RelationsUtils.buildWhereClause(subjectCsid, subjectType, predicate,
349                                         objectCsid, objectType);
350                         handler.getDocumentFilter().appendWhereClause(relationClause, IQueryManager.SEARCH_QUALIFIER_AND);                      
351                         getRepositoryClient(ctx).getFiltered(ctx, handler);
352                         relationList = (RelationsCommonList)handler.getCommonPartList();
353                 } catch (UnauthorizedException ue) {
354                         Response response = Response.status(Response.Status.UNAUTHORIZED).entity(
355                                         "Get relations failed reason " +
356                                         ue.getErrorReason()).type("text/plain").build();
357                         throw new WebApplicationException(response);
358                 } catch (Exception e) {
359                         if (logger.isDebugEnabled()) {
360                                 logger.debug("Caught exception in getRelationList", e);
361                         }
362                         Response response = Response.status(
363                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
364                                         "Index failed").type("text/plain").build();
365                         throw new WebApplicationException(response);
366                 }
367                 
368                 return relationList;
369         }
370 }