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