]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9a2b00bfd327d8f414c00503bf8633fb873ed68e
[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          RepositoryInstance repoSession = null;
381          NuxeoClient client = null;
382          try{
383                  // Note that docType defaults to the ServiceName, so we're fine with that.
384              RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
385              DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
386              DocumentFilter myFilter = new DocumentFilter(
387                          "vocabularyitems_common:inVocabulary='"+parentcsid+"'", 0, 0);
388              handler.setDocumentFilter(myFilter);
389              getRepositoryClient(ctx).getFiltered(ctx, handler);
390              vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList();
391          }catch(Exception e){
392              if(logger.isDebugEnabled()){
393                  logger.debug("Caught exception in getVocabularyItemList", e);
394              }
395              Response response = Response.status(
396                      Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
397              throw new WebApplicationException(response);
398          } finally {
399              if(repoSession != null) {
400                      try {
401                          // release session
402                          client.releaseRepository(repoSession);
403                      } catch (Exception e) {
404                          logger.error("Could not close the repository session", e);
405                          // no need to throw this service specific exception
406                      }
407              }
408          }
409          return vocabularyItemObjectList;
410      }
411
412      @PUT
413      @Path("{csid}/items/{itemcsid}")
414      public MultipartOutput updateVocabularyItem(
415                  @PathParam("csid") String parentcsid,
416              @PathParam("itemcsid") String itemcsid,
417              MultipartInput theUpdate) {
418          if(logger.isDebugEnabled()){
419              logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
420          }
421          if(parentcsid == null || "".equals(parentcsid)){
422              logger.error("updateVocabularyItem: missing csid!");
423              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
424                      "update failed on VocabularyItem parentcsid=" + parentcsid).type(
425                      "text/plain").build();
426              throw new WebApplicationException(response);
427          }
428          if(itemcsid == null || "".equals(itemcsid)){
429              logger.error("updateVocabularyItem: missing itemcsid!");
430              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
431                      "update failed on VocabularyItem=" + itemcsid).type(
432                      "text/plain").build();
433              throw new WebApplicationException(response);
434          }
435          MultipartOutput result = null;
436          try{
437                  // Note that we have to create the service context for the Items, not the main service 
438              RemoteServiceContext ctx = createServiceContext(theUpdate, getItemServiceName());
439              DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
440              getRepositoryClient(ctx).update(ctx, itemcsid, handler);
441              result = ctx.getOutput();
442          }catch(DocumentNotFoundException dnfe){
443              if(logger.isDebugEnabled()){
444                  logger.debug("caugth exception in updateVocabularyItem", dnfe);
445              }
446              Response response = Response.status(Response.Status.NOT_FOUND).entity(
447                      "Update failed on VocabularyItem csid=" + itemcsid).type(
448                      "text/plain").build();
449              throw new WebApplicationException(response);
450          }catch(Exception e){
451              Response response = Response.status(
452                      Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
453              throw new WebApplicationException(response);
454          }
455          return result;
456      }
457
458      @DELETE
459      @Path("{csid}/items/{itemcsid}")
460      public Response deleteVocabularyItem(
461                  @PathParam("csid") String parentcsid,
462          @PathParam("itemcsid") String itemcsid) {
463          if(logger.isDebugEnabled()){
464              logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
465          }
466          if(parentcsid == null || "".equals(parentcsid)){
467              logger.error("deleteVocabularyItem: missing csid!");
468              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
469                      "delete failed on VocabularyItem parentcsid=" + parentcsid).type(
470                      "text/plain").build();
471              throw new WebApplicationException(response);
472          }
473          if(itemcsid == null || "".equals(itemcsid)){
474              logger.error("deleteVocabularyItem: missing itemcsid!");
475              Response response = Response.status(Response.Status.BAD_REQUEST).entity(
476                      "delete failed on VocabularyItem=" + itemcsid).type(
477                      "text/plain").build();
478              throw new WebApplicationException(response);
479          }
480          try{
481                  // Note that we have to create the service context for the Items, not the main service 
482              RemoteServiceContext ctx = createServiceContext(null, getItemServiceName());
483              getRepositoryClient(ctx).delete(ctx, itemcsid);
484              return Response.status(HttpResponseCodes.SC_OK).build();
485          }catch(DocumentNotFoundException dnfe){
486              if(logger.isDebugEnabled()){
487                  logger.debug("caught exception in deleteVocabulary", dnfe);
488              }
489              Response response = Response.status(Response.Status.NOT_FOUND).entity(
490                      "Delete failed on VocabularyItem itemcsid=" + itemcsid).type(
491                      "text/plain").build();
492              throw new WebApplicationException(response);
493          }catch(Exception e){
494              Response response = Response.status(
495                      Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
496              throw new WebApplicationException(response);
497          }
498
499      }
500 }