]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
522b3aca3cd952de8b62b82f60806bc94c0dddbd
[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     /**
600      * Gets the vocabulary item list.
601      * 
602      * @param parentcsid the parentcsid
603      * @param partialTerm the partial term
604      * @param ui the ui
605      * 
606      * @return the vocabulary item list
607      */
608     @GET
609     @Path("urn:cspace:name({specifier})/items")
610     @Produces("application/xml")
611     public VocabularyitemsCommonList getVocabularyItemListByVocabName(
612             @PathParam("specifier") String specifier,
613             @QueryParam(IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
614             @Context UriInfo ui) {
615         try {
616                 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
617                 String whereClause =
618                         VocabularyJAXBSchema.VOCABULARIES_COMMON+
619                         ":"+VocabularyJAXBSchema.SHORT_IDENTIFIER+
620                         "='"+specifier+"'";
621                 // Need to get an Authority by name
622                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
623                 String parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
624                 return getVocabularyItemList(parentcsid, partialTerm, ui);
625         } catch (UnauthorizedException ue) {
626             Response response = Response.status(
627                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
628             throw new WebApplicationException(response);
629         } catch (Exception e) {
630             if (logger.isDebugEnabled()) {
631                 logger.debug("Caught exception in getVocabularyItemListByVocabName", e);
632             }
633             Response response = Response.status(
634                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
635             throw new WebApplicationException(response);
636         }
637         
638     }
639     
640     
641
642     /**
643      * Update vocabulary item.
644      * 
645      * @param parentcsid the parentcsid
646      * @param itemcsid the itemcsid
647      * @param theUpdate the the update
648      * 
649      * @return the multipart output
650      */
651     @PUT
652     @Path("{csid}/items/{itemcsid}")
653     public MultipartOutput updateVocabularyItem(
654             @PathParam("csid") String parentcsid,
655             @PathParam("itemcsid") String itemcsid,
656             MultipartInput theUpdate) {
657         if (logger.isDebugEnabled()) {
658             logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
659         }
660         if (parentcsid == null || "".equals(parentcsid)) {
661             logger.error("updateVocabularyItem: missing csid!");
662             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
663                     "update failed on VocabularyItem parentcsid=" + parentcsid).type(
664                     "text/plain").build();
665             throw new WebApplicationException(response);
666         }
667         if (itemcsid == null || "".equals(itemcsid)) {
668             logger.error("updateVocabularyItem: missing itemcsid!");
669             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
670                     "update failed on VocabularyItem=" + itemcsid).type(
671                     "text/plain").build();
672             throw new WebApplicationException(response);
673         }
674         MultipartOutput result = null;
675         try {
676             // Note that we have to create the service context for the Items, not the main service
677             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
678                         theUpdate);
679             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
680             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
681             result = (MultipartOutput) ctx.getOutput();
682         } catch (BadRequestException bre) {
683             Response response = Response.status(
684                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
685             throw new WebApplicationException(response);
686         } catch (UnauthorizedException ue) {
687             Response response = Response.status(
688                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
689             throw new WebApplicationException(response);
690         } catch (DocumentNotFoundException dnfe) {
691             if (logger.isDebugEnabled()) {
692                 logger.debug("caught DNF exception in updateVocabularyItem", dnfe);
693             }
694             Response response = Response.status(Response.Status.NOT_FOUND).entity(
695                     "Update failed on VocabularyItem csid=" + itemcsid).type(
696                     "text/plain").build();
697             throw new WebApplicationException(response);
698         } catch (Exception e) {
699             Response response = Response.status(
700                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
701             throw new WebApplicationException(response);
702         }
703         return result;
704     }
705
706     /**
707      * Delete vocabulary item.
708      * 
709      * @param parentcsid the parentcsid
710      * @param itemcsid the itemcsid
711      * 
712      * @return the response
713      */
714     @DELETE
715     @Path("{csid}/items/{itemcsid}")
716     public Response deleteVocabularyItem(
717             @PathParam("csid") String parentcsid,
718             @PathParam("itemcsid") String itemcsid) {
719         if (logger.isDebugEnabled()) {
720             logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
721         }
722         if (parentcsid == null || "".equals(parentcsid)) {
723             logger.error("deleteVocabularyItem: missing csid!");
724             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
725                     "delete failed on VocabularyItem parentcsid=" + parentcsid).type(
726                     "text/plain").build();
727             throw new WebApplicationException(response);
728         }
729         if (itemcsid == null || "".equals(itemcsid)) {
730             logger.error("deleteVocabularyItem: missing itemcsid!");
731             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
732                     "delete failed on VocabularyItem=" + itemcsid).type(
733                     "text/plain").build();
734             throw new WebApplicationException(response);
735         }
736         try {
737             // Note that we have to create the service context for the Items, not the main service
738             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
739             getRepositoryClient(ctx).delete(ctx, itemcsid);
740             return Response.status(HttpResponseCodes.SC_OK).build();
741         } catch (UnauthorizedException ue) {
742             Response response = Response.status(
743                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
744             throw new WebApplicationException(response);
745         } catch (DocumentNotFoundException dnfe) {
746             if (logger.isDebugEnabled()) {
747                 logger.debug("caught exception in deleteVocabulary", dnfe);
748             }
749             Response response = Response.status(Response.Status.NOT_FOUND).entity(
750                     "Delete failed on VocabularyItem itemcsid=" + itemcsid).type(
751                     "text/plain").build();
752             throw new WebApplicationException(response);
753         } catch (Exception e) {
754             Response response = Response.status(
755                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
756             throw new WebApplicationException(response);
757         }
758     }
759 }