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;
39 import javax.xml.bind.JAXBContext;
40 import javax.xml.bind.Marshaller;
42 import org.collectionspace.services.acquisition.AcquisitionList.*;
44 import org.collectionspace.services.acquisition.nuxeo.AcquisitionConstants;
45 import org.collectionspace.services.acquisition.nuxeo.AcquisitionHandlerFactory;
46 import org.collectionspace.services.common.NuxeoClientType;
47 import org.collectionspace.services.common.ServiceMain;
48 import org.collectionspace.services.common.repository.DocumentNotFoundException;
49 import org.collectionspace.services.common.repository.DocumentHandler;
50 import org.collectionspace.services.common.repository.RepositoryClient;
51 import org.collectionspace.services.common.repository.RepositoryClientFactory;
52 import org.jboss.resteasy.util.HttpResponseCodes;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 @Path("/acquisitions")
57 @Consumes("application/xml")
58 @Produces("application/xml")
59 public class AcquisitionResource {
61 public final static String ACQUISITION_SERVICE_NAME = "acquisitions";
62 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
63 //FIXME retrieve client type from configuration
64 final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance().getNuxeoClientType();
66 public AcquisitionResource() {
71 public Response createAcquisition(
72 Acquisition acquisitionObject) {
76 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
77 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
78 AcquisitionHandlerFactory handlerFactory = AcquisitionHandlerFactory.getInstance();
79 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
80 handler.setCommonObject(acquisitionObject);
81 csid = client.create(ACQUISITION_SERVICE_NAME, handler);
82 acquisitionObject.setCsid(csid);
83 if(logger.isDebugEnabled()){
84 verbose("createAcquisition: ", acquisitionObject);
86 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
88 Response response = Response.created(path.build()).build();
91 if(logger.isDebugEnabled()){
92 logger.debug("Caught exception in createAcquisition", e);
94 Response response = Response.status(
95 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
96 throw new WebApplicationException(response);
102 public Acquisition getAcquisition(
103 @PathParam("csid") String csid) {
104 if(logger.isDebugEnabled()){
105 verbose("getAcquisition with csid=" + csid);
107 if(csid == null || "".equals(csid)){
108 logger.error("getAcquisition: missing csid!");
109 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
110 "get failed on Acquisition csid=" + csid).type(
111 "text/plain").build();
112 throw new WebApplicationException(response);
114 Acquisition acquisitionObject = null;
116 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
117 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
118 AcquisitionHandlerFactory handlerFactory = AcquisitionHandlerFactory.getInstance();
119 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
120 client.get(csid, handler);
121 acquisitionObject = (Acquisition) handler.getCommonObject();
122 }catch(DocumentNotFoundException dnfe){
123 if(logger.isDebugEnabled()){
124 logger.debug("getAcquisition", dnfe);
126 Response response = Response.status(Response.Status.NOT_FOUND).entity(
127 "Get failed on Acquisition csid=" + csid).type(
128 "text/plain").build();
129 throw new WebApplicationException(response);
131 if(logger.isDebugEnabled()){
132 logger.debug("getAcquisition", e);
134 Response response = Response.status(
135 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
136 throw new WebApplicationException(response);
139 if(acquisitionObject == null){
140 Response response = Response.status(Response.Status.NOT_FOUND).entity(
141 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
142 "text/plain").build();
143 throw new WebApplicationException(response);
145 if(logger.isDebugEnabled()){
146 verbose("getAcquisition: ", acquisitionObject);
148 return acquisitionObject;
152 public AcquisitionList getAcquisitionList(@Context UriInfo ui) {
153 AcquisitionList acquisitionObjectList = new AcquisitionList();
155 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
156 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
157 AcquisitionHandlerFactory handlerFactory = AcquisitionHandlerFactory.getInstance();
158 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
159 client.getAll(ACQUISITION_SERVICE_NAME, handler);
160 acquisitionObjectList = (AcquisitionList) handler.getCommonObjectList();
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 Acquisition updateAcquisition(
175 @PathParam("csid") String csid,
176 Acquisition theUpdate) {
177 if(logger.isDebugEnabled()){
178 verbose("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 verbose("updateAcquisition with input: ", theUpdate);
191 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
192 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
193 AcquisitionHandlerFactory handlerFactory = AcquisitionHandlerFactory.getInstance();
194 DocumentHandler handler = (DocumentHandler) handlerFactory.getHandler(CLIENT_TYPE.toString());
195 handler.setCommonObject(theUpdate);
196 client.update(csid, handler);
197 }catch(DocumentNotFoundException dnfe){
198 if(logger.isDebugEnabled()){
199 logger.debug("caugth exception in updateAcquisition", dnfe);
201 Response response = Response.status(Response.Status.NOT_FOUND).entity(
202 "Update failed on Acquisition csid=" + csid).type(
203 "text/plain").build();
204 throw new WebApplicationException(response);
206 Response response = Response.status(
207 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
208 throw new WebApplicationException(response);
215 public Response deleteAcquisition(@PathParam("csid") String csid) {
217 if(logger.isDebugEnabled()){
218 verbose("deleteAcquisition with csid=" + csid);
220 if(csid == null || "".equals(csid)){
221 logger.error("deleteAcquisition: missing csid!");
222 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
223 "delete failed on Acquisition csid=" + csid).type(
224 "text/plain").build();
225 throw new WebApplicationException(response);
228 RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
229 RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
231 return Response.status(HttpResponseCodes.SC_OK).build();
232 }catch(DocumentNotFoundException dnfe){
233 if(logger.isDebugEnabled()){
234 logger.debug("caught exception in deleteAcquisition", dnfe);
236 Response response = Response.status(Response.Status.NOT_FOUND).entity(
237 "Delete failed on Acquisition csid=" + csid).type(
238 "text/plain").build();
239 throw new WebApplicationException(response);
241 Response response = Response.status(
242 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
243 throw new WebApplicationException(response);
248 private void verbose(String msg, Acquisition acquisitionObject) {
251 JAXBContext jc = JAXBContext.newInstance(
254 Marshaller m = jc.createMarshaller();
255 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
256 m.marshal(acquisitionObject, System.out);
263 private void verbose(String msg) {
264 System.out.println("AcquisitionResource. " + msg);