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.collectionspace.services.jaxb.AbstractCommonList;
59 import org.collectionspace.services.nuxeo.client.java.CommonList;
61 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
62 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
63 import org.jboss.resteasy.util.HttpResponseCodes;
64 import org.nuxeo.ecm.core.api.DocumentModel;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
69 * AcquisitionResource.java
71 * Handles requests to the Acquisition service, orchestrates the retrieval
72 * of relevant resources, and returns responses to the client.
74 @Path("/acquisitions")
75 @Consumes("multipart/mixed")
76 @Produces("multipart/mixed")
77 public class AcquisitionResource
78 extends AbstractMultiPartCollectionSpaceResourceImpl {
80 /** The service name. */
81 final private String serviceName = "acquisitions";
83 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
86 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
89 protected String getVersionString() {
90 /** The last change revision. */
91 final String lastChangeRevision = "$LastChangedRevision$";
92 return lastChangeRevision;
96 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
99 public String getServiceName() {
104 public Class<AcquisitionsCommon> getCommonPartClass() {
105 return AcquisitionsCommon.class;
109 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
112 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
113 // DocumentHandler docHandler = ctx.getDocumentHandler();
114 // if (ctx.getInput() != null) {
115 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
116 // if (obj != null) {
117 // docHandler.setCommonPart((AcquisitionsCommon) obj);
120 // return docHandler;
123 * Instantiates a new acquisition resource.
125 public AcquisitionResource() {
130 * Creates the acquisition.
132 * @param input the input
134 * @return the response
137 public Response createAcquisition(MultipartInput input) {
140 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
141 DocumentHandler handler = createDocumentHandler(ctx);
142 String csid = getRepositoryClient(ctx).create(ctx, handler);
143 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
144 path.path("" + csid);
145 Response response = Response.created(path.build()).build();
147 } catch (UnauthorizedException ue) {
148 Response response = Response.status(
149 Response.Status.UNAUTHORIZED).entity(
150 ServiceMessages.CREATE_FAILED + ue.getErrorReason()).type("text/plain").build();
151 throw new WebApplicationException(response);
152 } catch (Exception e) {
153 if (logger.isDebugEnabled()) {
154 logger.debug("Caught exception in createAcquisition", e);
156 Response response = Response.status(
157 Response.Status.INTERNAL_SERVER_ERROR).entity(
158 ServiceMessages.CREATE_FAILED + e.getMessage()).type("text/plain").build();
159 throw new WebApplicationException(response);
164 * Gets the acquisition.
166 * @param csid the csid
168 * @return the acquisition
172 public MultipartOutput getAcquisition(
173 @PathParam("csid") String csid) {
174 if (logger.isDebugEnabled()) {
175 logger.debug("getAcquisition with csid=" + csid);
177 if (csid == null || "".equals(csid)) {
178 logger.error("getAcquisition:" + ServiceMessages.MISSING_CSID);
179 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
180 ServiceMessages.READ_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
181 throw new WebApplicationException(response);
183 MultipartOutput result = null;
185 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
186 DocumentHandler handler = createDocumentHandler(ctx);
187 getRepositoryClient(ctx).get(ctx, csid, handler);
188 result = (MultipartOutput) ctx.getOutput();
189 } catch (UnauthorizedException ue) {
190 Response response = Response.status(
191 Response.Status.UNAUTHORIZED).entity(
192 ServiceMessages.READ_FAILED + ue.getErrorReason()).type("text/plain").build();
193 throw new WebApplicationException(response);
194 } catch (DocumentNotFoundException dnfe) {
195 if (logger.isDebugEnabled()) {
196 logger.debug("getAcquisition", dnfe);
198 Response response = Response.status(Response.Status.NOT_FOUND).entity(
199 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
200 throw new WebApplicationException(response);
201 } catch (Exception e) {
202 if (logger.isDebugEnabled()) {
203 logger.debug("getAcquisition", e);
205 Response response = Response.status(
206 Response.Status.INTERNAL_SERVER_ERROR).entity(
207 ServiceMessages.READ_FAILED + e.getMessage()).type("text/plain").build();
208 throw new WebApplicationException(response);
211 if (result == null) {
212 Response response = Response.status(Response.Status.NOT_FOUND).entity(
213 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
214 throw new WebApplicationException(response);
220 * Gets the acquisition list.
223 * @param keywords the keywords
225 * @return the acquisition list
228 @Produces("application/xml")
229 public CommonList getAcquisitionList(@Context UriInfo ui,
230 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
231 CommonList result = null;
232 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
233 if (keywords != null) {
234 result = searchAcquisitions(queryParams, keywords);
236 result = getAcquisitionsList(queryParams);
243 * Gets the acquisitions list.
245 * @return the acquisitions list
247 private CommonList getAcquisitionsList(MultivaluedMap<String, String> queryParams) {
248 CommonList commonList;
250 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
251 DocumentHandler handler = createDocumentHandler(ctx);
252 getRepositoryClient(ctx).getFiltered(ctx, handler);
253 commonList = (CommonList) handler.getCommonPartList();
254 } catch (UnauthorizedException ue) {
255 Response response = Response.status(
256 Response.Status.UNAUTHORIZED).entity(
257 ServiceMessages.LIST_FAILED + ue.getErrorReason()).type("text/plain").build();
258 throw new WebApplicationException(response);
259 } catch (Exception e) {
260 if (logger.isDebugEnabled()) {
261 logger.debug("Caught exception in getAcquisitionList", e);
263 Response response = Response.status(
264 Response.Status.INTERNAL_SERVER_ERROR).entity(
265 ServiceMessages.LIST_FAILED + e.getMessage()).type("text/plain").build();
266 throw new WebApplicationException(response);
272 * Update acquisition.
274 * @param csid the csid
275 * @param theUpdate the the update
277 * @return the multipart output
281 public MultipartOutput updateAcquisition(
282 @PathParam("csid") String csid,
283 MultipartInput theUpdate) {
284 if (logger.isDebugEnabled()) {
285 logger.debug("updateAcquisition with csid=" + csid);
287 if (csid == null || "".equals(csid)) {
288 logger.error("updateAcquisition: missing csid!");
289 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
290 ServiceMessages.UPDATE_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
291 throw new WebApplicationException(response);
293 if (logger.isDebugEnabled()) {
294 logger.debug("updateAcquisition with input: ", theUpdate);
296 MultipartOutput result = null;
298 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
299 DocumentHandler handler = createDocumentHandler(ctx);
300 getRepositoryClient(ctx).update(ctx, csid, handler);
301 result = (MultipartOutput) ctx.getOutput();
302 } catch (UnauthorizedException ue) {
303 Response response = Response.status(
304 Response.Status.UNAUTHORIZED).entity(
305 ServiceMessages.UPDATE_FAILED + ue.getErrorReason()).type("text/plain").build();
306 throw new WebApplicationException(response);
307 } catch (DocumentNotFoundException dnfe) {
308 if (logger.isDebugEnabled()) {
309 logger.debug("caught exception in updateAcquisition", dnfe);
311 Response response = Response.status(Response.Status.NOT_FOUND).entity(
312 ServiceMessages.UPDATE_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
313 throw new WebApplicationException(response);
314 } catch (Exception e) {
315 Response response = Response.status(
316 Response.Status.INTERNAL_SERVER_ERROR).entity(
317 ServiceMessages.UPDATE_FAILED + e.getMessage()).type("text/plain").build();
318 throw new WebApplicationException(response);
324 * Delete acquisition.
326 * @param csid the csid
328 * @return the response
332 public Response deleteAcquisition(@PathParam("csid") String csid) {
334 if (logger.isDebugEnabled()) {
335 logger.debug("deleteAcquisition with csid=" + csid);
337 if (csid == null || "".equals(csid)) {
338 logger.error("deleteAcquisition: missing csid!");
339 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
340 ServiceMessages.DELETE_FAILED + ServiceMessages.MISSING_CSID).type("text/plain").build();
341 throw new WebApplicationException(response);
344 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
345 getRepositoryClient(ctx).delete(ctx, csid);
346 return Response.status(HttpResponseCodes.SC_OK).build();
347 } catch (UnauthorizedException ue) {
348 Response response = Response.status(
349 Response.Status.UNAUTHORIZED).entity(
350 ServiceMessages.DELETE_FAILED + ue.getErrorReason()).type("text/plain").build();
351 throw new WebApplicationException(response);
352 } catch (DocumentNotFoundException dnfe) {
353 if (logger.isDebugEnabled()) {
354 logger.debug("caught exception in deleteAcquisition", dnfe);
356 Response response = Response.status(Response.Status.NOT_FOUND).entity(
357 ServiceMessages.DELETE_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
358 throw new WebApplicationException(response);
359 } catch (Exception e) {
360 Response response = Response.status(
361 Response.Status.INTERNAL_SERVER_ERROR).entity(
362 ServiceMessages.DELETE_FAILED + e.getMessage()).type("text/plain").build();
363 throw new WebApplicationException(response);
368 * Keywords search acquisitions.
371 * @param keywords the keywords
373 * @return the acquisitions common list
377 @Produces("application/xml")
379 public CommonList keywordsSearchAcquisitions(@Context UriInfo ui,
380 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
381 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
382 return searchAcquisitions(queryParams, keywords);
386 * Search acquisitions.
388 * @param keywords the keywords
390 * @return the acquisitions common list
392 private CommonList searchAcquisitions(
393 MultivaluedMap<String, String> queryParams,
395 CommonList commonList;
397 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
398 DocumentHandler handler = createDocumentHandler(ctx);
400 // perform a keyword search
401 if (keywords != null && !keywords.isEmpty()) {
402 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
403 DocumentFilter documentFilter = handler.getDocumentFilter();
404 documentFilter.setWhereClause(whereClause);
405 if (logger.isDebugEnabled()) {
406 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
409 getRepositoryClient(ctx).getFiltered(ctx, handler);
410 commonList = (CommonList) handler.getCommonPartList();
411 } catch (UnauthorizedException ue) {
412 Response response = Response.status(
413 Response.Status.UNAUTHORIZED).entity(
414 ServiceMessages.SEARCH_FAILED + ue.getErrorReason()).type("text/plain").build();
415 throw new WebApplicationException(response);
416 } catch (Exception e) {
417 if (logger.isDebugEnabled()) {
418 logger.debug("Caught exception in search for Acquisitions", e);
420 Response response = Response.status(
421 Response.Status.INTERNAL_SERVER_ERROR).entity(
422 ServiceMessages.SEARCH_FAILED + e.getMessage()).type("text/plain").build();
423 throw new WebApplicationException(response);
429 * Gets the authority refs.
431 * @param csid the csid
434 * @return the authority refs
437 @Path("{csid}/authorityrefs")
438 @Produces("application/xml")
439 public AuthorityRefList getAuthorityRefs(
440 @PathParam("csid") String csid,
441 @Context UriInfo ui) {
442 AuthorityRefList authRefList = null;
444 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
445 DocumentWrapper<DocumentModel> docWrapper =
446 getRepositoryClient(ctx).getDoc(ctx, csid);
447 DocumentModelHandler<MultipartInput, MultipartOutput> handler = (DocumentModelHandler<MultipartInput, MultipartOutput>) createDocumentHandler(ctx);
448 List<String> authRefFields =
449 ((MultipartServiceContextImpl) ctx).getCommonPartPropertyValues(
450 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
451 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
452 } catch (UnauthorizedException ue) {
453 Response response = Response.status(
454 Response.Status.UNAUTHORIZED).entity(
455 ServiceMessages.AUTH_REFS_FAILED + ue.getErrorReason()).type("text/plain").build();
456 throw new WebApplicationException(response);
457 } catch (Exception e) {
458 if (logger.isDebugEnabled()) {
459 logger.debug("Caught exception in getAuthorityRefs", e);
461 Response response = Response.status(
462 Response.Status.INTERNAL_SERVER_ERROR).entity(
463 ServiceMessages.AUTH_REFS_FAILED + e.getMessage()).type("text/plain").build();
464 throw new WebApplicationException(response);