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