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