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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.person;
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;
44 import org.collectionspace.services.PersonAuthorityJAXBSchema;
45 import org.collectionspace.services.PersonJAXBSchema;
46 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.ClientType;
48 import org.collectionspace.services.common.ServiceMain;
49 //import org.collectionspace.services.common.context.MultipartServiceContext;
50 //import org.collectionspace.services.common.context.MultipartServiceContextFactory;
51 import org.collectionspace.services.common.context.ServiceContext;
52 import org.collectionspace.services.common.document.BadRequestException;
53 import org.collectionspace.services.common.document.DocumentFilter;
54 import org.collectionspace.services.common.document.DocumentHandler;
55 import org.collectionspace.services.common.document.DocumentNotFoundException;
56 //import org.collectionspace.services.common.document.DocumentWrapper;
57 import org.collectionspace.services.common.security.UnauthorizedException;
58 //import org.collectionspace.services.common.vocabulary.RefNameUtils;
59 import org.collectionspace.services.common.query.IQueryManager;
60 import org.collectionspace.services.contact.ContactResource;
61 import org.collectionspace.services.contact.ContactsCommon;
62 import org.collectionspace.services.contact.ContactsCommonList;
63 import org.collectionspace.services.contact.ContactJAXBSchema;
64 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
65 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
66 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
67 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
68 import org.jboss.resteasy.util.HttpResponseCodes;
69 //import org.nuxeo.ecm.core.api.DocumentModel;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 * The Class PersonAuthorityResource.
76 @Path("/personauthorities")
77 @Consumes("multipart/mixed")
78 @Produces("multipart/mixed")
79 public class PersonAuthorityResource extends
80 AbstractMultiPartCollectionSpaceResourceImpl {
82 /** The Constant personAuthorityServiceName. */
83 private final static String personAuthorityServiceName = "personauthorities";
85 /** The Constant personServiceName. */
86 private final static String personServiceName = "persons";
89 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
90 //FIXME retrieve client type from configuration
91 /** The Constant CLIENT_TYPE. */
92 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
94 /** The contact resource. */
95 private ContactResource contactResource = new ContactResource();
98 * Instantiates a new person authority resource.
100 public PersonAuthorityResource() {
105 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
108 protected String getVersionString() {
109 /** The last change revision. */
110 final String lastChangeRevision = "$LastChangedRevision$";
111 return lastChangeRevision;
115 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
118 public String getServiceName() {
119 return personAuthorityServiceName;
123 public Class<PersonauthoritiesCommon> getCommonPartClass() {
124 return PersonauthoritiesCommon.class;
128 * Gets the item service name.
130 * @return the item service name
132 public String getItemServiceName() {
133 return personServiceName;
137 * Gets the contact service name.
139 * @return the contact service name
141 public String getContactServiceName() {
142 return contactResource.getServiceName();
146 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
147 // DocumentHandler docHandler = ctx.getDocumentHandler();
148 // if (ctx.getInput() != null) {
149 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
150 // if (obj != null) {
151 // docHandler.setCommonPart((PersonauthoritiesCommon) obj);
154 // return docHandler;
158 * Creates the item document handler.
161 * @param inAuthority the in authority
163 * @return the document handler
165 * @throws Exception the exception
167 private DocumentHandler createItemDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx,
168 String inAuthority) throws Exception {
169 PersonDocumentModelHandler docHandler = (PersonDocumentModelHandler)createDocumentHandler(ctx,
170 ctx.getCommonPartLabel(getItemServiceName()),
171 PersonsCommon.class);
172 docHandler.setInAuthority(inAuthority);
178 * Creates the contact document handler.
181 * @param inAuthority the in authority
182 * @param inItem the in item
184 * @return the document handler
186 * @throws Exception the exception
188 private DocumentHandler createContactDocumentHandler(
189 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
190 String inItem) throws Exception {
191 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(ctx,
192 ctx.getCommonPartLabel(getContactServiceName()),
193 ContactsCommon.class);
194 docHandler.setInAuthority(inAuthority);
195 docHandler.setInItem(inItem);
197 // DocumentHandler docHandler = ctx.getDocumentHandler();
198 // // Set the inAuthority and inItem values, which specify the
199 // // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
200 // // (e.g. Person, Organization) with which the Contact is associated.
201 // ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
202 // ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
203 // if (ctx.getInput() != null) {
204 // Object obj = ((MultipartServiceContext) ctx)
205 // .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
206 // ContactsCommon.class);
207 // if (obj != null) {
208 // docHandler.setCommonPart((ContactsCommon) obj);
215 * Creates the person authority.
217 * @param input the input
219 * @return the response
222 public Response createPersonAuthority(MultipartInput input) {
224 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
225 DocumentHandler handler = createDocumentHandler(ctx);
226 String csid = getRepositoryClient(ctx).create(ctx, handler);
227 //personAuthorityObject.setCsid(csid);
228 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
229 path.path("" + csid);
230 Response response = Response.created(path.build()).build();
232 } catch (BadRequestException bre) {
233 Response response = Response.status(
234 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
235 throw new WebApplicationException(response);
236 } catch (UnauthorizedException ue) {
237 Response response = Response.status(
238 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
239 throw new WebApplicationException(response);
240 } catch (Exception e) {
241 if (logger.isDebugEnabled()) {
242 logger.debug("Caught exception in createPersonAuthority", e);
244 Response response = Response.status(
245 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
246 throw new WebApplicationException(response);
251 * Gets the person authority by name.
253 * @param specifier the specifier
255 * @return the person authority by name
258 @Path("urn:cspace:name({specifier})")
259 public MultipartOutput getPersonAuthorityByName(@PathParam("specifier") String specifier) {
260 String idValue = null;
261 if (specifier == null) {
262 logger.error("getPersonAuthority: missing name!");
263 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
264 "get failed on PersonAuthority (missing specifier)").type(
265 "text/plain").build();
266 throw new WebApplicationException(response);
269 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
270 ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+
272 // We only get a single doc - if there are multiple,
273 // it is an error in use.
275 if (logger.isDebugEnabled()) {
276 logger.debug("getPersonAuthority with name=" + specifier);
278 MultipartOutput result = null;
280 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
281 DocumentHandler handler = createDocumentHandler(ctx);
282 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
283 handler.setDocumentFilter(myFilter);
284 getRepositoryClient(ctx).get(ctx, handler);
285 result = (MultipartOutput) ctx.getOutput();
286 } catch (UnauthorizedException ue) {
287 Response response = Response.status(
288 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
289 throw new WebApplicationException(response);
290 } catch (DocumentNotFoundException dnfe) {
291 if (logger.isDebugEnabled()) {
292 logger.debug("getPersonAuthority", dnfe);
294 Response response = Response.status(Response.Status.NOT_FOUND).entity(
295 "Get failed on PersonAuthority spec=" + specifier).type(
296 "text/plain").build();
297 throw new WebApplicationException(response);
298 } catch (Exception e) {
299 if (logger.isDebugEnabled()) {
300 logger.debug("getPersonAuthority", e);
302 Response response = Response.status(
303 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
304 throw new WebApplicationException(response);
306 if (result == null) {
307 Response response = Response.status(Response.Status.NOT_FOUND).entity(
308 "Get failed, the requested PersonAuthority spec:" + specifier + ": was not found.").type(
309 "text/plain").build();
310 throw new WebApplicationException(response);
316 * Gets the person authority.
318 * @param csid the csid
320 * @return the person authority
324 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
326 logger.error("getPersonAuthority: missing csid!");
327 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
328 "get failed on PersonAuthority csid=" + csid).type(
329 "text/plain").build();
330 throw new WebApplicationException(response);
332 if (logger.isDebugEnabled()) {
333 logger.debug("getPersonAuthority with path(id)=" + csid);
335 MultipartOutput result = null;
337 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
338 DocumentHandler handler = createDocumentHandler(ctx);
339 getRepositoryClient(ctx).get(ctx, csid, handler);
340 result = (MultipartOutput) ctx.getOutput();
341 } catch (UnauthorizedException ue) {
342 Response response = Response.status(
343 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
344 throw new WebApplicationException(response);
345 } catch (DocumentNotFoundException dnfe) {
346 if (logger.isDebugEnabled()) {
347 logger.debug("getPersonAuthority", dnfe);
349 Response response = Response.status(Response.Status.NOT_FOUND).entity(
350 "Get failed on PersonAuthority csid=" + csid).type(
351 "text/plain").build();
352 throw new WebApplicationException(response);
353 } catch (Exception e) {
354 if (logger.isDebugEnabled()) {
355 logger.debug("getPersonAuthority", e);
357 Response response = Response.status(
358 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
359 throw new WebApplicationException(response);
361 if (result == null) {
362 Response response = Response.status(Response.Status.NOT_FOUND).entity(
363 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
364 "text/plain").build();
365 throw new WebApplicationException(response);
371 * Gets the person authority list.
375 * @return the person authority list
378 @Produces("application/xml")
379 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
380 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
382 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
383 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
384 DocumentHandler handler = createDocumentHandler(ctx);
385 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
386 myFilter.setPagination(queryParams); //FIXME
387 String nameQ = queryParams.getFirst("refName");
389 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
391 handler.setDocumentFilter(myFilter);
392 getRepositoryClient(ctx).getFiltered(ctx, handler);
393 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
394 } catch (UnauthorizedException ue) {
395 Response response = Response.status(
396 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
397 throw new WebApplicationException(response);
398 } catch (Exception e) {
399 if (logger.isDebugEnabled()) {
400 logger.debug("Caught exception in getPersonAuthorityList", e);
402 Response response = Response.status(
403 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
404 throw new WebApplicationException(response);
406 return personAuthorityObjectList;
410 * Update person authority.
412 * @param csid the csid
413 * @param theUpdate the the update
415 * @return the multipart output
419 public MultipartOutput updatePersonAuthority(
420 @PathParam("csid") String csid,
421 MultipartInput theUpdate) {
422 if (logger.isDebugEnabled()) {
423 logger.debug("updatePersonAuthority with csid=" + csid);
425 if (csid == null || "".equals(csid)) {
426 logger.error("updatePersonAuthority: missing csid!");
427 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
428 "update failed on PersonAuthority csid=" + csid).type(
429 "text/plain").build();
430 throw new WebApplicationException(response);
432 MultipartOutput result = null;
434 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
435 DocumentHandler handler = createDocumentHandler(ctx);
436 getRepositoryClient(ctx).update(ctx, csid, handler);
437 result = (MultipartOutput) ctx.getOutput();
438 } catch (BadRequestException bre) {
439 Response response = Response.status(
440 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
441 throw new WebApplicationException(response);
442 } catch (UnauthorizedException ue) {
443 Response response = Response.status(
444 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
445 throw new WebApplicationException(response);
446 } catch (DocumentNotFoundException dnfe) {
447 if (logger.isDebugEnabled()) {
448 logger.debug("caugth exception in updatePersonAuthority", dnfe);
450 Response response = Response.status(Response.Status.NOT_FOUND).entity(
451 "Update failed on PersonAuthority csid=" + csid).type(
452 "text/plain").build();
453 throw new WebApplicationException(response);
454 } catch (Exception e) {
455 Response response = Response.status(
456 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
457 throw new WebApplicationException(response);
463 * Delete person authority.
465 * @param csid the csid
467 * @return the response
471 public Response deletePersonAuthority(@PathParam("csid") String csid) {
473 if (logger.isDebugEnabled()) {
474 logger.debug("deletePersonAuthority with csid=" + csid);
476 if (csid == null || "".equals(csid)) {
477 logger.error("deletePersonAuthority: missing csid!");
478 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
479 "delete failed on PersonAuthority csid=" + csid).type(
480 "text/plain").build();
481 throw new WebApplicationException(response);
484 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
485 getRepositoryClient(ctx).delete(ctx, csid);
486 return Response.status(HttpResponseCodes.SC_OK).build();
487 } catch (UnauthorizedException ue) {
488 Response response = Response.status(
489 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
490 throw new WebApplicationException(response);
491 } catch (DocumentNotFoundException dnfe) {
492 if (logger.isDebugEnabled()) {
493 logger.debug("caught exception in deletePersonAuthority", dnfe);
495 Response response = Response.status(Response.Status.NOT_FOUND).entity(
496 "Delete failed on PersonAuthority csid=" + csid).type(
497 "text/plain").build();
498 throw new WebApplicationException(response);
499 } catch (Exception e) {
500 Response response = Response.status(
501 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
502 throw new WebApplicationException(response);
507 /*************************************************************************
508 * Person parts - this is a sub-resource of PersonAuthority
509 *************************************************************************/
511 @Path("{csid}/items")
512 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
514 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(), input);
515 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
516 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
517 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
518 path.path(parentcsid + "/items/" + itemcsid);
519 Response response = Response.created(path.build()).build();
521 } catch (BadRequestException bre) {
522 Response response = Response.status(
523 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
524 throw new WebApplicationException(response);
525 } catch (UnauthorizedException ue) {
526 Response response = Response.status(
527 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
528 throw new WebApplicationException(response);
529 } catch (Exception e) {
530 if (logger.isDebugEnabled()) {
531 logger.debug("Caught exception in createPerson", e);
533 Response response = Response.status(
534 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
535 throw new WebApplicationException(response);
542 * @param parentcsid the parentcsid
543 * @param itemcsid the itemcsid
548 @Path("{csid}/items/{itemcsid}")
549 public MultipartOutput getPerson(
550 @PathParam("csid") String parentcsid,
551 @PathParam("itemcsid") String itemcsid) {
552 if (logger.isDebugEnabled()) {
553 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
555 if (parentcsid == null || "".equals(parentcsid)) {
556 logger.error("getPerson: missing csid!");
557 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
558 "get failed on Person csid=" + parentcsid).type(
559 "text/plain").build();
560 throw new WebApplicationException(response);
562 if (itemcsid == null || "".equals(itemcsid)) {
563 logger.error("getPerson: missing itemcsid!");
564 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
565 "get failed on Person itemcsid=" + itemcsid).type(
566 "text/plain").build();
567 throw new WebApplicationException(response);
569 MultipartOutput result = null;
571 // Note that we have to create the service context for the Items, not the main service
572 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
573 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
574 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
575 // TODO should we assert that the item is in the passed personAuthority?
576 result = (MultipartOutput) ctx.getOutput();
577 } catch (UnauthorizedException ue) {
578 Response response = Response.status(
579 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
580 throw new WebApplicationException(response);
581 } catch (DocumentNotFoundException dnfe) {
582 if (logger.isDebugEnabled()) {
583 logger.debug("getPerson", dnfe);
585 Response response = Response.status(Response.Status.NOT_FOUND).entity(
586 "Get failed on Person csid=" + itemcsid).type(
587 "text/plain").build();
588 throw new WebApplicationException(response);
589 } catch (Exception e) {
590 if (logger.isDebugEnabled()) {
591 logger.debug("getPerson", e);
593 Response response = Response.status(
594 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
595 throw new WebApplicationException(response);
597 if (result == null) {
598 Response response = Response.status(Response.Status.NOT_FOUND).entity(
599 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
600 "text/plain").build();
601 throw new WebApplicationException(response);
607 * Gets the person list.
609 * @param parentcsid the parentcsid
610 * @param partialTerm the partial term
613 * @return the person list
616 @Path("{csid}/items")
617 @Produces("application/xml")
618 public PersonsCommonList getPersonList(
619 @PathParam("csid") String parentcsid,
620 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
621 @Context UriInfo ui) {
622 PersonsCommonList personObjectList = new PersonsCommonList();
624 // Note that docType defaults to the ServiceName, so we're fine with that.
625 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
626 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
628 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
629 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
630 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
632 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
633 myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
634 PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
636 // AND persons_common:displayName LIKE '%partialTerm%'
637 if (partialTerm != null && !partialTerm.isEmpty()) {
638 String ptClause = "AND " +
639 PersonJAXBSchema.PERSONS_COMMON + ":" +
640 PersonJAXBSchema.DISPLAY_NAME +
642 "'%" + partialTerm + "%'";
643 myFilter.appendWhereClause(ptClause);
646 handler.setDocumentFilter(myFilter);
647 getRepositoryClient(ctx).getFiltered(ctx, handler);
648 personObjectList = (PersonsCommonList) handler.getCommonPartList();
649 } catch (UnauthorizedException ue) {
650 Response response = Response.status(
651 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
652 throw new WebApplicationException(response);
653 } catch (Exception e) {
654 if (logger.isDebugEnabled()) {
655 logger.debug("Caught exception in getPersonList", e);
657 Response response = Response.status(
658 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
659 throw new WebApplicationException(response);
661 return personObjectList;
665 * Gets the person list by auth name.
667 * @param parentSpecifier the parent specifier
668 * @param partialTerm the partial term
671 * @return the person list by auth name
674 @Path("urn:cspace:name({specifier})/items")
675 @Produces("application/xml")
676 public PersonsCommonList getPersonListByAuthName(
677 @PathParam("specifier") String parentSpecifier,
678 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
679 @Context UriInfo ui) {
680 PersonsCommonList personObjectList = new PersonsCommonList();
682 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
684 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
685 ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+
686 "='"+parentSpecifier+"'";
687 // Need to get an Authority by name
688 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
690 getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
692 ctx = createServiceContext(getItemServiceName(), queryParams);
693 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
694 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
695 myFilter.setPagination(queryParams); //FIXME
697 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
698 myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
699 PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
701 // AND persons_common:displayName LIKE '%partialTerm%'
702 if (partialTerm != null && !partialTerm.isEmpty()) {
703 String ptClause = "AND " +
704 PersonJAXBSchema.PERSONS_COMMON + ":" +
705 PersonJAXBSchema.DISPLAY_NAME +
707 "'%" + partialTerm + "%'";
708 myFilter.appendWhereClause(ptClause);
711 handler.setDocumentFilter(myFilter);
712 getRepositoryClient(ctx).getFiltered(ctx, handler);
713 personObjectList = (PersonsCommonList) handler.getCommonPartList();
714 } catch (UnauthorizedException ue) {
715 Response response = Response.status(
716 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
717 throw new WebApplicationException(response);
718 } catch (Exception e) {
719 if (logger.isDebugEnabled()) {
720 logger.debug("Caught exception in getPersonList", e);
722 Response response = Response.status(
723 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
724 throw new WebApplicationException(response);
726 return personObjectList;
732 * @param parentcsid the parentcsid
733 * @param itemcsid the itemcsid
734 * @param theUpdate the the update
736 * @return the multipart output
739 @Path("{csid}/items/{itemcsid}")
740 public MultipartOutput updatePerson(
741 @PathParam("csid") String parentcsid,
742 @PathParam("itemcsid") String itemcsid,
743 MultipartInput theUpdate) {
744 if (logger.isDebugEnabled()) {
745 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
747 if (parentcsid == null || "".equals(parentcsid)) {
748 logger.error("updatePerson: missing csid!");
749 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
750 "update failed on Person parentcsid=" + parentcsid).type(
751 "text/plain").build();
752 throw new WebApplicationException(response);
754 if (itemcsid == null || "".equals(itemcsid)) {
755 logger.error("updatePerson: missing itemcsid!");
756 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
757 "update failed on Person=" + itemcsid).type(
758 "text/plain").build();
759 throw new WebApplicationException(response);
761 MultipartOutput result = null;
763 // Note that we have to create the service context for the Items, not the main service
764 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
766 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
767 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
768 result = (MultipartOutput) ctx.getOutput();
769 } catch (BadRequestException bre) {
770 Response response = Response.status(
771 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
772 throw new WebApplicationException(response);
773 } catch (UnauthorizedException ue) {
774 Response response = Response.status(
775 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
776 throw new WebApplicationException(response);
777 } catch (DocumentNotFoundException dnfe) {
778 if (logger.isDebugEnabled()) {
779 logger.debug("caught exception in updatePerson", dnfe);
781 Response response = Response.status(Response.Status.NOT_FOUND).entity(
782 "Update failed on Person csid=" + itemcsid).type(
783 "text/plain").build();
784 throw new WebApplicationException(response);
785 } catch (Exception e) {
786 Response response = Response.status(
787 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
788 throw new WebApplicationException(response);
796 * @param parentcsid the parentcsid
797 * @param itemcsid the itemcsid
799 * @return the response
802 @Path("{csid}/items/{itemcsid}")
803 public Response deletePerson(
804 @PathParam("csid") String parentcsid,
805 @PathParam("itemcsid") String itemcsid) {
806 if (logger.isDebugEnabled()) {
807 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
809 if (parentcsid == null || "".equals(parentcsid)) {
810 logger.error("deletePerson: missing csid!");
811 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
812 "delete failed on Person parentcsid=" + parentcsid).type(
813 "text/plain").build();
814 throw new WebApplicationException(response);
816 if (itemcsid == null || "".equals(itemcsid)) {
817 logger.error("deletePerson: missing itemcsid!");
818 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
819 "delete failed on Person=" + itemcsid).type(
820 "text/plain").build();
821 throw new WebApplicationException(response);
824 // Note that we have to create the service context for the Items, not the main service
825 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
826 getRepositoryClient(ctx).delete(ctx, itemcsid);
827 return Response.status(HttpResponseCodes.SC_OK).build();
828 } catch (UnauthorizedException ue) {
829 Response response = Response.status(
830 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
831 throw new WebApplicationException(response);
832 } catch (DocumentNotFoundException dnfe) {
833 if (logger.isDebugEnabled()) {
834 logger.debug("caught exception in deletePerson", dnfe);
836 Response response = Response.status(Response.Status.NOT_FOUND).entity(
837 "Delete failed on Person itemcsid=" + itemcsid).type(
838 "text/plain").build();
839 throw new WebApplicationException(response);
840 } catch (Exception e) {
841 Response response = Response.status(
842 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
843 throw new WebApplicationException(response);
848 /*************************************************************************
849 * Contact parts - this is a sub-resource of Person (or "item")
850 *************************************************************************/
852 @Path("{parentcsid}/items/{itemcsid}/contacts")
853 public Response createContact(
854 @PathParam("parentcsid") String parentcsid,
855 @PathParam("itemcsid") String itemcsid,
856 MultipartInput input) {
858 // Note that we have to create the service context and document
859 // handler for the Contact service, not the main service.
860 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
861 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
862 String csid = getRepositoryClient(ctx).create(ctx, handler);
863 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
864 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
865 Response response = Response.created(path.build()).build();
867 } catch (BadRequestException bre) {
868 Response response = Response.status(
869 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
870 throw new WebApplicationException(response);
871 } catch (UnauthorizedException ue) {
872 Response response = Response.status(
873 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
874 throw new WebApplicationException(response);
875 } catch (Exception e) {
876 if (logger.isDebugEnabled()) {
877 logger.debug("Caught exception in createContact", e);
879 Response response = Response.status(
880 Response.Status.INTERNAL_SERVER_ERROR)
881 .entity("Attempt to create Contact failed.")
882 .type("text/plain").build();
883 throw new WebApplicationException(response);
889 * Gets the contact list.
891 * @param parentcsid the parentcsid
892 * @param itemcsid the itemcsid
895 * @return the contact list
898 @Produces({"application/xml"})
899 @Path("{parentcsid}/items/{itemcsid}/contacts/")
900 public ContactsCommonList getContactList(
901 @PathParam("parentcsid") String parentcsid,
902 @PathParam("itemcsid") String itemcsid,
903 @Context UriInfo ui) {
904 ContactsCommonList contactObjectList = new ContactsCommonList();
906 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
907 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
909 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
910 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
911 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
912 myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
913 ContactJAXBSchema.IN_AUTHORITY +
914 "='" + parentcsid + "'" +
916 ContactJAXBSchema.CONTACTS_COMMON + ":" +
917 ContactJAXBSchema.IN_ITEM +
918 "='" + itemcsid + "'" +
919 " AND ecm:isProxy = 0");
920 handler.setDocumentFilter(myFilter);
921 getRepositoryClient(ctx).getFiltered(ctx, handler);
922 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
923 } catch (UnauthorizedException ue) {
924 Response response = Response.status(
925 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
926 throw new WebApplicationException(response);
927 } catch (Exception e) {
928 if (logger.isDebugEnabled()) {
929 logger.debug("Caught exception in getContactsList", e);
931 Response response = Response.status(
932 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
933 throw new WebApplicationException(response);
935 return contactObjectList;
941 * @param parentcsid the parentcsid
942 * @param itemcsid the itemcsid
943 * @param csid the csid
945 * @return the contact
948 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
949 public MultipartOutput getContact(
950 @PathParam("parentcsid") String parentcsid,
951 @PathParam("itemcsid") String itemcsid,
952 @PathParam("csid") String csid) {
953 MultipartOutput result = null;
954 if (logger.isDebugEnabled()) {
955 logger.debug("getContact with parentCsid=" + parentcsid +
956 " itemcsid=" + itemcsid + " csid=" + csid);
959 // Note that we have to create the service context and document
960 // handler for the Contact service, not the main service.
961 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
962 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
963 getRepositoryClient(ctx).get(ctx, csid, handler);
964 result = (MultipartOutput) ctx.getOutput();
965 } catch (UnauthorizedException ue) {
966 Response response = Response.status(
967 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
968 throw new WebApplicationException(response);
969 } catch (DocumentNotFoundException dnfe) {
970 if (logger.isDebugEnabled()) {
971 logger.debug("getContact", dnfe);
973 Response response = Response.status(Response.Status.NOT_FOUND)
974 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
975 .type("text/plain").build();
976 throw new WebApplicationException(response);
977 } catch (Exception e) {
978 if (logger.isDebugEnabled()) {
979 logger.debug("getContact", e);
981 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
982 .entity("Get contact failed")
983 .type("text/plain").build();
984 throw new WebApplicationException(response);
986 if (result == null) {
987 Response response = Response.status(Response.Status.NOT_FOUND)
988 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
989 .type("text/plain").build();
990 throw new WebApplicationException(response);
999 * @param parentcsid the parentcsid
1000 * @param itemcsid the itemcsid
1001 * @param csid the csid
1002 * @param theUpdate the the update
1004 * @return the multipart output
1007 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1008 public MultipartOutput updateContact(
1009 @PathParam("parentcsid") String parentcsid,
1010 @PathParam("itemcsid") String itemcsid,
1011 @PathParam("csid") String csid,
1012 MultipartInput theUpdate) {
1013 if (logger.isDebugEnabled()) {
1014 logger.debug("updateContact with parentcsid=" + parentcsid +
1015 " itemcsid=" + itemcsid + " csid=" + csid);
1017 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1018 logger.error("updateContact: missing csid!");
1019 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1020 "update failed on Contact parentcsid=" + parentcsid).type(
1021 "text/plain").build();
1022 throw new WebApplicationException(response);
1024 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1025 logger.error("updateContact: missing itemcsid!");
1026 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1027 "update failed on Contact=" + itemcsid).type(
1028 "text/plain").build();
1029 throw new WebApplicationException(response);
1031 if (csid == null || csid.trim().isEmpty()) {
1032 logger.error("updateContact: missing csid!");
1033 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1034 "update failed on Contact=" + csid).type(
1035 "text/plain").build();
1036 throw new WebApplicationException(response);
1038 MultipartOutput result = null;
1040 // Note that we have to create the service context and document
1041 // handler for the Contact service, not the main service.
1042 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
1044 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1045 getRepositoryClient(ctx).update(ctx, csid, handler);
1046 result = (MultipartOutput) ctx.getOutput();
1047 } catch (BadRequestException bre) {
1048 Response response = Response.status(
1049 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1050 throw new WebApplicationException(response);
1051 } catch (UnauthorizedException ue) {
1052 Response response = Response.status(
1053 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1054 throw new WebApplicationException(response);
1055 } catch (DocumentNotFoundException dnfe) {
1056 if (logger.isDebugEnabled()) {
1057 logger.debug("caught exception in updateContact", dnfe);
1059 Response response = Response.status(Response.Status.NOT_FOUND).entity(
1060 "Update failed on Contact csid=" + itemcsid).type(
1061 "text/plain").build();
1062 throw new WebApplicationException(response);
1063 } catch (Exception e) {
1064 Response response = Response.status(
1065 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1066 throw new WebApplicationException(response);
1074 * @param parentcsid the parentcsid
1075 * @param itemcsid the itemcsid
1076 * @param csid the csid
1078 * @return the response
1081 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1082 public Response deleteContact(
1083 @PathParam("parentcsid") String parentcsid,
1084 @PathParam("itemcsid") String itemcsid,
1085 @PathParam("csid") String csid) {
1086 if (logger.isDebugEnabled()) {
1087 logger.debug("deleteContact with parentCsid=" + parentcsid +
1088 " itemcsid=" + itemcsid + " csid=" + csid);
1090 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1091 logger.error("deleteContact: missing parentcsid!");
1092 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1093 "delete contact failed on parentcsid=" + parentcsid).type(
1094 "text/plain").build();
1095 throw new WebApplicationException(response);
1097 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1098 logger.error("deleteContact: missing itemcsid!");
1099 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1100 "delete contact failed on itemcsid=" + itemcsid).type(
1101 "text/plain").build();
1102 throw new WebApplicationException(response);
1104 if (csid == null || csid.trim().isEmpty()) {
1105 logger.error("deleteContact: missing csid!");
1106 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1107 "delete contact failed on csid=" + csid).type(
1108 "text/plain").build();
1109 throw new WebApplicationException(response);
1112 // Note that we have to create the service context for the
1113 // Contact service, not the main service.
1114 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1115 getRepositoryClient(ctx).delete(ctx, csid);
1116 return Response.status(HttpResponseCodes.SC_OK).build();
1117 } catch (UnauthorizedException ue) {
1118 Response response = Response.status(
1119 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1120 throw new WebApplicationException(response);
1121 } catch (DocumentNotFoundException dnfe) {
1122 if (logger.isDebugEnabled()) {
1123 logger.debug("Caught exception in deleteContact", dnfe);
1125 Response response = Response.status(Response.Status.NOT_FOUND)
1126 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1127 .type("text/plain").build();
1128 throw new WebApplicationException(response);
1129 } catch (Exception e) {
1130 Response response = Response.status(
1131 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1132 throw new WebApplicationException(response);