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 java.util.List;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriBuilder;
43 import javax.ws.rs.core.UriInfo;
45 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
46 import org.collectionspace.services.common.ClientType;
47 import org.collectionspace.services.common.ServiceMain;
48 import org.collectionspace.services.common.context.RemoteServiceContext;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.repository.DocumentFilter;
51 import org.collectionspace.services.common.repository.DocumentHandler;
52 import org.collectionspace.services.common.repository.DocumentNotFoundException;
53 import org.collectionspace.services.vocabulary.nuxeo.VocabularyHandlerFactory;
54 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemDocumentModelHandler;
55 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemHandlerFactory;
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.repository.RepositoryInstance;
60 import org.nuxeo.ecm.core.client.NuxeoClient;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
64 @Path("/vocabularies")
65 @Consumes("multipart/mixed")
66 @Produces("multipart/mixed")
67 public class VocabularyResource extends AbstractCollectionSpaceResource {
69 private final static String vocabularyServiceName = "vocabularies";
70 private final static String vocabularyItemServiceName = "vocabularyitems";
71 final Logger logger = LoggerFactory.getLogger(VocabularyResource.class);
72 //FIXME retrieve client type from configuration
73 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
75 public VocabularyResource() {
80 public String getServiceName() {
81 return vocabularyServiceName;
84 public String getItemServiceName() {
85 return vocabularyItemServiceName;
89 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
90 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
97 public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
98 DocumentHandler docHandler = VocabularyHandlerFactory.getInstance().getHandler(
99 ctx.getRepositoryClientType().toString());
100 docHandler.setServiceContext(ctx);
101 if(ctx.getInput() != null){
102 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class);
104 docHandler.setCommonPart((VocabulariesCommon) obj);
110 private DocumentHandler createItemDocumentHandler(
111 RemoteServiceContext ctx,
112 String inVocabulary) throws Exception {
113 DocumentHandler docHandler = VocabularyItemHandlerFactory.getInstance().getHandler(
114 ctx.getRepositoryClientType().toString());
115 docHandler.setServiceContext(ctx);
116 ((VocabularyItemDocumentModelHandler)docHandler).setInVocabulary(inVocabulary);
117 if(ctx.getInput() != null){
118 Object obj = ctx.getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
119 VocabularyitemsCommon.class);
121 docHandler.setCommonPart((VocabularyitemsCommon) obj);
128 public Response createVocabulary(MultipartInput input) {
130 RemoteServiceContext ctx = createServiceContext(input);
131 DocumentHandler handler = createDocumentHandler(ctx);
132 String csid = getRepositoryClient(ctx).create(ctx, handler);
133 //vocabularyObject.setCsid(csid);
134 UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
135 path.path("" + csid);
136 Response response = Response.created(path.build()).build();
139 if(logger.isDebugEnabled()){
140 logger.debug("Caught exception in createVocabulary", e);
142 Response response = Response.status(
143 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
144 throw new WebApplicationException(response);
150 public MultipartOutput getVocabulary(@PathParam("csid") String csid) {
151 String idValue = null;
153 logger.error("getVocabulary: missing csid!");
154 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
155 "get failed on Vocabulary csid=" + csid).type(
156 "text/plain").build();
157 throw new WebApplicationException(response);
159 if(logger.isDebugEnabled()){
160 logger.debug("getVocabulary with path(id)=" + csid);
162 MultipartOutput result = null;
164 RemoteServiceContext ctx = createServiceContext(null);
165 DocumentHandler handler = createDocumentHandler(ctx);
166 getRepositoryClient(ctx).get(ctx, csid, handler);
167 result = ctx.getOutput();
168 }catch(DocumentNotFoundException dnfe){
169 if(logger.isDebugEnabled()){
170 logger.debug("getVocabulary", dnfe);
172 Response response = Response.status(Response.Status.NOT_FOUND).entity(
173 "Get failed on Vocabulary csid=" + csid).type(
174 "text/plain").build();
175 throw new WebApplicationException(response);
177 if(logger.isDebugEnabled()){
178 logger.debug("getVocabulary", e);
180 Response response = Response.status(
181 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
182 throw new WebApplicationException(response);
185 Response response = Response.status(Response.Status.NOT_FOUND).entity(
186 "Get failed, the requested Vocabulary CSID:" + csid + ": was not found.").type(
187 "text/plain").build();
188 throw new WebApplicationException(response);
194 @Produces("application/xml")
195 public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) {
196 VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList();
198 RemoteServiceContext ctx = createServiceContext(null);
199 MultivaluedMap<String,String> queryParams = ui.getQueryParameters();
200 DocumentHandler handler = createDocumentHandler(ctx);
201 DocumentFilter myFilter =
202 DocumentFilter.CreatePaginatedDocumentFilter(queryParams);
203 String nameQ = queryParams.getFirst("name");
205 myFilter.setWhereClause("vocabularies_common:refName='"+nameQ+"'");
207 handler.setDocumentFilter(myFilter);
208 getRepositoryClient(ctx).getFiltered(ctx, handler);
209 vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList();
211 if(logger.isDebugEnabled()){
212 logger.debug("Caught exception in getVocabularyList", e);
214 Response response = Response.status(
215 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
216 throw new WebApplicationException(response);
218 return vocabularyObjectList;
223 public MultipartOutput updateVocabulary(
224 @PathParam("csid") String csid,
225 MultipartInput theUpdate) {
226 if(logger.isDebugEnabled()){
227 logger.debug("updateVocabulary with csid=" + csid);
229 if(csid == null || "".equals(csid)){
230 logger.error("updateVocabulary: missing csid!");
231 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
232 "update failed on Vocabulary csid=" + csid).type(
233 "text/plain").build();
234 throw new WebApplicationException(response);
236 MultipartOutput result = null;
238 RemoteServiceContext ctx = createServiceContext(theUpdate);
239 DocumentHandler handler = createDocumentHandler(ctx);
240 getRepositoryClient(ctx).update(ctx, csid, handler);
241 result = ctx.getOutput();
242 }catch(DocumentNotFoundException dnfe){
243 if(logger.isDebugEnabled()){
244 logger.debug("caugth exception in updateVocabulary", dnfe);
246 Response response = Response.status(Response.Status.NOT_FOUND).entity(
247 "Update failed on Vocabulary csid=" + csid).type(
248 "text/plain").build();
249 throw new WebApplicationException(response);
251 Response response = Response.status(
252 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
253 throw new WebApplicationException(response);
260 public Response deleteVocabulary(@PathParam("csid") String csid) {
262 if(logger.isDebugEnabled()){
263 logger.debug("deleteVocabulary with csid=" + csid);
265 if(csid == null || "".equals(csid)){
266 logger.error("deleteVocabulary: missing csid!");
267 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
268 "delete failed on Vocabulary csid=" + csid).type(
269 "text/plain").build();
270 throw new WebApplicationException(response);
273 ServiceContext ctx = createServiceContext(null);
274 getRepositoryClient(ctx).delete(ctx, csid);
275 return Response.status(HttpResponseCodes.SC_OK).build();
276 }catch(DocumentNotFoundException dnfe){
277 if(logger.isDebugEnabled()){
278 logger.debug("caught exception in deleteVocabulary", dnfe);
280 Response response = Response.status(Response.Status.NOT_FOUND).entity(
281 "Delete failed on Vocabulary csid=" + csid).type(
282 "text/plain").build();
283 throw new WebApplicationException(response);
285 Response response = Response.status(
286 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
287 throw new WebApplicationException(response);
292 /*************************************************************************
293 * VocabularyItem parts - this is a sub-resource of Vocabulary
294 *************************************************************************/
297 @Path("{csid}/items")
298 public Response createVocabularyItem(@PathParam("csid") String parentcsid, MultipartInput input) {
300 RemoteServiceContext ctx = createServiceContext(input, getItemServiceName());
301 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
302 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
303 UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
304 path.path(parentcsid + "/items/" + itemcsid);
305 Response response = Response.created(path.build()).build();
308 if(logger.isDebugEnabled()){
309 logger.debug("Caught exception in createVocabularyItem", e);
311 Response response = Response.status(
312 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
313 throw new WebApplicationException(response);
318 @Path("{csid}/items/{itemcsid}")
319 public MultipartOutput getVocabularyItem(
320 @PathParam("csid") String parentcsid,
321 @PathParam("itemcsid") String itemcsid) {
322 if(logger.isDebugEnabled()){
323 logger.debug("getVocabularyItem with parentcsid="
324 + parentcsid + " and itemcsid=" + itemcsid);
326 if(parentcsid == null || "".equals(parentcsid)){
327 logger.error("getVocabularyItem: missing csid!");
328 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
329 "get failed on VocabularyItem csid=" + parentcsid).type(
330 "text/plain").build();
331 throw new WebApplicationException(response);
333 if(itemcsid == null || "".equals(itemcsid)){
334 logger.error("getVocabularyItem: missing itemcsid!");
335 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
336 "get failed on VocabularyItem itemcsid=" + itemcsid).type(
337 "text/plain").build();
338 throw new WebApplicationException(response);
340 MultipartOutput result = null;
342 // Note that we have to create the service context for the Items, not the main service
343 RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
344 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
345 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
346 // TODO should we assert that the item is in the passed vocab?
347 result = ctx.getOutput();
348 }catch(DocumentNotFoundException dnfe){
349 if(logger.isDebugEnabled()){
350 logger.debug("getVocabularyItem", dnfe);
352 Response response = Response.status(Response.Status.NOT_FOUND).entity(
353 "Get failed on VocabularyItem csid=" + itemcsid).type(
354 "text/plain").build();
355 throw new WebApplicationException(response);
357 if(logger.isDebugEnabled()){
358 logger.debug("getVocabularyItem", e);
360 Response response = Response.status(
361 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
362 throw new WebApplicationException(response);
365 Response response = Response.status(Response.Status.NOT_FOUND).entity(
366 "Get failed, the requested VocabularyItem CSID:" + itemcsid + ": was not found.").type(
367 "text/plain").build();
368 throw new WebApplicationException(response);
374 @Path("{csid}/items")
375 @Produces("application/xml")
376 public VocabularyitemsCommonList getVocabularyItemList(
377 @PathParam("csid") String parentcsid,
378 @Context UriInfo ui) {
379 VocabularyitemsCommonList vocabularyItemObjectList = new VocabularyitemsCommonList();
380 RepositoryInstance repoSession = null;
381 NuxeoClient client = null;
383 // Note that docType defaults to the ServiceName, so we're fine with that.
384 RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
385 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
386 DocumentFilter myFilter = new DocumentFilter(
387 "vocabularyitems_common:inVocabulary='"+parentcsid+"'", 0, 0);
388 handler.setDocumentFilter(myFilter);
389 getRepositoryClient(ctx).getFiltered(ctx, handler);
390 vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList();
392 if(logger.isDebugEnabled()){
393 logger.debug("Caught exception in getVocabularyItemList", e);
395 Response response = Response.status(
396 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
397 throw new WebApplicationException(response);
399 if(repoSession != null) {
402 client.releaseRepository(repoSession);
403 } catch (Exception e) {
404 logger.error("Could not close the repository session", e);
405 // no need to throw this service specific exception
409 return vocabularyItemObjectList;
413 @Path("{csid}/items/{itemcsid}")
414 public MultipartOutput updateVocabularyItem(
415 @PathParam("csid") String parentcsid,
416 @PathParam("itemcsid") String itemcsid,
417 MultipartInput theUpdate) {
418 if(logger.isDebugEnabled()){
419 logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
421 if(parentcsid == null || "".equals(parentcsid)){
422 logger.error("updateVocabularyItem: missing csid!");
423 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
424 "update failed on VocabularyItem parentcsid=" + parentcsid).type(
425 "text/plain").build();
426 throw new WebApplicationException(response);
428 if(itemcsid == null || "".equals(itemcsid)){
429 logger.error("updateVocabularyItem: missing itemcsid!");
430 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
431 "update failed on VocabularyItem=" + itemcsid).type(
432 "text/plain").build();
433 throw new WebApplicationException(response);
435 MultipartOutput result = null;
437 // Note that we have to create the service context for the Items, not the main service
438 RemoteServiceContext ctx = createServiceContext(theUpdate, getItemServiceName());
439 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
440 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
441 result = ctx.getOutput();
442 }catch(DocumentNotFoundException dnfe){
443 if(logger.isDebugEnabled()){
444 logger.debug("caugth exception in updateVocabularyItem", dnfe);
446 Response response = Response.status(Response.Status.NOT_FOUND).entity(
447 "Update failed on VocabularyItem csid=" + itemcsid).type(
448 "text/plain").build();
449 throw new WebApplicationException(response);
451 Response response = Response.status(
452 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
453 throw new WebApplicationException(response);
459 @Path("{csid}/items/{itemcsid}")
460 public Response deleteVocabularyItem(
461 @PathParam("csid") String parentcsid,
462 @PathParam("itemcsid") String itemcsid) {
463 if(logger.isDebugEnabled()){
464 logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
466 if(parentcsid == null || "".equals(parentcsid)){
467 logger.error("deleteVocabularyItem: missing csid!");
468 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
469 "delete failed on VocabularyItem parentcsid=" + parentcsid).type(
470 "text/plain").build();
471 throw new WebApplicationException(response);
473 if(itemcsid == null || "".equals(itemcsid)){
474 logger.error("deleteVocabularyItem: missing itemcsid!");
475 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
476 "delete failed on VocabularyItem=" + itemcsid).type(
477 "text/plain").build();
478 throw new WebApplicationException(response);
481 // Note that we have to create the service context for the Items, not the main service
482 RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
483 getRepositoryClient(ctx).delete(ctx, itemcsid);
484 return Response.status(HttpResponseCodes.SC_OK).build();
485 }catch(DocumentNotFoundException dnfe){
486 if(logger.isDebugEnabled()){
487 logger.debug("caught exception in deleteVocabulary", dnfe);
489 Response response = Response.status(Response.Status.NOT_FOUND).entity(
490 "Delete failed on VocabularyItem itemcsid=" + itemcsid).type(
491 "text/plain").build();
492 throw new WebApplicationException(response);
494 Response response = Response.status(
495 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
496 throw new WebApplicationException(response);