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