]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e711140ee28577cacf67e8462aca277f4ee73255
[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     /*
106     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
107     RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
108     ctx.setInput(input);
109     return ctx;
110     }
111      */
112     @Override
113     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
114         DocumentHandler docHandler = ctx.getDocumentHandler();
115         if (ctx.getInput() != null) {
116             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
117             if (obj != null) {
118                 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
119             }
120         }
121         return docHandler;
122     }
123
124     private DocumentHandler createItemDocumentHandler(
125             ServiceContext ctx,
126             String inAuthority) throws Exception {
127         DocumentHandler docHandler = ctx.getDocumentHandler();
128         ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
129         if (ctx.getInput() != null) {
130             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
131                     PersonsCommon.class);
132             if (obj != null) {
133                 docHandler.setCommonPart((PersonsCommon) obj);
134             }
135         }
136         return docHandler;
137     }
138
139     private DocumentHandler createContactDocumentHandler(
140             ServiceContext ctx, String inAuthority,
141             String inItem) throws Exception {
142         DocumentHandler docHandler = ctx.getDocumentHandler();
143         // Set the inAuthority and inItem values, which specify the
144         // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
145         // (e.g. Person, Organization) with which the Contact is associated.
146         ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
147         ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
148         if (ctx.getInput() != null) {
149             Object obj = ((MultipartServiceContext) ctx)
150                 .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
151                 ContactsCommon.class);
152             if (obj != null) {
153                 docHandler.setCommonPart((ContactsCommon) obj);
154             }
155         }
156         return docHandler;
157     }
158
159
160     @POST
161     public Response createPersonAuthority(MultipartInput input) {
162         try {
163             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
164             DocumentHandler handler = createDocumentHandler(ctx);
165             String csid = getRepositoryClient(ctx).create(ctx, handler);
166             //personAuthorityObject.setCsid(csid);
167             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
168             path.path("" + csid);
169             Response response = Response.created(path.build()).build();
170             return response;
171         } catch (UnauthorizedException ue) {
172             Response response = Response.status(
173                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
174             throw new WebApplicationException(response);
175         } catch (Exception e) {
176             if (logger.isDebugEnabled()) {
177                 logger.debug("Caught exception in createPersonAuthority", e);
178             }
179             Response response = Response.status(
180                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
181             throw new WebApplicationException(response);
182         }
183     }
184
185     @GET
186     @Path("{csid}")
187     public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
188         String idValue = null;
189         if (csid == null) {
190             logger.error("getPersonAuthority: missing csid!");
191             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
192                     "get failed on PersonAuthority csid=" + csid).type(
193                     "text/plain").build();
194             throw new WebApplicationException(response);
195         }
196         if (logger.isDebugEnabled()) {
197             logger.debug("getPersonAuthority with path(id)=" + csid);
198         }
199         MultipartOutput result = null;
200         try {
201             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
202             DocumentHandler handler = createDocumentHandler(ctx);
203             getRepositoryClient(ctx).get(ctx, csid, handler);
204             result = (MultipartOutput) ctx.getOutput();
205         } catch (UnauthorizedException ue) {
206             Response response = Response.status(
207                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
208             throw new WebApplicationException(response);
209         } catch (DocumentNotFoundException dnfe) {
210             if (logger.isDebugEnabled()) {
211                 logger.debug("getPersonAuthority", dnfe);
212             }
213             Response response = Response.status(Response.Status.NOT_FOUND).entity(
214                     "Get failed on PersonAuthority csid=" + csid).type(
215                     "text/plain").build();
216             throw new WebApplicationException(response);
217         } catch (Exception e) {
218             if (logger.isDebugEnabled()) {
219                 logger.debug("getPersonAuthority", e);
220             }
221             Response response = Response.status(
222                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
223             throw new WebApplicationException(response);
224         }
225         if (result == null) {
226             Response response = Response.status(Response.Status.NOT_FOUND).entity(
227                     "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
228                     "text/plain").build();
229             throw new WebApplicationException(response);
230         }
231         return result;
232     }
233
234     @GET
235     @Produces("application/xml")
236     public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
237         PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
238         try {
239             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
240             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
241             DocumentHandler handler = createDocumentHandler(ctx);
242             DocumentFilter myFilter = new DocumentFilter();
243             myFilter.setPagination(queryParams);
244             String nameQ = queryParams.getFirst("refName");
245             if (nameQ != null) {
246                 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
247             }
248             handler.setDocumentFilter(myFilter);
249             getRepositoryClient(ctx).getFiltered(ctx, handler);
250             personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
251         } catch (UnauthorizedException ue) {
252             Response response = Response.status(
253                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
254             throw new WebApplicationException(response);
255         } catch (Exception e) {
256             if (logger.isDebugEnabled()) {
257                 logger.debug("Caught exception in getPersonAuthorityList", e);
258             }
259             Response response = Response.status(
260                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
261             throw new WebApplicationException(response);
262         }
263         return personAuthorityObjectList;
264     }
265
266     @PUT
267     @Path("{csid}")
268     public MultipartOutput updatePersonAuthority(
269             @PathParam("csid") String csid,
270             MultipartInput theUpdate) {
271         if (logger.isDebugEnabled()) {
272             logger.debug("updatePersonAuthority with csid=" + csid);
273         }
274         if (csid == null || "".equals(csid)) {
275             logger.error("updatePersonAuthority: missing csid!");
276             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
277                     "update failed on PersonAuthority csid=" + csid).type(
278                     "text/plain").build();
279             throw new WebApplicationException(response);
280         }
281         MultipartOutput result = null;
282         try {
283             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
284             DocumentHandler handler = createDocumentHandler(ctx);
285             getRepositoryClient(ctx).update(ctx, csid, handler);
286             result = (MultipartOutput) ctx.getOutput();
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     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     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 }