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