]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fad74f7b2c671147b7ccf6cb4a009ae9f403e9c7
[tmp/jakarta-migration.git] /
1 /**     
2  * NewRelationResource.java
3  *
4  * {Purpose of This Class}
5  *
6  * {Other Notes Relating to This Class (Optional)}
7  *
8  * $LastChangedBy: $
9  * $LastChangedRevision$
10  * $LastChangedDate: $
11  *
12  * This document is a part of the source code and related artifacts
13  * for CollectionSpace, an open source collections management system
14  * for museums and related institutions:
15  *
16  * http://www.collectionspace.org
17  * http://wiki.collectionspace.org
18  *
19  * Copyright � 2009 {Contributing Institution}
20  *
21  * Licensed under the Educational Community License (ECL), Version 2.0.
22  * You may not use this file except in compliance with this License.
23  *
24  * You may obtain a copy of the ECL 2.0 License at
25  * https://source.collectionspace.org/collection-space/LICENSE.txt
26  */
27 package org.collectionspace.services.relation;
28
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriBuilder;
43 import javax.ws.rs.core.UriInfo;
44
45 import org.collectionspace.services.common.query.IQueryManager;
46 import org.collectionspace.services.common.relation.IRelationsManager;
47 import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;
48 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.security.UnauthorizedException;
53
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
56 import org.jboss.resteasy.util.HttpResponseCodes;
57
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * The Class NewRelationResource.
63  */
64 @Path("/relations")
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class NewRelationResource extends
68                 AbstractMultiPartCollectionSpaceResourceImpl {
69
70         /** The Constant serviceName. */
71         public final static String serviceName = "relations";
72         
73         /** The logger. */
74         final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
75
76         /* (non-Javadoc)
77          * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
78          */
79         @Override
80         protected String getVersionString() {
81                 /** The last change revision. */
82                 final String lastChangeRevision = "$LastChangedRevision$";
83                 return lastChangeRevision;
84         }
85
86         /* (non-Javadoc)
87          * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
88          */
89         @Override
90         public String getServiceName() {
91                 return serviceName;
92         }
93
94         /* (non-Javadoc)
95          * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
96          */
97         @Override
98     public Class<RelationsCommon> getCommonPartClass() {
99         return RelationsCommon.class;
100     }
101
102         /**
103          * Creates the relation.
104          * 
105          * @param input the input
106          * 
107          * @return the response
108          */
109         @POST
110         public Response createRelation(MultipartInput input) {
111                 try {
112                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
113                         DocumentHandler handler = createDocumentHandler(ctx);
114                         String csid = getRepositoryClient(ctx).create(ctx, handler);
115                         UriBuilder path = UriBuilder
116                                         .fromResource(NewRelationResource.class);
117                         path.path("" + csid);
118                         Response response = Response.created(path.build()).build();
119                         return response;
120                 } catch (UnauthorizedException ue) {
121                         Response response = Response.status(Response.Status.UNAUTHORIZED)
122                                         .entity("Create failed reason " + ue.getErrorReason())
123                                         .type("text/plain").build();
124                         throw new WebApplicationException(response);
125                 } catch (Exception e) {
126                         if (logger.isDebugEnabled()) {
127                                 logger.debug("Caught exception in createRelation", e);
128                         }
129                         Response response = Response.status(
130                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
131                                         "Create failed").type("text/plain").build();
132                         throw new WebApplicationException(response);
133                 }
134         }
135         
136         /**
137          * Gets the relation.
138          *
139          * @param ui the ui
140          * @param csid the csid
141          * @return the relation
142          */
143         @GET
144         @Path("{csid}")
145         public MultipartOutput getRelation(@Context UriInfo ui,
146                         @PathParam("csid") String csid) {
147                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
148                 if (logger.isDebugEnabled()) {
149                         logger.debug("getRelation with csid=" + csid);
150                 }
151                 if (csid == null || "".equals(csid)) {
152                         logger.error("getRelation: missing csid!");
153                         Response response = Response.status(Response.Status.BAD_REQUEST)
154                                         .entity("get failed on Relation csid=" + csid).type(
155                                                         "text/plain").build();
156                         throw new WebApplicationException(response);
157                 }
158                 MultipartOutput result = null;
159                 try {
160                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
161                         DocumentHandler handler = createDocumentHandler(ctx);
162                         getRepositoryClient(ctx).get(ctx, csid, handler);
163                         result = (MultipartOutput) ctx.getOutput();
164                 } catch (UnauthorizedException ue) {
165                         Response response = Response.status(Response.Status.UNAUTHORIZED)
166                                         .entity("Get failed reason " + ue.getErrorReason()).type(
167                                                         "text/plain").build();
168                         throw new WebApplicationException(response);
169                 } catch (DocumentNotFoundException dnfe) {
170                         if (logger.isDebugEnabled()) {
171                                 logger.debug("getRelation", dnfe);
172                         }
173                         Response response = Response.status(Response.Status.NOT_FOUND)
174                                         .entity("Get failed on Relation csid=" + csid).type(
175                                                         "text/plain").build();
176                         throw new WebApplicationException(response);
177                 } catch (Exception e) {
178                         if (logger.isDebugEnabled()) {
179                                 logger.debug("getRelation", e);
180                         }
181                         Response response = Response.status(
182                                         Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
183                                         .type("text/plain").build();
184                         throw new WebApplicationException(response);
185                 }
186
187                 if (result == null) {
188                         Response response = Response.status(Response.Status.NOT_FOUND)
189                                         .entity(
190                                                         "Get failed, the requested Relation CSID:" + csid
191                                                                         + ": was not found.").type("text/plain")
192                                         .build();
193                         throw new WebApplicationException(response);
194                 }
195                 return result;
196         }
197
198         /**
199          * Gets the relation list.
200          * 
201          * @param ui the ui
202          * @param subjectCsid 
203          * @param predicate 
204          * @param objectCsid 
205          * 
206          * @return the relation list
207          */
208         @GET
209         @Produces("application/xml")
210         public RelationsCommonList getRelationList(@Context UriInfo ui,
211                         @QueryParam(IRelationsManager.SUBJECT_QP) String subjectCsid,
212                         @QueryParam(IRelationsManager.SUBJECT_TYPE_QP) String subjectType,
213                         @QueryParam(IRelationsManager.PREDICATE_QP) String predicate,
214                         @QueryParam(IRelationsManager.OBJECT_QP) String objectCsid,
215                         @QueryParam(IRelationsManager.OBJECT_TYPE_QP) String objectType) {
216                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
217                 return this.getRelationList(queryParams, subjectCsid, subjectType,
218                                 predicate,
219                                 objectCsid, objectType);
220         }
221
222         /**
223          * Update relation.
224          * 
225          * @param csid the csid
226          * @param theUpdate the the update
227          * 
228          * @return the multipart output
229          */
230         @PUT
231         @Path("{csid}")
232         public MultipartOutput updateRelation(@PathParam("csid") String csid,
233                         MultipartInput theUpdate) {
234                 if (logger.isDebugEnabled()) {
235                         logger.debug("updateRelation with csid=" + csid);
236                 }
237                 if (csid == null || "".equals(csid)) {
238                         logger.error("updateRelation: missing csid!");
239                         Response response = Response.status(Response.Status.BAD_REQUEST)
240                                         .entity("update failed on Relation csid=" + csid).type(
241                                                         "text/plain").build();
242                         throw new WebApplicationException(response);
243                 }
244                 MultipartOutput result = null;
245                 try {
246                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
247                         DocumentHandler handler = createDocumentHandler(ctx);
248                         getRepositoryClient(ctx).update(ctx, csid, handler);
249                         result = (MultipartOutput) ctx.getOutput();
250                 } catch (UnauthorizedException ue) {
251                         Response response = Response.status(Response.Status.UNAUTHORIZED)
252                                         .entity("Update failed reason " + ue.getErrorReason())
253                                         .type("text/plain").build();
254                         throw new WebApplicationException(response);
255                 } catch (DocumentNotFoundException dnfe) {
256                         if (logger.isDebugEnabled()) {
257                                 logger.debug("caugth exception in updateRelation", dnfe);
258                         }
259                         Response response = Response.status(Response.Status.NOT_FOUND)
260                                         .entity("Update failed on Relation csid=" + csid).type(
261                                                         "text/plain").build();
262                         throw new WebApplicationException(response);
263                 } catch (Exception e) {
264                         Response response = Response.status(
265                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
266                                         "Update failed").type("text/plain").build();
267                         throw new WebApplicationException(response);
268                 }
269                 return result;
270         }
271
272         /**
273          * Delete relation.
274          * 
275          * @param csid the csid
276          * 
277          * @return the response
278          */
279         @DELETE
280         @Path("{csid}")
281         public Response deleteRelation(@PathParam("csid") String csid) {
282
283                 if (logger.isDebugEnabled()) {
284                         logger.debug("deleteRelation with csid=" + csid);
285                 }
286                 if (csid == null || "".equals(csid)) {
287                         logger.error("deleteRelation: missing csid!");
288                         Response response = Response.status(Response.Status.BAD_REQUEST)
289                                         .entity("delete failed on Relation csid=" + csid).type(
290                                                         "text/plain").build();
291                         throw new WebApplicationException(response);
292                 }
293                 try {
294                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
295                         getRepositoryClient(ctx).delete(ctx, csid);
296                         return Response.status(HttpResponseCodes.SC_OK).build();
297                 } catch (UnauthorizedException ue) {
298                         Response response = Response.status(Response.Status.UNAUTHORIZED)
299                                         .entity("Delete failed reason " + ue.getErrorReason())
300                                         .type("text/plain").build();
301                         throw new WebApplicationException(response);
302                 } catch (DocumentNotFoundException dnfe) {
303                         if (logger.isDebugEnabled()) {
304                                 logger.debug("caught exception in deleteRelation", dnfe);
305                         }
306                         Response response = Response.status(Response.Status.NOT_FOUND)
307                                         .entity("Delete failed on Relation csid=" + csid).type(
308                                                         "text/plain").build();
309                         throw new WebApplicationException(response);
310                 } catch (Exception e) {
311                         Response response = Response.status(
312                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
313                                         "Delete failed").type("text/plain").build();
314                         throw new WebApplicationException(response);
315                 }
316
317         }
318
319         /**
320          * Gets the relation list.
321          *
322          * @param queryParams the query params
323          * @param subjectCsid the subject csid
324          * @param predicate the predicate
325          * @param objectCsid the object csid
326          * @return the relation list
327          * @throws WebApplicationException the web application exception
328          */
329         public RelationsCommonList getRelationList(
330                         MultivaluedMap<String, String> queryParams,
331                         String subjectCsid,
332                         String subjectType,
333                         String predicate, 
334                         String objectCsid,
335                         String objectType) throws WebApplicationException {
336                 RelationsCommonList relationList = new RelationsCommonList();
337                 try {
338                         ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
339                         DocumentHandler handler = createDocumentHandler(ctx);
340                         String relationClause = RelationsUtils.buildWhereClause(subjectCsid, subjectType, predicate,
341                                         objectCsid, objectType);
342                         handler.getDocumentFilter().appendWhereClause(relationClause, IQueryManager.SEARCH_QUALIFIER_AND);                      
343                         getRepositoryClient(ctx).getFiltered(ctx, handler);
344                         relationList = (RelationsCommonList)handler.getCommonPartList();
345                 } catch (UnauthorizedException ue) {
346                         Response response = Response.status(Response.Status.UNAUTHORIZED).entity(
347                                         "Get relations failed reason " +
348                                         ue.getErrorReason()).type("text/plain").build();
349                         throw new WebApplicationException(response);
350                 } catch (Exception e) {
351                         if (logger.isDebugEnabled()) {
352                                 logger.debug("Caught exception in getRelationList", e);
353                         }
354                         Response response = Response.status(
355                                         Response.Status.INTERNAL_SERVER_ERROR).entity(
356                                         "Index failed").type("text/plain").build();
357                         throw new WebApplicationException(response);
358                 }
359                 
360                 return relationList;
361         }
362 }