]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ff4a5c44d2ef2cff09750fa07321ced85d1e7ab8
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.acquisition;
25
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;
41
42 import org.collectionspace.services.acquisition.AcquisitionList.*;
43
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;
55
56 @Path("/acquisitions")
57 @Consumes("application/xml")
58 @Produces("application/xml")
59 public class AcquisitionResource {
60
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();
65
66     public AcquisitionResource() {
67         // do nothing
68     }
69
70     @POST
71     public Response createAcquisition(
72             Acquisition acquisitionObject) {
73
74         String csid = null;
75         try{
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);
85             }
86             UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
87             path.path("" + csid);
88             Response response = Response.created(path.build()).build();
89             return response;
90         }catch(Exception e){
91             if(logger.isDebugEnabled()){
92                 logger.debug("Caught exception in createAcquisition", e);
93             }
94             Response response = Response.status(
95                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
96             throw new WebApplicationException(response);
97         }
98     }
99
100     @GET
101     @Path("{csid}")
102     public Acquisition getAcquisition(
103             @PathParam("csid") String csid) {
104         if(logger.isDebugEnabled()){
105             verbose("getAcquisition with csid=" + csid);
106         }
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);
113         }
114         Acquisition acquisitionObject = null;
115         try{
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);
125             }
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);
130         }catch(Exception e){
131             if(logger.isDebugEnabled()){
132                 logger.debug("getAcquisition", e);
133             }
134             Response response = Response.status(
135                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
136             throw new WebApplicationException(response);
137         }
138
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);
144         }
145         if(logger.isDebugEnabled()){
146             verbose("getAcquisition: ", acquisitionObject);
147         }
148         return acquisitionObject;
149     }
150
151     @GET
152     public AcquisitionList getAcquisitionList(@Context UriInfo ui) {
153         AcquisitionList acquisitionObjectList = new AcquisitionList();
154         try{
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();
161         }catch(Exception e){
162             if(logger.isDebugEnabled()){
163                 logger.debug("Caught exception in getAcquisitionList", e);
164             }
165             Response response = Response.status(
166                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
167             throw new WebApplicationException(response);
168         }
169         return acquisitionObjectList;
170     }
171
172     @PUT
173     @Path("{csid}")
174     public Acquisition updateAcquisition(
175             @PathParam("csid") String csid,
176             Acquisition theUpdate) {
177         if(logger.isDebugEnabled()){
178             verbose("updateAcquisition with csid=" + csid);
179         }
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);
186         }
187         if(logger.isDebugEnabled()){
188             verbose("updateAcquisition with input: ", theUpdate);
189         }
190         try{
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);
200             }
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);
205         }catch(Exception e){
206             Response response = Response.status(
207                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
208             throw new WebApplicationException(response);
209         }
210         return theUpdate;
211     }
212
213     @DELETE
214     @Path("{csid}")
215     public Response deleteAcquisition(@PathParam("csid") String csid) {
216
217         if(logger.isDebugEnabled()){
218             verbose("deleteAcquisition with csid=" + csid);
219         }
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);
226         }
227         try{
228             RepositoryClientFactory clientFactory = RepositoryClientFactory.getInstance();
229             RepositoryClient client = clientFactory.getClient(CLIENT_TYPE.toString());
230             client.delete(csid);
231             return Response.status(HttpResponseCodes.SC_OK).build();
232         }catch(DocumentNotFoundException dnfe){
233             if(logger.isDebugEnabled()){
234                 logger.debug("caught exception in deleteAcquisition", dnfe);
235             }
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);
240         }catch(Exception e){
241             Response response = Response.status(
242                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
243             throw new WebApplicationException(response);
244         }
245
246     }
247
248     private void verbose(String msg, Acquisition acquisitionObject) {
249         try{
250             verbose(msg);
251             JAXBContext jc = JAXBContext.newInstance(
252                     Acquisition.class);
253
254             Marshaller m = jc.createMarshaller();
255             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
256             m.marshal(acquisitionObject, System.out);
257         }catch(Exception e){
258             e.printStackTrace();
259         }
260
261     }
262
263     private void verbose(String msg) {
264         System.out.println("AcquisitionResource. " + msg);
265     }
266 }