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.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.common.vocabulary.RefNameServiceUtils;
57 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
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 * The Class AcquisitionResource.
68 @Path("/acquisitions")
69 @Consumes("multipart/mixed")
70 @Produces("multipart/mixed")
71 public class AcquisitionResource
72 extends AbstractCollectionSpaceResourceImpl {
74 /** The service name. */
75 final private String serviceName = "acquisitions";
78 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
81 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
84 protected String getVersionString() {
85 /** The last change revision. */
86 final String lastChangeRevision = "$LastChangedRevision$";
87 return lastChangeRevision;
91 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
94 public String getServiceName() {
99 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
102 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
103 DocumentHandler docHandler = ctx.getDocumentHandler();
104 if (ctx.getInput() != null) {
105 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
107 docHandler.setCommonPart((AcquisitionsCommon) obj);
114 * Instantiates a new acquisition resource.
116 public AcquisitionResource() {
121 * Creates the acquisition.
123 * @param input the input
125 * @return the response
128 public Response createAcquisition(MultipartInput input) {
131 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
132 DocumentHandler handler = createDocumentHandler(ctx);
133 String csid = getRepositoryClient(ctx).create(ctx, handler);
134 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
135 path.path("" + csid);
136 Response response = Response.created(path.build()).build();
138 } catch (UnauthorizedException ue) {
139 Response response = Response.status(
140 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
141 throw new WebApplicationException(response);
142 } catch (Exception e) {
143 if (logger.isDebugEnabled()) {
144 logger.debug("Caught exception in createAcquisition", e);
146 Response response = Response.status(
147 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
148 throw new WebApplicationException(response);
153 * Gets the acquisition.
155 * @param csid the csid
157 * @return the acquisition
161 public MultipartOutput getAcquisition(
162 @PathParam("csid") String csid) {
163 if (logger.isDebugEnabled()) {
164 logger.debug("getAcquisition with csid=" + csid);
166 if (csid == null || "".equals(csid)) {
167 logger.error("getAcquisition: missing csid!");
168 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
169 "get failed on Acquisition csid=" + csid).type(
170 "text/plain").build();
171 throw new WebApplicationException(response);
173 MultipartOutput result = null;
175 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
176 DocumentHandler handler = createDocumentHandler(ctx);
177 getRepositoryClient(ctx).get(ctx, csid, handler);
178 result = (MultipartOutput) ctx.getOutput();
179 } catch (UnauthorizedException ue) {
180 Response response = Response.status(
181 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
182 throw new WebApplicationException(response);
183 } catch (DocumentNotFoundException dnfe) {
184 if (logger.isDebugEnabled()) {
185 logger.debug("getAcquisition", dnfe);
187 Response response = Response.status(Response.Status.NOT_FOUND).entity(
188 "Get failed on Acquisition csid=" + csid).type(
189 "text/plain").build();
190 throw new WebApplicationException(response);
191 } catch (Exception e) {
192 if (logger.isDebugEnabled()) {
193 logger.debug("getAcquisition", e);
195 Response response = Response.status(
196 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
197 throw new WebApplicationException(response);
200 if (result == null) {
201 Response response = Response.status(Response.Status.NOT_FOUND).entity(
202 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
203 "text/plain").build();
204 throw new WebApplicationException(response);
210 * Gets the acquisition list.
213 * @param keywords the keywords
215 * @return the acquisition list
218 @Produces("application/xml")
219 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui,
220 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
221 AcquisitionsCommonList result = null;
222 if (keywords != null) {
223 result = searchAcquisitions(keywords);
225 result = getAcquisitionsList();
232 * Gets the acquisitions list.
234 * @return the acquisitions list
236 private AcquisitionsCommonList getAcquisitionsList() {
237 AcquisitionsCommonList acquisitionObjectList;
239 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
240 DocumentHandler handler = createDocumentHandler(ctx);
241 getRepositoryClient(ctx).getAll(ctx, handler);
242 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
243 } catch (UnauthorizedException ue) {
244 Response response = Response.status(
245 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
246 throw new WebApplicationException(response);
247 } catch (Exception e) {
248 if (logger.isDebugEnabled()) {
249 logger.debug("Caught exception in getAcquisitionList", e);
251 Response response = Response.status(
252 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
253 throw new WebApplicationException(response);
255 return acquisitionObjectList;
259 * Update acquisition.
261 * @param csid the csid
262 * @param theUpdate the the update
264 * @return the multipart output
268 public MultipartOutput updateAcquisition(
269 @PathParam("csid") String csid,
270 MultipartInput theUpdate) {
271 if (logger.isDebugEnabled()) {
272 logger.debug("updateAcquisition with csid=" + csid);
274 if (csid == null || "".equals(csid)) {
275 logger.error("updateAcquisition: missing csid!");
276 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
277 "update failed on Acquisition csid=" + csid).type(
278 "text/plain").build();
279 throw new WebApplicationException(response);
281 if (logger.isDebugEnabled()) {
282 logger.debug("updateAcquisition with input: ", theUpdate);
284 MultipartOutput result = null;
286 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
287 DocumentHandler handler = createDocumentHandler(ctx);
288 getRepositoryClient(ctx).update(ctx, csid, handler);
289 result = (MultipartOutput) ctx.getOutput();
290 } catch (UnauthorizedException ue) {
291 Response response = Response.status(
292 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
293 throw new WebApplicationException(response);
294 } catch (DocumentNotFoundException dnfe) {
295 if (logger.isDebugEnabled()) {
296 logger.debug("caugth exception in updateAcquisition", dnfe);
298 Response response = Response.status(Response.Status.NOT_FOUND).entity(
299 "Update failed on Acquisition csid=" + csid).type(
300 "text/plain").build();
301 throw new WebApplicationException(response);
302 } catch (Exception e) {
303 Response response = Response.status(
304 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
305 throw new WebApplicationException(response);
311 * Delete acquisition.
313 * @param csid the csid
315 * @return the response
319 public Response deleteAcquisition(@PathParam("csid") String csid) {
321 if (logger.isDebugEnabled()) {
322 logger.debug("deleteAcquisition with csid=" + csid);
324 if (csid == null || "".equals(csid)) {
325 logger.error("deleteAcquisition: missing csid!");
326 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
327 "delete failed on Acquisition csid=" + csid).type(
328 "text/plain").build();
329 throw new WebApplicationException(response);
332 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
333 getRepositoryClient(ctx).delete(ctx, csid);
334 return Response.status(HttpResponseCodes.SC_OK).build();
335 } catch (UnauthorizedException ue) {
336 Response response = Response.status(
337 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
338 throw new WebApplicationException(response);
339 } catch (DocumentNotFoundException dnfe) {
340 if (logger.isDebugEnabled()) {
341 logger.debug("caught exception in deleteAcquisition", dnfe);
343 Response response = Response.status(Response.Status.NOT_FOUND).entity(
344 "Delete failed on Acquisition csid=" + csid).type(
345 "text/plain").build();
346 throw new WebApplicationException(response);
347 } catch (Exception e) {
348 Response response = Response.status(
349 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
350 throw new WebApplicationException(response);
355 * Keywords search acquisitions.
358 * @param keywords the keywords
360 * @return the acquisitions common list
364 @Produces("application/xml")
365 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
366 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
367 return searchAcquisitions(keywords);
371 * Search acquisitions.
373 * @param keywords the keywords
375 * @return the acquisitions common list
377 private AcquisitionsCommonList searchAcquisitions(String keywords) {
378 AcquisitionsCommonList acquisitionObjectList;
380 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
381 DocumentHandler handler = createDocumentHandler(ctx);
383 // perform a keyword search
384 if (keywords != null && !keywords.isEmpty()) {
385 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
386 DocumentFilter documentFilter = handler.createDocumentFilter(ctx);
387 documentFilter.setWhereClause(whereClause);
388 if (logger.isDebugEnabled()) {
389 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
391 getRepositoryClient(ctx).getFiltered(ctx, handler);
393 getRepositoryClient(ctx).getAll(ctx, handler);
395 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
397 } catch (UnauthorizedException ue) {
398 Response response = Response.status(
399 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
400 throw new WebApplicationException(response);
401 } catch (Exception e) {
402 if (logger.isDebugEnabled()) {
403 logger.debug("Caught exception in search for Acquisitions", e);
405 Response response = Response.status(
406 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
407 throw new WebApplicationException(response);
409 return acquisitionObjectList;
413 * Gets the authority refs.
415 * @param csid the csid
418 * @return the authority refs
421 @Path("{csid}/authorityrefs")
422 @Produces("application/xml")
423 public AuthorityRefList getAuthorityRefs(
424 @PathParam("csid") String csid,
425 @Context UriInfo ui) {
426 AuthorityRefList authRefList = null;
428 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
429 DocumentWrapper<DocumentModel> docWrapper =
430 getRepositoryClient(ctx).getDoc(ctx, csid);
431 RemoteDocumentModelHandlerImpl handler
432 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
433 List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(RefNameServiceUtils.AUTH_REF_PROP);
434 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
435 } catch (UnauthorizedException ue) {
436 Response response = Response.status(
437 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
438 throw new WebApplicationException(response);
439 } catch (Exception e) {
440 if (logger.isDebugEnabled()) {
441 logger.debug("Caught exception in getAuthorityRefs", e);
443 Response response = Response.status(
444 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
445 throw new WebApplicationException(response);