]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8bdf3a9b5eaaebecf9b828e57fb801a42980bfac
[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         if (csid == null) {
247             logger.error("getPersonAuthority: missing csid!");
248             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
249                     "get failed on PersonAuthority csid=" + csid).type(
250                     "text/plain").build();
251             throw new WebApplicationException(response);
252         }
253         if (logger.isDebugEnabled()) {
254             logger.debug("getPersonAuthority with path(id)=" + csid);
255         } 
256         MultipartOutput result = null;
257         try {
258             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
259             DocumentHandler handler = createDocumentHandler(ctx);
260             getRepositoryClient(ctx).get(ctx, csid, handler);
261             result = (MultipartOutput) ctx.getOutput();
262         } catch (UnauthorizedException ue) {
263             Response response = Response.status(
264                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
265             throw new WebApplicationException(response);
266         } catch (DocumentNotFoundException dnfe) {
267             if (logger.isDebugEnabled()) {
268                 logger.debug("getPersonAuthority", dnfe);
269             }
270             Response response = Response.status(Response.Status.NOT_FOUND).entity(
271                     "Get failed on PersonAuthority csid=" + csid).type(
272                     "text/plain").build();
273             throw new WebApplicationException(response);
274         } catch (Exception e) {
275             if (logger.isDebugEnabled()) {
276                 logger.debug("getPersonAuthority", e);
277             }
278             Response response = Response.status(
279                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
280             throw new WebApplicationException(response);
281         }
282         if (result == null) {
283             Response response = Response.status(Response.Status.NOT_FOUND).entity(
284                     "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
285                     "text/plain").build();
286             throw new WebApplicationException(response);
287         }
288         return result;
289     }
290
291     @GET
292     @Produces("application/xml")
293     public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
294         PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
295         try {
296             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
297             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
298             DocumentHandler handler = createDocumentHandler(ctx);
299             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
300             myFilter.setPagination(queryParams);
301             String nameQ = queryParams.getFirst("refName");
302             if (nameQ != null) {
303                 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
304             }
305             handler.setDocumentFilter(myFilter);
306             getRepositoryClient(ctx).getFiltered(ctx, handler);
307             personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
308         } catch (UnauthorizedException ue) {
309             Response response = Response.status(
310                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
311             throw new WebApplicationException(response);
312         } catch (Exception e) {
313             if (logger.isDebugEnabled()) {
314                 logger.debug("Caught exception in getPersonAuthorityList", e);
315             }
316             Response response = Response.status(
317                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
318             throw new WebApplicationException(response);
319         }
320         return personAuthorityObjectList;
321     }
322
323     @PUT
324     @Path("{csid}")
325     public MultipartOutput updatePersonAuthority(
326             @PathParam("csid") String csid,
327             MultipartInput theUpdate) {
328         if (logger.isDebugEnabled()) {
329             logger.debug("updatePersonAuthority with csid=" + csid);
330         }
331         if (csid == null || "".equals(csid)) {
332             logger.error("updatePersonAuthority: missing csid!");
333             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
334                     "update failed on PersonAuthority csid=" + csid).type(
335                     "text/plain").build();
336             throw new WebApplicationException(response);
337         }
338         MultipartOutput result = null;
339         try {
340             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
341             DocumentHandler handler = createDocumentHandler(ctx);
342             getRepositoryClient(ctx).update(ctx, csid, handler);
343             result = (MultipartOutput) ctx.getOutput();
344         } catch (BadRequestException bre) {
345             Response response = Response.status(
346                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
347             throw new WebApplicationException(response);
348         } catch (UnauthorizedException ue) {
349             Response response = Response.status(
350                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
351             throw new WebApplicationException(response);
352         } catch (DocumentNotFoundException dnfe) {
353             if (logger.isDebugEnabled()) {
354                 logger.debug("caugth exception in updatePersonAuthority", dnfe);
355             }
356             Response response = Response.status(Response.Status.NOT_FOUND).entity(
357                     "Update failed on PersonAuthority csid=" + csid).type(
358                     "text/plain").build();
359             throw new WebApplicationException(response);
360         } catch (Exception e) {
361             Response response = Response.status(
362                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
363             throw new WebApplicationException(response);
364         }
365         return result;
366     }
367
368     @DELETE
369     @Path("{csid}")
370     public Response deletePersonAuthority(@PathParam("csid") String csid) {
371
372         if (logger.isDebugEnabled()) {
373             logger.debug("deletePersonAuthority with csid=" + csid);
374         }
375         if (csid == null || "".equals(csid)) {
376             logger.error("deletePersonAuthority: missing csid!");
377             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
378                     "delete failed on PersonAuthority csid=" + csid).type(
379                     "text/plain").build();
380             throw new WebApplicationException(response);
381         }
382         try {
383             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
384             getRepositoryClient(ctx).delete(ctx, csid);
385             return Response.status(HttpResponseCodes.SC_OK).build();
386         } catch (UnauthorizedException ue) {
387             Response response = Response.status(
388                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
389             throw new WebApplicationException(response);
390         } catch (DocumentNotFoundException dnfe) {
391             if (logger.isDebugEnabled()) {
392                 logger.debug("caught exception in deletePersonAuthority", dnfe);
393             }
394             Response response = Response.status(Response.Status.NOT_FOUND).entity(
395                     "Delete failed on PersonAuthority csid=" + csid).type(
396                     "text/plain").build();
397             throw new WebApplicationException(response);
398         } catch (Exception e) {
399             Response response = Response.status(
400                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
401             throw new WebApplicationException(response);
402         }
403
404     }
405
406     /*************************************************************************
407      * Person parts - this is a sub-resource of PersonAuthority
408      *************************************************************************/
409     @POST
410     @Path("{csid}/items")
411     public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
412         try {
413             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
414             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
415             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
416             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
417             path.path(parentcsid + "/items/" + itemcsid);
418             Response response = Response.created(path.build()).build();
419             return response;
420         } catch (BadRequestException bre) {
421             Response response = Response.status(
422                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
423             throw new WebApplicationException(response);
424         } catch (UnauthorizedException ue) {
425             Response response = Response.status(
426                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
427             throw new WebApplicationException(response);
428         } catch (Exception e) {
429             if (logger.isDebugEnabled()) {
430                 logger.debug("Caught exception in createPerson", e);
431             }
432             Response response = Response.status(
433                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
434             throw new WebApplicationException(response);
435         }
436     }
437
438     @GET
439     @Path("{csid}/items/{itemcsid}")
440     public MultipartOutput getPerson(
441             @PathParam("csid") String parentcsid,
442             @PathParam("itemcsid") String itemcsid) {
443         if (logger.isDebugEnabled()) {
444             logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
445         }
446         if (parentcsid == null || "".equals(parentcsid)) {
447             logger.error("getPerson: missing csid!");
448             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
449                     "get failed on Person csid=" + parentcsid).type(
450                     "text/plain").build();
451             throw new WebApplicationException(response);
452         }
453         if (itemcsid == null || "".equals(itemcsid)) {
454             logger.error("getPerson: missing itemcsid!");
455             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
456                     "get failed on Person itemcsid=" + itemcsid).type(
457                     "text/plain").build();
458             throw new WebApplicationException(response);
459         }
460         MultipartOutput result = null;
461         try {
462             // Note that we have to create the service context for the Items, not the main service
463             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
464             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
465             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
466             // TODO should we assert that the item is in the passed personAuthority?
467             result = (MultipartOutput) ctx.getOutput();
468         } catch (UnauthorizedException ue) {
469             Response response = Response.status(
470                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
471             throw new WebApplicationException(response);
472         } catch (DocumentNotFoundException dnfe) {
473             if (logger.isDebugEnabled()) {
474                 logger.debug("getPerson", dnfe);
475             }
476             Response response = Response.status(Response.Status.NOT_FOUND).entity(
477                     "Get failed on Person csid=" + itemcsid).type(
478                     "text/plain").build();
479             throw new WebApplicationException(response);
480         } catch (Exception e) {
481             if (logger.isDebugEnabled()) {
482                 logger.debug("getPerson", e);
483             }
484             Response response = Response.status(
485                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
486             throw new WebApplicationException(response);
487         }
488         if (result == null) {
489             Response response = Response.status(Response.Status.NOT_FOUND).entity(
490                     "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
491                     "text/plain").build();
492             throw new WebApplicationException(response);
493         }
494         return result;
495     }
496
497     @GET
498     @Path("{csid}/items")
499     @Produces("application/xml")
500     public PersonsCommonList getPersonList(
501             @PathParam("csid") String parentcsid,
502             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
503             @Context UriInfo ui) {
504         PersonsCommonList personObjectList = new PersonsCommonList();
505         try {
506             // Note that docType defaults to the ServiceName, so we're fine with that.
507             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
508             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
509             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
510             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
511             myFilter.setPagination(queryParams);
512
513             // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
514             myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
515                         PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
516             
517             // AND persons_common:displayName LIKE '%partialTerm%'
518             if (partialTerm != null && !partialTerm.isEmpty()) {
519                 String ptClause = "AND " +
520                 PersonJAXBSchema.PERSONS_COMMON + ":" +
521                         PersonJAXBSchema.DISPLAY_NAME +
522                         " LIKE " +
523                         "'%" + partialTerm + "%'";
524                 myFilter.appendWhereClause(ptClause);
525             }
526             
527             handler.setDocumentFilter(myFilter);
528             getRepositoryClient(ctx).getFiltered(ctx, handler);
529             personObjectList = (PersonsCommonList) handler.getCommonPartList();
530         } catch (UnauthorizedException ue) {
531             Response response = Response.status(
532                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
533             throw new WebApplicationException(response);
534         } catch (Exception e) {
535             if (logger.isDebugEnabled()) {
536                 logger.debug("Caught exception in getPersonList", e);
537             }
538             Response response = Response.status(
539                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
540             throw new WebApplicationException(response);
541         }
542         return personObjectList;
543     }
544
545     @GET
546     @Path("urn:cspace:name({specifier})/items")
547     @Produces("application/xml")
548     public PersonsCommonList getPersonListByAuthName(
549                 @PathParam("specifier") String parentSpecifier,
550             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
551             @Context UriInfo ui) {
552         PersonsCommonList personObjectList = new PersonsCommonList();
553         try {
554             String whereClause =
555                 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
556                 ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+
557                 "='"+parentSpecifier+"'";
558             // Need to get an Authority by name
559             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
560             String parentcsid = 
561                 getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
562
563             ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
564             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
565             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
566             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
567             myFilter.setPagination(queryParams);
568
569             // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
570             myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
571                         PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
572             
573             // AND persons_common:displayName LIKE '%partialTerm%'
574             if (partialTerm != null && !partialTerm.isEmpty()) {
575                 String ptClause = "AND " +
576                 PersonJAXBSchema.PERSONS_COMMON + ":" +
577                         PersonJAXBSchema.DISPLAY_NAME +
578                         " LIKE " +
579                         "'%" + partialTerm + "%'";
580                 myFilter.appendWhereClause(ptClause);
581             }
582             
583             handler.setDocumentFilter(myFilter);
584             getRepositoryClient(ctx).getFiltered(ctx, handler);
585             personObjectList = (PersonsCommonList) handler.getCommonPartList();
586         } catch (UnauthorizedException ue) {
587             Response response = Response.status(
588                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
589             throw new WebApplicationException(response);
590         } catch (Exception e) {
591             if (logger.isDebugEnabled()) {
592                 logger.debug("Caught exception in getPersonList", e);
593             }
594             Response response = Response.status(
595                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
596             throw new WebApplicationException(response);
597         }
598         return personObjectList;
599     }
600
601     @PUT
602     @Path("{csid}/items/{itemcsid}")
603     public MultipartOutput updatePerson(
604             @PathParam("csid") String parentcsid,
605             @PathParam("itemcsid") String itemcsid,
606             MultipartInput theUpdate) {
607         if (logger.isDebugEnabled()) {
608             logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
609         }
610         if (parentcsid == null || "".equals(parentcsid)) {
611             logger.error("updatePerson: missing csid!");
612             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
613                     "update failed on Person parentcsid=" + parentcsid).type(
614                     "text/plain").build();
615             throw new WebApplicationException(response);
616         }
617         if (itemcsid == null || "".equals(itemcsid)) {
618             logger.error("updatePerson: missing itemcsid!");
619             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
620                     "update failed on Person=" + itemcsid).type(
621                     "text/plain").build();
622             throw new WebApplicationException(response);
623         }
624         MultipartOutput result = null;
625         try {
626             // Note that we have to create the service context for the Items, not the main service
627             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
628             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
629             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
630             result = (MultipartOutput) ctx.getOutput();
631         } catch (BadRequestException bre) {
632             Response response = Response.status(
633                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
634             throw new WebApplicationException(response);
635         } catch (UnauthorizedException ue) {
636             Response response = Response.status(
637                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
638             throw new WebApplicationException(response);
639         } catch (DocumentNotFoundException dnfe) {
640             if (logger.isDebugEnabled()) {
641                 logger.debug("caught exception in updatePerson", dnfe);
642             }
643             Response response = Response.status(Response.Status.NOT_FOUND).entity(
644                     "Update failed on Person csid=" + itemcsid).type(
645                     "text/plain").build();
646             throw new WebApplicationException(response);
647         } catch (Exception e) {
648             Response response = Response.status(
649                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
650             throw new WebApplicationException(response);
651         }
652         return result;
653     }
654
655     @DELETE
656     @Path("{csid}/items/{itemcsid}")
657     public Response deletePerson(
658             @PathParam("csid") String parentcsid,
659             @PathParam("itemcsid") String itemcsid) {
660         if (logger.isDebugEnabled()) {
661             logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
662         }
663         if (parentcsid == null || "".equals(parentcsid)) {
664             logger.error("deletePerson: missing csid!");
665             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
666                     "delete failed on Person parentcsid=" + parentcsid).type(
667                     "text/plain").build();
668             throw new WebApplicationException(response);
669         }
670         if (itemcsid == null || "".equals(itemcsid)) {
671             logger.error("deletePerson: missing itemcsid!");
672             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
673                     "delete failed on Person=" + itemcsid).type(
674                     "text/plain").build();
675             throw new WebApplicationException(response);
676         }
677         try {
678             // Note that we have to create the service context for the Items, not the main service
679             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
680             getRepositoryClient(ctx).delete(ctx, itemcsid);
681             return Response.status(HttpResponseCodes.SC_OK).build();
682         } catch (UnauthorizedException ue) {
683             Response response = Response.status(
684                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
685             throw new WebApplicationException(response);
686         } catch (DocumentNotFoundException dnfe) {
687             if (logger.isDebugEnabled()) {
688                 logger.debug("caught exception in deletePerson", dnfe);
689             }
690             Response response = Response.status(Response.Status.NOT_FOUND).entity(
691                     "Delete failed on Person itemcsid=" + itemcsid).type(
692                     "text/plain").build();
693             throw new WebApplicationException(response);
694         } catch (Exception e) {
695             Response response = Response.status(
696                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
697             throw new WebApplicationException(response);
698         }
699
700     }
701
702     /*************************************************************************
703      * Contact parts - this is a sub-resource of Person (or "item")
704      *************************************************************************/
705     @POST
706     @Path("{parentcsid}/items/{itemcsid}/contacts")
707     public Response createContact(
708             @PathParam("parentcsid") String parentcsid,
709             @PathParam("itemcsid") String itemcsid,
710             MultipartInput input) {
711         try {
712             // Note that we have to create the service context and document
713             // handler for the Contact service, not the main service.
714             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getContactServiceName());
715             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
716             String csid = getRepositoryClient(ctx).create(ctx, handler);
717             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
718             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
719             Response response = Response.created(path.build()).build();
720             return response;
721         } catch (BadRequestException bre) {
722             Response response = Response.status(
723                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
724             throw new WebApplicationException(response);
725         } catch (UnauthorizedException ue) {
726             Response response = Response.status(
727                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
728             throw new WebApplicationException(response);
729         } catch (Exception e) {
730             if (logger.isDebugEnabled()) {
731                 logger.debug("Caught exception in createContact", e);
732             }
733             Response response = Response.status(
734                 Response.Status.INTERNAL_SERVER_ERROR)
735                 .entity("Attempt to create Contact failed.")
736                 .type("text/plain").build();
737             throw new WebApplicationException(response);
738         }
739         
740     }
741         
742     @GET
743     @Produces({"application/xml"})
744     @Path("{parentcsid}/items/{itemcsid}/contacts/")
745     public ContactsCommonList getContactList(
746             @PathParam("parentcsid") String parentcsid,
747             @PathParam("itemcsid") String itemcsid,
748             @Context UriInfo ui) {
749         ContactsCommonList contactObjectList = new ContactsCommonList();
750         try {
751             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
752             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
753             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
754             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
755             myFilter.setPagination(queryParams);
756             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
757                 ContactJAXBSchema.IN_AUTHORITY +
758                 "='" + parentcsid + "'" +
759                 " AND " +
760                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
761                 ContactJAXBSchema.IN_ITEM +
762                 "='" + itemcsid + "'" +
763                 " AND ecm:isProxy = 0");
764             handler.setDocumentFilter(myFilter);
765             getRepositoryClient(ctx).getFiltered(ctx, handler);
766             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
767         } catch (UnauthorizedException ue) {
768             Response response = Response.status(
769                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
770             throw new WebApplicationException(response);
771         } catch (Exception e) {
772             if (logger.isDebugEnabled()) {
773                 logger.debug("Caught exception in getContactsList", e);
774             }
775             Response response = Response.status(
776                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
777             throw new WebApplicationException(response);
778         }
779         return contactObjectList;
780     }
781
782     @GET
783     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
784     public MultipartOutput getContact(
785             @PathParam("parentcsid") String parentcsid,
786             @PathParam("itemcsid") String itemcsid,
787             @PathParam("csid") String csid) {
788         MultipartOutput result = null;
789        if (logger.isDebugEnabled()) {
790             logger.debug("getContact with parentCsid=" + parentcsid +
791             " itemcsid=" + itemcsid + " csid=" + csid);
792         }
793         try {
794             // Note that we have to create the service context and document
795             // handler for the Contact service, not the main service.
796             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
797             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
798             getRepositoryClient(ctx).get(ctx, csid, handler);
799             result = (MultipartOutput) ctx.getOutput();
800         } catch (UnauthorizedException ue) {
801             Response response = Response.status(
802                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
803             throw new WebApplicationException(response);
804         } catch (DocumentNotFoundException dnfe) {
805             if (logger.isDebugEnabled()) {
806                 logger.debug("getContact", dnfe);
807             }
808             Response response = Response.status(Response.Status.NOT_FOUND)
809                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
810                 .type("text/plain").build();
811             throw new WebApplicationException(response);
812         } catch (Exception e) {
813             if (logger.isDebugEnabled()) {
814                 logger.debug("getContact", e);
815             }
816             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
817                 .entity("Get contact failed")
818                 .type("text/plain").build();
819             throw new WebApplicationException(response);
820         }
821         if (result == null) {
822             Response response = Response.status(Response.Status.NOT_FOUND)
823                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
824                 .type("text/plain").build();
825             throw new WebApplicationException(response);
826         }
827         return result;
828
829     }
830
831     @PUT
832     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
833     public MultipartOutput updateContact(
834             @PathParam("parentcsid") String parentcsid,
835             @PathParam("itemcsid") String itemcsid,
836             @PathParam("csid") String csid,
837             MultipartInput theUpdate) {
838        if (logger.isDebugEnabled()) {
839             logger.debug("updateContact with parentcsid=" + parentcsid +
840             " itemcsid=" + itemcsid + " csid=" + csid);
841         }
842        if (parentcsid == null || parentcsid.trim().isEmpty()) {
843             logger.error("updateContact: missing csid!");
844             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
845                     "update failed on Contact parentcsid=" + parentcsid).type(
846                     "text/plain").build();
847             throw new WebApplicationException(response);
848         }
849         if (itemcsid == null || itemcsid.trim().isEmpty()) {
850             logger.error("updateContact: missing itemcsid!");
851             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
852                     "update failed on Contact=" + itemcsid).type(
853                     "text/plain").build();
854             throw new WebApplicationException(response);
855         }
856         if (csid == null || csid.trim().isEmpty()) {
857             logger.error("updateContact: missing csid!");
858             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
859                     "update failed on Contact=" + csid).type(
860                     "text/plain").build();
861             throw new WebApplicationException(response);
862         }
863         MultipartOutput result = null;
864         try {
865             // Note that we have to create the service context and document
866             // handler for the Contact service, not the main service.
867             ServiceContext ctx = MultipartServiceContextFactory.get()
868                 .createServiceContext(theUpdate, getContactServiceName());
869             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
870             getRepositoryClient(ctx).update(ctx, csid, handler);
871             result = (MultipartOutput) ctx.getOutput();
872         } catch (BadRequestException bre) {
873             Response response = Response.status(
874                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
875             throw new WebApplicationException(response);
876         } catch (UnauthorizedException ue) {
877             Response response = Response.status(
878                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
879             throw new WebApplicationException(response);
880         } catch (DocumentNotFoundException dnfe) {
881             if (logger.isDebugEnabled()) {
882                 logger.debug("caught exception in updateContact", dnfe);
883             }
884             Response response = Response.status(Response.Status.NOT_FOUND).entity(
885                     "Update failed on Contact csid=" + itemcsid).type(
886                     "text/plain").build();
887             throw new WebApplicationException(response);
888         } catch (Exception e) {
889             Response response = Response.status(
890                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
891             throw new WebApplicationException(response);
892         }
893         return result;
894     }
895
896     @DELETE
897     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
898     public Response deleteContact(
899             @PathParam("parentcsid") String parentcsid,
900             @PathParam("itemcsid") String itemcsid,
901             @PathParam("csid") String csid) {
902         if (logger.isDebugEnabled()) {
903             logger.debug("deleteContact with parentCsid=" + parentcsid +
904             " itemcsid=" + itemcsid + " csid=" + csid);
905         }
906         if (parentcsid == null || parentcsid.trim().isEmpty()) {
907             logger.error("deleteContact: missing parentcsid!");
908             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
909                     "delete contact failed on parentcsid=" + parentcsid).type(
910                     "text/plain").build();
911             throw new WebApplicationException(response);
912         }
913         if (itemcsid == null || itemcsid.trim().isEmpty()) {
914             logger.error("deleteContact: missing itemcsid!");
915             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
916                     "delete contact failed on itemcsid=" + itemcsid).type(
917                     "text/plain").build();
918             throw new WebApplicationException(response);
919         }
920         if (csid == null || csid.trim().isEmpty()) {
921             logger.error("deleteContact: missing csid!");
922             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
923                     "delete contact failed on csid=" + csid).type(
924                     "text/plain").build();
925             throw new WebApplicationException(response);
926         }
927         try {
928             // Note that we have to create the service context for the
929             // Contact service, not the main service.
930             ServiceContext ctx =
931                 MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
932             getRepositoryClient(ctx).delete(ctx, csid);
933             return Response.status(HttpResponseCodes.SC_OK).build();   
934          } catch (UnauthorizedException ue) {
935             Response response = Response.status(
936                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
937             throw new WebApplicationException(response);
938          } catch (DocumentNotFoundException dnfe) {
939             if (logger.isDebugEnabled()) {
940                 logger.debug("Caught exception in deleteContact", dnfe);
941             }
942             Response response = Response.status(Response.Status.NOT_FOUND)
943                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
944                 .type("text/plain").build();
945             throw new WebApplicationException(response);
946        } catch (Exception e) {
947             Response response = Response.status(
948                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
949             throw new WebApplicationException(response);
950         }
951     }
952
953 }