]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d98f2748e664066ada66caf2aed9d3299c9bf886
[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.net.URI;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.DELETE;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.HttpHeaders;
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.PersonJAXBSchema;
45 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
46 import org.collectionspace.services.common.ClientType;
47 import org.collectionspace.services.common.ServiceMain;
48 import org.collectionspace.services.common.context.MultipartServiceContext;
49 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
50 import org.collectionspace.services.common.context.ServiceContext;
51 import org.collectionspace.services.common.document.BadRequestException;
52 import org.collectionspace.services.common.document.DocumentFilter;
53 import org.collectionspace.services.common.document.DocumentHandler;
54 import org.collectionspace.services.common.document.DocumentNotFoundException;
55 import org.collectionspace.services.common.security.UnauthorizedException;
56 import org.collectionspace.services.common.query.IQueryManager;
57 import org.collectionspace.services.contact.ContactResource;
58 import org.collectionspace.services.contact.ContactsCommon;
59 import org.collectionspace.services.contact.ContactsCommonList;
60 import org.collectionspace.services.contact.ContactJAXBSchema;
61 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
62 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
63 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
64 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
65 import org.jboss.resteasy.util.HttpResponseCodes;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 @Path("/personauthorities")
70 @Consumes("multipart/mixed")
71 @Produces("multipart/mixed")
72 public class PersonAuthorityResource extends AbstractCollectionSpaceResourceImpl {
73
74     private final static String personAuthorityServiceName = "personauthorities";
75     private final static String personServiceName = "persons";
76     final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
77     //FIXME retrieve client type from configuration
78     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
79     private ContactResource contactResource = new ContactResource();
80
81     public PersonAuthorityResource() {
82         // do nothing
83     }
84
85     @Override
86     protected String getVersionString() {
87         /** The last change revision. */
88         final String lastChangeRevision = "$LastChangedRevision$";
89         return lastChangeRevision;
90     }
91     
92     @Override
93     public String getServiceName() {
94         return personAuthorityServiceName;
95     }
96
97     public String getItemServiceName() {
98         return personServiceName;
99     }
100
101     public String getContactServiceName() {
102         return contactResource.getServiceName();
103     }
104
105     @Override
106     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
107         DocumentHandler docHandler = ctx.getDocumentHandler();
108         if (ctx.getInput() != null) {
109             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
110             if (obj != null) {
111                 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
112             }
113         }
114         return docHandler;
115     }
116
117     private DocumentHandler createItemDocumentHandler(
118             ServiceContext ctx,
119             String inAuthority) throws Exception {
120         DocumentHandler docHandler = ctx.getDocumentHandler();
121         ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
122         if (ctx.getInput() != null) {
123             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
124                     PersonsCommon.class);
125             if (obj != null) {
126                 docHandler.setCommonPart((PersonsCommon) obj);
127             }
128         }
129         return docHandler;
130     }
131
132     private DocumentHandler createContactDocumentHandler(
133             ServiceContext ctx, String inAuthority,
134             String inItem) throws Exception {
135         DocumentHandler docHandler = ctx.getDocumentHandler();
136         // Set the inAuthority and inItem values, which specify the
137         // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
138         // (e.g. Person, Organization) with which the Contact is associated.
139         ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
140         ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
141         if (ctx.getInput() != null) {
142             Object obj = ((MultipartServiceContext) ctx)
143                 .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
144                 ContactsCommon.class);
145             if (obj != null) {
146                 docHandler.setCommonPart((ContactsCommon) obj);
147             }
148         }
149         return docHandler;
150     }
151
152     @POST
153     public Response createPersonAuthority(MultipartInput input) {
154         try {
155             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
156             DocumentHandler handler = createDocumentHandler(ctx);
157             String csid = getRepositoryClient(ctx).create(ctx, handler);
158             //personAuthorityObject.setCsid(csid);
159             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
160             path.path("" + csid);
161             Response response = Response.created(path.build()).build();
162             return response;
163         } catch (BadRequestException bre) {
164             Response response = Response.status(
165                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
166             throw new WebApplicationException(response);
167         } catch (UnauthorizedException ue) {
168             Response response = Response.status(
169                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
170             throw new WebApplicationException(response);
171         } catch (Exception e) {
172             if (logger.isDebugEnabled()) {
173                 logger.debug("Caught exception in createPersonAuthority", e);
174             }
175             Response response = Response.status(
176                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
177             throw new WebApplicationException(response);
178         }
179     }
180
181     @GET
182     @Path("{csid}")
183     public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
184         String idValue = null;
185         if (csid == null) {
186             logger.error("getPersonAuthority: missing csid!");
187             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
188                     "get failed on PersonAuthority csid=" + csid).type(
189                     "text/plain").build();
190             throw new WebApplicationException(response);
191         }
192         if (logger.isDebugEnabled()) {
193             logger.debug("getPersonAuthority with path(id)=" + csid);
194         }
195         MultipartOutput result = null;
196         try {
197             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
198             DocumentHandler handler = createDocumentHandler(ctx);
199             getRepositoryClient(ctx).get(ctx, csid, handler);
200             result = (MultipartOutput) ctx.getOutput();
201         } catch (UnauthorizedException ue) {
202             Response response = Response.status(
203                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
204             throw new WebApplicationException(response);
205         } catch (DocumentNotFoundException dnfe) {
206             if (logger.isDebugEnabled()) {
207                 logger.debug("getPersonAuthority", dnfe);
208             }
209             Response response = Response.status(Response.Status.NOT_FOUND).entity(
210                     "Get failed on PersonAuthority csid=" + csid).type(
211                     "text/plain").build();
212             throw new WebApplicationException(response);
213         } catch (Exception e) {
214             if (logger.isDebugEnabled()) {
215                 logger.debug("getPersonAuthority", e);
216             }
217             Response response = Response.status(
218                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
219             throw new WebApplicationException(response);
220         }
221         if (result == null) {
222             Response response = Response.status(Response.Status.NOT_FOUND).entity(
223                     "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
224                     "text/plain").build();
225             throw new WebApplicationException(response);
226         }
227         return result;
228     }
229
230     @GET
231     @Produces("application/xml")
232     public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
233         PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
234         try {
235             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
236             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
237             DocumentHandler handler = createDocumentHandler(ctx);
238             DocumentFilter myFilter = new DocumentFilter();
239             myFilter.setPagination(queryParams);
240             String nameQ = queryParams.getFirst("refName");
241             if (nameQ != null) {
242                 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
243             }
244             handler.setDocumentFilter(myFilter);
245             getRepositoryClient(ctx).getFiltered(ctx, handler);
246             personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
247         } catch (UnauthorizedException ue) {
248             Response response = Response.status(
249                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
250             throw new WebApplicationException(response);
251         } catch (Exception e) {
252             if (logger.isDebugEnabled()) {
253                 logger.debug("Caught exception in getPersonAuthorityList", e);
254             }
255             Response response = Response.status(
256                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
257             throw new WebApplicationException(response);
258         }
259         return personAuthorityObjectList;
260     }
261
262     @PUT
263     @Path("{csid}")
264     public MultipartOutput updatePersonAuthority(
265             @PathParam("csid") String csid,
266             MultipartInput theUpdate) {
267         if (logger.isDebugEnabled()) {
268             logger.debug("updatePersonAuthority with csid=" + csid);
269         }
270         if (csid == null || "".equals(csid)) {
271             logger.error("updatePersonAuthority: missing csid!");
272             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
273                     "update failed on PersonAuthority csid=" + csid).type(
274                     "text/plain").build();
275             throw new WebApplicationException(response);
276         }
277         MultipartOutput result = null;
278         try {
279             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
280             DocumentHandler handler = createDocumentHandler(ctx);
281             getRepositoryClient(ctx).update(ctx, csid, handler);
282             result = (MultipartOutput) ctx.getOutput();
283         } catch (BadRequestException bre) {
284             Response response = Response.status(
285                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
286             throw new WebApplicationException(response);
287         } catch (UnauthorizedException ue) {
288             Response response = Response.status(
289                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
290             throw new WebApplicationException(response);
291         } catch (DocumentNotFoundException dnfe) {
292             if (logger.isDebugEnabled()) {
293                 logger.debug("caugth exception in updatePersonAuthority", dnfe);
294             }
295             Response response = Response.status(Response.Status.NOT_FOUND).entity(
296                     "Update failed on PersonAuthority csid=" + csid).type(
297                     "text/plain").build();
298             throw new WebApplicationException(response);
299         } catch (Exception e) {
300             Response response = Response.status(
301                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
302             throw new WebApplicationException(response);
303         }
304         return result;
305     }
306
307     @DELETE
308     @Path("{csid}")
309     public Response deletePersonAuthority(@PathParam("csid") String csid) {
310
311         if (logger.isDebugEnabled()) {
312             logger.debug("deletePersonAuthority with csid=" + csid);
313         }
314         if (csid == null || "".equals(csid)) {
315             logger.error("deletePersonAuthority: missing csid!");
316             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
317                     "delete failed on PersonAuthority csid=" + csid).type(
318                     "text/plain").build();
319             throw new WebApplicationException(response);
320         }
321         try {
322             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
323             getRepositoryClient(ctx).delete(ctx, csid);
324             return Response.status(HttpResponseCodes.SC_OK).build();
325         } catch (UnauthorizedException ue) {
326             Response response = Response.status(
327                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
328             throw new WebApplicationException(response);
329         } catch (DocumentNotFoundException dnfe) {
330             if (logger.isDebugEnabled()) {
331                 logger.debug("caught exception in deletePersonAuthority", dnfe);
332             }
333             Response response = Response.status(Response.Status.NOT_FOUND).entity(
334                     "Delete failed on PersonAuthority csid=" + csid).type(
335                     "text/plain").build();
336             throw new WebApplicationException(response);
337         } catch (Exception e) {
338             Response response = Response.status(
339                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
340             throw new WebApplicationException(response);
341         }
342
343     }
344
345     /*************************************************************************
346      * Person parts - this is a sub-resource of PersonAuthority
347      *************************************************************************/
348     @POST
349     @Path("{csid}/items")
350     public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
351         try {
352             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
353             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
354             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
355             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
356             path.path(parentcsid + "/items/" + itemcsid);
357             Response response = Response.created(path.build()).build();
358             return response;
359         } catch (BadRequestException bre) {
360             Response response = Response.status(
361                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
362             throw new WebApplicationException(response);
363         } catch (UnauthorizedException ue) {
364             Response response = Response.status(
365                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
366             throw new WebApplicationException(response);
367         } catch (Exception e) {
368             if (logger.isDebugEnabled()) {
369                 logger.debug("Caught exception in createPerson", e);
370             }
371             Response response = Response.status(
372                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
373             throw new WebApplicationException(response);
374         }
375     }
376
377     @GET
378     @Path("{csid}/items/{itemcsid}")
379     public MultipartOutput getPerson(
380             @PathParam("csid") String parentcsid,
381             @PathParam("itemcsid") String itemcsid) {
382         if (logger.isDebugEnabled()) {
383             logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
384         }
385         if (parentcsid == null || "".equals(parentcsid)) {
386             logger.error("getPerson: missing csid!");
387             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
388                     "get failed on Person csid=" + parentcsid).type(
389                     "text/plain").build();
390             throw new WebApplicationException(response);
391         }
392         if (itemcsid == null || "".equals(itemcsid)) {
393             logger.error("getPerson: missing itemcsid!");
394             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
395                     "get failed on Person itemcsid=" + itemcsid).type(
396                     "text/plain").build();
397             throw new WebApplicationException(response);
398         }
399         MultipartOutput result = null;
400         try {
401             // Note that we have to create the service context for the Items, not the main service
402             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
403             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
404             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
405             // TODO should we assert that the item is in the passed personAuthority?
406             result = (MultipartOutput) ctx.getOutput();
407         } catch (UnauthorizedException ue) {
408             Response response = Response.status(
409                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
410             throw new WebApplicationException(response);
411         } catch (DocumentNotFoundException dnfe) {
412             if (logger.isDebugEnabled()) {
413                 logger.debug("getPerson", dnfe);
414             }
415             Response response = Response.status(Response.Status.NOT_FOUND).entity(
416                     "Get failed on Person csid=" + itemcsid).type(
417                     "text/plain").build();
418             throw new WebApplicationException(response);
419         } catch (Exception e) {
420             if (logger.isDebugEnabled()) {
421                 logger.debug("getPerson", e);
422             }
423             Response response = Response.status(
424                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
425             throw new WebApplicationException(response);
426         }
427         if (result == null) {
428             Response response = Response.status(Response.Status.NOT_FOUND).entity(
429                     "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
430                     "text/plain").build();
431             throw new WebApplicationException(response);
432         }
433         return result;
434     }
435
436     @GET
437     @Path("{csid}/items")
438     @Produces("application/xml")
439     public PersonsCommonList getPersonList(
440             @PathParam("csid") String parentcsid,
441             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
442             @Context UriInfo ui) {
443         PersonsCommonList personObjectList = new PersonsCommonList();
444         try {
445             // Note that docType defaults to the ServiceName, so we're fine with that.
446             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
447             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
448             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
449             DocumentFilter myFilter = new DocumentFilter();
450             myFilter.setPagination(queryParams);
451
452             // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
453             myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
454                         PersonJAXBSchema.IN_AUTHORITY +
455                         "='" + parentcsid + "'" + "AND ecm:isProxy = 0");
456             
457             // AND persons_common:displayName LIKE '%partialTerm%'
458             if (partialTerm != null && !partialTerm.isEmpty()) {
459                 String ptClause = "AND " +
460                 PersonJAXBSchema.PERSONS_COMMON + ":" +
461                         PersonJAXBSchema.DISPLAY_NAME +
462                         " LIKE " +
463                         "'%" + partialTerm + "%'";
464                 myFilter.appendWhereClause(ptClause);
465             }
466             
467             handler.setDocumentFilter(myFilter);
468             getRepositoryClient(ctx).getFiltered(ctx, handler);
469             personObjectList = (PersonsCommonList) handler.getCommonPartList();
470         } catch (UnauthorizedException ue) {
471             Response response = Response.status(
472                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
473             throw new WebApplicationException(response);
474         } catch (Exception e) {
475             if (logger.isDebugEnabled()) {
476                 logger.debug("Caught exception in getPersonList", e);
477             }
478             Response response = Response.status(
479                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
480             throw new WebApplicationException(response);
481         }
482         return personObjectList;
483     }
484
485     @PUT
486     @Path("{csid}/items/{itemcsid}")
487     public MultipartOutput updatePerson(
488             @PathParam("csid") String parentcsid,
489             @PathParam("itemcsid") String itemcsid,
490             MultipartInput theUpdate) {
491         if (logger.isDebugEnabled()) {
492             logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
493         }
494         if (parentcsid == null || "".equals(parentcsid)) {
495             logger.error("updatePerson: missing csid!");
496             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
497                     "update failed on Person parentcsid=" + parentcsid).type(
498                     "text/plain").build();
499             throw new WebApplicationException(response);
500         }
501         if (itemcsid == null || "".equals(itemcsid)) {
502             logger.error("updatePerson: missing itemcsid!");
503             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
504                     "update failed on Person=" + itemcsid).type(
505                     "text/plain").build();
506             throw new WebApplicationException(response);
507         }
508         MultipartOutput result = null;
509         try {
510             // Note that we have to create the service context for the Items, not the main service
511             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
512             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
513             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
514             result = (MultipartOutput) ctx.getOutput();
515         } catch (BadRequestException bre) {
516             Response response = Response.status(
517                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
518             throw new WebApplicationException(response);
519         } catch (UnauthorizedException ue) {
520             Response response = Response.status(
521                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
522             throw new WebApplicationException(response);
523         } catch (DocumentNotFoundException dnfe) {
524             if (logger.isDebugEnabled()) {
525                 logger.debug("caught exception in updatePerson", dnfe);
526             }
527             Response response = Response.status(Response.Status.NOT_FOUND).entity(
528                     "Update failed on Person csid=" + itemcsid).type(
529                     "text/plain").build();
530             throw new WebApplicationException(response);
531         } catch (Exception e) {
532             Response response = Response.status(
533                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
534             throw new WebApplicationException(response);
535         }
536         return result;
537     }
538
539     @DELETE
540     @Path("{csid}/items/{itemcsid}")
541     public Response deletePerson(
542             @PathParam("csid") String parentcsid,
543             @PathParam("itemcsid") String itemcsid) {
544         if (logger.isDebugEnabled()) {
545             logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
546         }
547         if (parentcsid == null || "".equals(parentcsid)) {
548             logger.error("deletePerson: missing csid!");
549             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
550                     "delete failed on Person parentcsid=" + parentcsid).type(
551                     "text/plain").build();
552             throw new WebApplicationException(response);
553         }
554         if (itemcsid == null || "".equals(itemcsid)) {
555             logger.error("deletePerson: missing itemcsid!");
556             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
557                     "delete failed on Person=" + itemcsid).type(
558                     "text/plain").build();
559             throw new WebApplicationException(response);
560         }
561         try {
562             // Note that we have to create the service context for the Items, not the main service
563             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
564             getRepositoryClient(ctx).delete(ctx, itemcsid);
565             return Response.status(HttpResponseCodes.SC_OK).build();
566         } catch (UnauthorizedException ue) {
567             Response response = Response.status(
568                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
569             throw new WebApplicationException(response);
570         } catch (DocumentNotFoundException dnfe) {
571             if (logger.isDebugEnabled()) {
572                 logger.debug("caught exception in deletePerson", dnfe);
573             }
574             Response response = Response.status(Response.Status.NOT_FOUND).entity(
575                     "Delete failed on Person itemcsid=" + itemcsid).type(
576                     "text/plain").build();
577             throw new WebApplicationException(response);
578         } catch (Exception e) {
579             Response response = Response.status(
580                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
581             throw new WebApplicationException(response);
582         }
583
584     }
585
586     /*************************************************************************
587      * Contact parts - this is a sub-resource of Person (or "item")
588      *************************************************************************/
589     @POST
590     @Path("{parentcsid}/items/{itemcsid}/contacts")
591     public Response createContact(
592             @PathParam("parentcsid") String parentcsid,
593             @PathParam("itemcsid") String itemcsid,
594             MultipartInput input) {
595         try {
596             // Note that we have to create the service context and document
597             // handler for the Contact service, not the main service.
598             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getContactServiceName());
599             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
600             String csid = getRepositoryClient(ctx).create(ctx, handler);
601             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
602             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
603             Response response = Response.created(path.build()).build();
604             return response;
605         } catch (BadRequestException bre) {
606             Response response = Response.status(
607                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
608             throw new WebApplicationException(response);
609         } catch (UnauthorizedException ue) {
610             Response response = Response.status(
611                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
612             throw new WebApplicationException(response);
613         } catch (Exception e) {
614             if (logger.isDebugEnabled()) {
615                 logger.debug("Caught exception in createContact", e);
616             }
617             Response response = Response.status(
618                 Response.Status.INTERNAL_SERVER_ERROR)
619                 .entity("Attempt to create Contact failed.")
620                 .type("text/plain").build();
621             throw new WebApplicationException(response);
622         }
623         
624     }
625         
626     @GET
627     @Produces({"application/xml"})
628     @Path("{parentcsid}/items/{itemcsid}/contacts/")
629     public ContactsCommonList getContactList(
630             @PathParam("parentcsid") String parentcsid,
631             @PathParam("itemcsid") String itemcsid,
632             @Context UriInfo ui) {
633         ContactsCommonList contactObjectList = new ContactsCommonList();
634         try {
635             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
636             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
637             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
638             DocumentFilter myFilter = new DocumentFilter();
639             myFilter.setPagination(queryParams);
640             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
641                 ContactJAXBSchema.IN_AUTHORITY +
642                 "='" + parentcsid + "'" +
643                 " AND " +
644                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
645                 ContactJAXBSchema.IN_ITEM +
646                 "='" + itemcsid + "'" +
647                 " AND ecm:isProxy = 0");
648             handler.setDocumentFilter(myFilter);
649             getRepositoryClient(ctx).getFiltered(ctx, handler);
650             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
651         } catch (UnauthorizedException ue) {
652             Response response = Response.status(
653                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
654             throw new WebApplicationException(response);
655         } catch (Exception e) {
656             if (logger.isDebugEnabled()) {
657                 logger.debug("Caught exception in getContactsList", e);
658             }
659             Response response = Response.status(
660                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
661             throw new WebApplicationException(response);
662         }
663         return contactObjectList;
664     }
665
666     @GET
667     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
668     public MultipartOutput getContact(
669             @PathParam("parentcsid") String parentcsid,
670             @PathParam("itemcsid") String itemcsid,
671             @PathParam("csid") String csid) {
672         MultipartOutput result = null;
673        if (logger.isDebugEnabled()) {
674             logger.debug("getContact with parentCsid=" + parentcsid +
675             " itemcsid=" + itemcsid + " csid=" + csid);
676         }
677         try {
678             // Note that we have to create the service context and document
679             // handler for the Contact service, not the main service.
680             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
681             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
682             getRepositoryClient(ctx).get(ctx, csid, handler);
683             result = (MultipartOutput) ctx.getOutput();
684         } catch (UnauthorizedException ue) {
685             Response response = Response.status(
686                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
687             throw new WebApplicationException(response);
688         } catch (DocumentNotFoundException dnfe) {
689             if (logger.isDebugEnabled()) {
690                 logger.debug("getContact", dnfe);
691             }
692             Response response = Response.status(Response.Status.NOT_FOUND)
693                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
694                 .type("text/plain").build();
695             throw new WebApplicationException(response);
696         } catch (Exception e) {
697             if (logger.isDebugEnabled()) {
698                 logger.debug("getContact", e);
699             }
700             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
701                 .entity("Get contact failed")
702                 .type("text/plain").build();
703             throw new WebApplicationException(response);
704         }
705         if (result == null) {
706             Response response = Response.status(Response.Status.NOT_FOUND)
707                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
708                 .type("text/plain").build();
709             throw new WebApplicationException(response);
710         }
711         return result;
712
713     }
714
715     @PUT
716     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
717     public MultipartOutput updateContact(
718             @PathParam("parentcsid") String parentcsid,
719             @PathParam("itemcsid") String itemcsid,
720             @PathParam("csid") String csid,
721             MultipartInput theUpdate) {
722        if (logger.isDebugEnabled()) {
723             logger.debug("updateContact with parentcsid=" + parentcsid +
724             " itemcsid=" + itemcsid + " csid=" + csid);
725         }
726        if (parentcsid == null || parentcsid.trim().isEmpty()) {
727             logger.error("updateContact: missing csid!");
728             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
729                     "update failed on Contact parentcsid=" + parentcsid).type(
730                     "text/plain").build();
731             throw new WebApplicationException(response);
732         }
733         if (itemcsid == null || itemcsid.trim().isEmpty()) {
734             logger.error("updateContact: missing itemcsid!");
735             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
736                     "update failed on Contact=" + itemcsid).type(
737                     "text/plain").build();
738             throw new WebApplicationException(response);
739         }
740         if (csid == null || csid.trim().isEmpty()) {
741             logger.error("updateContact: missing csid!");
742             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
743                     "update failed on Contact=" + csid).type(
744                     "text/plain").build();
745             throw new WebApplicationException(response);
746         }
747         MultipartOutput result = null;
748         try {
749             // Note that we have to create the service context and document
750             // handler for the Contact service, not the main service.
751             ServiceContext ctx = MultipartServiceContextFactory.get()
752                 .createServiceContext(theUpdate, getContactServiceName());
753             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
754             getRepositoryClient(ctx).update(ctx, csid, handler);
755             result = (MultipartOutput) ctx.getOutput();
756         } catch (BadRequestException bre) {
757             Response response = Response.status(
758                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
759             throw new WebApplicationException(response);
760         } catch (UnauthorizedException ue) {
761             Response response = Response.status(
762                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
763             throw new WebApplicationException(response);
764         } catch (DocumentNotFoundException dnfe) {
765             if (logger.isDebugEnabled()) {
766                 logger.debug("caught exception in updateContact", dnfe);
767             }
768             Response response = Response.status(Response.Status.NOT_FOUND).entity(
769                     "Update failed on Contact csid=" + itemcsid).type(
770                     "text/plain").build();
771             throw new WebApplicationException(response);
772         } catch (Exception e) {
773             Response response = Response.status(
774                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
775             throw new WebApplicationException(response);
776         }
777         return result;
778     }
779
780     @DELETE
781     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
782     public Response deleteContact(
783             @PathParam("parentcsid") String parentcsid,
784             @PathParam("itemcsid") String itemcsid,
785             @PathParam("csid") String csid) {
786         if (logger.isDebugEnabled()) {
787             logger.debug("deleteContact with parentCsid=" + parentcsid +
788             " itemcsid=" + itemcsid + " csid=" + csid);
789         }
790         if (parentcsid == null || parentcsid.trim().isEmpty()) {
791             logger.error("deleteContact: missing parentcsid!");
792             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
793                     "delete contact failed on parentcsid=" + parentcsid).type(
794                     "text/plain").build();
795             throw new WebApplicationException(response);
796         }
797         if (itemcsid == null || itemcsid.trim().isEmpty()) {
798             logger.error("deleteContact: missing itemcsid!");
799             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
800                     "delete contact failed on itemcsid=" + itemcsid).type(
801                     "text/plain").build();
802             throw new WebApplicationException(response);
803         }
804         if (csid == null || csid.trim().isEmpty()) {
805             logger.error("deleteContact: missing csid!");
806             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
807                     "delete contact failed on csid=" + csid).type(
808                     "text/plain").build();
809             throw new WebApplicationException(response);
810         }
811         try {
812             // Note that we have to create the service context for the
813             // Contact service, not the main service.
814             ServiceContext ctx =
815                 MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
816             getRepositoryClient(ctx).delete(ctx, csid);
817             return Response.status(HttpResponseCodes.SC_OK).build();   
818          } catch (UnauthorizedException ue) {
819             Response response = Response.status(
820                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
821             throw new WebApplicationException(response);
822          } catch (DocumentNotFoundException dnfe) {
823             if (logger.isDebugEnabled()) {
824                 logger.debug("Caught exception in deleteContact", dnfe);
825             }
826             Response response = Response.status(Response.Status.NOT_FOUND)
827                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
828                 .type("text/plain").build();
829             throw new WebApplicationException(response);
830        } catch (Exception e) {
831             Response response = Response.status(
832                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
833             throw new WebApplicationException(response);
834         }
835     }
836
837 }