]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
910b733f29fd553d24fe4e57f97c9137208b463c
[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 java.util.List;
27
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.QueryParam;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo;
42
43 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
44 import org.collectionspace.services.common.authorityref.AuthorityRefList;
45 import org.collectionspace.services.common.context.MultipartServiceContext;
46 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
47 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.document.DocumentWrapper;
53 import org.collectionspace.services.common.query.IQueryManager;
54 import org.collectionspace.services.common.query.QueryManager;
55 import org.collectionspace.services.common.security.UnauthorizedException;
56 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
59 import org.jboss.resteasy.util.HttpResponseCodes;
60 import org.nuxeo.ecm.core.api.DocumentModel;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 @Path("/acquisitions")
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class AcquisitionResource
68         extends AbstractCollectionSpaceResourceImpl {
69
70     final private String serviceName = "acquisitions";
71     final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
72
73     @Override
74     protected String getVersionString() {
75         /** The last change revision. */
76         final String lastChangeRevision = "$LastChangedRevision$";
77         return lastChangeRevision;
78     }
79     
80     @Override
81     public String getServiceName() {
82         return serviceName;
83     }
84
85     @Override
86     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
87         DocumentHandler docHandler = ctx.getDocumentHandler();
88         if (ctx.getInput() != null) {
89             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
90             if (obj != null) {
91                 docHandler.setCommonPart((AcquisitionsCommon) obj);
92             }
93         }
94         return docHandler;
95     }
96
97     public AcquisitionResource() {
98         // do nothing
99     }
100
101     @POST
102     public Response createAcquisition(MultipartInput input) {
103
104         try {
105             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
106             DocumentHandler handler = createDocumentHandler(ctx);
107             String csid = getRepositoryClient(ctx).create(ctx, handler);
108             UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
109             path.path("" + csid);
110             Response response = Response.created(path.build()).build();
111             return response;
112         } catch (UnauthorizedException ue) {
113             Response response = Response.status(
114                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
115             throw new WebApplicationException(response);
116         } catch (Exception e) {
117             if (logger.isDebugEnabled()) {
118                 logger.debug("Caught exception in createAcquisition", e);
119             }
120             Response response = Response.status(
121                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
122             throw new WebApplicationException(response);
123         }
124     }
125
126     @GET
127     @Path("{csid}")
128     public MultipartOutput getAcquisition(
129             @PathParam("csid") String csid) {
130         if (logger.isDebugEnabled()) {
131             logger.debug("getAcquisition with csid=" + csid);
132         }
133         if (csid == null || "".equals(csid)) {
134             logger.error("getAcquisition: missing csid!");
135             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
136                     "get failed on Acquisition csid=" + csid).type(
137                     "text/plain").build();
138             throw new WebApplicationException(response);
139         }
140         MultipartOutput result = null;
141         try {
142             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
143             DocumentHandler handler = createDocumentHandler(ctx);
144             getRepositoryClient(ctx).get(ctx, csid, handler);
145             result = (MultipartOutput) ctx.getOutput();
146         } catch (UnauthorizedException ue) {
147             Response response = Response.status(
148                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
149             throw new WebApplicationException(response);
150         } catch (DocumentNotFoundException dnfe) {
151             if (logger.isDebugEnabled()) {
152                 logger.debug("getAcquisition", dnfe);
153             }
154             Response response = Response.status(Response.Status.NOT_FOUND).entity(
155                     "Get failed on Acquisition csid=" + csid).type(
156                     "text/plain").build();
157             throw new WebApplicationException(response);
158         } catch (Exception e) {
159             if (logger.isDebugEnabled()) {
160                 logger.debug("getAcquisition", e);
161             }
162             Response response = Response.status(
163                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
164             throw new WebApplicationException(response);
165         }
166
167         if (result == null) {
168             Response response = Response.status(Response.Status.NOT_FOUND).entity(
169                     "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
170                     "text/plain").build();
171             throw new WebApplicationException(response);
172         }
173         return result;
174     }
175
176     @GET
177     @Produces("application/xml")
178     public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
179         AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
180         try {
181             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
182             DocumentHandler handler = createDocumentHandler(ctx);
183             getRepositoryClient(ctx).getAll(ctx, handler);
184             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
185         } catch (UnauthorizedException ue) {
186             Response response = Response.status(
187                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
188             throw new WebApplicationException(response);
189         } catch (Exception e) {
190             if (logger.isDebugEnabled()) {
191                 logger.debug("Caught exception in getAcquisitionList", e);
192             }
193             Response response = Response.status(
194                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
195             throw new WebApplicationException(response);
196         }
197         return acquisitionObjectList;
198     }
199
200     @PUT
201     @Path("{csid}")
202     public MultipartOutput updateAcquisition(
203             @PathParam("csid") String csid,
204             MultipartInput theUpdate) {
205         if (logger.isDebugEnabled()) {
206             logger.debug("updateAcquisition with csid=" + csid);
207         }
208         if (csid == null || "".equals(csid)) {
209             logger.error("updateAcquisition: missing csid!");
210             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
211                     "update failed on Acquisition csid=" + csid).type(
212                     "text/plain").build();
213             throw new WebApplicationException(response);
214         }
215         if (logger.isDebugEnabled()) {
216             logger.debug("updateAcquisition with input: ", theUpdate);
217         }
218         MultipartOutput result = null;
219         try {
220             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
221             DocumentHandler handler = createDocumentHandler(ctx);
222             getRepositoryClient(ctx).update(ctx, csid, handler);
223             result = (MultipartOutput) ctx.getOutput();
224         } catch (UnauthorizedException ue) {
225             Response response = Response.status(
226                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
227             throw new WebApplicationException(response);
228         } catch (DocumentNotFoundException dnfe) {
229             if (logger.isDebugEnabled()) {
230                 logger.debug("caugth exception in updateAcquisition", dnfe);
231             }
232             Response response = Response.status(Response.Status.NOT_FOUND).entity(
233                     "Update failed on Acquisition csid=" + csid).type(
234                     "text/plain").build();
235             throw new WebApplicationException(response);
236         } catch (Exception e) {
237             Response response = Response.status(
238                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
239             throw new WebApplicationException(response);
240         }
241         return result;
242     }
243
244     @DELETE
245     @Path("{csid}")
246     public Response deleteAcquisition(@PathParam("csid") String csid) {
247
248         if (logger.isDebugEnabled()) {
249             logger.debug("deleteAcquisition with csid=" + csid);
250         }
251         if (csid == null || "".equals(csid)) {
252             logger.error("deleteAcquisition: missing csid!");
253             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
254                     "delete failed on Acquisition csid=" + csid).type(
255                     "text/plain").build();
256             throw new WebApplicationException(response);
257         }
258         try {
259             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
260             getRepositoryClient(ctx).delete(ctx, csid);
261             return Response.status(HttpResponseCodes.SC_OK).build();
262         } catch (UnauthorizedException ue) {
263             Response response = Response.status(
264                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
265             throw new WebApplicationException(response);
266         } catch (DocumentNotFoundException dnfe) {
267             if (logger.isDebugEnabled()) {
268                 logger.debug("caught exception in deleteAcquisition", dnfe);
269             }
270             Response response = Response.status(Response.Status.NOT_FOUND).entity(
271                     "Delete failed on Acquisition csid=" + csid).type(
272                     "text/plain").build();
273             throw new WebApplicationException(response);
274         } catch (Exception e) {
275             Response response = Response.status(
276                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
277             throw new WebApplicationException(response);
278         }
279     }
280     
281     @GET
282     @Path("/search")    
283     @Produces("application/xml")
284     public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
285                 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
286         AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
287         try {
288             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
289             DocumentHandler handler = createDocumentHandler(ctx);
290
291             // perform a keyword search
292             if (keywords != null && !keywords.isEmpty()) {
293                 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
294                     DocumentFilter documentFilter = handler.getDocumentFilter();
295                     documentFilter.setWhereClause(whereClause);
296                     if (logger.isDebugEnabled()) {
297                         logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
298                     }
299                     getRepositoryClient(ctx).getFiltered(ctx, handler);
300             } else {
301                 getRepositoryClient(ctx).getAll(ctx, handler);
302             }            
303             acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
304             
305         } catch (UnauthorizedException ue) {
306             Response response = Response.status(
307                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
308             throw new WebApplicationException(response);
309         } catch (Exception e) {
310             if (logger.isDebugEnabled()) {
311                 logger.debug("Caught exception in search for Acquisitions", e);
312             }
313             Response response = Response.status(
314                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
315             throw new WebApplicationException(response);
316         }
317         return acquisitionObjectList;
318     }   
319     
320     @GET
321     @Path("{csid}/authorityrefs")
322     @Produces("application/xml")
323     public AuthorityRefList getAuthorityRefs(
324                 @PathParam("csid") String csid, 
325                 @Context UriInfo ui) {
326         AuthorityRefList authRefList = null;
327         try {
328             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
329             DocumentWrapper<DocumentModel> docWrapper = 
330                 getRepositoryClient(ctx).getDoc(ctx, csid);
331             RemoteDocumentModelHandlerImpl handler 
332                 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
333             List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues("authRef");
334             String prefix = ctx.getCommonPartLabel()+":";
335             authRefList = handler.getAuthorityRefs(docWrapper, prefix, authRefFields);
336         } catch (UnauthorizedException ue) {
337             Response response = Response.status(
338                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
339             throw new WebApplicationException(response);
340         } catch (Exception e) {
341             if (logger.isDebugEnabled()) {
342                 logger.debug("Caught exception in getAuthorityRefs", e);
343             }
344             Response response = Response.status(
345                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
346             throw new WebApplicationException(response);
347         }
348         return authRefList;
349     }
350     
351     
352 }