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.ServiceContext;
47 import org.collectionspace.services.common.document.DocumentFilter;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.document.DocumentWrapper;
51 import org.collectionspace.services.common.query.IQueryManager;
52 import org.collectionspace.services.common.query.QueryManager;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
55 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
58 import org.jboss.resteasy.util.HttpResponseCodes;
59 import org.nuxeo.ecm.core.api.DocumentModel;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
64 * The Class AcquisitionResource.
66 @Path("/acquisitions")
67 @Consumes("multipart/mixed")
68 @Produces("multipart/mixed")
69 public class AcquisitionResource
70 extends AbstractMultiPartCollectionSpaceResourceImpl {
72 /** The service name. */
73 final private String serviceName = "acquisitions";
76 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
79 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
82 protected String getVersionString() {
83 /** The last change revision. */
84 final String lastChangeRevision = "$LastChangedRevision$";
85 return lastChangeRevision;
89 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
92 public String getServiceName() {
97 public Class<AcquisitionsCommon> getCommonPartClass() {
98 return AcquisitionsCommon.class;
102 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
105 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
106 // DocumentHandler docHandler = ctx.getDocumentHandler();
107 // if (ctx.getInput() != null) {
108 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
109 // if (obj != null) {
110 // docHandler.setCommonPart((AcquisitionsCommon) obj);
113 // return docHandler;
117 * Instantiates a new acquisition resource.
119 public AcquisitionResource() {
124 * Creates the acquisition.
126 * @param input the input
128 * @return the response
131 public Response createAcquisition(MultipartInput input) {
134 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
135 DocumentHandler handler = createDocumentHandler(ctx);
136 String csid = getRepositoryClient(ctx).create(ctx, handler);
137 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
138 path.path("" + csid);
139 Response response = Response.created(path.build()).build();
141 } catch (UnauthorizedException ue) {
142 Response response = Response.status(
143 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
144 throw new WebApplicationException(response);
145 } catch (Exception e) {
146 if (logger.isDebugEnabled()) {
147 logger.debug("Caught exception in createAcquisition", e);
149 Response response = Response.status(
150 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
151 throw new WebApplicationException(response);
156 * Gets the acquisition.
158 * @param csid the csid
160 * @return the acquisition
164 public MultipartOutput getAcquisition(
165 @PathParam("csid") String csid) {
166 if (logger.isDebugEnabled()) {
167 logger.debug("getAcquisition with csid=" + csid);
169 if (csid == null || "".equals(csid)) {
170 logger.error("getAcquisition: missing csid!");
171 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
172 "get failed on Acquisition csid=" + csid).type(
173 "text/plain").build();
174 throw new WebApplicationException(response);
176 MultipartOutput result = null;
178 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
179 DocumentHandler handler = createDocumentHandler(ctx);
180 getRepositoryClient(ctx).get(ctx, csid, handler);
181 result = (MultipartOutput) ctx.getOutput();
182 } catch (UnauthorizedException ue) {
183 Response response = Response.status(
184 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
185 throw new WebApplicationException(response);
186 } catch (DocumentNotFoundException dnfe) {
187 if (logger.isDebugEnabled()) {
188 logger.debug("getAcquisition", dnfe);
190 Response response = Response.status(Response.Status.NOT_FOUND).entity(
191 "Get failed on Acquisition csid=" + csid).type(
192 "text/plain").build();
193 throw new WebApplicationException(response);
194 } catch (Exception e) {
195 if (logger.isDebugEnabled()) {
196 logger.debug("getAcquisition", e);
198 Response response = Response.status(
199 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
200 throw new WebApplicationException(response);
203 if (result == null) {
204 Response response = Response.status(Response.Status.NOT_FOUND).entity(
205 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
206 "text/plain").build();
207 throw new WebApplicationException(response);
213 * Gets the acquisition list.
216 * @param keywords the keywords
218 * @return the acquisition list
221 @Produces("application/xml")
222 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui,
223 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords) {
224 AcquisitionsCommonList result = null;
225 if (keywords != null) {
226 result = searchAcquisitions(keywords);
228 result = getAcquisitionsList();
235 * Gets the acquisitions list.
237 * @return the acquisitions list
239 private AcquisitionsCommonList getAcquisitionsList() {
240 AcquisitionsCommonList acquisitionObjectList;
242 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
243 DocumentHandler handler = createDocumentHandler(ctx);
244 getRepositoryClient(ctx).getAll(ctx, handler);
245 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
246 } catch (UnauthorizedException ue) {
247 Response response = Response.status(
248 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
249 throw new WebApplicationException(response);
250 } catch (Exception e) {
251 if (logger.isDebugEnabled()) {
252 logger.debug("Caught exception in getAcquisitionList", e);
254 Response response = Response.status(
255 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
256 throw new WebApplicationException(response);
258 return acquisitionObjectList;
262 * Update acquisition.
264 * @param csid the csid
265 * @param theUpdate the the update
267 * @return the multipart output
271 public MultipartOutput updateAcquisition(
272 @PathParam("csid") String csid,
273 MultipartInput theUpdate) {
274 if (logger.isDebugEnabled()) {
275 logger.debug("updateAcquisition with csid=" + csid);
277 if (csid == null || "".equals(csid)) {
278 logger.error("updateAcquisition: missing csid!");
279 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
280 "update failed on Acquisition csid=" + csid).type(
281 "text/plain").build();
282 throw new WebApplicationException(response);
284 if (logger.isDebugEnabled()) {
285 logger.debug("updateAcquisition with input: ", theUpdate);
287 MultipartOutput result = null;
289 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
290 DocumentHandler handler = createDocumentHandler(ctx);
291 getRepositoryClient(ctx).update(ctx, csid, handler);
292 result = (MultipartOutput) ctx.getOutput();
293 } catch (UnauthorizedException ue) {
294 Response response = Response.status(
295 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
296 throw new WebApplicationException(response);
297 } catch (DocumentNotFoundException dnfe) {
298 if (logger.isDebugEnabled()) {
299 logger.debug("caugth exception in updateAcquisition", dnfe);
301 Response response = Response.status(Response.Status.NOT_FOUND).entity(
302 "Update failed on Acquisition csid=" + csid).type(
303 "text/plain").build();
304 throw new WebApplicationException(response);
305 } catch (Exception e) {
306 Response response = Response.status(
307 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
308 throw new WebApplicationException(response);
314 * Delete acquisition.
316 * @param csid the csid
318 * @return the response
322 public Response deleteAcquisition(@PathParam("csid") String csid) {
324 if (logger.isDebugEnabled()) {
325 logger.debug("deleteAcquisition with csid=" + csid);
327 if (csid == null || "".equals(csid)) {
328 logger.error("deleteAcquisition: missing csid!");
329 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
330 "delete failed on Acquisition csid=" + csid).type(
331 "text/plain").build();
332 throw new WebApplicationException(response);
335 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
336 getRepositoryClient(ctx).delete(ctx, csid);
337 return Response.status(HttpResponseCodes.SC_OK).build();
338 } catch (UnauthorizedException ue) {
339 Response response = Response.status(
340 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
341 throw new WebApplicationException(response);
342 } catch (DocumentNotFoundException dnfe) {
343 if (logger.isDebugEnabled()) {
344 logger.debug("caught exception in deleteAcquisition", dnfe);
346 Response response = Response.status(Response.Status.NOT_FOUND).entity(
347 "Delete failed on Acquisition csid=" + csid).type(
348 "text/plain").build();
349 throw new WebApplicationException(response);
350 } catch (Exception e) {
351 Response response = Response.status(
352 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
353 throw new WebApplicationException(response);
358 * Keywords search acquisitions.
361 * @param keywords the keywords
363 * @return the acquisitions common list
367 @Produces("application/xml")
368 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
369 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
370 return searchAcquisitions(keywords);
374 * Search acquisitions.
376 * @param keywords the keywords
378 * @return the acquisitions common list
380 private AcquisitionsCommonList searchAcquisitions(String keywords) {
381 AcquisitionsCommonList acquisitionObjectList;
383 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
384 DocumentHandler handler = createDocumentHandler(ctx);
386 // perform a keyword search
387 if (keywords != null && !keywords.isEmpty()) {
388 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
389 DocumentFilter documentFilter = handler.getDocumentFilter();
390 documentFilter.setWhereClause(whereClause);
391 if (logger.isDebugEnabled()) {
392 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
394 getRepositoryClient(ctx).getFiltered(ctx, handler);
396 getRepositoryClient(ctx).getAll(ctx, handler);
398 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
400 } catch (UnauthorizedException ue) {
401 Response response = Response.status(
402 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
403 throw new WebApplicationException(response);
404 } catch (Exception e) {
405 if (logger.isDebugEnabled()) {
406 logger.debug("Caught exception in search for Acquisitions", e);
408 Response response = Response.status(
409 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
410 throw new WebApplicationException(response);
412 return acquisitionObjectList;
416 * Gets the authority refs.
418 * @param csid the csid
421 * @return the authority refs
424 @Path("{csid}/authorityrefs")
425 @Produces("application/xml")
426 public AuthorityRefList getAuthorityRefs(
427 @PathParam("csid") String csid,
428 @Context UriInfo ui) {
429 AuthorityRefList authRefList = null;
431 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
432 DocumentWrapper<DocumentModel> docWrapper =
433 getRepositoryClient(ctx).getDoc(ctx, csid);
434 RemoteDocumentModelHandlerImpl handler
435 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
436 List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(RefNameServiceUtils.AUTH_REF_PROP);
437 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
438 } catch (UnauthorizedException ue) {
439 Response response = Response.status(
440 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
441 throw new WebApplicationException(response);
442 } catch (Exception e) {
443 if (logger.isDebugEnabled()) {
444 logger.debug("Caught exception in getAuthorityRefs", e);
446 Response response = Response.status(
447 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
448 throw new WebApplicationException(response);