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.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;
64 @Path("/acquisitions")
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class AcquisitionResource
68 extends AbstractCollectionSpaceResourceImpl {
70 final private String serviceName = "acquisitions";
71 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
74 protected String getVersionString() {
75 /** The last change revision. */
76 final String lastChangeRevision = "$LastChangedRevision$";
77 return lastChangeRevision;
81 public String getServiceName() {
86 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
87 DocumentHandler docHandler = ctx.getDocumentHandler();
88 if (ctx.getInput() != null) {
89 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
91 docHandler.setCommonPart((AcquisitionsCommon) obj);
97 public AcquisitionResource() {
102 public Response createAcquisition(MultipartInput input) {
105 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
106 DocumentHandler handler = createDocumentHandler(ctx);
107 String csid = getRepositoryClient(ctx).create(ctx, handler);
108 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
109 path.path("" + csid);
110 Response response = Response.created(path.build()).build();
112 } catch (UnauthorizedException ue) {
113 Response response = Response.status(
114 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
115 throw new WebApplicationException(response);
116 } catch (Exception e) {
117 if (logger.isDebugEnabled()) {
118 logger.debug("Caught exception in createAcquisition", e);
120 Response response = Response.status(
121 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
122 throw new WebApplicationException(response);
128 public MultipartOutput getAcquisition(
129 @PathParam("csid") String csid) {
130 if (logger.isDebugEnabled()) {
131 logger.debug("getAcquisition with csid=" + csid);
133 if (csid == null || "".equals(csid)) {
134 logger.error("getAcquisition: missing csid!");
135 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
136 "get failed on Acquisition csid=" + csid).type(
137 "text/plain").build();
138 throw new WebApplicationException(response);
140 MultipartOutput result = null;
142 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
143 DocumentHandler handler = createDocumentHandler(ctx);
144 getRepositoryClient(ctx).get(ctx, csid, handler);
145 result = (MultipartOutput) ctx.getOutput();
146 } catch (UnauthorizedException ue) {
147 Response response = Response.status(
148 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
149 throw new WebApplicationException(response);
150 } catch (DocumentNotFoundException dnfe) {
151 if (logger.isDebugEnabled()) {
152 logger.debug("getAcquisition", dnfe);
154 Response response = Response.status(Response.Status.NOT_FOUND).entity(
155 "Get failed on Acquisition csid=" + csid).type(
156 "text/plain").build();
157 throw new WebApplicationException(response);
158 } catch (Exception e) {
159 if (logger.isDebugEnabled()) {
160 logger.debug("getAcquisition", e);
162 Response response = Response.status(
163 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
164 throw new WebApplicationException(response);
167 if (result == null) {
168 Response response = Response.status(Response.Status.NOT_FOUND).entity(
169 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
170 "text/plain").build();
171 throw new WebApplicationException(response);
177 @Produces("application/xml")
178 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
179 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
181 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
182 DocumentHandler handler = createDocumentHandler(ctx);
183 getRepositoryClient(ctx).getAll(ctx, handler);
184 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
185 } catch (UnauthorizedException ue) {
186 Response response = Response.status(
187 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
188 throw new WebApplicationException(response);
189 } catch (Exception e) {
190 if (logger.isDebugEnabled()) {
191 logger.debug("Caught exception in getAcquisitionList", e);
193 Response response = Response.status(
194 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
195 throw new WebApplicationException(response);
197 return acquisitionObjectList;
202 public MultipartOutput updateAcquisition(
203 @PathParam("csid") String csid,
204 MultipartInput theUpdate) {
205 if (logger.isDebugEnabled()) {
206 logger.debug("updateAcquisition with csid=" + csid);
208 if (csid == null || "".equals(csid)) {
209 logger.error("updateAcquisition: missing csid!");
210 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
211 "update failed on Acquisition csid=" + csid).type(
212 "text/plain").build();
213 throw new WebApplicationException(response);
215 if (logger.isDebugEnabled()) {
216 logger.debug("updateAcquisition with input: ", theUpdate);
218 MultipartOutput result = null;
220 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
221 DocumentHandler handler = createDocumentHandler(ctx);
222 getRepositoryClient(ctx).update(ctx, csid, handler);
223 result = (MultipartOutput) ctx.getOutput();
224 } catch (UnauthorizedException ue) {
225 Response response = Response.status(
226 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
227 throw new WebApplicationException(response);
228 } catch (DocumentNotFoundException dnfe) {
229 if (logger.isDebugEnabled()) {
230 logger.debug("caugth exception in updateAcquisition", dnfe);
232 Response response = Response.status(Response.Status.NOT_FOUND).entity(
233 "Update failed on Acquisition csid=" + csid).type(
234 "text/plain").build();
235 throw new WebApplicationException(response);
236 } catch (Exception e) {
237 Response response = Response.status(
238 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
239 throw new WebApplicationException(response);
246 public Response deleteAcquisition(@PathParam("csid") String csid) {
248 if (logger.isDebugEnabled()) {
249 logger.debug("deleteAcquisition with csid=" + csid);
251 if (csid == null || "".equals(csid)) {
252 logger.error("deleteAcquisition: missing csid!");
253 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
254 "delete failed on Acquisition csid=" + csid).type(
255 "text/plain").build();
256 throw new WebApplicationException(response);
259 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
260 getRepositoryClient(ctx).delete(ctx, csid);
261 return Response.status(HttpResponseCodes.SC_OK).build();
262 } catch (UnauthorizedException ue) {
263 Response response = Response.status(
264 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
265 throw new WebApplicationException(response);
266 } catch (DocumentNotFoundException dnfe) {
267 if (logger.isDebugEnabled()) {
268 logger.debug("caught exception in deleteAcquisition", dnfe);
270 Response response = Response.status(Response.Status.NOT_FOUND).entity(
271 "Delete failed on Acquisition csid=" + csid).type(
272 "text/plain").build();
273 throw new WebApplicationException(response);
274 } catch (Exception e) {
275 Response response = Response.status(
276 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
277 throw new WebApplicationException(response);
283 @Produces("application/xml")
284 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
285 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
286 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
288 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
289 DocumentHandler handler = createDocumentHandler(ctx);
291 // perform a keyword search
292 if (keywords != null && !keywords.isEmpty()) {
293 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
294 DocumentFilter documentFilter = handler.getDocumentFilter();
295 documentFilter.setWhereClause(whereClause);
296 if (logger.isDebugEnabled()) {
297 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
299 getRepositoryClient(ctx).getFiltered(ctx, handler);
301 getRepositoryClient(ctx).getAll(ctx, handler);
303 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
305 } catch (UnauthorizedException ue) {
306 Response response = Response.status(
307 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
308 throw new WebApplicationException(response);
309 } catch (Exception e) {
310 if (logger.isDebugEnabled()) {
311 logger.debug("Caught exception in search for Acquisitions", e);
313 Response response = Response.status(
314 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
315 throw new WebApplicationException(response);
317 return acquisitionObjectList;
321 @Path("{csid}/authorityrefs")
322 @Produces("application/xml")
323 public AuthorityRefList getAuthorityRefs(
324 @PathParam("csid") String csid,
325 @Context UriInfo ui) {
326 AuthorityRefList authRefList = null;
328 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
329 DocumentWrapper<DocumentModel> docWrapper =
330 getRepositoryClient(ctx).getDoc(ctx, csid);
331 RemoteDocumentModelHandlerImpl handler
332 = (RemoteDocumentModelHandlerImpl)createDocumentHandler(ctx);
333 List<String> authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues("authRef");
334 String prefix = ctx.getCommonPartLabel()+":";
335 authRefList = handler.getAuthorityRefs(docWrapper, prefix, 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);