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.ServiceContext;
43 import org.collectionspace.services.common.repository.DocumentNotFoundException;
44 import org.collectionspace.services.common.repository.DocumentHandler;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.util.HttpResponseCodes;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 @Path("/acquisitions")
52 @Consumes("multipart/mixed")
53 @Produces("multipart/mixed")
54 public class AcquisitionResource
55 extends AbstractCollectionSpaceResource {
57 final private String serviceName = "acquisitions";
58 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
61 public String getServiceName() {
66 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
67 DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler(
68 ctx.getRepositoryClientType().toString());
69 docHandler.setServiceContext(ctx);
70 if(ctx.getInput() != null){
71 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
73 docHandler.setCommonPart((AcquisitionsCommon) obj);
79 public AcquisitionResource() {
84 public Response createAcquisition(MultipartInput input) {
87 ServiceContext ctx = createServiceContext(input);
88 DocumentHandler handler = createDocumentHandler(ctx);
89 String csid = getRepositoryClient(ctx).create(ctx, handler);
90 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
92 Response response = Response.created(path.build()).build();
95 if(logger.isDebugEnabled()){
96 logger.debug("Caught exception in createAcquisition", e);
98 Response response = Response.status(
99 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
100 throw new WebApplicationException(response);
106 public MultipartOutput getAcquisition(
107 @PathParam("csid") String csid) {
108 if(logger.isDebugEnabled()){
109 logger.debug("getAcquisition with csid=" + csid);
111 if(csid == null || "".equals(csid)){
112 logger.error("getAcquisition: missing csid!");
113 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
114 "get failed on Acquisition csid=" + csid).type(
115 "text/plain").build();
116 throw new WebApplicationException(response);
118 MultipartOutput result = null;
120 ServiceContext ctx = createServiceContext(null);
121 DocumentHandler handler = createDocumentHandler(ctx);
122 getRepositoryClient(ctx).get(ctx, csid, handler);
123 result = ctx.getOutput();
124 }catch(DocumentNotFoundException dnfe){
125 if(logger.isDebugEnabled()){
126 logger.debug("getAcquisition", dnfe);
128 Response response = Response.status(Response.Status.NOT_FOUND).entity(
129 "Get failed on Acquisition csid=" + csid).type(
130 "text/plain").build();
131 throw new WebApplicationException(response);
133 if(logger.isDebugEnabled()){
134 logger.debug("getAcquisition", e);
136 Response response = Response.status(
137 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
138 throw new WebApplicationException(response);
142 Response response = Response.status(Response.Status.NOT_FOUND).entity(
143 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
144 "text/plain").build();
145 throw new WebApplicationException(response);
151 @Produces("application/xml")
152 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
153 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
155 ServiceContext ctx = createServiceContext(null);
156 DocumentHandler handler = createDocumentHandler(ctx);
157 getRepositoryClient(ctx).getAll(ctx, handler);
158 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
160 if(logger.isDebugEnabled()){
161 logger.debug("Caught exception in getAcquisitionList", e);
163 Response response = Response.status(
164 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
165 throw new WebApplicationException(response);
167 return acquisitionObjectList;
172 public MultipartOutput updateAcquisition(
173 @PathParam("csid") String csid,
174 MultipartInput theUpdate) {
175 if(logger.isDebugEnabled()){
176 logger.debug("updateAcquisition with csid=" + csid);
178 if(csid == null || "".equals(csid)){
179 logger.error("updateAcquisition: missing csid!");
180 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
181 "update failed on Acquisition csid=" + csid).type(
182 "text/plain").build();
183 throw new WebApplicationException(response);
185 if(logger.isDebugEnabled()){
186 logger.debug("updateAcquisition with input: ", theUpdate);
188 MultipartOutput result = null;
190 ServiceContext ctx = createServiceContext(theUpdate);
191 DocumentHandler handler = createDocumentHandler(ctx);
192 getRepositoryClient(ctx).update(ctx, csid, handler);
193 result = ctx.getOutput();
194 }catch(DocumentNotFoundException dnfe){
195 if(logger.isDebugEnabled()){
196 logger.debug("caugth exception in updateAcquisition", dnfe);
198 Response response = Response.status(Response.Status.NOT_FOUND).entity(
199 "Update failed on Acquisition csid=" + csid).type(
200 "text/plain").build();
201 throw new WebApplicationException(response);
203 Response response = Response.status(
204 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
205 throw new WebApplicationException(response);
212 public Response deleteAcquisition(@PathParam("csid") String csid) {
214 if(logger.isDebugEnabled()){
215 logger.debug("deleteAcquisition with csid=" + csid);
217 if(csid == null || "".equals(csid)){
218 logger.error("deleteAcquisition: missing csid!");
219 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
220 "delete failed on Acquisition csid=" + csid).type(
221 "text/plain").build();
222 throw new WebApplicationException(response);
225 ServiceContext ctx = createServiceContext(null);
226 getRepositoryClient(ctx).delete(ctx, csid);
227 return Response.status(HttpResponseCodes.SC_OK).build();
228 }catch(DocumentNotFoundException dnfe){
229 if(logger.isDebugEnabled()){
230 logger.debug("caught exception in deleteAcquisition", dnfe);
232 Response response = Response.status(Response.Status.NOT_FOUND).entity(
233 "Delete failed on Acquisition csid=" + csid).type(
234 "text/plain").build();
235 throw new WebApplicationException(response);
237 Response response = Response.status(
238 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
239 throw new WebApplicationException(response);