]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f0ea4e94bb88b72f3d7233645d763347a1ea01ca
[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.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39
40 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
41 import org.collectionspace.services.common.ClientType;
42 import org.collectionspace.services.common.ServiceMain;
43 import org.collectionspace.services.common.context.RemoteServiceContext;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.repository.DocumentHandler;
46 import org.collectionspace.services.common.repository.DocumentNotFoundException;
47 import org.collectionspace.services.vocabulary.nuxeo.VocabularyHandlerFactory;
48 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemDocumentModelHandler;
49 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemHandlerFactory;
50 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
51 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
52 import org.jboss.resteasy.util.HttpResponseCodes;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 @Path("/vocabularies")
57 @Consumes("multipart/mixed")
58 @Produces("multipart/mixed")
59 public class VocabularyResource extends AbstractCollectionSpaceResource {
60
61     private final static String vocabularyServiceName = "vocabularies";
62     private final static String vocabularyItemServiceName = "vocabularyitems";
63     final Logger logger = LoggerFactory.getLogger(VocabularyResource.class);
64     //FIXME retrieve client type from configuration
65     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
66
67     public VocabularyResource() {
68         // do nothing
69     }
70
71     @Override
72     public String getServiceName() {
73         return vocabularyServiceName;
74     }
75
76     public String getItemServiceName() {
77         return vocabularyItemServiceName;
78     }
79
80     /*
81     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
82         RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
83         ctx.setInput(input);
84         return ctx;
85     }
86     */
87
88     @Override
89     public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception {
90         DocumentHandler docHandler = VocabularyHandlerFactory.getInstance().getHandler(
91                 ctx.getRepositoryClientType().toString());
92         docHandler.setServiceContext(ctx);
93         if(ctx.getInput() != null){
94             Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class);
95             if(obj != null){
96                 docHandler.setCommonPart((VocabulariesCommon) obj);
97             }
98         }
99         return docHandler;
100     }
101
102     private DocumentHandler createItemDocumentHandler(
103                 RemoteServiceContext ctx,
104                 String inVocabulary) throws Exception {
105         DocumentHandler docHandler = VocabularyItemHandlerFactory.getInstance().getHandler(
106                 ctx.getRepositoryClientType().toString());
107         docHandler.setServiceContext(ctx);
108         ((VocabularyItemDocumentModelHandler)docHandler).setInVocabulary(inVocabulary);
109         if(ctx.getInput() != null){
110             Object obj = ctx.getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
111                                                                                                 VocabularyitemsCommon.class);
112             if(obj != null){
113                 docHandler.setCommonPart((VocabularyitemsCommon) obj);
114             }
115         }
116         return docHandler;
117     }
118
119     @POST
120     public Response createVocabulary(MultipartInput input) {
121         try{
122             RemoteServiceContext ctx = createServiceContext(input);
123             DocumentHandler handler = createDocumentHandler(ctx);
124             String csid = getRepositoryClient(ctx).create(ctx, handler);
125             //vocabularyObject.setCsid(csid);
126             UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
127             path.path("" + csid);
128             Response response = Response.created(path.build()).build();
129             return response;
130         }catch(Exception e){
131             if(logger.isDebugEnabled()){
132                 logger.debug("Caught exception in createVocabulary", e);
133             }
134             Response response = Response.status(
135                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
136             throw new WebApplicationException(response);
137         }
138     }
139
140     @GET
141     @Path("{csid}")
142     public MultipartOutput getVocabulary(
143             @PathParam("csid") String csid) {
144         if(logger.isDebugEnabled()){
145             logger.debug("getVocabulary with csid=" + csid);
146         }
147         if(csid == null || "".equals(csid)){
148             logger.error("getVocabulary: missing csid!");
149             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
150                     "get failed on Vocabulary csid=" + csid).type(
151                     "text/plain").build();
152             throw new WebApplicationException(response);
153         }
154         MultipartOutput result = null;
155         try{
156             RemoteServiceContext ctx = createServiceContext(null);
157             DocumentHandler handler = createDocumentHandler(ctx);
158             getRepositoryClient(ctx).get(ctx, csid, handler);
159             result = ctx.getOutput();
160         }catch(DocumentNotFoundException dnfe){
161             if(logger.isDebugEnabled()){
162                 logger.debug("getVocabulary", dnfe);
163             }
164             Response response = Response.status(Response.Status.NOT_FOUND).entity(
165                     "Get failed on Vocabulary csid=" + csid).type(
166                     "text/plain").build();
167             throw new WebApplicationException(response);
168         }catch(Exception e){
169             if(logger.isDebugEnabled()){
170                 logger.debug("getVocabulary", e);
171             }
172             Response response = Response.status(
173                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
174             throw new WebApplicationException(response);
175         }
176         if(result == null){
177             Response response = Response.status(Response.Status.NOT_FOUND).entity(
178                     "Get failed, the requested Vocabulary CSID:" + csid + ": was not found.").type(
179                     "text/plain").build();
180             throw new WebApplicationException(response);
181         }
182         return result;
183     }
184
185     @GET
186     @Produces("application/xml")
187     public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) {
188         VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList();
189         try{
190             RemoteServiceContext ctx = createServiceContext(null);
191             DocumentHandler handler = createDocumentHandler(ctx);
192             getRepositoryClient(ctx).getAll(ctx, handler);
193             vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList();
194         }catch(Exception e){
195             if(logger.isDebugEnabled()){
196                 logger.debug("Caught exception in getVocabularyList", e);
197             }
198             Response response = Response.status(
199                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
200             throw new WebApplicationException(response);
201         }
202         return vocabularyObjectList;
203     }
204
205     @PUT
206     @Path("{csid}")
207     public MultipartOutput updateVocabulary(
208             @PathParam("csid") String csid,
209             MultipartInput theUpdate) {
210         if(logger.isDebugEnabled()){
211             logger.debug("updateVocabulary with csid=" + csid);
212         }
213         if(csid == null || "".equals(csid)){
214             logger.error("updateVocabulary: missing csid!");
215             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
216                     "update failed on Vocabulary csid=" + csid).type(
217                     "text/plain").build();
218             throw new WebApplicationException(response);
219         }
220         MultipartOutput result = null;
221         try{
222             RemoteServiceContext ctx = createServiceContext(theUpdate);
223             DocumentHandler handler = createDocumentHandler(ctx);
224             getRepositoryClient(ctx).update(ctx, csid, handler);
225             result = ctx.getOutput();
226         }catch(DocumentNotFoundException dnfe){
227             if(logger.isDebugEnabled()){
228                 logger.debug("caugth exception in updateVocabulary", dnfe);
229             }
230             Response response = Response.status(Response.Status.NOT_FOUND).entity(
231                     "Update failed on Vocabulary csid=" + csid).type(
232                     "text/plain").build();
233             throw new WebApplicationException(response);
234         }catch(Exception e){
235             Response response = Response.status(
236                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
237             throw new WebApplicationException(response);
238         }
239         return result;
240     }
241
242     @DELETE
243     @Path("{csid}")
244     public Response deleteVocabulary(@PathParam("csid") String csid) {
245
246         if(logger.isDebugEnabled()){
247             logger.debug("deleteVocabulary with csid=" + csid);
248         }
249         if(csid == null || "".equals(csid)){
250             logger.error("deleteVocabulary: missing csid!");
251             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
252                     "delete failed on Vocabulary csid=" + csid).type(
253                     "text/plain").build();
254             throw new WebApplicationException(response);
255         }
256         try{
257             ServiceContext ctx = createServiceContext(null);
258             getRepositoryClient(ctx).delete(ctx, csid);
259             return Response.status(HttpResponseCodes.SC_OK).build();
260         }catch(DocumentNotFoundException dnfe){
261             if(logger.isDebugEnabled()){
262                 logger.debug("caught exception in deleteVocabulary", dnfe);
263             }
264             Response response = Response.status(Response.Status.NOT_FOUND).entity(
265                     "Delete failed on Vocabulary csid=" + csid).type(
266                     "text/plain").build();
267             throw new WebApplicationException(response);
268         }catch(Exception e){
269             Response response = Response.status(
270                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
271             throw new WebApplicationException(response);
272         }
273
274     }
275
276     /*************************************************************************
277      * VocabularyItem parts - this is a sub-resource of Vocabulary
278      *************************************************************************/
279
280      @POST
281      @Path("{csid}/items")
282      public Response createVocabularyItem(@PathParam("csid") String parentcsid, MultipartInput input) {
283          try{
284              RemoteServiceContext ctx = createServiceContext(input, getItemServiceName());
285              DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
286              String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
287              UriBuilder path = UriBuilder.fromResource(VocabularyResource.class);
288              path.path(parentcsid + "/items/" + itemcsid);
289              Response response = Response.created(path.build()).build();
290              return response;
291          }catch(Exception e){
292              if(logger.isDebugEnabled()){
293                  logger.debug("Caught exception in createVocabularyItem", e);
294              }
295              Response response = Response.status(
296                      Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
297              throw new WebApplicationException(response);
298          }
299      }
300
301      @GET
302      @Path("{csid}/items/{itemcsid}")
303      public MultipartOutput getVocabularyItem(
304                  @PathParam("csid") String parentcsid,
305              @PathParam("itemcsid") String itemcsid) {
306          if(logger.isDebugEnabled()){
307              logger.debug("getVocabularyItem with parentcsid=" 
308                          + parentcsid + " and itemcsid=" + itemcsid);
309          }
310          if(parentcsid == null || "".equals(parentcsid)){
311              logger.error("getVocabularyItem: missing csid!");
312              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
313                      "get failed on VocabularyItem csid=" + parentcsid).type(
314                      "text/plain").build();
315              throw new WebApplicationException(response);
316          }
317          if(itemcsid == null || "".equals(itemcsid)){
318              logger.error("getVocabularyItem: missing itemcsid!");
319              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
320                      "get failed on VocabularyItem itemcsid=" + itemcsid).type(
321                      "text/plain").build();
322              throw new WebApplicationException(response);
323          }
324          MultipartOutput result = null;
325          try{
326                  // Note that we have to create the service context for the Items, not the main service 
327              RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
328              DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
329              getRepositoryClient(ctx).get(ctx, itemcsid, handler);
330              // TODO should we assert that the item is in the passed vocab?
331              result = ctx.getOutput();
332          }catch(DocumentNotFoundException dnfe){
333              if(logger.isDebugEnabled()){
334                  logger.debug("getVocabularyItem", dnfe);
335              }
336              Response response = Response.status(Response.Status.NOT_FOUND).entity(
337                      "Get failed on VocabularyItem csid=" + itemcsid).type(
338                      "text/plain").build();
339              throw new WebApplicationException(response);
340          }catch(Exception e){
341              if(logger.isDebugEnabled()){
342                  logger.debug("getVocabularyItem", e);
343              }
344              Response response = Response.status(
345                      Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
346              throw new WebApplicationException(response);
347          }
348          if(result == null){
349              Response response = Response.status(Response.Status.NOT_FOUND).entity(
350                      "Get failed, the requested VocabularyItem CSID:" + itemcsid + ": was not found.").type(
351                      "text/plain").build();
352              throw new WebApplicationException(response);
353          }
354          return result;
355      }
356
357      @GET
358      @Path("{csid}/items")
359      @Produces("application/xml")
360      public VocabularyitemsCommonList getVocabularyItemList(
361                  @PathParam("csid") String parentcsid,
362                  @Context UriInfo ui) {
363          VocabularyitemsCommonList vocabularyItemObjectList = new VocabularyitemsCommonList();
364          try{
365                  // Note that we have to create the service context for the Items, not the main service 
366              RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
367              DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
368              // HACK This should be a search with the parentcsid. The 
369              // handler.getCommonPartList method will filter these for us, 
370              // which is really silly, but works for now.
371              getRepositoryClient(ctx).getAll(ctx, handler);
372              vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList();
373          }catch(Exception e){
374              if(logger.isDebugEnabled()){
375                  logger.debug("Caught exception in getVocabularyItemList", e);
376              }
377              Response response = Response.status(
378                      Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
379              throw new WebApplicationException(response);
380          }
381          return vocabularyItemObjectList;
382      }
383
384      @PUT
385      @Path("{csid}/items/{itemcsid}")
386      public MultipartOutput updateVocabularyItem(
387                  @PathParam("csid") String parentcsid,
388              @PathParam("itemcsid") String itemcsid,
389              MultipartInput theUpdate) {
390          if(logger.isDebugEnabled()){
391              logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
392          }
393          if(parentcsid == null || "".equals(parentcsid)){
394              logger.error("updateVocabularyItem: missing csid!");
395              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
396                      "update failed on VocabularyItem parentcsid=" + parentcsid).type(
397                      "text/plain").build();
398              throw new WebApplicationException(response);
399          }
400          if(itemcsid == null || "".equals(itemcsid)){
401              logger.error("updateVocabularyItem: missing itemcsid!");
402              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
403                      "update failed on VocabularyItem=" + itemcsid).type(
404                      "text/plain").build();
405              throw new WebApplicationException(response);
406          }
407          MultipartOutput result = null;
408          try{
409                  // Note that we have to create the service context for the Items, not the main service 
410              RemoteServiceContext ctx = createServiceContext(theUpdate, getItemServiceName());
411              DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
412              getRepositoryClient(ctx).update(ctx, itemcsid, handler);
413              result = ctx.getOutput();
414          }catch(DocumentNotFoundException dnfe){
415              if(logger.isDebugEnabled()){
416                  logger.debug("caugth exception in updateVocabularyItem", dnfe);
417              }
418              Response response = Response.status(Response.Status.NOT_FOUND).entity(
419                      "Update failed on VocabularyItem csid=" + itemcsid).type(
420                      "text/plain").build();
421              throw new WebApplicationException(response);
422          }catch(Exception e){
423              Response response = Response.status(
424                      Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
425              throw new WebApplicationException(response);
426          }
427          return result;
428      }
429
430      @DELETE
431      @Path("{csid}/items/{itemcsid}")
432      public Response deleteVocabularyItem(
433                  @PathParam("csid") String parentcsid,
434          @PathParam("itemcsid") String itemcsid) {
435          if(logger.isDebugEnabled()){
436              logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
437          }
438          if(parentcsid == null || "".equals(parentcsid)){
439              logger.error("deleteVocabularyItem: missing csid!");
440              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
441                      "delete failed on VocabularyItem parentcsid=" + parentcsid).type(
442                      "text/plain").build();
443              throw new WebApplicationException(response);
444          }
445          if(itemcsid == null || "".equals(itemcsid)){
446              logger.error("deleteVocabularyItem: missing itemcsid!");
447              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
448                      "delete failed on VocabularyItem=" + itemcsid).type(
449                      "text/plain").build();
450              throw new WebApplicationException(response);
451          }
452          try{
453                  // Note that we have to create the service context for the Items, not the main service 
454              RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
455              getRepositoryClient(ctx).delete(ctx, itemcsid);
456              return Response.status(HttpResponseCodes.SC_OK).build();
457          }catch(DocumentNotFoundException dnfe){
458              if(logger.isDebugEnabled()){
459                  logger.debug("caught exception in deleteVocabulary", dnfe);
460              }
461              Response response = Response.status(Response.Status.NOT_FOUND).entity(
462                      "Delete failed on VocabularyItem itemcsid=" + itemcsid).type(
463                      "text/plain").build();
464              throw new WebApplicationException(response);
465          }catch(Exception e){
466              Response response = Response.status(
467                      Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
468              throw new WebApplicationException(response);
469          }
470
471      }
472 }