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.collectionspace.services.common.security.UnauthorizedException;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
49 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
50 import org.jboss.resteasy.util.HttpResponseCodes;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 @Path("/acquisitions")
55 @Consumes("multipart/mixed")
56 @Produces("multipart/mixed")
57 public class AcquisitionResource
58 extends AbstractCollectionSpaceResource {
60 final private String serviceName = "acquisitions";
61 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
64 public String getServiceName() {
69 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
70 DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler(
71 ctx.getRepositoryClientType().toString());
72 docHandler.setServiceContext(ctx);
73 if (ctx.getInput() != null) {
74 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
76 docHandler.setCommonPart((AcquisitionsCommon) obj);
82 public AcquisitionResource() {
87 public Response createAcquisition(MultipartInput input) {
90 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
91 DocumentHandler handler = createDocumentHandler(ctx);
92 String csid = getRepositoryClient(ctx).create(ctx, handler);
93 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
95 Response response = Response.created(path.build()).build();
97 } catch (UnauthorizedException ue) {
98 Response response = Response.status(
99 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
100 throw new WebApplicationException(response);
101 } catch (Exception e) {
102 if (logger.isDebugEnabled()) {
103 logger.debug("Caught exception in createAcquisition", e);
105 Response response = Response.status(
106 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
107 throw new WebApplicationException(response);
113 public MultipartOutput getAcquisition(
114 @PathParam("csid") String csid) {
115 if (logger.isDebugEnabled()) {
116 logger.debug("getAcquisition with csid=" + csid);
118 if (csid == null || "".equals(csid)) {
119 logger.error("getAcquisition: missing csid!");
120 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
121 "get failed on Acquisition csid=" + csid).type(
122 "text/plain").build();
123 throw new WebApplicationException(response);
125 MultipartOutput result = null;
127 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
128 DocumentHandler handler = createDocumentHandler(ctx);
129 getRepositoryClient(ctx).get(ctx, csid, handler);
130 result = (MultipartOutput) ctx.getOutput();
131 } catch (UnauthorizedException ue) {
132 Response response = Response.status(
133 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
134 throw new WebApplicationException(response);
135 } catch (DocumentNotFoundException dnfe) {
136 if (logger.isDebugEnabled()) {
137 logger.debug("getAcquisition", dnfe);
139 Response response = Response.status(Response.Status.NOT_FOUND).entity(
140 "Get failed on Acquisition csid=" + csid).type(
141 "text/plain").build();
142 throw new WebApplicationException(response);
143 } catch (Exception e) {
144 if (logger.isDebugEnabled()) {
145 logger.debug("getAcquisition", e);
147 Response response = Response.status(
148 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
149 throw new WebApplicationException(response);
152 if (result == null) {
153 Response response = Response.status(Response.Status.NOT_FOUND).entity(
154 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
155 "text/plain").build();
156 throw new WebApplicationException(response);
162 @Produces("application/xml")
163 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
164 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
166 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
167 DocumentHandler handler = createDocumentHandler(ctx);
168 getRepositoryClient(ctx).getAll(ctx, handler);
169 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
170 } catch (UnauthorizedException ue) {
171 Response response = Response.status(
172 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
173 throw new WebApplicationException(response);
174 } catch (Exception e) {
175 if (logger.isDebugEnabled()) {
176 logger.debug("Caught exception in getAcquisitionList", e);
178 Response response = Response.status(
179 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
180 throw new WebApplicationException(response);
182 return acquisitionObjectList;
187 public MultipartOutput updateAcquisition(
188 @PathParam("csid") String csid,
189 MultipartInput theUpdate) {
190 if (logger.isDebugEnabled()) {
191 logger.debug("updateAcquisition with csid=" + csid);
193 if (csid == null || "".equals(csid)) {
194 logger.error("updateAcquisition: missing csid!");
195 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
196 "update failed on Acquisition csid=" + csid).type(
197 "text/plain").build();
198 throw new WebApplicationException(response);
200 if (logger.isDebugEnabled()) {
201 logger.debug("updateAcquisition with input: ", theUpdate);
203 MultipartOutput result = null;
205 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
206 DocumentHandler handler = createDocumentHandler(ctx);
207 getRepositoryClient(ctx).update(ctx, csid, handler);
208 result = (MultipartOutput) ctx.getOutput();
209 } catch (UnauthorizedException ue) {
210 Response response = Response.status(
211 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
212 throw new WebApplicationException(response);
213 } catch (DocumentNotFoundException dnfe) {
214 if (logger.isDebugEnabled()) {
215 logger.debug("caugth exception in updateAcquisition", dnfe);
217 Response response = Response.status(Response.Status.NOT_FOUND).entity(
218 "Update failed on Acquisition csid=" + csid).type(
219 "text/plain").build();
220 throw new WebApplicationException(response);
221 } catch (Exception e) {
222 Response response = Response.status(
223 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
224 throw new WebApplicationException(response);
231 public Response deleteAcquisition(@PathParam("csid") String csid) {
233 if (logger.isDebugEnabled()) {
234 logger.debug("deleteAcquisition with csid=" + csid);
236 if (csid == null || "".equals(csid)) {
237 logger.error("deleteAcquisition: missing csid!");
238 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
239 "delete failed on Acquisition csid=" + csid).type(
240 "text/plain").build();
241 throw new WebApplicationException(response);
244 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
245 getRepositoryClient(ctx).delete(ctx, csid);
246 return Response.status(HttpResponseCodes.SC_OK).build();
247 } catch (UnauthorizedException ue) {
248 Response response = Response.status(
249 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
250 throw new WebApplicationException(response);
251 } catch (DocumentNotFoundException dnfe) {
252 if (logger.isDebugEnabled()) {
253 logger.debug("caught exception in deleteAcquisition", dnfe);
255 Response response = Response.status(Response.Status.NOT_FOUND).entity(
256 "Delete failed on Acquisition csid=" + csid).type(
257 "text/plain").build();
258 throw new WebApplicationException(response);
259 } catch (Exception e) {
260 Response response = Response.status(
261 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
262 throw new WebApplicationException(response);