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