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;
65 @Path("/acquisitions")
66 @Consumes("multipart/mixed")
67 @Produces("multipart/mixed")
68 public class AcquisitionResource
69 extends AbstractCollectionSpaceResourceImpl {
71 final private String serviceName = "acquisitions";
72 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
75 protected String getVersionString() {
76 /** The last change revision. */
77 final String lastChangeRevision = "$LastChangedRevision$";
78 return lastChangeRevision;
82 public String getServiceName() {
87 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
88 DocumentHandler docHandler = ctx.getDocumentHandler();
89 if (ctx.getInput() != null) {
90 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
92 docHandler.setCommonPart((AcquisitionsCommon) obj);
98 public AcquisitionResource() {
103 public Response createAcquisition(MultipartInput input) {
106 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
107 DocumentHandler handler = createDocumentHandler(ctx);
108 String csid = getRepositoryClient(ctx).create(ctx, handler);
109 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
110 path.path("" + csid);
111 Response response = Response.created(path.build()).build();
113 } catch (UnauthorizedException ue) {
114 Response response = Response.status(
115 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
116 throw new WebApplicationException(response);
117 } catch (Exception e) {
118 if (logger.isDebugEnabled()) {
119 logger.debug("Caught exception in createAcquisition", e);
121 Response response = Response.status(
122 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
123 throw new WebApplicationException(response);
129 public MultipartOutput getAcquisition(
130 @PathParam("csid") String csid) {
131 if (logger.isDebugEnabled()) {
132 logger.debug("getAcquisition with csid=" + csid);
134 if (csid == null || "".equals(csid)) {
135 logger.error("getAcquisition: missing csid!");
136 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
137 "get failed on Acquisition csid=" + csid).type(
138 "text/plain").build();
139 throw new WebApplicationException(response);
141 MultipartOutput result = null;
143 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
144 DocumentHandler handler = createDocumentHandler(ctx);
145 getRepositoryClient(ctx).get(ctx, csid, handler);
146 result = (MultipartOutput) ctx.getOutput();
147 } catch (UnauthorizedException ue) {
148 Response response = Response.status(
149 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
150 throw new WebApplicationException(response);
151 } catch (DocumentNotFoundException dnfe) {
152 if (logger.isDebugEnabled()) {
153 logger.debug("getAcquisition", dnfe);
155 Response response = Response.status(Response.Status.NOT_FOUND).entity(
156 "Get failed on Acquisition csid=" + csid).type(
157 "text/plain").build();
158 throw new WebApplicationException(response);
159 } catch (Exception e) {
160 if (logger.isDebugEnabled()) {
161 logger.debug("getAcquisition", e);
163 Response response = Response.status(
164 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
165 throw new WebApplicationException(response);
168 if (result == null) {
169 Response response = Response.status(Response.Status.NOT_FOUND).entity(
170 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
171 "text/plain").build();
172 throw new WebApplicationException(response);
178 @Produces("application/xml")
179 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
180 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
182 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
183 DocumentHandler handler = createDocumentHandler(ctx);
184 getRepositoryClient(ctx).getAll(ctx, handler);
185 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
186 } catch (UnauthorizedException ue) {
187 Response response = Response.status(
188 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
189 throw new WebApplicationException(response);
190 } catch (Exception e) {
191 if (logger.isDebugEnabled()) {
192 logger.debug("Caught exception in getAcquisitionList", e);
194 Response response = Response.status(
195 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
196 throw new WebApplicationException(response);
198 return acquisitionObjectList;
203 public MultipartOutput updateAcquisition(
204 @PathParam("csid") String csid,
205 MultipartInput theUpdate) {
206 if (logger.isDebugEnabled()) {
207 logger.debug("updateAcquisition with csid=" + csid);
209 if (csid == null || "".equals(csid)) {
210 logger.error("updateAcquisition: missing csid!");
211 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
212 "update failed on Acquisition csid=" + csid).type(
213 "text/plain").build();
214 throw new WebApplicationException(response);
216 if (logger.isDebugEnabled()) {
217 logger.debug("updateAcquisition with input: ", theUpdate);
219 MultipartOutput result = null;
221 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
222 DocumentHandler handler = createDocumentHandler(ctx);
223 getRepositoryClient(ctx).update(ctx, csid, handler);
224 result = (MultipartOutput) ctx.getOutput();
225 } catch (UnauthorizedException ue) {
226 Response response = Response.status(
227 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
228 throw new WebApplicationException(response);
229 } catch (DocumentNotFoundException dnfe) {
230 if (logger.isDebugEnabled()) {
231 logger.debug("caugth exception in updateAcquisition", dnfe);
233 Response response = Response.status(Response.Status.NOT_FOUND).entity(
234 "Update failed on Acquisition csid=" + csid).type(
235 "text/plain").build();
236 throw new WebApplicationException(response);
237 } catch (Exception e) {
238 Response response = Response.status(
239 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
240 throw new WebApplicationException(response);
247 public Response deleteAcquisition(@PathParam("csid") String csid) {
249 if (logger.isDebugEnabled()) {
250 logger.debug("deleteAcquisition with csid=" + csid);
252 if (csid == null || "".equals(csid)) {
253 logger.error("deleteAcquisition: missing csid!");
254 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
255 "delete failed on Acquisition csid=" + csid).type(
256 "text/plain").build();
257 throw new WebApplicationException(response);
260 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
261 getRepositoryClient(ctx).delete(ctx, csid);
262 return Response.status(HttpResponseCodes.SC_OK).build();
263 } catch (UnauthorizedException ue) {
264 Response response = Response.status(
265 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
266 throw new WebApplicationException(response);
267 } catch (DocumentNotFoundException dnfe) {
268 if (logger.isDebugEnabled()) {
269 logger.debug("caught exception in deleteAcquisition", dnfe);
271 Response response = Response.status(Response.Status.NOT_FOUND).entity(
272 "Delete failed on Acquisition csid=" + csid).type(
273 "text/plain").build();
274 throw new WebApplicationException(response);
275 } catch (Exception e) {
276 Response response = Response.status(
277 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
278 throw new WebApplicationException(response);
284 @Produces("application/xml")
285 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
286 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
287 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
289 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
290 DocumentHandler handler = createDocumentHandler(ctx);
292 // perform a keyword search
293 if (keywords != null && !keywords.isEmpty()) {
294 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
295 DocumentFilter documentFilter = handler.getDocumentFilter();
296 documentFilter.setWhereClause(whereClause);
297 if (logger.isDebugEnabled()) {
298 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
300 getRepositoryClient(ctx).getFiltered(ctx, handler);
302 getRepositoryClient(ctx).getAll(ctx, handler);
304 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
306 } catch (UnauthorizedException ue) {
307 Response response = Response.status(
308 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
309 throw new WebApplicationException(response);
310 } catch (Exception e) {
311 if (logger.isDebugEnabled()) {
312 logger.debug("Caught exception in search for Acquisitions", e);
314 Response response = Response.status(
315 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
316 throw new WebApplicationException(response);
318 return acquisitionObjectList;
322 @Path("{csid}/authorityrefs")
323 @Produces("application/xml")
324 public AuthorityRefList getAuthorityRefs(
325 @PathParam("csid") String csid,
326 @Context UriInfo ui) {
327 AuthorityRefList authRefList = null;
329 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
330 DocumentWrapper<DocumentModel> docWrapper =
331 getRepositoryClient(ctx).getDoc(ctx, csid);
332 RemoteDocumentModelHandlerImpl handler
333 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
334 List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(RefNameServiceUtils.AUTH_REF_PROP);
335 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
336 } catch (UnauthorizedException ue) {
337 Response response = Response.status(
338 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
339 throw new WebApplicationException(response);
340 } catch (Exception e) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("Caught exception in getAuthorityRefs", e);
344 Response response = Response.status(
345 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
346 throw new WebApplicationException(response);