2 * NewRelationResource.java
4 * {Purpose of This Class}
6 * {Other Notes Relating to This Class (Optional)}
9 * $LastChangedRevision$
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:
16 * http://www.collectionspace.org
17 * http://wiki.collectionspace.org
19 * Copyright � 2009 {Contributing Institution}
21 * Licensed under the Educational Community License (ECL), Version 2.0.
22 * You may not use this file except in compliance with this License.
24 * You may obtain a copy of the ECL 2.0 License at
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
27 package org.collectionspace.services.relation;
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;
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;
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;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 * The Class NewRelationResource.
66 @Consumes("multipart/mixed")
67 @Produces("multipart/mixed")
68 public class NewRelationResource extends
69 AbstractMultiPartCollectionSpaceResourceImpl {
71 /** The Constant serviceName. */
72 public final static String serviceName = "relations";
75 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
78 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
81 protected String getVersionString() {
82 /** The last change revision. */
83 final String lastChangeRevision = "$LastChangedRevision$";
84 return lastChangeRevision;
88 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
91 public String getServiceName() {
96 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
99 public Class<RelationsCommon> getCommonPartClass() {
100 return RelationsCommon.class;
104 * Creates the relation.
106 * @param input the input
108 * @return the response
111 public Response createRelation(MultipartInput input) {
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();
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);
135 Response response = Response.status(
136 Response.Status.INTERNAL_SERVER_ERROR).entity(
137 "Create failed").type("text/plain").build();
138 throw new WebApplicationException(response);
146 * @param csid the csid
147 * @return the relation
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);
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);
164 MultipartOutput result = null;
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);
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);
187 Response response = Response.status(
188 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
189 .type("text/plain").build();
190 throw new WebApplicationException(response);
193 if (result == null) {
194 Response response = Response.status(Response.Status.NOT_FOUND)
196 "Get failed, the requested Relation CSID:" + csid
197 + ": was not found.").type("text/plain")
199 throw new WebApplicationException(response);
205 * Gets the relation list.
212 * @return the relation list
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,
225 objectCsid, objectType);
231 * @param csid the csid
232 * @param theUpdate the the update
234 * @return the multipart output
238 public MultipartOutput updateRelation(@PathParam("csid") String csid,
239 MultipartInput theUpdate) {
240 if (logger.isDebugEnabled()) {
241 logger.debug("updateRelation with csid=" + csid);
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);
250 MultipartOutput result = null;
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);
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);
281 * @param csid the csid
283 * @return the response
287 public Response deleteRelation(@PathParam("csid") String csid) {
289 if (logger.isDebugEnabled()) {
290 logger.debug("deleteRelation with csid=" + csid);
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);
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);
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);
326 * Gets the relation list.
328 * @param queryParams the query params
329 * @param subjectCsid the subject csid
331 * @param predicate the predicate
332 * @param objectCsid the object csid
334 * @return the relation list
335 * @throws WebApplicationException the web application exception
337 public RelationsCommonList getRelationList(
338 MultivaluedMap<String, String> queryParams,
343 String objectType) throws WebApplicationException {
344 RelationsCommonList relationList = new RelationsCommonList();
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);
362 Response response = Response.status(
363 Response.Status.INTERNAL_SERVER_ERROR).entity(
364 "Index failed").type("text/plain").build();
365 throw new WebApplicationException(response);