]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c177be7295b994a33f1424e8b78e26a8225508e6
[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(ctx,
375                         repoClient, 
376                         serviceType,
377                         refName,
378                         myFilter.getPageSize(), myFilter.getStartPage(), true /*computeTotal*/ );
379     } catch (UnauthorizedException ue) {
380         Response response = Response.status(
381                 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
382         throw new WebApplicationException(response);
383     } catch (DocumentNotFoundException dnfe) {
384         if (logger.isDebugEnabled()) {
385             logger.debug("getReferencingObjects", dnfe);
386         }
387         Response response = Response.status(Response.Status.NOT_FOUND).entity(
388                 "GetReferencingObjects failed with parentcsid=" 
389                         + parentcsid + " and itemcsid=" + itemcsid).type(
390                 "text/plain").build();
391         throw new WebApplicationException(response);
392     } catch (Exception e) {     // Includes DocumentException
393         if (logger.isDebugEnabled()) {
394             logger.debug("GetReferencingObjects", e);
395         }
396         Response response = Response.status(
397                 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
398         throw new WebApplicationException(response);
399     }
400     if (authRefDocList == null) {
401         Response response = Response.status(Response.Status.NOT_FOUND).entity(
402                 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
403                 "text/plain").build();
404         throw new WebApplicationException(response);
405     }
406     return authRefDocList;
407     }
408
409     @GET
410     @Path("{csid}")
411     public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
412         if (csid == null) {
413             logger.error("getPersonAuthority: missing csid!");
414             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
415                     "get failed on PersonAuthority csid=" + csid).type(
416                     "text/plain").build();
417             throw new WebApplicationException(response);
418         }
419         if (logger.isDebugEnabled()) {
420             logger.debug("getPersonAuthority with path(id)=" + csid);
421         } 
422         MultipartOutput result = null;
423         try {
424                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
425             DocumentHandler handler = createDocumentHandler(ctx);
426             getRepositoryClient(ctx).get(ctx, csid, handler);
427             result = (MultipartOutput) ctx.getOutput();
428         } catch (UnauthorizedException ue) {
429             Response response = Response.status(
430                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
431             throw new WebApplicationException(response);
432         } catch (DocumentNotFoundException dnfe) {
433             if (logger.isDebugEnabled()) {
434                 logger.debug("getPersonAuthority", dnfe);
435             }
436             Response response = Response.status(Response.Status.NOT_FOUND).entity(
437                     "Get failed on PersonAuthority csid=" + csid).type(
438                     "text/plain").build();
439             throw new WebApplicationException(response);
440         } catch (Exception e) {
441             if (logger.isDebugEnabled()) {
442                 logger.debug("getPersonAuthority", e);
443             }
444             Response response = Response.status(
445                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
446             throw new WebApplicationException(response);
447         }
448         if (result == null) {
449             Response response = Response.status(Response.Status.NOT_FOUND).entity(
450                     "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
451                     "text/plain").build();
452             throw new WebApplicationException(response);
453         }
454         return result;
455     }
456
457     /**
458      * Gets the person authority list.
459      * 
460      * @param ui the ui
461      * 
462      * @return the person authority list
463      */
464     @GET
465     @Produces("application/xml")
466     public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
467         PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
468         try {
469             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
470                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
471             DocumentHandler handler = createDocumentHandler(ctx);
472             String nameQ = queryParams.getFirst("refName");
473             if (nameQ != null) {
474                 handler.getDocumentFilter().setWhereClause("personauthorities_common:refName='" + nameQ + "'");
475             }
476             getRepositoryClient(ctx).getFiltered(ctx, handler);
477             personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
478         } catch (UnauthorizedException ue) {
479             Response response = Response.status(
480                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
481             throw new WebApplicationException(response);
482         } catch (Exception e) {
483             if (logger.isDebugEnabled()) {
484                 logger.debug("Caught exception in getPersonAuthorityList", e);
485             }
486             Response response = Response.status(
487                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
488             throw new WebApplicationException(response);
489         }
490         return personAuthorityObjectList;
491     }
492
493     /**
494      * Update person authority.
495      * 
496      * @param csid the csid
497      * @param theUpdate the the update
498      * 
499      * @return the multipart output
500      */
501     @PUT
502     @Path("{csid}")
503     public MultipartOutput updatePersonAuthority(
504             @PathParam("csid") String csid,
505             MultipartInput theUpdate) {
506         if (logger.isDebugEnabled()) {
507             logger.debug("updatePersonAuthority with csid=" + csid);
508         }
509         if (csid == null || "".equals(csid)) {
510             logger.error("updatePersonAuthority: missing csid!");
511             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
512                     "update failed on PersonAuthority csid=" + csid).type(
513                     "text/plain").build();
514             throw new WebApplicationException(response);
515         }
516         MultipartOutput result = null;
517         try {
518                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
519             DocumentHandler handler = createDocumentHandler(ctx);
520             getRepositoryClient(ctx).update(ctx, csid, handler);
521             result = (MultipartOutput) ctx.getOutput();
522         } catch (BadRequestException bre) {
523             Response response = Response.status(
524                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
525             throw new WebApplicationException(response);
526         } catch (UnauthorizedException ue) {
527             Response response = Response.status(
528                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
529             throw new WebApplicationException(response);
530         } catch (DocumentNotFoundException dnfe) {
531             if (logger.isDebugEnabled()) {
532                 logger.debug("caugth exception in updatePersonAuthority", dnfe);
533             }
534             Response response = Response.status(Response.Status.NOT_FOUND).entity(
535                     "Update failed on PersonAuthority csid=" + csid).type(
536                     "text/plain").build();
537             throw new WebApplicationException(response);
538         } catch (Exception e) {
539             Response response = Response.status(
540                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
541             throw new WebApplicationException(response);
542         }
543         return result;
544     }
545
546     /**
547      * Delete person authority.
548      * 
549      * @param csid the csid
550      * 
551      * @return the response
552      */
553     @DELETE
554     @Path("{csid}")
555     public Response deletePersonAuthority(@PathParam("csid") String csid) {
556
557         if (logger.isDebugEnabled()) {
558             logger.debug("deletePersonAuthority with csid=" + csid);
559         }
560         if (csid == null || "".equals(csid)) {
561             logger.error("deletePersonAuthority: missing csid!");
562             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
563                     "delete failed on PersonAuthority csid=" + csid).type(
564                     "text/plain").build();
565             throw new WebApplicationException(response);
566         }
567         try {
568                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
569             getRepositoryClient(ctx).delete(ctx, csid);
570             return Response.status(HttpResponseCodes.SC_OK).build();
571         } catch (UnauthorizedException ue) {
572             Response response = Response.status(
573                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
574             throw new WebApplicationException(response);
575         } catch (DocumentNotFoundException dnfe) {
576             if (logger.isDebugEnabled()) {
577                 logger.debug("caught exception in deletePersonAuthority", dnfe);
578             }
579             Response response = Response.status(Response.Status.NOT_FOUND).entity(
580                     "Delete failed on PersonAuthority csid=" + csid).type(
581                     "text/plain").build();
582             throw new WebApplicationException(response);
583         } catch (Exception e) {
584             Response response = Response.status(
585                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
586             throw new WebApplicationException(response);
587         }
588
589     }
590
591     /**
592      * ***********************************************************************
593      * Person parts - this is a sub-resource of PersonAuthority
594      * ***********************************************************************.
595      *
596      * @param parentcsid the parentcsid
597      * @param input the input
598      * @return the response
599      */    
600     @POST
601     @Path("{csid}/items")
602     public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
603         try {
604                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(), input);
605             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
606             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
607             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
608             path.path(parentcsid + "/items/" + itemcsid);
609             Response response = Response.created(path.build()).build();
610             return response;
611         } catch (BadRequestException bre) {
612             Response response = Response.status(
613                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
614             throw new WebApplicationException(response);
615         } catch (UnauthorizedException ue) {
616             Response response = Response.status(
617                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
618             throw new WebApplicationException(response);
619         } catch (Exception e) {
620             if (logger.isDebugEnabled()) {
621                 logger.debug("Caught exception in createPerson", e);
622             }
623             Response response = Response.status(
624                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
625             throw new WebApplicationException(response);
626         }
627     }
628
629     /**
630      * Gets the person.
631      * 
632      * @param parentcsid the parentcsid
633      * @param itemcsid the itemcsid
634      * 
635      * @return the person
636      */
637     @GET
638     @Path("{csid}/items/{itemcsid}")
639     public MultipartOutput getPerson(
640             @PathParam("csid") String parentcsid,
641             @PathParam("itemcsid") String itemcsid) {
642         if (logger.isDebugEnabled()) {
643             logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
644         }
645         if (parentcsid == null || "".equals(parentcsid)
646             || itemcsid == null || "".equals(itemcsid)) {
647             logger.error("getPerson: missing parentcsid or itemcsid!");
648             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
649                     "get failed on Person with parentcsid=" 
650                         + parentcsid + " and itemcsid=" + itemcsid).type(
651                     "text/plain").build();
652             throw new WebApplicationException(response);
653         }
654         MultipartOutput result = null;
655         try {
656             // Note that we have to create the service context for the Items, not the main service
657                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
658             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
659             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
660             // TODO should we assert that the item is in the passed personAuthority?
661             result = (MultipartOutput) ctx.getOutput();
662         } catch (UnauthorizedException ue) {
663             Response response = Response.status(
664                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
665             throw new WebApplicationException(response);
666         } catch (DocumentNotFoundException dnfe) {
667             if (logger.isDebugEnabled()) {
668                 logger.debug("getPerson", dnfe);
669             }
670             Response response = Response.status(Response.Status.NOT_FOUND).entity(
671                     "Get failed on Person csid=" + itemcsid).type(
672                     "text/plain").build();
673             throw new WebApplicationException(response);
674         } catch (Exception e) {
675             if (logger.isDebugEnabled()) {
676                 logger.debug("getPerson", e);
677             }
678             Response response = Response.status(
679                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
680             throw new WebApplicationException(response);
681         }
682         if (result == null) {
683             Response response = Response.status(Response.Status.NOT_FOUND).entity(
684                     "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
685                     "text/plain").build();
686             throw new WebApplicationException(response);
687         }
688         return result;
689     }
690
691     /**
692      * Gets the person list.
693      * 
694      * @param parentcsid the parentcsid
695      * @param partialTerm the partial term
696      * @param ui the ui
697      * 
698      * @return the person list
699      */
700     @GET
701     @Path("{csid}/items")
702     @Produces("application/xml")
703     public PersonsCommonList getPersonList(
704             @PathParam("csid") String parentcsid,
705             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
706             @Context UriInfo ui) {
707         PersonsCommonList personObjectList = new PersonsCommonList();
708         try {
709             // Note that docType defaults to the ServiceName, so we're fine with that.
710             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
711                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
712                                 queryParams);
713             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
714             // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
715             handler.getDocumentFilter().setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
716                         PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
717             
718             // AND persons_common:displayName LIKE '%partialTerm%'
719             if (partialTerm != null && !partialTerm.isEmpty()) {
720                 String ptClause = "AND " +
721                 PersonJAXBSchema.PERSONS_COMMON + ":" +
722                         PersonJAXBSchema.DISPLAY_NAME +
723                         " LIKE " +
724                         "'%" + partialTerm + "%'";
725                 handler.getDocumentFilter().appendWhereClause(ptClause);
726             }            
727             getRepositoryClient(ctx).getFiltered(ctx, handler);
728             personObjectList = (PersonsCommonList) handler.getCommonPartList();
729         } catch (UnauthorizedException ue) {
730             Response response = Response.status(
731                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
732             throw new WebApplicationException(response);
733         } catch (Exception e) {
734             if (logger.isDebugEnabled()) {
735                 logger.debug("Caught exception in getPersonList", e);
736             }
737             Response response = Response.status(
738                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
739             throw new WebApplicationException(response);
740         }
741         return personObjectList;
742     }
743     
744
745     /**
746      * Gets the person list by auth name.
747      * 
748      * @param parentSpecifier the parent specifier
749      * @param partialTerm the partial term
750      * @param ui the ui
751      * 
752      * @return the person list by auth name
753      */
754     @GET
755     @Path("urn:cspace:name({specifier})/items")
756     @Produces("application/xml")
757     public PersonsCommonList getPersonListByAuthName(
758                 @PathParam("specifier") String parentSpecifier,
759             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
760             @Context UriInfo ui) {
761         PersonsCommonList personObjectList = new PersonsCommonList();
762         try {
763             String whereClause =
764                 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
765                 ":" + PersonAuthorityJAXBSchema.DISPLAY_NAME+
766                 "='" + parentSpecifier+"'";
767             // Need to get an Authority by name
768             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
769             ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
770             String parentcsid = 
771                 getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
772             ctx = createServiceContext(getItemServiceName(), queryParams);
773             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
774
775             // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
776             handler.getDocumentFilter().setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
777                         PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
778             
779             // AND persons_common:displayName LIKE '%partialTerm%'
780             if (partialTerm != null && !partialTerm.isEmpty()) {
781                 String ptClause = "AND " +
782                 PersonJAXBSchema.PERSONS_COMMON + ":" +
783                         PersonJAXBSchema.DISPLAY_NAME +
784                         " LIKE " +
785                         "'%" + partialTerm + "%'";
786                 handler.getDocumentFilter().appendWhereClause(ptClause);
787             }            
788             getRepositoryClient(ctx).getFiltered(ctx, handler);
789             personObjectList = (PersonsCommonList) handler.getCommonPartList();
790         } catch (UnauthorizedException ue) {
791             Response response = Response.status(
792                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
793             throw new WebApplicationException(response);
794         } catch (Exception e) {
795             if (logger.isDebugEnabled()) {
796                 logger.debug("Caught exception in getPersonList", e);
797             }
798             Response response = Response.status(
799                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
800             throw new WebApplicationException(response);
801         }
802         return personObjectList;
803     }
804
805     /**
806      * Update person.
807      * 
808      * @param parentcsid the parentcsid
809      * @param itemcsid the itemcsid
810      * @param theUpdate the the update
811      * 
812      * @return the multipart output
813      */
814     @PUT
815     @Path("{csid}/items/{itemcsid}")
816     public MultipartOutput updatePerson(
817             @PathParam("csid") String parentcsid,
818             @PathParam("itemcsid") String itemcsid,
819             MultipartInput theUpdate) {
820         if (logger.isDebugEnabled()) {
821             logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
822         }
823         if (parentcsid == null || "".equals(parentcsid)) {
824             logger.error("updatePerson: missing csid!");
825             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
826                     "update failed on Person parentcsid=" + parentcsid).type(
827                     "text/plain").build();
828             throw new WebApplicationException(response);
829         }
830         if (itemcsid == null || "".equals(itemcsid)) {
831             logger.error("updatePerson: missing itemcsid!");
832             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
833                     "update failed on Person=" + itemcsid).type(
834                     "text/plain").build();
835             throw new WebApplicationException(response);
836         }
837         MultipartOutput result = null;
838         try {
839             // Note that we have to create the service context for the Items, not the main service
840                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
841                                 theUpdate);
842             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
843             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
844             result = (MultipartOutput) ctx.getOutput();
845         } catch (BadRequestException bre) {
846             Response response = Response.status(
847                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
848             throw new WebApplicationException(response);
849         } catch (UnauthorizedException ue) {
850             Response response = Response.status(
851                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
852             throw new WebApplicationException(response);
853         } catch (DocumentNotFoundException dnfe) {
854             if (logger.isDebugEnabled()) {
855                 logger.debug("caught exception in updatePerson", dnfe);
856             }
857             Response response = Response.status(Response.Status.NOT_FOUND).entity(
858                     "Update failed on Person csid=" + itemcsid).type(
859                     "text/plain").build();
860             throw new WebApplicationException(response);
861         } catch (Exception e) {
862             Response response = Response.status(
863                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
864             throw new WebApplicationException(response);
865         }
866         return result;
867     }
868
869     /**
870      * Delete person.
871      * 
872      * @param parentcsid the parentcsid
873      * @param itemcsid the itemcsid
874      * 
875      * @return the response
876      */
877     @DELETE
878     @Path("{csid}/items/{itemcsid}")
879     public Response deletePerson(
880             @PathParam("csid") String parentcsid,
881             @PathParam("itemcsid") String itemcsid) {
882         if (logger.isDebugEnabled()) {
883             logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
884         }
885         if (parentcsid == null || "".equals(parentcsid)) {
886             logger.error("deletePerson: missing csid!");
887             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
888                     "delete failed on Person parentcsid=" + parentcsid).type(
889                     "text/plain").build();
890             throw new WebApplicationException(response);
891         }
892         if (itemcsid == null || "".equals(itemcsid)) {
893             logger.error("deletePerson: missing itemcsid!");
894             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
895                     "delete failed on Person=" + itemcsid).type(
896                     "text/plain").build();
897             throw new WebApplicationException(response);
898         }
899         try {
900             // Note that we have to create the service context for the Items, not the main service
901                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
902             getRepositoryClient(ctx).delete(ctx, itemcsid);
903             return Response.status(HttpResponseCodes.SC_OK).build();
904         } catch (UnauthorizedException ue) {
905             Response response = Response.status(
906                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
907             throw new WebApplicationException(response);
908         } catch (DocumentNotFoundException dnfe) {
909             if (logger.isDebugEnabled()) {
910                 logger.debug("caught exception in deletePerson", dnfe);
911             }
912             Response response = Response.status(Response.Status.NOT_FOUND).entity(
913                     "Delete failed on Person itemcsid=" + itemcsid).type(
914                     "text/plain").build();
915             throw new WebApplicationException(response);
916         } catch (Exception e) {
917             Response response = Response.status(
918                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
919             throw new WebApplicationException(response);
920         }
921
922     }
923
924     /**
925      * ***********************************************************************
926      * Contact parts - this is a sub-resource of Person (or "item")
927      * ***********************************************************************.
928      *
929      * @param parentcsid the parentcsid
930      * @param itemcsid the itemcsid
931      * @param input the input
932      * @return the response
933      */
934     @POST
935     @Path("{parentcsid}/items/{itemcsid}/contacts")
936     public Response createContact(
937             @PathParam("parentcsid") String parentcsid,
938             @PathParam("itemcsid") String itemcsid,
939             MultipartInput input) {
940         try {
941             // Note that we have to create the service context and document
942             // handler for the Contact service, not the main service.
943                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
944             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
945             String csid = getRepositoryClient(ctx).create(ctx, handler);
946             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
947             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
948             Response response = Response.created(path.build()).build();
949             return response;
950         } catch (BadRequestException bre) {
951             Response response = Response.status(
952                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
953             throw new WebApplicationException(response);
954         } catch (UnauthorizedException ue) {
955             Response response = Response.status(
956                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
957             throw new WebApplicationException(response);
958         } catch (Exception e) {
959             if (logger.isDebugEnabled()) {
960                 logger.debug("Caught exception in createContact", e);
961             }
962             Response response = Response.status(
963                 Response.Status.INTERNAL_SERVER_ERROR)
964                 .entity("Attempt to create Contact failed.")
965                 .type("text/plain").build();
966             throw new WebApplicationException(response);
967         }
968         
969     }
970         
971     /**
972      * Gets the contact list.
973      * 
974      * @param parentcsid the parentcsid
975      * @param itemcsid the itemcsid
976      * @param ui the ui
977      * 
978      * @return the contact list
979      */
980     @GET
981     @Produces({"application/xml"})
982     @Path("{parentcsid}/items/{itemcsid}/contacts/")
983     public ContactsCommonList getContactList(
984             @PathParam("parentcsid") String parentcsid,
985             @PathParam("itemcsid") String itemcsid,
986             @Context UriInfo ui) {
987         ContactsCommonList contactObjectList = new ContactsCommonList();
988         try {
989             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
990                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
991                                 queryParams);
992             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
993             handler.getDocumentFilter().setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
994                 ContactJAXBSchema.IN_AUTHORITY +
995                 "='" + parentcsid + "'" +
996                 " AND " +
997                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
998                 ContactJAXBSchema.IN_ITEM +
999                 "='" + itemcsid + "'" +
1000                 " AND ecm:isProxy = 0");
1001             getRepositoryClient(ctx).getFiltered(ctx, handler);
1002             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
1003         } catch (UnauthorizedException ue) {
1004             Response response = Response.status(
1005                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
1006             throw new WebApplicationException(response);
1007         } catch (Exception e) {
1008             if (logger.isDebugEnabled()) {
1009                 logger.debug("Caught exception in getContactsList", e);
1010             }
1011             Response response = Response.status(
1012                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
1013             throw new WebApplicationException(response);
1014         }
1015         return contactObjectList;
1016     }
1017
1018     /**
1019      * Gets the contact.
1020      * 
1021      * @param parentcsid the parentcsid
1022      * @param itemcsid the itemcsid
1023      * @param csid the csid
1024      * 
1025      * @return the contact
1026      */
1027     @GET
1028     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1029     public MultipartOutput getContact(
1030             @PathParam("parentcsid") String parentcsid,
1031             @PathParam("itemcsid") String itemcsid,
1032             @PathParam("csid") String csid) {
1033         MultipartOutput result = null;
1034        if (logger.isDebugEnabled()) {
1035             logger.debug("getContact with parentCsid=" + parentcsid +
1036             " itemcsid=" + itemcsid + " csid=" + csid);
1037         }
1038         try {
1039             // Note that we have to create the service context and document
1040             // handler for the Contact service, not the main service.
1041                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1042             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1043             getRepositoryClient(ctx).get(ctx, csid, handler);
1044             result = (MultipartOutput) ctx.getOutput();
1045         } catch (UnauthorizedException ue) {
1046             Response response = Response.status(
1047                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
1048             throw new WebApplicationException(response);
1049         } catch (DocumentNotFoundException dnfe) {
1050             if (logger.isDebugEnabled()) {
1051                 logger.debug("getContact", dnfe);
1052             }
1053             Response response = Response.status(Response.Status.NOT_FOUND)
1054                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1055                 .type("text/plain").build();
1056             throw new WebApplicationException(response);
1057         } catch (Exception e) {
1058             if (logger.isDebugEnabled()) {
1059                 logger.debug("getContact", e);
1060             }
1061             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
1062                 .entity("Get contact failed")
1063                 .type("text/plain").build();
1064             throw new WebApplicationException(response);
1065         }
1066         if (result == null) {
1067             Response response = Response.status(Response.Status.NOT_FOUND)
1068                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1069                 .type("text/plain").build();
1070             throw new WebApplicationException(response);
1071         }
1072         return result;
1073
1074     }
1075
1076     /**
1077      * Update contact.
1078      * 
1079      * @param parentcsid the parentcsid
1080      * @param itemcsid the itemcsid
1081      * @param csid the csid
1082      * @param theUpdate the the update
1083      * 
1084      * @return the multipart output
1085      */
1086     @PUT
1087     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1088     public MultipartOutput updateContact(
1089             @PathParam("parentcsid") String parentcsid,
1090             @PathParam("itemcsid") String itemcsid,
1091             @PathParam("csid") String csid,
1092             MultipartInput theUpdate) {
1093        if (logger.isDebugEnabled()) {
1094             logger.debug("updateContact with parentcsid=" + parentcsid +
1095             " itemcsid=" + itemcsid + " csid=" + csid);
1096         }
1097        if (parentcsid == null || parentcsid.trim().isEmpty()) {
1098             logger.error("updateContact: missing csid!");
1099             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1100                     "update failed on Contact parentcsid=" + parentcsid).type(
1101                     "text/plain").build();
1102             throw new WebApplicationException(response);
1103         }
1104         if (itemcsid == null || itemcsid.trim().isEmpty()) {
1105             logger.error("updateContact: missing itemcsid!");
1106             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1107                     "update failed on Contact=" + itemcsid).type(
1108                     "text/plain").build();
1109             throw new WebApplicationException(response);
1110         }
1111         if (csid == null || csid.trim().isEmpty()) {
1112             logger.error("updateContact: missing csid!");
1113             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1114                     "update failed on Contact=" + csid).type(
1115                     "text/plain").build();
1116             throw new WebApplicationException(response);
1117         }
1118         MultipartOutput result = null;
1119         try {
1120             // Note that we have to create the service context and document
1121             // handler for the Contact service, not the main service.
1122                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
1123                                 theUpdate);
1124             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1125             getRepositoryClient(ctx).update(ctx, csid, handler);
1126             result = (MultipartOutput) ctx.getOutput();
1127         } catch (BadRequestException bre) {
1128             Response response = Response.status(
1129                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1130             throw new WebApplicationException(response);
1131         } catch (UnauthorizedException ue) {
1132             Response response = Response.status(
1133                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1134             throw new WebApplicationException(response);
1135         } catch (DocumentNotFoundException dnfe) {
1136             if (logger.isDebugEnabled()) {
1137                 logger.debug("caught exception in updateContact", dnfe);
1138             }
1139             Response response = Response.status(Response.Status.NOT_FOUND).entity(
1140                     "Update failed on Contact csid=" + itemcsid).type(
1141                     "text/plain").build();
1142             throw new WebApplicationException(response);
1143         } catch (Exception e) {
1144             Response response = Response.status(
1145                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1146             throw new WebApplicationException(response);
1147         }
1148         return result;
1149     }
1150
1151     /**
1152      * Delete contact.
1153      * 
1154      * @param parentcsid the parentcsid
1155      * @param itemcsid the itemcsid
1156      * @param csid the csid
1157      * 
1158      * @return the response
1159      */
1160     @DELETE
1161     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1162     public Response deleteContact(
1163             @PathParam("parentcsid") String parentcsid,
1164             @PathParam("itemcsid") String itemcsid,
1165             @PathParam("csid") String csid) {
1166         if (logger.isDebugEnabled()) {
1167             logger.debug("deleteContact with parentCsid=" + parentcsid +
1168             " itemcsid=" + itemcsid + " csid=" + csid);
1169         }
1170         if (parentcsid == null || parentcsid.trim().isEmpty()) {
1171             logger.error("deleteContact: missing parentcsid!");
1172             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1173                     "delete contact failed on parentcsid=" + parentcsid).type(
1174                     "text/plain").build();
1175             throw new WebApplicationException(response);
1176         }
1177         if (itemcsid == null || itemcsid.trim().isEmpty()) {
1178             logger.error("deleteContact: missing itemcsid!");
1179             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1180                     "delete contact failed on itemcsid=" + itemcsid).type(
1181                     "text/plain").build();
1182             throw new WebApplicationException(response);
1183         }
1184         if (csid == null || csid.trim().isEmpty()) {
1185             logger.error("deleteContact: missing csid!");
1186             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1187                     "delete contact failed on csid=" + csid).type(
1188                     "text/plain").build();
1189             throw new WebApplicationException(response);
1190         }
1191         try {
1192             // Note that we have to create the service context for the
1193             // Contact service, not the main service.
1194                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1195             getRepositoryClient(ctx).delete(ctx, csid);
1196             return Response.status(HttpResponseCodes.SC_OK).build();   
1197          } catch (UnauthorizedException ue) {
1198             Response response = Response.status(
1199                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1200             throw new WebApplicationException(response);
1201          } catch (DocumentNotFoundException dnfe) {
1202             if (logger.isDebugEnabled()) {
1203                 logger.debug("Caught exception in deleteContact", dnfe);
1204             }
1205             Response response = Response.status(Response.Status.NOT_FOUND)
1206                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1207                 .type("text/plain").build();
1208             throw new WebApplicationException(response);
1209        } catch (Exception e) {
1210             Response response = Response.status(
1211                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1212             throw new WebApplicationException(response);
1213         }
1214     }
1215
1216 }