2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.acquisition;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
40 import org.collectionspace.services.acquisition.nuxeo.AcquisitionHandlerFactory;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
42 import org.collectionspace.services.common.context.MultipartServiceContext;
43 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.document.DocumentNotFoundException;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
49 import org.jboss.resteasy.util.HttpResponseCodes;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 @Path("/acquisitions")
54 @Consumes("multipart/mixed")
55 @Produces("multipart/mixed")
56 public class AcquisitionResource
57 extends AbstractCollectionSpaceResource {
59 final private String serviceName = "acquisitions";
60 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
63 public String getServiceName() {
68 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
69 DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler(
70 ctx.getRepositoryClientType().toString());
71 docHandler.setServiceContext(ctx);
72 if (ctx.getInput() != null) {
73 Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
75 docHandler.setCommonPart((AcquisitionsCommon) obj);
81 public AcquisitionResource() {
86 public Response createAcquisition(MultipartInput input) {
89 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
90 DocumentHandler handler = createDocumentHandler(ctx);
91 String csid = getRepositoryClient(ctx).create(ctx, handler);
92 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
94 Response response = Response.created(path.build()).build();
96 } catch (Exception e) {
97 if (logger.isDebugEnabled()) {
98 logger.debug("Caught exception in createAcquisition", e);
100 Response response = Response.status(
101 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
102 throw new WebApplicationException(response);
108 public MultipartOutput getAcquisition(
109 @PathParam("csid") String csid) {
110 if (logger.isDebugEnabled()) {
111 logger.debug("getAcquisition with csid=" + csid);
113 if (csid == null || "".equals(csid)) {
114 logger.error("getAcquisition: missing csid!");
115 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
116 "get failed on Acquisition csid=" + csid).type(
117 "text/plain").build();
118 throw new WebApplicationException(response);
120 MultipartOutput result = null;
122 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
123 DocumentHandler handler = createDocumentHandler(ctx);
124 getRepositoryClient(ctx).get(ctx, csid, handler);
125 result = (MultipartOutput) ctx.getOutput();
126 } catch (DocumentNotFoundException dnfe) {
127 if (logger.isDebugEnabled()) {
128 logger.debug("getAcquisition", dnfe);
130 Response response = Response.status(Response.Status.NOT_FOUND).entity(
131 "Get failed on Acquisition csid=" + csid).type(
132 "text/plain").build();
133 throw new WebApplicationException(response);
134 } catch (Exception e) {
135 if (logger.isDebugEnabled()) {
136 logger.debug("getAcquisition", e);
138 Response response = Response.status(
139 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
140 throw new WebApplicationException(response);
143 if (result == null) {
144 Response response = Response.status(Response.Status.NOT_FOUND).entity(
145 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
146 "text/plain").build();
147 throw new WebApplicationException(response);
153 @Produces("application/xml")
154 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
155 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
157 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
158 DocumentHandler handler = createDocumentHandler(ctx);
159 getRepositoryClient(ctx).getAll(ctx, handler);
160 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
161 } catch (Exception e) {
162 if (logger.isDebugEnabled()) {
163 logger.debug("Caught exception in getAcquisitionList", e);
165 Response response = Response.status(
166 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
167 throw new WebApplicationException(response);
169 return acquisitionObjectList;
174 public MultipartOutput updateAcquisition(
175 @PathParam("csid") String csid,
176 MultipartInput theUpdate) {
177 if (logger.isDebugEnabled()) {
178 logger.debug("updateAcquisition with csid=" + csid);
180 if (csid == null || "".equals(csid)) {
181 logger.error("updateAcquisition: missing csid!");
182 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
183 "update failed on Acquisition csid=" + csid).type(
184 "text/plain").build();
185 throw new WebApplicationException(response);
187 if (logger.isDebugEnabled()) {
188 logger.debug("updateAcquisition with input: ", theUpdate);
190 MultipartOutput result = null;
192 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
193 DocumentHandler handler = createDocumentHandler(ctx);
194 getRepositoryClient(ctx).update(ctx, csid, handler);
195 result = (MultipartOutput) ctx.getOutput();
196 } catch (DocumentNotFoundException dnfe) {
197 if (logger.isDebugEnabled()) {
198 logger.debug("caugth exception in updateAcquisition", dnfe);
200 Response response = Response.status(Response.Status.NOT_FOUND).entity(
201 "Update failed on Acquisition csid=" + csid).type(
202 "text/plain").build();
203 throw new WebApplicationException(response);
204 } catch (Exception e) {
205 Response response = Response.status(
206 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
207 throw new WebApplicationException(response);
214 public Response deleteAcquisition(@PathParam("csid") String csid) {
216 if (logger.isDebugEnabled()) {
217 logger.debug("deleteAcquisition with csid=" + csid);
219 if (csid == null || "".equals(csid)) {
220 logger.error("deleteAcquisition: missing csid!");
221 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
222 "delete failed on Acquisition csid=" + csid).type(
223 "text/plain").build();
224 throw new WebApplicationException(response);
227 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
228 getRepositoryClient(ctx).delete(ctx, csid);
229 return Response.status(HttpResponseCodes.SC_OK).build();
230 } catch (DocumentNotFoundException dnfe) {
231 if (logger.isDebugEnabled()) {
232 logger.debug("caught exception in deleteAcquisition", dnfe);
234 Response response = Response.status(Response.Status.NOT_FOUND).entity(
235 "Delete failed on Acquisition csid=" + csid).type(
236 "text/plain").build();
237 throw new WebApplicationException(response);
238 } catch (Exception e) {
239 Response response = Response.status(
240 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
241 throw new WebApplicationException(response);