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