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