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.RemoteServiceContext;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.document.DocumentNotFoundException;
45 import org.collectionspace.services.common.document.DocumentHandler;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.util.HttpResponseCodes;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 @Path("/acquisitions")
53 @Consumes("multipart/mixed")
54 @Produces("multipart/mixed")
55 public class AcquisitionResource
56 extends AbstractCollectionSpaceResource {
58 final private String serviceName = "acquisitions";
59 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
62 public String getServiceName() {
67 public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
68 DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler(
69 ctx.getRepositoryClientType().toString());
70 docHandler.setServiceContext(ctx);
71 if(ctx.getInput() != null){
72 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
74 docHandler.setCommonPart((AcquisitionsCommon) obj);
80 public AcquisitionResource() {
85 public Response createAcquisition(MultipartInput input) {
88 RemoteServiceContext ctx = createServiceContext(input);
89 DocumentHandler handler = createDocumentHandler(ctx);
90 String csid = getRepositoryClient(ctx).create(ctx, handler);
91 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
93 Response response = Response.created(path.build()).build();
96 if(logger.isDebugEnabled()){
97 logger.debug("Caught exception in createAcquisition", e);
99 Response response = Response.status(
100 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
101 throw new WebApplicationException(response);
107 public MultipartOutput getAcquisition(
108 @PathParam("csid") String csid) {
109 if(logger.isDebugEnabled()){
110 logger.debug("getAcquisition with csid=" + csid);
112 if(csid == null || "".equals(csid)){
113 logger.error("getAcquisition: missing csid!");
114 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
115 "get failed on Acquisition csid=" + csid).type(
116 "text/plain").build();
117 throw new WebApplicationException(response);
119 MultipartOutput result = null;
121 RemoteServiceContext ctx = createServiceContext(null);
122 DocumentHandler handler = createDocumentHandler(ctx);
123 getRepositoryClient(ctx).get(ctx, csid, handler);
124 result = ctx.getOutput();
125 }catch(DocumentNotFoundException dnfe){
126 if(logger.isDebugEnabled()){
127 logger.debug("getAcquisition", dnfe);
129 Response response = Response.status(Response.Status.NOT_FOUND).entity(
130 "Get failed on Acquisition csid=" + csid).type(
131 "text/plain").build();
132 throw new WebApplicationException(response);
134 if(logger.isDebugEnabled()){
135 logger.debug("getAcquisition", e);
137 Response response = Response.status(
138 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
139 throw new WebApplicationException(response);
143 Response response = Response.status(Response.Status.NOT_FOUND).entity(
144 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
145 "text/plain").build();
146 throw new WebApplicationException(response);
152 @Produces("application/xml")
153 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
154 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
156 RemoteServiceContext ctx = createServiceContext(null);
157 DocumentHandler handler = createDocumentHandler(ctx);
158 getRepositoryClient(ctx).getAll(ctx, handler);
159 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
161 if(logger.isDebugEnabled()){
162 logger.debug("Caught exception in getAcquisitionList", e);
164 Response response = Response.status(
165 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
166 throw new WebApplicationException(response);
168 return acquisitionObjectList;
173 public MultipartOutput updateAcquisition(
174 @PathParam("csid") String csid,
175 MultipartInput theUpdate) {
176 if(logger.isDebugEnabled()){
177 logger.debug("updateAcquisition with csid=" + csid);
179 if(csid == null || "".equals(csid)){
180 logger.error("updateAcquisition: missing csid!");
181 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
182 "update failed on Acquisition csid=" + csid).type(
183 "text/plain").build();
184 throw new WebApplicationException(response);
186 if(logger.isDebugEnabled()){
187 logger.debug("updateAcquisition with input: ", theUpdate);
189 MultipartOutput result = null;
191 RemoteServiceContext ctx = createServiceContext(theUpdate);
192 DocumentHandler handler = createDocumentHandler(ctx);
193 getRepositoryClient(ctx).update(ctx, csid, handler);
194 result = ctx.getOutput();
195 }catch(DocumentNotFoundException dnfe){
196 if(logger.isDebugEnabled()){
197 logger.debug("caugth exception in updateAcquisition", dnfe);
199 Response response = Response.status(Response.Status.NOT_FOUND).entity(
200 "Update failed on Acquisition csid=" + csid).type(
201 "text/plain").build();
202 throw new WebApplicationException(response);
204 Response response = Response.status(
205 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
206 throw new WebApplicationException(response);
213 public Response deleteAcquisition(@PathParam("csid") String csid) {
215 if(logger.isDebugEnabled()){
216 logger.debug("deleteAcquisition with csid=" + csid);
218 if(csid == null || "".equals(csid)){
219 logger.error("deleteAcquisition: missing csid!");
220 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
221 "delete failed on Acquisition csid=" + csid).type(
222 "text/plain").build();
223 throw new WebApplicationException(response);
226 ServiceContext ctx = createServiceContext(null);
227 getRepositoryClient(ctx).delete(ctx, csid);
228 return Response.status(HttpResponseCodes.SC_OK).build();
229 }catch(DocumentNotFoundException dnfe){
230 if(logger.isDebugEnabled()){
231 logger.debug("caught exception in deleteAcquisition", dnfe);
233 Response response = Response.status(Response.Status.NOT_FOUND).entity(
234 "Delete failed on Acquisition csid=" + csid).type(
235 "text/plain").build();
236 throw new WebApplicationException(response);
238 Response response = Response.status(
239 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
240 throw new WebApplicationException(response);