]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f41add84e448c7cff9b97495b991953774e9b448
[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.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriBuilder;
40 import javax.ws.rs.core.UriInfo;
41
42 import org.collectionspace.services.OrganizationJAXBSchema;
43 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
44 import org.collectionspace.services.common.ClientType;
45 import org.collectionspace.services.common.ServiceMain;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.BadRequestException;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.contact.ContactResource;
56 import org.collectionspace.services.contact.ContactsCommon;
57 import org.collectionspace.services.contact.ContactsCommonList;
58 import org.collectionspace.services.contact.ContactJAXBSchema;
59 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
60 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
61 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
62 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
63 import org.jboss.resteasy.util.HttpResponseCodes;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 @Path("/orgauthorities")
68 @Consumes("multipart/mixed")
69 @Produces("multipart/mixed")
70 public class OrgAuthorityResource extends AbstractCollectionSpaceResourceImpl {
71
72     private final static String orgAuthorityServiceName = "orgauthorities";
73     private final static String organizationServiceName = "organizations";
74     final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
75     //FIXME retrieve client type from configuration
76     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
77     private ContactResource contactResource = new ContactResource();
78
79     public OrgAuthorityResource() {
80         // do nothing
81     }
82
83     @Override
84     protected String getVersionString() {
85         /** The last change revision. */
86         final String lastChangeRevision = "$LastChangedRevision$";
87         return lastChangeRevision;
88     }
89     
90     @Override
91     public String getServiceName() {
92         return orgAuthorityServiceName;
93     }
94
95     public String getItemServiceName() {
96         return organizationServiceName;
97     }
98
99     public String getContactServiceName() {
100         return contactResource.getServiceName();
101     }
102
103     /*
104     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
105     RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
106     ctx.setInput(input);
107     return ctx;
108     }
109      */
110     @Override
111     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
112         DocumentHandler docHandler =ctx.getDocumentHandler();
113         if (ctx.getInput() != null) {
114             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
115             if (obj != null) {
116                 docHandler.setCommonPart((OrgauthoritiesCommon) obj);
117             }
118         }
119         return docHandler;
120     }
121
122     private DocumentHandler createItemDocumentHandler(
123             ServiceContext ctx,
124             String inAuthority) throws Exception {
125         DocumentHandler docHandler = ctx.getDocumentHandler();
126         ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
127         if (ctx.getInput() != null) {
128             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
129                     OrganizationsCommon.class);
130             if (obj != null) {
131                 docHandler.setCommonPart((OrganizationsCommon) obj);
132             }
133         }
134         return docHandler;
135     }
136
137     private DocumentHandler createContactDocumentHandler(
138             ServiceContext ctx, String inAuthority,
139             String inItem) throws Exception {
140         DocumentHandler docHandler = ctx.getDocumentHandler();
141         // Set the inAuthority and inItem values, which specify the
142         // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
143         // (e.g. Person, Organization) with which the Contact is associated.
144         ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
145         ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
146         if (ctx.getInput() != null) {
147             Object obj = ((MultipartServiceContext) ctx)
148                 .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
149                 ContactsCommon.class);
150             if (obj != null) {
151                 docHandler.setCommonPart((ContactsCommon) obj);
152             }
153         }
154         return docHandler;
155     }
156
157     @POST
158     public Response createOrgAuthority(MultipartInput input) {
159         try {
160             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
161             DocumentHandler handler = createDocumentHandler(ctx);
162             String csid = getRepositoryClient(ctx).create(ctx, handler);
163             //orgAuthorityObject.setCsid(csid);
164             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
165             path.path("" + csid);
166             Response response = Response.created(path.build()).build();
167             return response;
168         } catch (BadRequestException bre) {
169             Response response = Response.status(
170                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
171             throw new WebApplicationException(response);
172         } catch (UnauthorizedException ue) {
173             Response response = Response.status(
174                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
175             throw new WebApplicationException(response);
176         } catch (Exception e) {
177             if (logger.isDebugEnabled()) {
178                 logger.debug("Caught exception in createOrgAuthority", e);
179             }
180             Response response = Response.status(
181                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
182             throw new WebApplicationException(response);
183         }
184     }
185
186     @GET
187     @Path("{csid}")
188     public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
189         String idValue = null;
190         if (csid == null) {
191             logger.error("getOrgAuthority: missing csid!");
192             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
193                     "get failed on OrgAuthority csid=" + csid).type(
194                     "text/plain").build();
195             throw new WebApplicationException(response);
196         }
197         if (logger.isDebugEnabled()) {
198             logger.debug("getOrgAuthority with path(id)=" + csid);
199         }
200         MultipartOutput result = null;
201         try {
202             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
203             DocumentHandler handler = createDocumentHandler(ctx);
204             getRepositoryClient(ctx).get(ctx, csid, handler);
205             result = (MultipartOutput) ctx.getOutput();
206         } catch (UnauthorizedException ue) {
207             Response response = Response.status(
208                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
209             throw new WebApplicationException(response);
210         } catch (DocumentNotFoundException dnfe) {
211             if (logger.isDebugEnabled()) {
212                 logger.debug("getOrgAuthority", dnfe);
213             }
214             Response response = Response.status(Response.Status.NOT_FOUND).entity(
215                     "Get failed on OrgAuthority csid=" + csid).type(
216                     "text/plain").build();
217             throw new WebApplicationException(response);
218         } catch (Exception e) {
219             if (logger.isDebugEnabled()) {
220                 logger.debug("getOrgAuthority", e);
221             }
222             Response response = Response.status(
223                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
224             throw new WebApplicationException(response);
225         }
226         if (result == null) {
227             Response response = Response.status(Response.Status.NOT_FOUND).entity(
228                     "Get failed, the requested OrgAuthority CSID:" + csid + ": was not found.").type(
229                     "text/plain").build();
230             throw new WebApplicationException(response);
231         }
232         return result;
233     }
234
235     @GET
236     @Produces("application/xml")
237     public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
238         OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
239         try {
240             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
241             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
242             DocumentHandler handler = createDocumentHandler(ctx);
243             DocumentFilter myFilter = new DocumentFilter();
244             myFilter.setPagination(queryParams);
245             String nameQ = queryParams.getFirst("refName");
246             if (nameQ != null) {
247                 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
248             }
249             handler.setDocumentFilter(myFilter);
250             getRepositoryClient(ctx).getFiltered(ctx, handler);
251             orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
252         } catch (UnauthorizedException ue) {
253             Response response = Response.status(
254                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
255             throw new WebApplicationException(response);
256         } catch (Exception e) {
257             if (logger.isDebugEnabled()) {
258                 logger.debug("Caught exception in getOrgAuthorityList", e);
259             }
260             Response response = Response.status(
261                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
262             throw new WebApplicationException(response);
263         }
264         return orgAuthorityObjectList;
265     }
266
267     @PUT
268     @Path("{csid}")
269     public MultipartOutput updateOrgAuthority(
270             @PathParam("csid") String csid,
271             MultipartInput theUpdate) {
272         if (logger.isDebugEnabled()) {
273             logger.debug("updateOrgAuthority with csid=" + csid);
274         }
275         if (csid == null || "".equals(csid)) {
276             logger.error("updateOrgAuthority: missing csid!");
277             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
278                     "update failed on OrgAuthority csid=" + csid).type(
279                     "text/plain").build();
280             throw new WebApplicationException(response);
281         }
282         MultipartOutput result = null;
283         try {
284             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
285             DocumentHandler handler = createDocumentHandler(ctx);
286             getRepositoryClient(ctx).update(ctx, csid, handler);
287             result = (MultipartOutput) ctx.getOutput();
288         } catch (BadRequestException bre) {
289             Response response = Response.status(
290                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
291             throw new WebApplicationException(response);
292         } catch (UnauthorizedException ue) {
293             Response response = Response.status(
294                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
295             throw new WebApplicationException(response);
296         } catch (DocumentNotFoundException dnfe) {
297             if (logger.isDebugEnabled()) {
298                 logger.debug("caught exception in updateOrgAuthority", dnfe);
299             }
300             Response response = Response.status(Response.Status.NOT_FOUND).entity(
301                     "Update failed on OrgAuthority csid=" + csid).type(
302                     "text/plain").build();
303             throw new WebApplicationException(response);
304         } catch (Exception e) {
305             Response response = Response.status(
306                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
307             throw new WebApplicationException(response);
308         }
309         return result;
310     }
311
312     @DELETE
313     @Path("{csid}")
314     public Response deleteOrgAuthority(@PathParam("csid") String csid) {
315
316         if (logger.isDebugEnabled()) {
317             logger.debug("deleteOrgAuthority with csid=" + csid);
318         }
319         if (csid == null || "".equals(csid)) {
320             logger.error("deleteOrgAuthority: missing csid!");
321             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
322                     "delete failed on OrgAuthority csid=" + csid).type(
323                     "text/plain").build();
324             throw new WebApplicationException(response);
325         }
326         try {
327             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
328             getRepositoryClient(ctx).delete(ctx, csid);
329             return Response.status(HttpResponseCodes.SC_OK).build();
330         } catch (UnauthorizedException ue) {
331             Response response = Response.status(
332                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
333             throw new WebApplicationException(response);
334         } catch (DocumentNotFoundException dnfe) {
335             if (logger.isDebugEnabled()) {
336                 logger.debug("caught exception in deleteOrgAuthority", dnfe);
337             }
338             Response response = Response.status(Response.Status.NOT_FOUND).entity(
339                     "Delete failed on OrgAuthority csid=" + csid).type(
340                     "text/plain").build();
341             throw new WebApplicationException(response);
342         } catch (Exception e) {
343             Response response = Response.status(
344                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
345             throw new WebApplicationException(response);
346         }
347
348     }
349
350     /*************************************************************************
351      * Organization parts - this is a sub-resource of OrgAuthority
352      *************************************************************************/
353     @POST
354     @Path("{csid}/items")
355     public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
356         try {
357             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
358             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
359             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
360             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
361             path.path(parentcsid + "/items/" + itemcsid);
362             Response response = Response.created(path.build()).build();
363             return response;
364         } catch (BadRequestException bre) {
365             Response response = Response.status(
366                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
367             throw new WebApplicationException(response);
368         } catch (UnauthorizedException ue) {
369             Response response = Response.status(
370                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
371             throw new WebApplicationException(response);
372         } catch (Exception e) {
373             if (logger.isDebugEnabled()) {
374                 logger.debug("Caught exception in createOrganization", e);
375             }
376             Response response = Response.status(
377                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
378             throw new WebApplicationException(response);
379         }
380     }
381
382     @GET
383     @Path("{csid}/items/{itemcsid}")
384     public MultipartOutput getOrganization(
385             @PathParam("csid") String parentcsid,
386             @PathParam("itemcsid") String itemcsid) {
387         if (logger.isDebugEnabled()) {
388             logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
389         }
390         if (parentcsid == null || "".equals(parentcsid)) {
391             logger.error("getOrganization: missing csid!");
392             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
393                     "get failed on Organization csid=" + parentcsid).type(
394                     "text/plain").build();
395             throw new WebApplicationException(response);
396         }
397         if (itemcsid == null || "".equals(itemcsid)) {
398             logger.error("getOrganization: missing itemcsid!");
399             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
400                     "get failed on Organization itemcsid=" + itemcsid).type(
401                     "text/plain").build();
402             throw new WebApplicationException(response);
403         }
404         MultipartOutput result = null;
405         try {
406             // Note that we have to create the service context for the Items, not the main service
407             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
408             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
409             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
410             // TODO should we assert that the item is in the passed orgAuthority?
411             result = (MultipartOutput) ctx.getOutput();
412         } catch (UnauthorizedException ue) {
413             Response response = Response.status(
414                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
415             throw new WebApplicationException(response);
416         } catch (DocumentNotFoundException dnfe) {
417             if (logger.isDebugEnabled()) {
418                 logger.debug("getOrganization", dnfe);
419             }
420             Response response = Response.status(Response.Status.NOT_FOUND).entity(
421                     "Get failed on Organization csid=" + itemcsid).type(
422                     "text/plain").build();
423             throw new WebApplicationException(response);
424         } catch (Exception e) {
425             if (logger.isDebugEnabled()) {
426                 logger.debug("getOrganization", e);
427             }
428             Response response = Response.status(
429                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
430             throw new WebApplicationException(response);
431         }
432         if (result == null) {
433             Response response = Response.status(Response.Status.NOT_FOUND).entity(
434                     "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
435                     "text/plain").build();
436             throw new WebApplicationException(response);
437         }
438         return result;
439     }
440
441     @GET
442     @Path("{csid}/items")
443     @Produces("application/xml")
444     public OrganizationsCommonList getOrganizationList(
445             @PathParam("csid") String parentcsid,
446             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
447             @Context UriInfo ui) {
448         OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
449         try {
450             // Note that docType defaults to the ServiceName, so we're fine with that.
451             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
452             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
453             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
454             DocumentFilter myFilter = new DocumentFilter();
455             myFilter.setPagination(queryParams);
456             myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
457                         ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
458                         "'" + parentcsid + "'");
459             
460             // AND organizations_common:displayName LIKE '%partialTerm%'
461             if (partialTerm != null && !partialTerm.isEmpty()) {
462                 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
463                         ":" + OrganizationJAXBSchema.DISPLAY_NAME +
464                         " LIKE " + "'%" + partialTerm + "%'";
465                 myFilter.appendWhereClause(ptClause);
466             }            
467             handler.setDocumentFilter(myFilter);
468             getRepositoryClient(ctx).getFiltered(ctx, handler);
469             organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
470         } catch (UnauthorizedException ue) {
471             Response response = Response.status(
472                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
473             throw new WebApplicationException(response);
474         } catch (Exception e) {
475             if (logger.isDebugEnabled()) {
476                 logger.debug("Caught exception in getOrganizationList", e);
477             }
478             Response response = Response.status(
479                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
480             throw new WebApplicationException(response);
481         }
482         return organizationObjectList;
483     }
484
485     @PUT
486     @Path("{csid}/items/{itemcsid}")
487     public MultipartOutput updateOrganization(
488             @PathParam("csid") String parentcsid,
489             @PathParam("itemcsid") String itemcsid,
490             MultipartInput theUpdate) {
491         if (logger.isDebugEnabled()) {
492             logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
493         }
494         if (parentcsid == null || "".equals(parentcsid)) {
495             logger.error("updateOrganization: missing csid!");
496             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
497                     "update failed on Organization parentcsid=" + parentcsid).type(
498                     "text/plain").build();
499             throw new WebApplicationException(response);
500         }
501         if (itemcsid == null || "".equals(itemcsid)) {
502             logger.error("updateOrganization: missing itemcsid!");
503             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
504                     "update failed on Organization=" + itemcsid).type(
505                     "text/plain").build();
506             throw new WebApplicationException(response);
507         }
508         MultipartOutput result = null;
509         try {
510             // Note that we have to create the service context for the Items, not the main service
511             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
512             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
513             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
514             result = (MultipartOutput) ctx.getOutput();
515         } catch (BadRequestException bre) {
516             Response response = Response.status(
517                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
518             throw new WebApplicationException(response);
519         } catch (UnauthorizedException ue) {
520             Response response = Response.status(
521                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
522             throw new WebApplicationException(response);
523         } catch (DocumentNotFoundException dnfe) {
524             if (logger.isDebugEnabled()) {
525                 logger.debug("caught exception in updateOrganization", dnfe);
526             }
527             Response response = Response.status(Response.Status.NOT_FOUND).entity(
528                     "Update failed on Organization csid=" + itemcsid).type(
529                     "text/plain").build();
530             throw new WebApplicationException(response);
531         } catch (Exception e) {
532             Response response = Response.status(
533                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
534             throw new WebApplicationException(response);
535         }
536         return result;
537     }
538
539     @DELETE
540     @Path("{csid}/items/{itemcsid}")
541     public Response deleteOrganization(
542             @PathParam("csid") String parentcsid,
543             @PathParam("itemcsid") String itemcsid) {
544         if (logger.isDebugEnabled()) {
545             logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
546         }
547         if (parentcsid == null || "".equals(parentcsid)) {
548             logger.error("deleteOrganization: missing csid!");
549             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
550                     "delete failed on Organization parentcsid=" + parentcsid).type(
551                     "text/plain").build();
552             throw new WebApplicationException(response);
553         }
554         if (itemcsid == null || "".equals(itemcsid)) {
555             logger.error("deleteOrganization: missing itemcsid!");
556             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
557                     "delete failed on Organization=" + itemcsid).type(
558                     "text/plain").build();
559             throw new WebApplicationException(response);
560         }
561         try {
562             // Note that we have to create the service context for the Items, not the main service
563             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
564             getRepositoryClient(ctx).delete(ctx, itemcsid);
565             return Response.status(HttpResponseCodes.SC_OK).build();
566         } catch (UnauthorizedException ue) {
567             Response response = Response.status(
568                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
569             throw new WebApplicationException(response);
570         } catch (DocumentNotFoundException dnfe) {
571             if (logger.isDebugEnabled()) {
572                 logger.debug("caught exception in deleteOrganization", dnfe);
573             }
574             Response response = Response.status(Response.Status.NOT_FOUND).entity(
575                     "Delete failed on Organization itemcsid=" + itemcsid).type(
576                     "text/plain").build();
577             throw new WebApplicationException(response);
578         } catch (Exception e) {
579             Response response = Response.status(
580                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
581             throw new WebApplicationException(response);
582         }
583
584     }
585
586     /*************************************************************************
587      * Contact parts - this is a sub-resource of Organization (or "item")
588      *************************************************************************/
589     @POST
590     @Path("{parentcsid}/items/{itemcsid}/contacts")
591     public Response createContact(
592             @PathParam("parentcsid") String parentcsid,
593             @PathParam("itemcsid") String itemcsid,
594             MultipartInput input) {
595         try {
596             // Note that we have to create the service context and document
597             // handler for the Contact service, not the main service.
598             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getContactServiceName());
599             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
600             String csid = getRepositoryClient(ctx).create(ctx, handler);
601             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
602             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
603             Response response = Response.created(path.build()).build();
604             return response;
605         } catch (BadRequestException bre) {
606             Response response = Response.status(
607                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
608             throw new WebApplicationException(response);
609         } catch (UnauthorizedException ue) {
610             Response response = Response.status(
611                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
612             throw new WebApplicationException(response);
613         } catch (Exception e) {
614             if (logger.isDebugEnabled()) {
615                 logger.debug("Caught exception in createContact", e);
616             }
617             Response response = Response.status(
618                 Response.Status.INTERNAL_SERVER_ERROR)
619                 .entity("Attempt to create Contact failed.")
620                 .type("text/plain").build();
621             throw new WebApplicationException(response);
622         }
623
624     }
625
626     @GET
627     @Produces({"application/xml"})
628     @Path("{parentcsid}/items/{itemcsid}/contacts/")
629     public ContactsCommonList getContactList(
630             @PathParam("parentcsid") String parentcsid,
631             @PathParam("itemcsid") String itemcsid,
632             @Context UriInfo ui) {
633         ContactsCommonList contactObjectList = new ContactsCommonList();
634         try {
635             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
636             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
637             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
638             DocumentFilter myFilter = new DocumentFilter();
639             myFilter.setPagination(queryParams);
640             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
641                 ContactJAXBSchema.IN_AUTHORITY +
642                 "='" + parentcsid + "'" +
643                 " AND " +
644                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
645                 ContactJAXBSchema.IN_ITEM +
646                 "='" + itemcsid + "'" +
647                 " AND ecm:isProxy = 0");
648             handler.setDocumentFilter(myFilter);
649             getRepositoryClient(ctx).getFiltered(ctx, handler);
650             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
651         } catch (UnauthorizedException ue) {
652             Response response = Response.status(
653                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
654             throw new WebApplicationException(response);
655         } catch (Exception e) {
656             if (logger.isDebugEnabled()) {
657                 logger.debug("Caught exception in getContactsList", e);
658             }
659             Response response = Response.status(
660                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
661             throw new WebApplicationException(response);
662         }
663         return contactObjectList;
664     }
665
666     @GET
667     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
668     public MultipartOutput getContact(
669             @PathParam("parentcsid") String parentcsid,
670             @PathParam("itemcsid") String itemcsid,
671             @PathParam("csid") String csid) {
672         MultipartOutput result = null;
673        if (logger.isDebugEnabled()) {
674             logger.debug("getContact with parentCsid=" + parentcsid +
675             " itemcsid=" + itemcsid + " csid=" + csid);
676         }
677         try {
678             // Note that we have to create the service context and document
679             // handler for the Contact service, not the main service.
680             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
681             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
682             getRepositoryClient(ctx).get(ctx, csid, handler);
683             result = (MultipartOutput) ctx.getOutput();
684         } catch (UnauthorizedException ue) {
685             Response response = Response.status(
686                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
687             throw new WebApplicationException(response);
688         } catch (DocumentNotFoundException dnfe) {
689             if (logger.isDebugEnabled()) {
690                 logger.debug("getContact", dnfe);
691             }
692             Response response = Response.status(Response.Status.NOT_FOUND)
693                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
694                 .type("text/plain").build();
695             throw new WebApplicationException(response);
696         } catch (Exception e) {
697             if (logger.isDebugEnabled()) {
698                 logger.debug("getContact", e);
699             }
700             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
701                 .entity("Get contact failed")
702                 .type("text/plain").build();
703             throw new WebApplicationException(response);
704         }
705         if (result == null) {
706             Response response = Response.status(Response.Status.NOT_FOUND)
707                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
708                 .type("text/plain").build();
709             throw new WebApplicationException(response);
710         }
711         return result;
712
713     }
714
715     @PUT
716     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
717     public MultipartOutput updateContact(
718             @PathParam("parentcsid") String parentcsid,
719             @PathParam("itemcsid") String itemcsid,
720             @PathParam("csid") String csid,
721             MultipartInput theUpdate) {
722        if (logger.isDebugEnabled()) {
723             logger.debug("updateContact with parentcsid=" + parentcsid +
724             " itemcsid=" + itemcsid + " csid=" + csid);
725         }
726        if (parentcsid == null || parentcsid.trim().isEmpty()) {
727             logger.error("updateContact: missing csid!");
728             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
729                     "update failed on Contact parentcsid=" + parentcsid).type(
730                     "text/plain").build();
731             throw new WebApplicationException(response);
732         }
733         if (itemcsid == null || itemcsid.trim().isEmpty()) {
734             logger.error("updateContact: missing itemcsid!");
735             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
736                     "update failed on Contact=" + itemcsid).type(
737                     "text/plain").build();
738             throw new WebApplicationException(response);
739         }
740         if (csid == null || csid.trim().isEmpty()) {
741             logger.error("updateContact: missing csid!");
742             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
743                     "update failed on Contact=" + csid).type(
744                     "text/plain").build();
745             throw new WebApplicationException(response);
746         }
747         MultipartOutput result = null;
748         try {
749             // Note that we have to create the service context and document
750             // handler for the Contact service, not the main service.
751             ServiceContext ctx = MultipartServiceContextFactory.get()
752                 .createServiceContext(theUpdate, getContactServiceName());
753             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
754             getRepositoryClient(ctx).update(ctx, csid, handler);
755             result = (MultipartOutput) ctx.getOutput();
756         } catch (BadRequestException bre) {
757             Response response = Response.status(
758                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
759             throw new WebApplicationException(response);
760         } catch (UnauthorizedException ue) {
761             Response response = Response.status(
762                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
763             throw new WebApplicationException(response);
764         } catch (DocumentNotFoundException dnfe) {
765             if (logger.isDebugEnabled()) {
766                 logger.debug("caught exception in updateContact", dnfe);
767             }
768             Response response = Response.status(Response.Status.NOT_FOUND).entity(
769                     "Update failed on Contact csid=" + itemcsid).type(
770                     "text/plain").build();
771             throw new WebApplicationException(response);
772         } catch (Exception e) {
773             Response response = Response.status(
774                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
775             throw new WebApplicationException(response);
776         }
777         return result;
778     }
779
780     @DELETE
781     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
782     public Response deleteContact(
783             @PathParam("parentcsid") String parentcsid,
784             @PathParam("itemcsid") String itemcsid,
785             @PathParam("csid") String csid) {
786         if (logger.isDebugEnabled()) {
787             logger.debug("deleteContact with parentCsid=" + parentcsid +
788             " itemcsid=" + itemcsid + " csid=" + csid);
789         }
790         if (parentcsid == null || parentcsid.trim().isEmpty()) {
791             logger.error("deleteContact: missing parentcsid!");
792             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
793                     "delete contact failed on parentcsid=" + parentcsid).type(
794                     "text/plain").build();
795             throw new WebApplicationException(response);
796         }
797         if (itemcsid == null || itemcsid.trim().isEmpty()) {
798             logger.error("deleteContact: missing itemcsid!");
799             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
800                     "delete contact failed on itemcsid=" + itemcsid).type(
801                     "text/plain").build();
802             throw new WebApplicationException(response);
803         }
804         if (csid == null || csid.trim().isEmpty()) {
805             logger.error("deleteContact: missing csid!");
806             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
807                     "delete contact failed on csid=" + csid).type(
808                     "text/plain").build();
809             throw new WebApplicationException(response);
810         }
811         try {
812             // Note that we have to create the service context for the
813             // Contact service, not the main service.
814             ServiceContext ctx =
815                 MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
816             getRepositoryClient(ctx).delete(ctx, csid);
817             return Response.status(HttpResponseCodes.SC_OK).build();
818          } catch (UnauthorizedException ue) {
819             Response response = Response.status(
820                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
821             throw new WebApplicationException(response);
822          } catch (DocumentNotFoundException dnfe) {
823             if (logger.isDebugEnabled()) {
824                 logger.debug("Caught exception in deleteContact", dnfe);
825             }
826             Response response = Response.status(Response.Status.NOT_FOUND)
827                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
828                 .type("text/plain").build();
829             throw new WebApplicationException(response);
830        } catch (Exception e) {
831             Response response = Response.status(
832                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
833             throw new WebApplicationException(response);
834         }
835     }
836
837 }