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.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.security.UnauthorizedException;
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;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * The Class NewRelationResource.
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class NewRelationResource extends
68 AbstractMultiPartCollectionSpaceResourceImpl {
70 /** The Constant serviceName. */
71 public final static String serviceName = "relations";
74 final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
77 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString()
80 protected String getVersionString() {
81 /** The last change revision. */
82 final String lastChangeRevision = "$LastChangedRevision$";
83 return lastChangeRevision;
87 * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName()
90 public String getServiceName() {
95 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
98 public Class<RelationsCommon> getCommonPartClass() {
99 return RelationsCommon.class;
103 * Creates the relation.
105 * @param input the input
107 * @return the response
110 public Response createRelation(MultipartInput input) {
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();
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);
129 Response response = Response.status(
130 Response.Status.INTERNAL_SERVER_ERROR).entity(
131 "Create failed").type("text/plain").build();
132 throw new WebApplicationException(response);
140 * @param csid the csid
141 * @return the relation
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);
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);
158 MultipartOutput result = null;
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);
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);
181 Response response = Response.status(
182 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
183 .type("text/plain").build();
184 throw new WebApplicationException(response);
187 if (result == null) {
188 Response response = Response.status(Response.Status.NOT_FOUND)
190 "Get failed, the requested Relation CSID:" + csid
191 + ": was not found.").type("text/plain")
193 throw new WebApplicationException(response);
199 * Gets the relation list.
206 * @return the relation list
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,
219 objectCsid, objectType);
225 * @param csid the csid
226 * @param theUpdate the the update
228 * @return the multipart output
232 public MultipartOutput updateRelation(@PathParam("csid") String csid,
233 MultipartInput theUpdate) {
234 if (logger.isDebugEnabled()) {
235 logger.debug("updateRelation with csid=" + csid);
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);
244 MultipartOutput result = null;
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);
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);
275 * @param csid the csid
277 * @return the response
281 public Response deleteRelation(@PathParam("csid") String csid) {
283 if (logger.isDebugEnabled()) {
284 logger.debug("deleteRelation with csid=" + csid);
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);
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);
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);
320 * Gets the relation list.
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
329 public RelationsCommonList getRelationList(
330 MultivaluedMap<String, String> queryParams,
335 String objectType) throws WebApplicationException {
336 RelationsCommonList relationList = new RelationsCommonList();
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);
354 Response response = Response.status(
355 Response.Status.INTERNAL_SERVER_ERROR).entity(
356 "Index failed").type("text/plain").build();
357 throw new WebApplicationException(response);