]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8edf382108519b4e85903beefb4ffa5d48ce592a
[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 java.util.List;
27
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.QueryParam;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
43
44 import org.collectionspace.services.OrgAuthorityJAXBSchema;
45 import org.collectionspace.services.OrganizationJAXBSchema;
46 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.ClientType;
48 import org.collectionspace.services.common.ServiceMain;
49 import org.collectionspace.services.common.authorityref.AuthorityRefList;
50 import org.collectionspace.services.common.context.MultipartServiceContext;
51 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
52 //import org.collectionspace.services.common.context.MultipartServiceContextFactory;
53 import org.collectionspace.services.common.context.ServiceBindingUtils;
54 import org.collectionspace.services.common.context.ServiceContext;
55 import org.collectionspace.services.common.document.BadRequestException;
56 import org.collectionspace.services.common.document.DocumentFilter;
57 import org.collectionspace.services.common.document.DocumentHandler;
58 import org.collectionspace.services.common.document.DocumentNotFoundException;
59 import org.collectionspace.services.common.document.DocumentWrapper;
60 import org.collectionspace.services.common.query.IQueryManager;
61 import org.collectionspace.services.contact.ContactResource;
62 import org.collectionspace.services.contact.ContactsCommon;
63 import org.collectionspace.services.contact.ContactsCommonList;
64 import org.collectionspace.services.contact.ContactJAXBSchema;
65 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
66 import org.collectionspace.services.common.security.UnauthorizedException;
67 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
68 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
69 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
70 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
71 import org.jboss.resteasy.util.HttpResponseCodes;
72 import org.nuxeo.ecm.core.api.DocumentModel;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76 /**
77  * The Class OrgAuthorityResource.
78  */
79 @Path("/orgauthorities")
80 @Consumes("multipart/mixed")
81 @Produces("multipart/mixed")
82 public class OrgAuthorityResource extends
83                 AbstractMultiPartCollectionSpaceResourceImpl {
84
85     /** The Constant orgAuthorityServiceName. */
86     private final static String orgAuthorityServiceName = "orgauthorities";
87     
88     /** The Constant organizationServiceName. */
89     private final static String organizationServiceName = "organizations";
90     
91     /** The logger. */
92     final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
93     //FIXME retrieve client type from configuration
94     /** The Constant CLIENT_TYPE. */
95     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
96     
97     /** The contact resource. */
98     private ContactResource contactResource = new ContactResource();
99
100     /**
101      * Instantiates a new org authority resource.
102      */
103     public OrgAuthorityResource() {
104         // do nothing
105     }
106
107     /* (non-Javadoc)
108      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
109      */
110     @Override
111     protected String getVersionString() {
112         /** The last change revision. */
113         final String lastChangeRevision = "$LastChangedRevision$";
114         return lastChangeRevision;
115     }
116     
117     /* (non-Javadoc)
118      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
119      */
120     @Override
121     public String getServiceName() {
122         return orgAuthorityServiceName;
123     }
124     
125     /* (non-Javadoc)
126      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
127      */
128     @Override
129     public Class<OrgauthoritiesCommon> getCommonPartClass() {
130         return OrgauthoritiesCommon.class;
131     }    
132
133     /**
134      * Gets the item service name.
135      * 
136      * @return the item service name
137      */
138     public String getItemServiceName() {
139         return organizationServiceName;
140     }
141
142     /**
143      * Gets the contact service name.
144      * 
145      * @return the contact service name
146      */
147     public String getContactServiceName() {
148         return contactResource.getServiceName();
149     }
150
151     /*
152     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
153     RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
154     ctx.setInput(input);
155     return ctx;
156     }
157      */
158     /* (non-Javadoc)
159      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
160      */
161 //    @Override
162 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
163 //        DocumentHandler docHandler =ctx.getDocumentHandler();
164 //        if (ctx.getInput() != null) {
165 //            Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
166 //            if (obj != null) {
167 //                docHandler.setCommonPart((OrgauthoritiesCommon) obj);
168 //            }
169 //        }
170 //        return docHandler;
171 //    }
172
173     /**
174      * Creates the item document handler.
175      * 
176      * @param ctx the ctx
177      * @param inAuthority the in authority
178      * 
179      * @return the document handler
180      * 
181      * @throws Exception the exception
182      */
183     private DocumentHandler createItemDocumentHandler(
184                 ServiceContext<MultipartInput, MultipartOutput> ctx,
185             String inAuthority) throws Exception {      
186         OrganizationDocumentModelHandler docHandler = (OrganizationDocumentModelHandler)createDocumentHandler(
187                         ctx,
188                         ctx.getCommonPartLabel(getItemServiceName()),
189                         OrganizationsCommon.class);             
190         docHandler.setInAuthority(inAuthority);
191         
192         
193 //        ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
194 //        if (ctx.getInput() != null) {
195 //            Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
196 //                    OrganizationsCommon.class);
197 //            if (obj != null) {
198 //                docHandler.setCommonPart((OrganizationsCommon) obj);
199 //            }
200 //        }
201
202         return docHandler;
203     }
204
205     /**
206      * Creates the contact document handler.
207      * 
208      * @param ctx the ctx
209      * @param inAuthority the in authority
210      * @param inItem the in item
211      * 
212      * @return the document handler
213      * 
214      * @throws Exception the exception
215      */
216     private DocumentHandler createContactDocumentHandler(
217                 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
218             String inItem) throws Exception {
219         
220         ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
221                         ctx,
222                         ctx.getCommonPartLabel(getContactServiceName()),
223                         ContactsCommon.class);          
224         docHandler.setInAuthority(inAuthority);
225         docHandler.setInItem(inItem);
226         
227 //        DocumentHandler docHandler = ctx.getDocumentHandler();
228 //        // Set the inAuthority and inItem values, which specify the
229 //        // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
230 //        // (e.g. Person, Organization) with which the Contact is associated.
231 //        ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
232 //        ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
233 //        if (ctx.getInput() != null) {
234 //            Object obj = ((MultipartServiceContext) ctx)
235 //                .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
236 //                ContactsCommon.class);
237 //            if (obj != null) {
238 //                docHandler.setCommonPart((ContactsCommon) obj);
239 //            }
240 //        }
241         return docHandler;
242     }
243
244     /**
245      * Creates the org authority.
246      * 
247      * @param input the input
248      * 
249      * @return the response
250      */
251     @POST
252     public Response createOrgAuthority(MultipartInput input) {
253         try {
254                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
255             DocumentHandler handler = createDocumentHandler(ctx);
256             String csid = getRepositoryClient(ctx).create(ctx, handler);
257             //orgAuthorityObject.setCsid(csid);
258             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
259             path.path("" + csid);
260             Response response = Response.created(path.build()).build();
261             return response;
262         } catch (BadRequestException bre) {
263             Response response = Response.status(
264                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
265             throw new WebApplicationException(response);
266         } catch (UnauthorizedException ue) {
267             Response response = Response.status(
268                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
269             throw new WebApplicationException(response);
270         } catch (Exception e) {
271             if (logger.isDebugEnabled()) {
272                 logger.debug("Caught exception in createOrgAuthority", e);
273             }
274             Response response = Response.status(
275                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
276             throw new WebApplicationException(response);
277         }
278     }
279
280     /**
281      * Gets the org authority by name.
282      * 
283      * @param specifier the specifier
284      * 
285      * @return the org authority by name
286      */
287     @GET
288     @Path("urn:cspace:name({specifier})")
289     public MultipartOutput getOrgAuthorityByName(@PathParam("specifier") String specifier) {
290         String idValue = null;
291         if (specifier == null) {
292             logger.error("getOrgAuthority: missing name!");
293             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
294                     "get failed on OrgAuthority (missing specifier)").type(
295                     "text/plain").build();
296             throw new WebApplicationException(response);
297         }
298         String whereClause =
299                 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
300                 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
301                 "='"+specifier+"'";
302         // We only get a single doc - if there are multiple,
303         // it is an error in use.
304
305         if (logger.isDebugEnabled()) {
306             logger.debug("getOrgAuthority with name=" + specifier);
307         } 
308         MultipartOutput result = null;
309         try {
310                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
311             DocumentHandler handler = createDocumentHandler(ctx);
312             DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
313             handler.setDocumentFilter(myFilter);
314             getRepositoryClient(ctx).get(ctx, handler);
315             result = (MultipartOutput) ctx.getOutput();
316         } catch (UnauthorizedException ue) {
317             Response response = Response.status(
318                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
319             throw new WebApplicationException(response);
320         } catch (DocumentNotFoundException dnfe) {
321             if (logger.isDebugEnabled()) {
322                 logger.debug("getOrgAuthority", dnfe);
323             }
324             Response response = Response.status(Response.Status.NOT_FOUND).entity(
325                     "Get failed on OrgAuthority spec=" + specifier).type(
326                     "text/plain").build();
327             throw new WebApplicationException(response);
328         } catch (Exception e) {
329             if (logger.isDebugEnabled()) {
330                 logger.debug("getOrgAuthority", e);
331             }
332             Response response = Response.status(
333                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
334             throw new WebApplicationException(response);
335         }
336         if (result == null) {
337             Response response = Response.status(Response.Status.NOT_FOUND).entity(
338                     "Get failed, the requested OrgAuthority spec:" + specifier + ": was not found.").type(
339                     "text/plain").build();
340             throw new WebApplicationException(response);
341         }
342         return result;
343     }
344
345     /**
346      * Gets the org authority.
347      * 
348      * @param csid the csid
349      * 
350      * @return the org authority
351      */
352     @GET
353     @Path("{csid}")
354     public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
355         String idValue = null;
356         if (csid == null) {
357             logger.error("getOrgAuthority: missing csid!");
358             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
359                     "get failed on OrgAuthority csid=" + csid).type(
360                     "text/plain").build();
361             throw new WebApplicationException(response);
362         }
363         if (logger.isDebugEnabled()) {
364             logger.debug("getOrgAuthority with path(id)=" + csid);
365         }
366         MultipartOutput result = null;
367         try {
368                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
369             DocumentHandler handler = createDocumentHandler(ctx);
370             getRepositoryClient(ctx).get(ctx, csid, handler);
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("getOrgAuthority", dnfe);
379             }
380             Response response = Response.status(Response.Status.NOT_FOUND).entity(
381                     "Get failed on OrgAuthority csid=" + csid).type(
382                     "text/plain").build();
383             throw new WebApplicationException(response);
384         } catch (Exception e) {
385             if (logger.isDebugEnabled()) {
386                 logger.debug("getOrgAuthority", 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 OrgAuthority CSID:" + csid + ": was not found.").type(
395                     "text/plain").build();
396             throw new WebApplicationException(response);
397         }
398         return result;
399     }
400
401     /**
402      * Gets the org authority list.
403      * 
404      * @param ui the ui
405      * 
406      * @return the org authority list
407      */
408     @GET
409     @Produces("application/xml")
410     public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
411         OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
412         try {
413                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
414             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
415             DocumentHandler handler = createDocumentHandler(ctx);
416             DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
417             myFilter.setPagination(queryParams);
418             String nameQ = queryParams.getFirst("refName");
419             if (nameQ != null) {
420                 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
421             }
422             handler.setDocumentFilter(myFilter);
423             getRepositoryClient(ctx).getFiltered(ctx, handler);
424             orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
425         } catch (UnauthorizedException ue) {
426             Response response = Response.status(
427                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
428             throw new WebApplicationException(response);
429         } catch (Exception e) {
430             if (logger.isDebugEnabled()) {
431                 logger.debug("Caught exception in getOrgAuthorityList", e);
432             }
433             Response response = Response.status(
434                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
435             throw new WebApplicationException(response);
436         }
437         return orgAuthorityObjectList;
438     }
439
440     /**
441      * Update org authority.
442      * 
443      * @param csid the csid
444      * @param theUpdate the the update
445      * 
446      * @return the multipart output
447      */
448     @PUT
449     @Path("{csid}")
450     public MultipartOutput updateOrgAuthority(
451             @PathParam("csid") String csid,
452             MultipartInput theUpdate) {
453         if (logger.isDebugEnabled()) {
454             logger.debug("updateOrgAuthority with csid=" + csid);
455         }
456         if (csid == null || "".equals(csid)) {
457             logger.error("updateOrgAuthority: missing csid!");
458             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
459                     "update failed on OrgAuthority csid=" + csid).type(
460                     "text/plain").build();
461             throw new WebApplicationException(response);
462         }
463         MultipartOutput result = null;
464         try {
465                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
466             DocumentHandler handler = createDocumentHandler(ctx);
467             getRepositoryClient(ctx).update(ctx, csid, handler);
468             result = (MultipartOutput) ctx.getOutput();
469         } catch (BadRequestException bre) {
470             Response response = Response.status(
471                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
472             throw new WebApplicationException(response);
473         } catch (UnauthorizedException ue) {
474             Response response = Response.status(
475                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
476             throw new WebApplicationException(response);
477         } catch (DocumentNotFoundException dnfe) {
478             if (logger.isDebugEnabled()) {
479                 logger.debug("caught exception in updateOrgAuthority", dnfe);
480             }
481             Response response = Response.status(Response.Status.NOT_FOUND).entity(
482                     "Update failed on OrgAuthority csid=" + csid).type(
483                     "text/plain").build();
484             throw new WebApplicationException(response);
485         } catch (Exception e) {
486             Response response = Response.status(
487                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
488             throw new WebApplicationException(response);
489         }
490         return result;
491     }
492
493     /**
494      * Delete org authority.
495      * 
496      * @param csid the csid
497      * 
498      * @return the response
499      */
500     @DELETE
501     @Path("{csid}")
502     public Response deleteOrgAuthority(@PathParam("csid") String csid) {
503
504         if (logger.isDebugEnabled()) {
505             logger.debug("deleteOrgAuthority with csid=" + csid);
506         }
507         if (csid == null || "".equals(csid)) {
508             logger.error("deleteOrgAuthority: missing csid!");
509             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
510                     "delete failed on OrgAuthority csid=" + csid).type(
511                     "text/plain").build();
512             throw new WebApplicationException(response);
513         }
514         try {
515                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
516             getRepositoryClient(ctx).delete(ctx, csid);
517             return Response.status(HttpResponseCodes.SC_OK).build();
518         } catch (UnauthorizedException ue) {
519             Response response = Response.status(
520                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
521             throw new WebApplicationException(response);
522         } catch (DocumentNotFoundException dnfe) {
523             if (logger.isDebugEnabled()) {
524                 logger.debug("caught exception in deleteOrgAuthority", dnfe);
525             }
526             Response response = Response.status(Response.Status.NOT_FOUND).entity(
527                     "Delete failed on OrgAuthority csid=" + csid).type(
528                     "text/plain").build();
529             throw new WebApplicationException(response);
530         } catch (Exception e) {
531             Response response = Response.status(
532                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
533             throw new WebApplicationException(response);
534         }
535
536     }
537
538     /*************************************************************************
539      * Organization parts - this is a sub-resource of OrgAuthority
540      *************************************************************************/
541     @POST
542     @Path("{csid}/items")
543     public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
544         try {
545                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
546                                 input);
547             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
548             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
549             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
550             path.path(parentcsid + "/items/" + itemcsid);
551             Response response = Response.created(path.build()).build();
552             return response;
553         } catch (BadRequestException bre) {
554             Response response = Response.status(
555                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
556             throw new WebApplicationException(response);
557         } catch (UnauthorizedException ue) {
558             Response response = Response.status(
559                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
560             throw new WebApplicationException(response);
561         } catch (Exception e) {
562             if (logger.isDebugEnabled()) {
563                 logger.debug("Caught exception in createOrganization", e);
564             }
565             Response response = Response.status(
566                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
567             throw new WebApplicationException(response);
568         }
569     }
570
571     /**
572      * Gets the organization.
573      * 
574      * @param csid The organization authority (parent) CSID.
575      * @param itemcsid The organization item CSID.
576      * 
577      * @return the organization.
578      */
579     @GET
580     @Path("{csid}/items/{itemcsid}")
581     public MultipartOutput getOrganization(
582             @PathParam("csid") String parentcsid,
583             @PathParam("itemcsid") String itemcsid) {
584         if (logger.isDebugEnabled()) {
585             logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
586         }
587         if (parentcsid == null || "".equals(parentcsid)) {
588             logger.error("getOrganization: missing csid!");
589             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
590                     "get failed on Organization csid=" + parentcsid).type(
591                     "text/plain").build();
592             throw new WebApplicationException(response);
593         }
594         if (itemcsid == null || "".equals(itemcsid)) {
595             logger.error("getOrganization: missing itemcsid!");
596             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
597                     "get failed on Organization itemcsid=" + itemcsid).type(
598                     "text/plain").build();
599             throw new WebApplicationException(response);
600         }
601         MultipartOutput result = null;
602         try {
603             // Note that we have to create the service context for the Items, not the main service
604                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
605             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
606             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
607             // TODO should we assert that the item is in the passed orgAuthority?
608             result = (MultipartOutput) ctx.getOutput();
609         } catch (UnauthorizedException ue) {
610             Response response = Response.status(
611                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
612             throw new WebApplicationException(response);
613         } catch (DocumentNotFoundException dnfe) {
614             if (logger.isDebugEnabled()) {
615                 logger.debug("getOrganization", dnfe);
616             }
617             Response response = Response.status(Response.Status.NOT_FOUND).entity(
618                     "Get failed on Organization csid=" + itemcsid).type(
619                     "text/plain").build();
620             throw new WebApplicationException(response);
621         } catch (Exception e) {
622             if (logger.isDebugEnabled()) {
623                 logger.debug("getOrganization", e);
624             }
625             Response response = Response.status(
626                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
627             throw new WebApplicationException(response);
628         }
629         if (result == null) {
630             Response response = Response.status(Response.Status.NOT_FOUND).entity(
631                     "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
632                     "text/plain").build();
633             throw new WebApplicationException(response);
634         }
635         return result;
636     }
637
638     /**
639      * Gets the authority refs for an Organization item.
640      *
641      * @param csid The organization authority (parent) CSID.
642      * @param itemcsid The organization item CSID.
643      *
644      * @return the authority refs for the Organization item.
645      */
646     @GET
647     @Path("{csid}/items/{itemcsid}/authorityrefs")
648     @Produces("application/xml")
649     public AuthorityRefList getOrganizationAuthorityRefs(
650                 @PathParam("csid") String parentcsid,
651                 @PathParam("itemcsid") String itemcsid,
652                 @Context UriInfo ui) {
653         AuthorityRefList authRefList = null;
654         try {
655             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
656             ServiceContext<MultipartInput, MultipartOutput> ctx =
657                 createServiceContext(getItemServiceName(), queryParams);
658             RemoteDocumentModelHandlerImpl handler =
659                 (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid);
660             DocumentWrapper<DocumentModel> docWrapper =
661                getRepositoryClient(ctx).getDoc(ctx, itemcsid);
662             List<String> authRefFields =
663                 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
664                 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
665             authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
666         } catch (UnauthorizedException ue) {
667             Response response = Response.status(
668                     Response.Status.UNAUTHORIZED).entity("Failed to retrieve authority references: reason " + ue.getErrorReason()).type("text/plain").build();
669             throw new WebApplicationException(response);
670         } catch (Exception e) {
671             if (logger.isDebugEnabled()) {
672                 logger.debug("Caught exception in getAuthorityRefs", e);
673             }
674             Response response = Response.status(
675                     Response.Status.INTERNAL_SERVER_ERROR).entity("Failed to retrieve authority references").type("text/plain").build();
676             throw new WebApplicationException(response);
677         }
678         return authRefList;
679     }
680
681     /**
682      * Gets the organization list.
683      * 
684      * @param parentcsid the parentcsid
685      * @param partialTerm the partial term
686      * @param ui the ui
687      * 
688      * @return the organization list
689      */
690     @GET
691     @Path("{csid}/items")
692     @Produces("application/xml")
693     public OrganizationsCommonList getOrganizationList(
694             @PathParam("csid") String parentcsid,
695             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
696             @Context UriInfo ui) {
697         OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
698         try {
699             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
700             // Note that docType defaults to the ServiceName, so we're fine with that.
701                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
702                                 queryParams);
703             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
704             DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
705             myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
706             myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
707                         ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
708                         "'" + parentcsid + "'");
709             
710             // AND organizations_common:displayName LIKE '%partialTerm%'
711             if (partialTerm != null && !partialTerm.isEmpty()) {
712                 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
713                         ":" + OrganizationJAXBSchema.DISPLAY_NAME +
714                         " LIKE " + "'%" + partialTerm + "%'";
715                 myFilter.appendWhereClause(ptClause);
716             }            
717             handler.setDocumentFilter(myFilter);
718             getRepositoryClient(ctx).getFiltered(ctx, handler);
719             organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
720         } catch (UnauthorizedException ue) {
721             Response response = Response.status(
722                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
723             throw new WebApplicationException(response);
724         } catch (Exception e) {
725             if (logger.isDebugEnabled()) {
726                 logger.debug("Caught exception in getOrganizationList", e);
727             }
728             Response response = Response.status(
729                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
730             throw new WebApplicationException(response);
731         }
732         return organizationObjectList;
733     }
734
735     /**
736      * Gets the organization list by auth name.
737      * 
738      * @param parentSpecifier the parent specifier
739      * @param partialTerm the partial term
740      * @param ui the ui
741      * 
742      * @return the organization list by auth name
743      */
744     @GET
745     @Path("urn:cspace:name({specifier})/items")
746     @Produces("application/xml")
747     public OrganizationsCommonList getOrganizationListByAuthName(
748                 @PathParam("specifier") String parentSpecifier,
749             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
750             @Context UriInfo ui) {
751         OrganizationsCommonList personObjectList = new OrganizationsCommonList();
752         try {
753             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
754             String whereClause =
755                 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
756                 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
757                 "='"+parentSpecifier+"'";
758             // Need to get an Authority by name
759             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
760             String parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
761
762             ctx = createServiceContext(getItemServiceName(), queryParams);
763             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
764             DocumentFilter myFilter = handler.createDocumentFilter();// new DocumentFilter();
765             myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
766
767             // Add the where clause "organizations_common:inAuthority='" + parentcsid + "'"
768             myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
769                         OrganizationJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
770             
771             // AND organizations_common:displayName LIKE '%partialTerm%'
772             if (partialTerm != null && !partialTerm.isEmpty()) {
773                 String ptClause = "AND " +
774                 OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
775                 OrganizationJAXBSchema.DISPLAY_NAME +
776                         " LIKE " +
777                         "'%" + partialTerm + "%'";
778                 myFilter.appendWhereClause(ptClause);
779             }
780             
781             handler.setDocumentFilter(myFilter);
782             getRepositoryClient(ctx).getFiltered(ctx, handler);
783             personObjectList = (OrganizationsCommonList) handler.getCommonPartList();
784         } catch (UnauthorizedException ue) {
785             Response response = Response.status(
786                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
787             throw new WebApplicationException(response);
788         } catch (Exception e) {
789             if (logger.isDebugEnabled()) {
790                 logger.debug("Caught exception in getOrganizationListByAuthName", e);
791             }
792             Response response = Response.status(
793                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
794             throw new WebApplicationException(response);
795         }
796         return personObjectList;
797     }
798
799     /**
800      * Update organization.
801      * 
802      * @param parentcsid the parentcsid
803      * @param itemcsid the itemcsid
804      * @param theUpdate the the update
805      * 
806      * @return the multipart output
807      */
808     @PUT
809     @Path("{csid}/items/{itemcsid}")
810     public MultipartOutput updateOrganization(
811             @PathParam("csid") String parentcsid,
812             @PathParam("itemcsid") String itemcsid,
813             MultipartInput theUpdate) {
814         if (logger.isDebugEnabled()) {
815             logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
816         }
817         if (parentcsid == null || "".equals(parentcsid)) {
818             logger.error("updateOrganization: missing csid!");
819             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
820                     "update failed on Organization parentcsid=" + parentcsid).type(
821                     "text/plain").build();
822             throw new WebApplicationException(response);
823         }
824         if (itemcsid == null || "".equals(itemcsid)) {
825             logger.error("updateOrganization: missing itemcsid!");
826             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
827                     "update failed on Organization=" + itemcsid).type(
828                     "text/plain").build();
829             throw new WebApplicationException(response);
830         }
831         MultipartOutput result = null;
832         try {
833             // Note that we have to create the service context for the Items, not the main service
834                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
835                                 theUpdate);
836             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
837             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
838             result = (MultipartOutput) ctx.getOutput();
839         } catch (BadRequestException bre) {
840             Response response = Response.status(
841                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
842             throw new WebApplicationException(response);
843         } catch (UnauthorizedException ue) {
844             Response response = Response.status(
845                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
846             throw new WebApplicationException(response);
847         } catch (DocumentNotFoundException dnfe) {
848             if (logger.isDebugEnabled()) {
849                 logger.debug("caught exception in updateOrganization", dnfe);
850             }
851             Response response = Response.status(Response.Status.NOT_FOUND).entity(
852                     "Update failed on Organization csid=" + itemcsid).type(
853                     "text/plain").build();
854             throw new WebApplicationException(response);
855         } catch (Exception e) {
856             Response response = Response.status(
857                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
858             throw new WebApplicationException(response);
859         }
860         return result;
861     }
862
863     /**
864      * Delete organization.
865      * 
866      * @param parentcsid the parentcsid
867      * @param itemcsid the itemcsid
868      * 
869      * @return the response
870      */
871     @DELETE
872     @Path("{csid}/items/{itemcsid}")
873     public Response deleteOrganization(
874             @PathParam("csid") String parentcsid,
875             @PathParam("itemcsid") String itemcsid) {
876         if (logger.isDebugEnabled()) {
877             logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
878         }
879         if (parentcsid == null || "".equals(parentcsid)) {
880             logger.error("deleteOrganization: missing csid!");
881             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
882                     "delete failed on Organization parentcsid=" + parentcsid).type(
883                     "text/plain").build();
884             throw new WebApplicationException(response);
885         }
886         if (itemcsid == null || "".equals(itemcsid)) {
887             logger.error("deleteOrganization: missing itemcsid!");
888             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
889                     "delete failed on Organization=" + itemcsid).type(
890                     "text/plain").build();
891             throw new WebApplicationException(response);
892         }
893         try {
894             // Note that we have to create the service context for the Items, not the main service
895                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
896             getRepositoryClient(ctx).delete(ctx, itemcsid);
897             return Response.status(HttpResponseCodes.SC_OK).build();
898         } catch (UnauthorizedException ue) {
899             Response response = Response.status(
900                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
901             throw new WebApplicationException(response);
902         } catch (DocumentNotFoundException dnfe) {
903             if (logger.isDebugEnabled()) {
904                 logger.debug("caught exception in deleteOrganization", dnfe);
905             }
906             Response response = Response.status(Response.Status.NOT_FOUND).entity(
907                     "Delete failed on Organization itemcsid=" + itemcsid).type(
908                     "text/plain").build();
909             throw new WebApplicationException(response);
910         } catch (Exception e) {
911             Response response = Response.status(
912                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
913             throw new WebApplicationException(response);
914         }
915
916     }
917
918     /*************************************************************************
919      * Contact parts - this is a sub-resource of Organization (or "item")
920      *************************************************************************/
921     @POST
922     @Path("{parentcsid}/items/{itemcsid}/contacts")
923     public Response createContact(
924             @PathParam("parentcsid") String parentcsid,
925             @PathParam("itemcsid") String itemcsid,
926             MultipartInput input) {
927         try {
928             // Note that we have to create the service context and document
929             // handler for the Contact service, not the main service.
930                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
931             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
932             String csid = getRepositoryClient(ctx).create(ctx, handler);
933             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
934             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
935             Response response = Response.created(path.build()).build();
936             return response;
937         } catch (BadRequestException bre) {
938             Response response = Response.status(
939                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
940             throw new WebApplicationException(response);
941         } catch (UnauthorizedException ue) {
942             Response response = Response.status(
943                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
944             throw new WebApplicationException(response);
945         } catch (Exception e) {
946             if (logger.isDebugEnabled()) {
947                 logger.debug("Caught exception in createContact", e);
948             }
949             Response response = Response.status(
950                 Response.Status.INTERNAL_SERVER_ERROR)
951                 .entity("Attempt to create Contact failed.")
952                 .type("text/plain").build();
953             throw new WebApplicationException(response);
954         }
955
956     }
957
958     /**
959      * Gets the contact list.
960      * 
961      * @param parentcsid the parentcsid
962      * @param itemcsid the itemcsid
963      * @param ui the ui
964      * 
965      * @return the contact list
966      */
967     @GET
968     @Produces({"application/xml"})
969     @Path("{parentcsid}/items/{itemcsid}/contacts/")
970     public ContactsCommonList getContactList(
971             @PathParam("parentcsid") String parentcsid,
972             @PathParam("itemcsid") String itemcsid,
973             @Context UriInfo ui) {
974         ContactsCommonList contactObjectList = new ContactsCommonList();
975         try {
976             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
977                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
978                                 queryParams);
979             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
980             DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
981             myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
982             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
983                 ContactJAXBSchema.IN_AUTHORITY +
984                 "='" + parentcsid + "'" +
985                 " AND " +
986                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
987                 ContactJAXBSchema.IN_ITEM +
988                 "='" + itemcsid + "'" +
989                 " AND ecm:isProxy = 0");
990             handler.setDocumentFilter(myFilter);
991             getRepositoryClient(ctx).getFiltered(ctx, handler);
992             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
993         } catch (UnauthorizedException ue) {
994             Response response = Response.status(
995                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
996             throw new WebApplicationException(response);
997         } catch (Exception e) {
998             if (logger.isDebugEnabled()) {
999                 logger.debug("Caught exception in getContactsList", e);
1000             }
1001             Response response = Response.status(
1002                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
1003             throw new WebApplicationException(response);
1004         }
1005         return contactObjectList;
1006     }
1007
1008     /**
1009      * Gets the contact.
1010      * 
1011      * @param parentcsid the parentcsid
1012      * @param itemcsid the itemcsid
1013      * @param csid the csid
1014      * 
1015      * @return the contact
1016      */
1017     @GET
1018     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1019     public MultipartOutput getContact(
1020             @PathParam("parentcsid") String parentcsid,
1021             @PathParam("itemcsid") String itemcsid,
1022             @PathParam("csid") String csid) {
1023         MultipartOutput result = null;
1024        if (logger.isDebugEnabled()) {
1025             logger.debug("getContact with parentCsid=" + parentcsid +
1026             " itemcsid=" + itemcsid + " csid=" + csid);
1027         }
1028         try {
1029             // Note that we have to create the service context and document
1030             // handler for the Contact service, not the main service.
1031                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1032             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1033             getRepositoryClient(ctx).get(ctx, csid, handler);
1034             result = (MultipartOutput) ctx.getOutput();
1035         } catch (UnauthorizedException ue) {
1036             Response response = Response.status(
1037                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
1038             throw new WebApplicationException(response);
1039         } catch (DocumentNotFoundException dnfe) {
1040             if (logger.isDebugEnabled()) {
1041                 logger.debug("getContact", dnfe);
1042             }
1043             Response response = Response.status(Response.Status.NOT_FOUND)
1044                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1045                 .type("text/plain").build();
1046             throw new WebApplicationException(response);
1047         } catch (Exception e) {
1048             if (logger.isDebugEnabled()) {
1049                 logger.debug("getContact", e);
1050             }
1051             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
1052                 .entity("Get contact failed")
1053                 .type("text/plain").build();
1054             throw new WebApplicationException(response);
1055         }
1056         if (result == null) {
1057             Response response = Response.status(Response.Status.NOT_FOUND)
1058                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1059                 .type("text/plain").build();
1060             throw new WebApplicationException(response);
1061         }
1062         return result;
1063
1064     }
1065
1066     /**
1067      * Update contact.
1068      * 
1069      * @param parentcsid the parentcsid
1070      * @param itemcsid the itemcsid
1071      * @param csid the csid
1072      * @param theUpdate the the update
1073      * 
1074      * @return the multipart output
1075      */
1076     @PUT
1077     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1078     public MultipartOutput updateContact(
1079             @PathParam("parentcsid") String parentcsid,
1080             @PathParam("itemcsid") String itemcsid,
1081             @PathParam("csid") String csid,
1082             MultipartInput theUpdate) {
1083        if (logger.isDebugEnabled()) {
1084             logger.debug("updateContact with parentcsid=" + parentcsid +
1085             " itemcsid=" + itemcsid + " csid=" + csid);
1086         }
1087        if (parentcsid == null || parentcsid.trim().isEmpty()) {
1088             logger.error("updateContact: missing csid!");
1089             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1090                     "update failed on Contact parentcsid=" + parentcsid).type(
1091                     "text/plain").build();
1092             throw new WebApplicationException(response);
1093         }
1094         if (itemcsid == null || itemcsid.trim().isEmpty()) {
1095             logger.error("updateContact: missing itemcsid!");
1096             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1097                     "update failed on Contact=" + itemcsid).type(
1098                     "text/plain").build();
1099             throw new WebApplicationException(response);
1100         }
1101         if (csid == null || csid.trim().isEmpty()) {
1102             logger.error("updateContact: missing csid!");
1103             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1104                     "update failed on Contact=" + csid).type(
1105                     "text/plain").build();
1106             throw new WebApplicationException(response);
1107         }
1108         MultipartOutput result = null;
1109         try {
1110             // Note that we have to create the service context and document
1111             // handler for the Contact service, not the main service.
1112                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), theUpdate);
1113             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1114             getRepositoryClient(ctx).update(ctx, csid, handler);
1115             result = (MultipartOutput) ctx.getOutput();
1116         } catch (BadRequestException bre) {
1117             Response response = Response.status(
1118                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1119             throw new WebApplicationException(response);
1120         } catch (UnauthorizedException ue) {
1121             Response response = Response.status(
1122                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1123             throw new WebApplicationException(response);
1124         } catch (DocumentNotFoundException dnfe) {
1125             if (logger.isDebugEnabled()) {
1126                 logger.debug("caught exception in updateContact", dnfe);
1127             }
1128             Response response = Response.status(Response.Status.NOT_FOUND).entity(
1129                     "Update failed on Contact csid=" + itemcsid).type(
1130                     "text/plain").build();
1131             throw new WebApplicationException(response);
1132         } catch (Exception e) {
1133             Response response = Response.status(
1134                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1135             throw new WebApplicationException(response);
1136         }
1137         return result;
1138     }
1139
1140     /**
1141      * Delete contact.
1142      * 
1143      * @param parentcsid the parentcsid
1144      * @param itemcsid the itemcsid
1145      * @param csid the csid
1146      * 
1147      * @return the response
1148      */
1149     @DELETE
1150     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1151     public Response deleteContact(
1152             @PathParam("parentcsid") String parentcsid,
1153             @PathParam("itemcsid") String itemcsid,
1154             @PathParam("csid") String csid) {
1155         if (logger.isDebugEnabled()) {
1156             logger.debug("deleteContact with parentCsid=" + parentcsid +
1157             " itemcsid=" + itemcsid + " csid=" + csid);
1158         }
1159         if (parentcsid == null || parentcsid.trim().isEmpty()) {
1160             logger.error("deleteContact: missing parentcsid!");
1161             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1162                     "delete contact failed on parentcsid=" + parentcsid).type(
1163                     "text/plain").build();
1164             throw new WebApplicationException(response);
1165         }
1166         if (itemcsid == null || itemcsid.trim().isEmpty()) {
1167             logger.error("deleteContact: missing itemcsid!");
1168             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1169                     "delete contact failed on itemcsid=" + itemcsid).type(
1170                     "text/plain").build();
1171             throw new WebApplicationException(response);
1172         }
1173         if (csid == null || csid.trim().isEmpty()) {
1174             logger.error("deleteContact: missing csid!");
1175             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1176                     "delete contact failed on csid=" + csid).type(
1177                     "text/plain").build();
1178             throw new WebApplicationException(response);
1179         }
1180         try {
1181             // Note that we have to create the service context for the
1182             // Contact service, not the main service.
1183                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1184             getRepositoryClient(ctx).delete(ctx, csid);
1185             return Response.status(HttpResponseCodes.SC_OK).build();
1186          } catch (UnauthorizedException ue) {
1187             Response response = Response.status(
1188                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1189             throw new WebApplicationException(response);
1190          } catch (DocumentNotFoundException dnfe) {
1191             if (logger.isDebugEnabled()) {
1192                 logger.debug("Caught exception in deleteContact", dnfe);
1193             }
1194             Response response = Response.status(Response.Status.NOT_FOUND)
1195                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1196                 .type("text/plain").build();
1197             throw new WebApplicationException(response);
1198        } catch (Exception e) {
1199             Response response = Response.status(
1200                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1201             throw new WebApplicationException(response);
1202         }
1203     }
1204
1205 }