]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5b8a12830badffcc1cf30f1e6424d8c9e19ea0c5
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.vocabulary;
25
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.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriBuilder;
40 import javax.ws.rs.core.UriInfo;
41
42 import org.collectionspace.services.VocabularyItemJAXBSchema;
43 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
44 import org.collectionspace.services.common.ClientType;
45 import org.collectionspace.services.common.ServiceMain;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.BadRequestException;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemDocumentModelHandler;
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.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * The Class VocabularyResource.
64  */
65 @Path("/vocabularies")
66 @Consumes("multipart/mixed")
67 @Produces("multipart/mixed")
68 public class VocabularyResource extends
69                 AbstractMultiPartCollectionSpaceResourceImpl {
70
71     /** The Constant vocabularyServiceName. */
72     private final static String vocabularyServiceName = "vocabularies";
73     
74     /** The Constant vocabularyItemServiceName. */
75     private final static String vocabularyItemServiceName = "vocabularyitems";
76     
77     /** The logger. */
78     final Logger logger = LoggerFactory.getLogger(VocabularyResource.class);
79     //FIXME retrieve client type from configuration
80     /** The Constant CLIENT_TYPE. */
81     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
82
83     /**
84      * Instantiates a new vocabulary resource.
85      */
86     public VocabularyResource() {
87         // do nothing
88     }
89
90     /* (non-Javadoc)
91      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
92      */
93     @Override
94     protected String getVersionString() {
95         /** The last change revision. */
96         final String lastChangeRevision = "$LastChangedRevision$";
97         return lastChangeRevision;
98     }
99
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
102      */
103     @Override
104     public String getServiceName() {
105         return vocabularyServiceName;
106     }
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
110      */
111     @Override
112     public Class<VocabulariesCommon> getCommonPartClass() {
113         return VocabulariesCommon.class;
114     }
115     
116     /**
117      * Gets the item service name.
118      * 
119      * @return the item service name
120      */
121     public String getItemServiceName() {
122         return vocabularyItemServiceName;
123     }
124
125     /*
126     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
127     RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
128     ctx.setInput(input);
129     return ctx;
130     }
131      */
132 //    @Override
133 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
134 //        DocumentHandler docHandler = ctx.getDocumentHandler();
135 //        if (ctx.getInput() != null) {
136 //            Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class);
137 //            if (obj != null) {
138 //                docHandler.setCommonPart((VocabulariesCommon) obj);
139 //            }
140 //        }
141 //        return docHandler;
142 //    }
143     
144 //    @Override
145 //    public DocumentHandler createDocumentHandler(ServiceContext ctx)
146 //              throws Exception {
147 //        DocumentHandler docHandler = createDocumentHandler(ctx, VocabulariesCommon.class);
148 //        return docHandler;
149 //    }
150     
151     /**
152      * Creates the item document handler.
153      * 
154      * @param ctx the ctx
155      * @param inVocabulary the in vocabulary
156      * 
157      * @return the document handler
158      * 
159      * @throws Exception the exception
160      */
161     private DocumentHandler createItemDocumentHandler(
162                 ServiceContext<MultipartInput, MultipartOutput> ctx,
163             String inVocabulary)
164                         throws Exception {
165         VocabularyItemDocumentModelHandler docHandler;
166         
167         docHandler = (VocabularyItemDocumentModelHandler)createDocumentHandler(ctx,
168                         ctx.getCommonPartLabel(getItemServiceName()),
169                         VocabularyitemsCommon.class);           
170         docHandler.setInVocabulary(inVocabulary);
171
172         return docHandler;
173     }
174
175     /**
176      * Creates the vocabulary.
177      * 
178      * @param input the input
179      * 
180      * @return the response
181      */
182     @POST
183     public Response createVocabulary(MultipartInput input) {
184         try {
185             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
186             DocumentHandler handler = createDocumentHandler(ctx);
187             String csid = getRepositoryClient(ctx).create(ctx, handler);
188             //vocabularyObject.setCsid(csid);
189             UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
190             path.path("" + csid);
191             Response response = Response.created(path.build()).build();
192             return response;
193         } catch (UnauthorizedException ue) {
194             Response response = Response.status(
195                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
196             throw new WebApplicationException(response);
197         } catch (Exception e) {
198             if (logger.isDebugEnabled()) {
199                 logger.debug("Caught exception in createVocabulary", e);
200             }
201             Response response = Response.status(
202                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
203             throw new WebApplicationException(response);
204         }
205     }
206
207     /**
208      * Gets the vocabulary.
209      * 
210      * @param csid the csid
211      * 
212      * @return the vocabulary
213      */
214     @GET
215     @Path("{csid}")
216     public MultipartOutput getVocabulary(@PathParam("csid") String csid) {
217         String idValue = null;
218         if (csid == null) {
219             logger.error("getVocabulary: missing csid!");
220             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
221                     "get failed on Vocabulary csid=" + csid).type(
222                     "text/plain").build();
223             throw new WebApplicationException(response);
224         }
225         if (logger.isDebugEnabled()) {
226             logger.debug("getVocabulary with path(id)=" + csid);
227         }
228         MultipartOutput result = null;
229         try {
230             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
231             DocumentHandler handler = createDocumentHandler(ctx);
232             getRepositoryClient(ctx).get(ctx, csid, handler);
233             result = (MultipartOutput) ctx.getOutput();
234         } catch (UnauthorizedException ue) {
235             Response response = Response.status(
236                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
237             throw new WebApplicationException(response);
238         } catch (DocumentNotFoundException dnfe) {
239             if (logger.isDebugEnabled()) {
240                 logger.debug("getVocabulary", dnfe);
241             }
242             Response response = Response.status(Response.Status.NOT_FOUND).entity(
243                     "Get failed on Vocabulary csid=" + csid).type(
244                     "text/plain").build();
245             throw new WebApplicationException(response);
246         } catch (Exception e) {
247             if (logger.isDebugEnabled()) {
248                 logger.debug("getVocabulary", e);
249             }
250             Response response = Response.status(
251                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
252             throw new WebApplicationException(response);
253         }
254         
255         if (result == null) {
256             Response response = Response.status(Response.Status.NOT_FOUND).entity(
257                     "Get failed, the requested Vocabulary CSID:" + csid + ": was not found.").type(
258                     "text/plain").build();
259             throw new WebApplicationException(response);
260         }
261         
262         return result;
263     }
264
265     /**
266      * Gets the vocabulary list.
267      * 
268      * @param ui the ui
269      * 
270      * @return the vocabulary list
271      */
272     @GET
273     @Produces("application/xml")
274     public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) {
275         VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList();
276         try {
277             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
278             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
279             DocumentHandler handler = createDocumentHandler(ctx);
280 //            DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
281             DocumentFilter myFilter = handler.getDocumentFilter();
282 //            myFilter.setPagination(queryParams);
283             String nameQ = queryParams.getFirst("refName");
284             if (nameQ != null) {
285                 myFilter.setWhereClause("vocabularies_common:refName='" + nameQ + "'");
286             }
287 //            handler.setDocumentFilter(myFilter);
288             getRepositoryClient(ctx).getFiltered(ctx, handler);
289             vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList();
290         } catch (UnauthorizedException ue) {
291             Response response = Response.status(
292                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
293             throw new WebApplicationException(response);
294         } catch (Exception e) {
295             if (logger.isDebugEnabled()) {
296                 logger.debug("Caught exception in getVocabularyList", e);
297             }
298             Response response = Response.status(
299                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
300             throw new WebApplicationException(response);
301         }
302         return vocabularyObjectList;
303     }
304
305     /**
306      * Update vocabulary.
307      * 
308      * @param csid the csid
309      * @param theUpdate the the update
310      * 
311      * @return the multipart output
312      */
313     @PUT
314     @Path("{csid}")
315     public MultipartOutput updateVocabulary(
316             @PathParam("csid") String csid,
317             MultipartInput theUpdate) {
318         if (logger.isDebugEnabled()) {
319             logger.debug("updateVocabulary with csid=" + csid);
320         }
321         if (csid == null || "".equals(csid)) {
322             logger.error("updateVocabulary: missing csid!");
323             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
324                     "update failed on Vocabulary csid=" + csid).type(
325                     "text/plain").build();
326             throw new WebApplicationException(response);
327         }
328         MultipartOutput result = null;
329         try {
330             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
331             DocumentHandler handler = createDocumentHandler(ctx);
332             getRepositoryClient(ctx).update(ctx, csid, handler);
333             result = (MultipartOutput) ctx.getOutput();
334         } catch (UnauthorizedException ue) {
335             Response response = Response.status(
336                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
337             throw new WebApplicationException(response);
338         } catch (DocumentNotFoundException dnfe) {
339             if (logger.isDebugEnabled()) {
340                 logger.debug("caugth exception in updateVocabulary", dnfe);
341             }
342             Response response = Response.status(Response.Status.NOT_FOUND).entity(
343                     "Update failed on Vocabulary csid=" + csid).type(
344                     "text/plain").build();
345             throw new WebApplicationException(response);
346         } catch (Exception e) {
347             Response response = Response.status(
348                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
349             throw new WebApplicationException(response);
350         }
351         return result;
352     }
353
354     /**
355      * Delete vocabulary.
356      * 
357      * @param csid the csid
358      * 
359      * @return the response
360      */
361     @DELETE
362     @Path("{csid}")
363     public Response deleteVocabulary(@PathParam("csid") String csid) {
364
365         if (logger.isDebugEnabled()) {
366             logger.debug("deleteVocabulary with csid=" + csid);
367         }
368         if (csid == null || "".equals(csid)) {
369             logger.error("deleteVocabulary: missing csid!");
370             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
371                     "delete failed on Vocabulary csid=" + csid).type(
372                     "text/plain").build();
373             throw new WebApplicationException(response);
374         }
375         try {
376             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
377             getRepositoryClient(ctx).delete(ctx, csid);
378             return Response.status(HttpResponseCodes.SC_OK).build();
379         } catch (UnauthorizedException ue) {
380             Response response = Response.status(
381                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
382             throw new WebApplicationException(response);
383         } catch (DocumentNotFoundException dnfe) {
384             if (logger.isDebugEnabled()) {
385                 logger.debug("caught exception in deleteVocabulary", dnfe);
386             }
387             Response response = Response.status(Response.Status.NOT_FOUND).entity(
388                     "Delete failed on Vocabulary csid=" + csid).type(
389                     "text/plain").build();
390             throw new WebApplicationException(response);
391         } catch (Exception e) {
392             Response response = Response.status(
393                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
394             throw new WebApplicationException(response);
395         }
396
397     }
398
399     /*************************************************************************
400      * VocabularyItem parts - this is a sub-resource of Vocabulary
401      *************************************************************************/
402     @POST
403     @Path("{csid}/items")
404     public Response createVocabularyItem(@PathParam("csid") String parentcsid, MultipartInput input) {
405         try {
406             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
407                         input);
408             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
409             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
410             UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
411             path.path(parentcsid + "/items/" + itemcsid);
412             Response response = Response.created(path.build()).build();
413             return response;
414         } catch (BadRequestException bre) {
415             Response response = Response.status(
416                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
417             throw new WebApplicationException(response);
418         } catch (UnauthorizedException ue) {
419             Response response = Response.status(
420                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
421             throw new WebApplicationException(response);
422         } catch (Exception e) {
423             if (logger.isDebugEnabled()) {
424                 logger.debug("Caught exception in createVocabularyItem", e);
425             }
426             Response response = Response.status(
427                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
428             throw new WebApplicationException(response);
429         }
430     }
431
432     /**
433      * Gets the vocabulary item.
434      * 
435      * @param parentcsid the parentcsid
436      * @param itemcsid the itemcsid
437      * 
438      * @return the vocabulary item
439      */
440     @GET
441     @Path("{csid}/items/{itemcsid}")
442     public MultipartOutput getVocabularyItem(
443             @PathParam("csid") String parentcsid,
444             @PathParam("itemcsid") String itemcsid) {
445         if (logger.isDebugEnabled()) {
446             logger.debug("getVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
447         }
448         if (parentcsid == null || "".equals(parentcsid)) {
449             logger.error("getVocabularyItem: missing csid!");
450             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
451                     "get failed on VocabularyItem csid=" + parentcsid).type(
452                     "text/plain").build();
453             throw new WebApplicationException(response);
454         }
455         if (itemcsid == null || "".equals(itemcsid)) {
456             logger.error("getVocabularyItem: missing itemcsid!");
457             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
458                     "get failed on VocabularyItem itemcsid=" + itemcsid).type(
459                     "text/plain").build();
460             throw new WebApplicationException(response);
461         }
462         MultipartOutput result = null;
463         try {
464             // Note that we have to create the service context for the Items, not the main service
465             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
466             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
467             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
468             // TODO should we assert that the item is in the passed vocab?
469             result = (MultipartOutput) ctx.getOutput();
470         } catch (UnauthorizedException ue) {
471             Response response = Response.status(
472                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
473             throw new WebApplicationException(response);
474         } catch (DocumentNotFoundException dnfe) {
475             if (logger.isDebugEnabled()) {
476                 logger.debug("getVocabularyItem", dnfe);
477             }
478             Response response = Response.status(Response.Status.NOT_FOUND).entity(
479                     "Get failed on VocabularyItem csid=" + itemcsid).type(
480                     "text/plain").build();
481             throw new WebApplicationException(response);
482         } catch (Exception e) {
483             if (logger.isDebugEnabled()) {
484                 logger.debug("getVocabularyItem", e);
485             }
486             Response response = Response.status(
487                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
488             throw new WebApplicationException(response);
489         }
490         if (result == null) {
491             Response response = Response.status(Response.Status.NOT_FOUND).entity(
492                     "Get failed, the requested VocabularyItem CSID:" + itemcsid + ": was not found.").type(
493                     "text/plain").build();
494             throw new WebApplicationException(response);
495         }
496         return result;
497     }
498
499     /**
500      * Gets the vocabulary item list.
501      * 
502      * @param parentcsid the parentcsid
503      * @param partialTerm the partial term
504      * @param ui the ui
505      * 
506      * @return the vocabulary item list
507      */
508     @GET
509     @Path("{csid}/items")
510     @Produces("application/xml")
511     public VocabularyitemsCommonList getVocabularyItemList(
512             @PathParam("csid") String parentcsid,
513             @QueryParam(IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
514             @Context UriInfo ui) {
515         VocabularyitemsCommonList vocabularyItemObjectList = new VocabularyitemsCommonList();
516         try {
517             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
518             // Note that docType defaults to the ServiceName, so we're fine with that.
519             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
520                         queryParams);
521             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
522 //            DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
523             DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
524 //            myFilter.setPagination(queryParams);
525             // "vocabularyitems_common:inVocabulary='" + parentcsid + "'");
526             myFilter.setWhereClause(
527                     VocabularyItemJAXBSchema.VOCABULARYITEMS_COMMON + ":"
528                     + VocabularyItemJAXBSchema.IN_VOCABULARY + "="
529                     + "'" + parentcsid + "'");
530
531             // AND vocabularyitems_common:displayName LIKE '%partialTerm%'
532             if (partialTerm != null && !partialTerm.isEmpty()) {
533                 String ptClause = "AND "
534                         + VocabularyItemJAXBSchema.VOCABULARYITEMS_COMMON + ":"
535                         + VocabularyItemJAXBSchema.DISPLAY_NAME
536                         + " LIKE "
537                         + "'%" + partialTerm + "%'";
538                 myFilter.appendWhereClause(ptClause);
539             }
540
541             if (logger.isDebugEnabled()) {
542                 logger.debug("getVocabularyItemList filtered WHERE clause: "
543                         + myFilter.getWhereClause());
544             }
545
546             handler.setDocumentFilter(myFilter);
547             getRepositoryClient(ctx).getFiltered(ctx, handler);
548
549             vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList();
550         } catch (UnauthorizedException ue) {
551             Response response = Response.status(
552                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
553             throw new WebApplicationException(response);
554         } catch (Exception e) {
555             if (logger.isDebugEnabled()) {
556                 logger.debug("Caught exception in getVocabularyItemList", e);
557             }
558             Response response = Response.status(
559                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
560             throw new WebApplicationException(response);
561         }
562         return vocabularyItemObjectList;
563     }
564
565     /**
566      * Update vocabulary item.
567      * 
568      * @param parentcsid the parentcsid
569      * @param itemcsid the itemcsid
570      * @param theUpdate the the update
571      * 
572      * @return the multipart output
573      */
574     @PUT
575     @Path("{csid}/items/{itemcsid}")
576     public MultipartOutput updateVocabularyItem(
577             @PathParam("csid") String parentcsid,
578             @PathParam("itemcsid") String itemcsid,
579             MultipartInput theUpdate) {
580         if (logger.isDebugEnabled()) {
581             logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
582         }
583         if (parentcsid == null || "".equals(parentcsid)) {
584             logger.error("updateVocabularyItem: missing csid!");
585             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
586                     "update failed on VocabularyItem parentcsid=" + parentcsid).type(
587                     "text/plain").build();
588             throw new WebApplicationException(response);
589         }
590         if (itemcsid == null || "".equals(itemcsid)) {
591             logger.error("updateVocabularyItem: missing itemcsid!");
592             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
593                     "update failed on VocabularyItem=" + itemcsid).type(
594                     "text/plain").build();
595             throw new WebApplicationException(response);
596         }
597         MultipartOutput result = null;
598         try {
599             // Note that we have to create the service context for the Items, not the main service
600             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
601                         theUpdate);
602             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
603             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
604             result = (MultipartOutput) ctx.getOutput();
605         } catch (BadRequestException bre) {
606             Response response = Response.status(
607                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
608             throw new WebApplicationException(response);
609         } catch (UnauthorizedException ue) {
610             Response response = Response.status(
611                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
612             throw new WebApplicationException(response);
613         } catch (DocumentNotFoundException dnfe) {
614             if (logger.isDebugEnabled()) {
615                 logger.debug("caught DNF exception in updateVocabularyItem", dnfe);
616             }
617             Response response = Response.status(Response.Status.NOT_FOUND).entity(
618                     "Update failed on VocabularyItem csid=" + itemcsid).type(
619                     "text/plain").build();
620             throw new WebApplicationException(response);
621         } catch (Exception e) {
622             Response response = Response.status(
623                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
624             throw new WebApplicationException(response);
625         }
626         return result;
627     }
628
629     /**
630      * Delete vocabulary item.
631      * 
632      * @param parentcsid the parentcsid
633      * @param itemcsid the itemcsid
634      * 
635      * @return the response
636      */
637     @DELETE
638     @Path("{csid}/items/{itemcsid}")
639     public Response deleteVocabularyItem(
640             @PathParam("csid") String parentcsid,
641             @PathParam("itemcsid") String itemcsid) {
642         if (logger.isDebugEnabled()) {
643             logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
644         }
645         if (parentcsid == null || "".equals(parentcsid)) {
646             logger.error("deleteVocabularyItem: missing csid!");
647             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
648                     "delete failed on VocabularyItem parentcsid=" + parentcsid).type(
649                     "text/plain").build();
650             throw new WebApplicationException(response);
651         }
652         if (itemcsid == null || "".equals(itemcsid)) {
653             logger.error("deleteVocabularyItem: missing itemcsid!");
654             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
655                     "delete failed on VocabularyItem=" + itemcsid).type(
656                     "text/plain").build();
657             throw new WebApplicationException(response);
658         }
659         try {
660             // Note that we have to create the service context for the Items, not the main service
661             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
662             getRepositoryClient(ctx).delete(ctx, itemcsid);
663             return Response.status(HttpResponseCodes.SC_OK).build();
664         } catch (UnauthorizedException ue) {
665             Response response = Response.status(
666                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
667             throw new WebApplicationException(response);
668         } catch (DocumentNotFoundException dnfe) {
669             if (logger.isDebugEnabled()) {
670                 logger.debug("caught exception in deleteVocabulary", dnfe);
671             }
672             Response response = Response.status(Response.Status.NOT_FOUND).entity(
673                     "Delete failed on VocabularyItem itemcsid=" + itemcsid).type(
674                     "text/plain").build();
675             throw new WebApplicationException(response);
676         } catch (Exception e) {
677             Response response = Response.status(
678                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
679             throw new WebApplicationException(response);
680         }
681
682     }
683 }