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.vocabulary;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
40 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
41 import org.collectionspace.services.common.ClientType;
42 import org.collectionspace.services.common.ServiceMain;
43 import org.collectionspace.services.common.context.RemoteServiceContext;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.repository.DocumentHandler;
46 import org.collectionspace.services.common.repository.DocumentNotFoundException;
47 import org.collectionspace.services.vocabulary.nuxeo.VocabularyHandlerFactory;
48 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemDocumentModelHandler;
49 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemHandlerFactory;
50 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
51 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
52 import org.jboss.resteasy.util.HttpResponseCodes;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 @Path("/vocabularies")
57 @Consumes("multipart/mixed")
58 @Produces("multipart/mixed")
59 public class VocabularyResource extends AbstractCollectionSpaceResource {
61 private final static String vocabularyServiceName = "vocabularies";
62 private final static String vocabularyItemServiceName = "vocabularyitems";
63 final Logger logger = LoggerFactory.getLogger(VocabularyResource.class);
64 //FIXME retrieve client type from configuration
65 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
67 public VocabularyResource() {
72 public String getServiceName() {
73 return vocabularyServiceName;
76 public String getItemServiceName() {
77 return vocabularyItemServiceName;
81 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
82 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
89 public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
90 DocumentHandler docHandler = VocabularyHandlerFactory.getInstance().getHandler(
91 ctx.getRepositoryClientType().toString());
92 docHandler.setServiceContext(ctx);
93 if(ctx.getInput() != null){
94 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class);
96 docHandler.setCommonPart((VocabulariesCommon) obj);
102 private DocumentHandler createItemDocumentHandler(
103 RemoteServiceContext ctx,
104 String inVocabulary) throws Exception {
105 DocumentHandler docHandler = VocabularyItemHandlerFactory.getInstance().getHandler(
106 ctx.getRepositoryClientType().toString());
107 docHandler.setServiceContext(ctx);
108 ((VocabularyItemDocumentModelHandler)docHandler).setInVocabulary(inVocabulary);
109 if(ctx.getInput() != null){
110 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
111 VocabularyitemsCommon.class);
113 docHandler.setCommonPart((VocabularyitemsCommon) obj);
120 public Response createVocabulary(MultipartInput input) {
122 RemoteServiceContext ctx = createServiceContext(input);
123 DocumentHandler handler = createDocumentHandler(ctx);
124 String csid = getRepositoryClient(ctx).create(ctx, handler);
125 //vocabularyObject.setCsid(csid);
126 UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
127 path.path("" + csid);
128 Response response = Response.created(path.build()).build();
131 if(logger.isDebugEnabled()){
132 logger.debug("Caught exception in createVocabulary", e);
134 Response response = Response.status(
135 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
136 throw new WebApplicationException(response);
142 public MultipartOutput getVocabulary(
143 @PathParam("csid") String csid) {
144 if(logger.isDebugEnabled()){
145 logger.debug("getVocabulary with csid=" + csid);
147 if(csid == null || "".equals(csid)){
148 logger.error("getVocabulary: missing csid!");
149 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
150 "get failed on Vocabulary csid=" + csid).type(
151 "text/plain").build();
152 throw new WebApplicationException(response);
154 MultipartOutput result = null;
156 RemoteServiceContext ctx = createServiceContext(null);
157 DocumentHandler handler = createDocumentHandler(ctx);
158 getRepositoryClient(ctx).get(ctx, csid, handler);
159 result = ctx.getOutput();
160 }catch(DocumentNotFoundException dnfe){
161 if(logger.isDebugEnabled()){
162 logger.debug("getVocabulary", dnfe);
164 Response response = Response.status(Response.Status.NOT_FOUND).entity(
165 "Get failed on Vocabulary csid=" + csid).type(
166 "text/plain").build();
167 throw new WebApplicationException(response);
169 if(logger.isDebugEnabled()){
170 logger.debug("getVocabulary", e);
172 Response response = Response.status(
173 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
174 throw new WebApplicationException(response);
177 Response response = Response.status(Response.Status.NOT_FOUND).entity(
178 "Get failed, the requested Vocabulary CSID:" + csid + ": was not found.").type(
179 "text/plain").build();
180 throw new WebApplicationException(response);
186 @Produces("application/xml")
187 public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) {
188 VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList();
190 RemoteServiceContext ctx = createServiceContext(null);
191 DocumentHandler handler = createDocumentHandler(ctx);
192 getRepositoryClient(ctx).getAll(ctx, handler);
193 vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList();
195 if(logger.isDebugEnabled()){
196 logger.debug("Caught exception in getVocabularyList", e);
198 Response response = Response.status(
199 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
200 throw new WebApplicationException(response);
202 return vocabularyObjectList;
207 public MultipartOutput updateVocabulary(
208 @PathParam("csid") String csid,
209 MultipartInput theUpdate) {
210 if(logger.isDebugEnabled()){
211 logger.debug("updateVocabulary with csid=" + csid);
213 if(csid == null || "".equals(csid)){
214 logger.error("updateVocabulary: missing csid!");
215 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
216 "update failed on Vocabulary csid=" + csid).type(
217 "text/plain").build();
218 throw new WebApplicationException(response);
220 MultipartOutput result = null;
222 RemoteServiceContext ctx = createServiceContext(theUpdate);
223 DocumentHandler handler = createDocumentHandler(ctx);
224 getRepositoryClient(ctx).update(ctx, csid, handler);
225 result = ctx.getOutput();
226 }catch(DocumentNotFoundException dnfe){
227 if(logger.isDebugEnabled()){
228 logger.debug("caugth exception in updateVocabulary", dnfe);
230 Response response = Response.status(Response.Status.NOT_FOUND).entity(
231 "Update failed on Vocabulary csid=" + csid).type(
232 "text/plain").build();
233 throw new WebApplicationException(response);
235 Response response = Response.status(
236 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
237 throw new WebApplicationException(response);
244 public Response deleteVocabulary(@PathParam("csid") String csid) {
246 if(logger.isDebugEnabled()){
247 logger.debug("deleteVocabulary with csid=" + csid);
249 if(csid == null || "".equals(csid)){
250 logger.error("deleteVocabulary: missing csid!");
251 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
252 "delete failed on Vocabulary csid=" + csid).type(
253 "text/plain").build();
254 throw new WebApplicationException(response);
257 ServiceContext ctx = createServiceContext(null);
258 getRepositoryClient(ctx).delete(ctx, csid);
259 return Response.status(HttpResponseCodes.SC_OK).build();
260 }catch(DocumentNotFoundException dnfe){
261 if(logger.isDebugEnabled()){
262 logger.debug("caught exception in deleteVocabulary", dnfe);
264 Response response = Response.status(Response.Status.NOT_FOUND).entity(
265 "Delete failed on Vocabulary csid=" + csid).type(
266 "text/plain").build();
267 throw new WebApplicationException(response);
269 Response response = Response.status(
270 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
271 throw new WebApplicationException(response);
276 /*************************************************************************
277 * VocabularyItem parts - this is a sub-resource of Vocabulary
278 *************************************************************************/
281 @Path("{csid}/items")
282 public Response createVocabularyItem(@PathParam("csid") String parentcsid, MultipartInput input) {
284 RemoteServiceContext ctx = createServiceContext(input, getItemServiceName());
285 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
286 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
287 UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
288 path.path(parentcsid + "/items/" + itemcsid);
289 Response response = Response.created(path.build()).build();
292 if(logger.isDebugEnabled()){
293 logger.debug("Caught exception in createVocabularyItem", e);
295 Response response = Response.status(
296 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
297 throw new WebApplicationException(response);
302 @Path("{csid}/items/{itemcsid}")
303 public MultipartOutput getVocabularyItem(
304 @PathParam("csid") String parentcsid,
305 @PathParam("itemcsid") String itemcsid) {
306 if(logger.isDebugEnabled()){
307 logger.debug("getVocabularyItem with parentcsid="
308 + parentcsid + " and itemcsid=" + itemcsid);
310 if(parentcsid == null || "".equals(parentcsid)){
311 logger.error("getVocabularyItem: missing csid!");
312 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
313 "get failed on VocabularyItem csid=" + parentcsid).type(
314 "text/plain").build();
315 throw new WebApplicationException(response);
317 if(itemcsid == null || "".equals(itemcsid)){
318 logger.error("getVocabularyItem: missing itemcsid!");
319 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
320 "get failed on VocabularyItem itemcsid=" + itemcsid).type(
321 "text/plain").build();
322 throw new WebApplicationException(response);
324 MultipartOutput result = null;
326 // Note that we have to create the service context for the Items, not the main service
327 RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
328 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
329 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
330 // TODO should we assert that the item is in the passed vocab?
331 result = ctx.getOutput();
332 }catch(DocumentNotFoundException dnfe){
333 if(logger.isDebugEnabled()){
334 logger.debug("getVocabularyItem", dnfe);
336 Response response = Response.status(Response.Status.NOT_FOUND).entity(
337 "Get failed on VocabularyItem csid=" + itemcsid).type(
338 "text/plain").build();
339 throw new WebApplicationException(response);
341 if(logger.isDebugEnabled()){
342 logger.debug("getVocabularyItem", e);
344 Response response = Response.status(
345 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
346 throw new WebApplicationException(response);
349 Response response = Response.status(Response.Status.NOT_FOUND).entity(
350 "Get failed, the requested VocabularyItem CSID:" + itemcsid + ": was not found.").type(
351 "text/plain").build();
352 throw new WebApplicationException(response);
358 @Path("{csid}/items")
359 @Produces("application/xml")
360 public VocabularyitemsCommonList getVocabularyItemList(
361 @PathParam("csid") String parentcsid,
362 @Context UriInfo ui) {
363 VocabularyitemsCommonList vocabularyItemObjectList = new VocabularyitemsCommonList();
365 // Note that we have to create the service context for the Items, not the main service
366 RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
367 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
368 // HACK This should be a search with the parentcsid. The
369 // handler.getCommonPartList method will filter these for us,
370 // which is really silly, but works for now.
371 getRepositoryClient(ctx).getAll(ctx, handler);
372 vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList();
374 if(logger.isDebugEnabled()){
375 logger.debug("Caught exception in getVocabularyItemList", e);
377 Response response = Response.status(
378 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
379 throw new WebApplicationException(response);
381 return vocabularyItemObjectList;
385 @Path("{csid}/items/{itemcsid}")
386 public MultipartOutput updateVocabularyItem(
387 @PathParam("csid") String parentcsid,
388 @PathParam("itemcsid") String itemcsid,
389 MultipartInput theUpdate) {
390 if(logger.isDebugEnabled()){
391 logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
393 if(parentcsid == null || "".equals(parentcsid)){
394 logger.error("updateVocabularyItem: missing csid!");
395 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
396 "update failed on VocabularyItem parentcsid=" + parentcsid).type(
397 "text/plain").build();
398 throw new WebApplicationException(response);
400 if(itemcsid == null || "".equals(itemcsid)){
401 logger.error("updateVocabularyItem: missing itemcsid!");
402 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
403 "update failed on VocabularyItem=" + itemcsid).type(
404 "text/plain").build();
405 throw new WebApplicationException(response);
407 MultipartOutput result = null;
409 // Note that we have to create the service context for the Items, not the main service
410 RemoteServiceContext ctx = createServiceContext(theUpdate, getItemServiceName());
411 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
412 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
413 result = ctx.getOutput();
414 }catch(DocumentNotFoundException dnfe){
415 if(logger.isDebugEnabled()){
416 logger.debug("caugth exception in updateVocabularyItem", dnfe);
418 Response response = Response.status(Response.Status.NOT_FOUND).entity(
419 "Update failed on VocabularyItem csid=" + itemcsid).type(
420 "text/plain").build();
421 throw new WebApplicationException(response);
423 Response response = Response.status(
424 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
425 throw new WebApplicationException(response);
431 @Path("{csid}/items/{itemcsid}")
432 public Response deleteVocabularyItem(
433 @PathParam("csid") String parentcsid,
434 @PathParam("itemcsid") String itemcsid) {
435 if(logger.isDebugEnabled()){
436 logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
438 if(parentcsid == null || "".equals(parentcsid)){
439 logger.error("deleteVocabularyItem: missing csid!");
440 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
441 "delete failed on VocabularyItem parentcsid=" + parentcsid).type(
442 "text/plain").build();
443 throw new WebApplicationException(response);
445 if(itemcsid == null || "".equals(itemcsid)){
446 logger.error("deleteVocabularyItem: missing itemcsid!");
447 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
448 "delete failed on VocabularyItem=" + itemcsid).type(
449 "text/plain").build();
450 throw new WebApplicationException(response);
453 // Note that we have to create the service context for the Items, not the main service
454 RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
455 getRepositoryClient(ctx).delete(ctx, itemcsid);
456 return Response.status(HttpResponseCodes.SC_OK).build();
457 }catch(DocumentNotFoundException dnfe){
458 if(logger.isDebugEnabled()){
459 logger.debug("caught exception in deleteVocabulary", dnfe);
461 Response response = Response.status(Response.Status.NOT_FOUND).entity(
462 "Delete failed on VocabularyItem itemcsid=" + itemcsid).type(
463 "text/plain").build();
464 throw new WebApplicationException(response);
466 Response response = Response.status(
467 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
468 throw new WebApplicationException(response);