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 javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
39 import javax.ws.rs.core.UriInfo;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
42 import org.collectionspace.services.common.context.MultipartServiceContext;
43 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.collectionspace.services.common.query.IQueryManager;
49 import org.collectionspace.services.common.query.QueryManager;
50 import org.collectionspace.services.common.security.UnauthorizedException;
51 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
52 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
53 import org.jboss.resteasy.util.HttpResponseCodes;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 @Path("/acquisitions")
58 @Consumes("multipart/mixed")
59 @Produces("multipart/mixed")
60 public class AcquisitionResource
61 extends AbstractCollectionSpaceResourceImpl {
63 final private String serviceName = "acquisitions";
64 final Logger logger = LoggerFactory.getLogger(AcquisitionResource.class);
67 protected String getVersionString() {
68 /** The last change revision. */
69 final String lastChangeRevision = "$LastChangedRevision$";
70 return lastChangeRevision;
74 public String getServiceName() {
79 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
80 DocumentHandler docHandler = ctx.getDocumentHandler();
81 if (ctx.getInput() != null) {
82 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class);
84 docHandler.setCommonPart((AcquisitionsCommon) obj);
90 public AcquisitionResource() {
95 public Response createAcquisition(MultipartInput input) {
98 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
99 DocumentHandler handler = createDocumentHandler(ctx);
100 String csid = getRepositoryClient(ctx).create(ctx, handler);
101 UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class);
102 path.path("" + csid);
103 Response response = Response.created(path.build()).build();
105 } catch (UnauthorizedException ue) {
106 Response response = Response.status(
107 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
108 throw new WebApplicationException(response);
109 } catch (Exception e) {
110 if (logger.isDebugEnabled()) {
111 logger.debug("Caught exception in createAcquisition", e);
113 Response response = Response.status(
114 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
115 throw new WebApplicationException(response);
121 public MultipartOutput getAcquisition(
122 @PathParam("csid") String csid) {
123 if (logger.isDebugEnabled()) {
124 logger.debug("getAcquisition with csid=" + csid);
126 if (csid == null || "".equals(csid)) {
127 logger.error("getAcquisition: missing csid!");
128 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
129 "get failed on Acquisition csid=" + csid).type(
130 "text/plain").build();
131 throw new WebApplicationException(response);
133 MultipartOutput result = null;
135 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
136 DocumentHandler handler = createDocumentHandler(ctx);
137 getRepositoryClient(ctx).get(ctx, csid, handler);
138 result = (MultipartOutput) ctx.getOutput();
139 } catch (UnauthorizedException ue) {
140 Response response = Response.status(
141 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
142 throw new WebApplicationException(response);
143 } catch (DocumentNotFoundException dnfe) {
144 if (logger.isDebugEnabled()) {
145 logger.debug("getAcquisition", dnfe);
147 Response response = Response.status(Response.Status.NOT_FOUND).entity(
148 "Get failed on Acquisition csid=" + csid).type(
149 "text/plain").build();
150 throw new WebApplicationException(response);
151 } catch (Exception e) {
152 if (logger.isDebugEnabled()) {
153 logger.debug("getAcquisition", e);
155 Response response = Response.status(
156 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
157 throw new WebApplicationException(response);
160 if (result == null) {
161 Response response = Response.status(Response.Status.NOT_FOUND).entity(
162 "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type(
163 "text/plain").build();
164 throw new WebApplicationException(response);
170 @Produces("application/xml")
171 public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) {
172 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
174 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
175 DocumentHandler handler = createDocumentHandler(ctx);
176 getRepositoryClient(ctx).getAll(ctx, handler);
177 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
178 } catch (UnauthorizedException ue) {
179 Response response = Response.status(
180 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
181 throw new WebApplicationException(response);
182 } catch (Exception e) {
183 if (logger.isDebugEnabled()) {
184 logger.debug("Caught exception in getAcquisitionList", e);
186 Response response = Response.status(
187 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
188 throw new WebApplicationException(response);
190 return acquisitionObjectList;
195 public MultipartOutput updateAcquisition(
196 @PathParam("csid") String csid,
197 MultipartInput theUpdate) {
198 if (logger.isDebugEnabled()) {
199 logger.debug("updateAcquisition with csid=" + csid);
201 if (csid == null || "".equals(csid)) {
202 logger.error("updateAcquisition: missing csid!");
203 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
204 "update failed on Acquisition csid=" + csid).type(
205 "text/plain").build();
206 throw new WebApplicationException(response);
208 if (logger.isDebugEnabled()) {
209 logger.debug("updateAcquisition with input: ", theUpdate);
211 MultipartOutput result = null;
213 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
214 DocumentHandler handler = createDocumentHandler(ctx);
215 getRepositoryClient(ctx).update(ctx, csid, handler);
216 result = (MultipartOutput) ctx.getOutput();
217 } catch (UnauthorizedException ue) {
218 Response response = Response.status(
219 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
220 throw new WebApplicationException(response);
221 } catch (DocumentNotFoundException dnfe) {
222 if (logger.isDebugEnabled()) {
223 logger.debug("caugth exception in updateAcquisition", dnfe);
225 Response response = Response.status(Response.Status.NOT_FOUND).entity(
226 "Update failed on Acquisition csid=" + csid).type(
227 "text/plain").build();
228 throw new WebApplicationException(response);
229 } catch (Exception e) {
230 Response response = Response.status(
231 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
232 throw new WebApplicationException(response);
239 public Response deleteAcquisition(@PathParam("csid") String csid) {
241 if (logger.isDebugEnabled()) {
242 logger.debug("deleteAcquisition with csid=" + csid);
244 if (csid == null || "".equals(csid)) {
245 logger.error("deleteAcquisition: missing csid!");
246 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
247 "delete failed on Acquisition csid=" + csid).type(
248 "text/plain").build();
249 throw new WebApplicationException(response);
252 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
253 getRepositoryClient(ctx).delete(ctx, csid);
254 return Response.status(HttpResponseCodes.SC_OK).build();
255 } catch (UnauthorizedException ue) {
256 Response response = Response.status(
257 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
258 throw new WebApplicationException(response);
259 } catch (DocumentNotFoundException dnfe) {
260 if (logger.isDebugEnabled()) {
261 logger.debug("caught exception in deleteAcquisition", dnfe);
263 Response response = Response.status(Response.Status.NOT_FOUND).entity(
264 "Delete failed on Acquisition csid=" + csid).type(
265 "text/plain").build();
266 throw new WebApplicationException(response);
267 } catch (Exception e) {
268 Response response = Response.status(
269 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
270 throw new WebApplicationException(response);
276 @Produces("application/xml")
277 public AcquisitionsCommonList keywordsSearchAcquisitions(@Context UriInfo ui,
278 @QueryParam (IQueryManager.SEARCH_TYPE_KEYWORDS) String keywords) {
279 AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList();
281 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
282 DocumentHandler handler = createDocumentHandler(ctx);
284 // perform a keyword search
285 if (keywords != null && !keywords.isEmpty()) {
286 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
287 DocumentFilter documentFilter = handler.getDocumentFilter();
288 documentFilter.setWhereClause(whereClause);
289 if (logger.isDebugEnabled()) {
290 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
292 getRepositoryClient(ctx).getFiltered(ctx, handler);
294 getRepositoryClient(ctx).getAll(ctx, handler);
296 acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList();
298 } catch (UnauthorizedException ue) {
299 Response response = Response.status(
300 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
301 throw new WebApplicationException(response);
302 } catch (Exception e) {
303 if (logger.isDebugEnabled()) {
304 logger.debug("Caught exception in search for Acquisitions", e);
306 Response response = Response.status(
307 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
308 throw new WebApplicationException(response);
310 return acquisitionObjectList;