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