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 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.Response;
40 import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo;
43 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
44 import org.collectionspace.services.common.authorityref.AuthorityRefList;
45 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
46 import org.collectionspace.services.common.context.ServiceBindingUtils;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.document.DocumentFilter;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.document.DocumentWrapper;
52 import org.collectionspace.services.common.query.IQueryManager;
53 import org.collectionspace.services.common.query.QueryManager;
54 import org.collectionspace.services.common.security.UnauthorizedException;
55 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
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;
65 * The Class AcquisitionResource.
67 @Path("/acquisitions")
68 @Consumes("multipart/mixed")
69 @Produces("multipart/mixed")
70 public class AcquisitionResource
71 extends AbstractMultiPartCollectionSpaceResourceImpl {
73 /** The service name. */
74 final private String serviceName = "acquisitions";
77 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
80 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
83 protected String getVersionString() {
84 /** The last change revision. */
85 final String lastChangeRevision = "$LastChangedRevision$";
86 return lastChangeRevision;
90 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
93 public String getServiceName() {
98 public Class<AcquisitionsCommon> getCommonPartClass() {
99 return AcquisitionsCommon.class;
103 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
106 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
107 // DocumentHandler docHandler = ctx.getDocumentHandler();
108 // if (ctx.getInput() != null) {
109 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
110 // if (obj != null) {
111 // docHandler.setCommonPart((AcquisitionsCommon) obj);
114 // return docHandler;
118 * Instantiates a new acquisition resource.
120 public AcquisitionResource() {
125 * Creates the acquisition.
127 * @param input the input
129 * @return the response
132 public Response createAcquisition(MultipartInput input) {
135 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
136 DocumentHandler handler = createDocumentHandler(ctx);
137 String csid = getRepositoryClient(ctx).create(ctx, handler);
138 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
139 path.path("" + csid);
140 Response response = Response.created(path.build()).build();
142 } catch (UnauthorizedException ue) {
143 Response response = Response.status(
144 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
145 throw new WebApplicationException(response);
146 } catch (Exception e) {
147 if (logger.isDebugEnabled()) {
148 logger.debug("Caught exception in createAcquisition", e);
150 Response response = Response.status(
151 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
152 throw new WebApplicationException(response);
157 * Gets the acquisition.
159 * @param csid the csid
161 * @return the acquisition
165 public MultipartOutput getAcquisition(
166 @PathParam("csid") String csid) {
167 if (logger.isDebugEnabled()) {
168 logger.debug("getAcquisition with csid=" + csid);
170 if (csid == null || "".equals(csid)) {
171 logger.error("getAcquisition: missing csid!");
172 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
173 "get failed on Acquisition csid=" + csid).type(
174 "text/plain").build();
175 throw new WebApplicationException(response);
177 MultipartOutput result = null;
179 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
180 DocumentHandler handler = createDocumentHandler(ctx);
181 getRepositoryClient(ctx).get(ctx, csid, handler);
182 result = (MultipartOutput) ctx.getOutput();
183 } catch (UnauthorizedException ue) {
184 Response response = Response.status(
185 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
186 throw new WebApplicationException(response);
187 } catch (DocumentNotFoundException dnfe) {
188 if (logger.isDebugEnabled()) {
189 logger.debug("getAcquisition", dnfe);
191 Response response = Response.status(Response.Status.NOT_FOUND).entity(
192 "Get failed on Acquisition csid=" + csid).type(
193 "text/plain").build();
194 throw new WebApplicationException(response);
195 } catch (Exception e) {
196 if (logger.isDebugEnabled()) {
197 logger.debug("getAcquisition", e);
199 Response response = Response.status(
200 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
201 throw new WebApplicationException(response);
204 if (result == null) {
205 Response response = Response.status(Response.Status.NOT_FOUND).entity(
206 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
207 "text/plain").build();
208 throw new WebApplicationException(response);
214 * Gets the acquisition list.
217 * @param keywords the keywords
219 * @return the acquisition list
222 @Produces("application/xml")
223 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui,
224 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
225 AcquisitionsCommonList result = null;
226 if (keywords != null) {
227 result = searchAcquisitions(keywords);
229 result = getAcquisitionsList();
236 * Gets the acquisitions list.
238 * @return the acquisitions list
240 private AcquisitionsCommonList getAcquisitionsList() {
241 AcquisitionsCommonList acquisitionObjectList;
243 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
244 DocumentHandler handler = createDocumentHandler(ctx);
245 getRepositoryClient(ctx).getAll(ctx, handler);
246 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
247 } catch (UnauthorizedException ue) {
248 Response response = Response.status(
249 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
250 throw new WebApplicationException(response);
251 } catch (Exception e) {
252 if (logger.isDebugEnabled()) {
253 logger.debug("Caught exception in getAcquisitionList", e);
255 Response response = Response.status(
256 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
257 throw new WebApplicationException(response);
259 return acquisitionObjectList;
263 * Update acquisition.
265 * @param csid the csid
266 * @param theUpdate the the update
268 * @return the multipart output
272 public MultipartOutput updateAcquisition(
273 @PathParam("csid") String csid,
274 MultipartInput theUpdate) {
275 if (logger.isDebugEnabled()) {
276 logger.debug("updateAcquisition with csid=" + csid);
278 if (csid == null || "".equals(csid)) {
279 logger.error("updateAcquisition: missing csid!");
280 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
281 "update failed on Acquisition csid=" + csid).type(
282 "text/plain").build();
283 throw new WebApplicationException(response);
285 if (logger.isDebugEnabled()) {
286 logger.debug("updateAcquisition with input: ", theUpdate);
288 MultipartOutput result = null;
290 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
291 DocumentHandler handler = createDocumentHandler(ctx);
292 getRepositoryClient(ctx).update(ctx, csid, handler);
293 result = (MultipartOutput) ctx.getOutput();
294 } catch (UnauthorizedException ue) {
295 Response response = Response.status(
296 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
297 throw new WebApplicationException(response);
298 } catch (DocumentNotFoundException dnfe) {
299 if (logger.isDebugEnabled()) {
300 logger.debug("caugth exception in updateAcquisition", dnfe);
302 Response response = Response.status(Response.Status.NOT_FOUND).entity(
303 "Update failed on Acquisition csid=" + csid).type(
304 "text/plain").build();
305 throw new WebApplicationException(response);
306 } catch (Exception e) {
307 Response response = Response.status(
308 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
309 throw new WebApplicationException(response);
315 * Delete acquisition.
317 * @param csid the csid
319 * @return the response
323 public Response deleteAcquisition(@PathParam("csid") String csid) {
325 if (logger.isDebugEnabled()) {
326 logger.debug("deleteAcquisition with csid=" + csid);
328 if (csid == null || "".equals(csid)) {
329 logger.error("deleteAcquisition: missing csid!");
330 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
331 "delete failed on Acquisition csid=" + csid).type(
332 "text/plain").build();
333 throw new WebApplicationException(response);
336 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
337 getRepositoryClient(ctx).delete(ctx, csid);
338 return Response.status(HttpResponseCodes.SC_OK).build();
339 } catch (UnauthorizedException ue) {
340 Response response = Response.status(
341 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
342 throw new WebApplicationException(response);
343 } catch (DocumentNotFoundException dnfe) {
344 if (logger.isDebugEnabled()) {
345 logger.debug("caught exception in deleteAcquisition", dnfe);
347 Response response = Response.status(Response.Status.NOT_FOUND).entity(
348 "Delete failed on Acquisition csid=" + csid).type(
349 "text/plain").build();
350 throw new WebApplicationException(response);
351 } catch (Exception e) {
352 Response response = Response.status(
353 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
354 throw new WebApplicationException(response);
359 * Keywords search acquisitions.
362 * @param keywords the keywords
364 * @return the acquisitions common list
368 @Produces("application/xml")
369 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
370 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
371 return searchAcquisitions(keywords);
375 * Search acquisitions.
377 * @param keywords the keywords
379 * @return the acquisitions common list
381 private AcquisitionsCommonList searchAcquisitions(String keywords) {
382 AcquisitionsCommonList acquisitionObjectList;
384 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
385 DocumentHandler handler = createDocumentHandler(ctx);
387 // perform a keyword search
388 if (keywords != null && !keywords.isEmpty()) {
389 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
390 DocumentFilter documentFilter = handler.getDocumentFilter();
391 documentFilter.setWhereClause(whereClause);
392 if (logger.isDebugEnabled()) {
393 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
395 getRepositoryClient(ctx).getFiltered(ctx, handler);
397 getRepositoryClient(ctx).getAll(ctx, handler);
399 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
401 } catch (UnauthorizedException ue) {
402 Response response = Response.status(
403 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
404 throw new WebApplicationException(response);
405 } catch (Exception e) {
406 if (logger.isDebugEnabled()) {
407 logger.debug("Caught exception in search for Acquisitions", e);
409 Response response = Response.status(
410 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
411 throw new WebApplicationException(response);
413 return acquisitionObjectList;
417 * Gets the authority refs.
419 * @param csid the csid
422 * @return the authority refs
425 @Path("{csid}/authorityrefs")
426 @Produces("application/xml")
427 public AuthorityRefList getAuthorityRefs(
428 @PathParam("csid") String csid,
429 @Context UriInfo ui) {
430 AuthorityRefList authRefList = null;
432 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
433 DocumentWrapper<DocumentModel> docWrapper =
434 getRepositoryClient(ctx).getDoc(ctx, csid);
435 RemoteDocumentModelHandlerImpl handler
436 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
437 List<String> authRefFields =
438 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
439 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
440 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
441 } catch (UnauthorizedException ue) {
442 Response response = Response.status(
443 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
444 throw new WebApplicationException(response);
445 } catch (Exception e) {
446 if (logger.isDebugEnabled()) {
447 logger.debug("Caught exception in getAuthorityRefs", e);
449 Response response = Response.status(
450 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
451 throw new WebApplicationException(response);