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 Regents of the University of California
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 java.util.List;
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.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
44 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
45 import org.collectionspace.services.common.authorityref.AuthorityRefList;
46 import org.collectionspace.services.common.ServiceMessages;
47 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentWrapper;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.common.query.QueryManager;
56 import org.collectionspace.services.common.security.UnauthorizedException;
57 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
59 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
60 import org.jboss.resteasy.util.HttpResponseCodes;
61 import org.nuxeo.ecm.core.api.DocumentModel;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * AcquisitionResource.java
68 * Handles requests to the Acquisition service, orchestrates the retrieval
69 * of relevant resources, and returns responses to the client.
71 @Path("/acquisitions")
72 @Consumes("multipart/mixed")
73 @Produces("multipart/mixed")
74 public class AcquisitionResource
75 extends AbstractMultiPartCollectionSpaceResourceImpl {
77 /** The service name. */
78 final private String serviceName = "acquisitions";
80 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
83 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
86 protected String getVersionString() {
87 /** The last change revision. */
88 final String lastChangeRevision = "$LastChangedRevision$";
89 return lastChangeRevision;
93 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
96 public String getServiceName() {
101 public Class<AcquisitionsCommon> getCommonPartClass() {
102 return AcquisitionsCommon.class;
106 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
109 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
110 // DocumentHandler docHandler = ctx.getDocumentHandler();
111 // if (ctx.getInput() != null) {
112 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
113 // if (obj != null) {
114 // docHandler.setCommonPart((AcquisitionsCommon) obj);
117 // return docHandler;
120 * Instantiates a new acquisition resource.
122 public AcquisitionResource() {
127 * Creates the acquisition.
129 * @param input the input
131 * @return the response
134 public Response createAcquisition(MultipartInput input) {
137 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
138 DocumentHandler handler = createDocumentHandler(ctx);
139 String csid = getRepositoryClient(ctx).create(ctx, handler);
140 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
141 path.path("" + csid);
142 Response response = Response.created(path.build()).build();
144 } catch (UnauthorizedException ue) {
145 Response response = Response.status(
146 Response.Status.UNAUTHORIZED).entity(
147 ServiceMessages.CREATE_FAILED + ue.getErrorReason()).type("text/plain").build();
148 throw new WebApplicationException(response);
149 } catch (Exception e) {
150 if (logger.isDebugEnabled()) {
151 logger.debug("Caught exception in createAcquisition", e);
153 Response response = Response.status(
154 Response.Status.INTERNAL_SERVER_ERROR).entity(
155 ServiceMessages.CREATE_FAILED + e.getMessage()).type("text/plain").build();
156 throw new WebApplicationException(response);
161 * Gets the acquisition.
163 * @param csid the csid
165 * @return the acquisition
169 public MultipartOutput getAcquisition(
170 @PathParam("csid") String csid) {
171 if (logger.isDebugEnabled()) {
172 logger.debug("getAcquisition with csid=" + csid);
174 if (csid == null || "".equals(csid)) {
175 logger.error("getAcquisition:" + ServiceMessages.MISSING_CSID);
176 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
177 ServiceMessages.READ_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
178 throw new WebApplicationException(response);
180 MultipartOutput result = null;
182 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
183 DocumentHandler handler = createDocumentHandler(ctx);
184 getRepositoryClient(ctx).get(ctx, csid, handler);
185 result = (MultipartOutput) ctx.getOutput();
186 } catch (UnauthorizedException ue) {
187 Response response = Response.status(
188 Response.Status.UNAUTHORIZED).entity(
189 ServiceMessages.READ_FAILED + ue.getErrorReason()).type("text/plain").build();
190 throw new WebApplicationException(response);
191 } catch (DocumentNotFoundException dnfe) {
192 if (logger.isDebugEnabled()) {
193 logger.debug("getAcquisition", dnfe);
195 Response response = Response.status(Response.Status.NOT_FOUND).entity(
196 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
197 throw new WebApplicationException(response);
198 } catch (Exception e) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("getAcquisition", e);
202 Response response = Response.status(
203 Response.Status.INTERNAL_SERVER_ERROR).entity(
204 ServiceMessages.READ_FAILED + e.getMessage()).type("text/plain").build();
205 throw new WebApplicationException(response);
208 if (result == null) {
209 Response response = Response.status(Response.Status.NOT_FOUND).entity(
210 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
211 throw new WebApplicationException(response);
217 * Gets the acquisition list.
220 * @param keywords the keywords
222 * @return the acquisition list
225 @Produces("application/xml")
226 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui,
227 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
228 AcquisitionsCommonList result = null;
229 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
230 if (keywords != null) {
231 result = searchAcquisitions(queryParams, keywords);
233 result = getAcquisitionsList(queryParams);
240 * Gets the acquisitions list.
242 * @return the acquisitions list
244 private AcquisitionsCommonList getAcquisitionsList(MultivaluedMap<String, String> queryParams) {
245 AcquisitionsCommonList acquisitionObjectList;
247 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
248 DocumentHandler handler = createDocumentHandler(ctx);
249 getRepositoryClient(ctx).getFiltered(ctx, handler);
250 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
251 } catch (UnauthorizedException ue) {
252 Response response = Response.status(
253 Response.Status.UNAUTHORIZED).entity(
254 ServiceMessages.LIST_FAILED + ue.getErrorReason()).type("text/plain").build();
255 throw new WebApplicationException(response);
256 } catch (Exception e) {
257 if (logger.isDebugEnabled()) {
258 logger.debug("Caught exception in getAcquisitionList", e);
260 Response response = Response.status(
261 Response.Status.INTERNAL_SERVER_ERROR).entity(
262 ServiceMessages.LIST_FAILED + e.getMessage()).type("text/plain").build();
263 throw new WebApplicationException(response);
265 return acquisitionObjectList;
269 * Update acquisition.
271 * @param csid the csid
272 * @param theUpdate the the update
274 * @return the multipart output
278 public MultipartOutput updateAcquisition(
279 @PathParam("csid") String csid,
280 MultipartInput theUpdate) {
281 if (logger.isDebugEnabled()) {
282 logger.debug("updateAcquisition with csid=" + csid);
284 if (csid == null || "".equals(csid)) {
285 logger.error("updateAcquisition: missing csid!");
286 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
287 ServiceMessages.UPDATE_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
288 throw new WebApplicationException(response);
290 if (logger.isDebugEnabled()) {
291 logger.debug("updateAcquisition with input: ", theUpdate);
293 MultipartOutput result = null;
295 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
296 DocumentHandler handler = createDocumentHandler(ctx);
297 getRepositoryClient(ctx).update(ctx, csid, handler);
298 result = (MultipartOutput) ctx.getOutput();
299 } catch (UnauthorizedException ue) {
300 Response response = Response.status(
301 Response.Status.UNAUTHORIZED).entity(
302 ServiceMessages.UPDATE_FAILED + ue.getErrorReason()).type("text/plain").build();
303 throw new WebApplicationException(response);
304 } catch (DocumentNotFoundException dnfe) {
305 if (logger.isDebugEnabled()) {
306 logger.debug("caught exception in updateAcquisition", dnfe);
308 Response response = Response.status(Response.Status.NOT_FOUND).entity(
309 ServiceMessages.UPDATE_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
310 throw new WebApplicationException(response);
311 } catch (Exception e) {
312 Response response = Response.status(
313 Response.Status.INTERNAL_SERVER_ERROR).entity(
314 ServiceMessages.UPDATE_FAILED + e.getMessage()).type("text/plain").build();
315 throw new WebApplicationException(response);
321 * Delete acquisition.
323 * @param csid the csid
325 * @return the response
329 public Response deleteAcquisition(@PathParam("csid") String csid) {
331 if (logger.isDebugEnabled()) {
332 logger.debug("deleteAcquisition with csid=" + csid);
334 if (csid == null || "".equals(csid)) {
335 logger.error("deleteAcquisition: missing csid!");
336 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
337 ServiceMessages.DELETE_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
338 throw new WebApplicationException(response);
341 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
342 getRepositoryClient(ctx).delete(ctx, csid);
343 return Response.status(HttpResponseCodes.SC_OK).build();
344 } catch (UnauthorizedException ue) {
345 Response response = Response.status(
346 Response.Status.UNAUTHORIZED).entity(
347 ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
348 throw new WebApplicationException(response);
349 } catch (DocumentNotFoundException dnfe) {
350 if (logger.isDebugEnabled()) {
351 logger.debug("caught exception in deleteAcquisition", dnfe);
353 Response response = Response.status(Response.Status.NOT_FOUND).entity(
354 ServiceMessages.DELETE_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
355 throw new WebApplicationException(response);
356 } catch (Exception e) {
357 Response response = Response.status(
358 Response.Status.INTERNAL_SERVER_ERROR).entity(
359 ServiceMessages.DELETE_FAILED + e.getMessage()).type("text/plain").build();
360 throw new WebApplicationException(response);
365 * Keywords search acquisitions.
368 * @param keywords the keywords
370 * @return the acquisitions common list
374 @Produces("application/xml")
376 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
377 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
378 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
379 return searchAcquisitions(queryParams, keywords);
383 * Search acquisitions.
385 * @param keywords the keywords
387 * @return the acquisitions common list
389 private AcquisitionsCommonList searchAcquisitions(
390 MultivaluedMap<String, String> queryParams,
392 AcquisitionsCommonList acquisitionObjectList;
394 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
395 DocumentHandler handler = createDocumentHandler(ctx);
397 // perform a keyword search
398 if (keywords != null && !keywords.isEmpty()) {
399 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
400 DocumentFilter documentFilter = handler.getDocumentFilter();
401 documentFilter.setWhereClause(whereClause);
402 if (logger.isDebugEnabled()) {
403 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
406 getRepositoryClient(ctx).getFiltered(ctx, handler);
407 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
408 } catch (UnauthorizedException ue) {
409 Response response = Response.status(
410 Response.Status.UNAUTHORIZED).entity(
411 ServiceMessages.SEARCH_FAILED + ue.getErrorReason()).type("text/plain").build();
412 throw new WebApplicationException(response);
413 } catch (Exception e) {
414 if (logger.isDebugEnabled()) {
415 logger.debug("Caught exception in search for Acquisitions", e);
417 Response response = Response.status(
418 Response.Status.INTERNAL_SERVER_ERROR).entity(
419 ServiceMessages.SEARCH_FAILED + e.getMessage()).type("text/plain").build();
420 throw new WebApplicationException(response);
422 return acquisitionObjectList;
426 * Gets the authority refs.
428 * @param csid the csid
431 * @return the authority refs
434 @Path("{csid}/authorityrefs")
435 @Produces("application/xml")
436 public AuthorityRefList getAuthorityRefs(
437 @PathParam("csid") String csid,
438 @Context UriInfo ui) {
439 AuthorityRefList authRefList = null;
441 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
442 DocumentWrapper<DocumentModel> docWrapper =
443 getRepositoryClient(ctx).getDoc(ctx, csid);
444 DocumentModelHandler<MultipartInput, MultipartOutput> handler = (DocumentModelHandler<MultipartInput, MultipartOutput>) createDocumentHandler(ctx);
445 List<String> authRefFields =
446 ((MultipartServiceContextImpl) ctx).getCommonPartPropertyValues(
447 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
448 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
449 } catch (UnauthorizedException ue) {
450 Response response = Response.status(
451 Response.Status.UNAUTHORIZED).entity(
452 ServiceMessages.AUTH_REFS_FAILED + ue.getErrorReason()).type("text/plain").build();
453 throw new WebApplicationException(response);
454 } catch (Exception e) {
455 if (logger.isDebugEnabled()) {
456 logger.debug("Caught exception in getAuthorityRefs", e);
458 Response response = Response.status(
459 Response.Status.INTERNAL_SERVER_ERROR).entity(
460 ServiceMessages.AUTH_REFS_FAILED + e.getMessage()).type("text/plain").build();
461 throw new WebApplicationException(response);