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