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.util.List;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.QueryParam;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
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.authorityref.AuthorityRefDocList;
50 import org.collectionspace.services.common.context.ServiceBindingUtils;
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.repository.RepositoryClient;
58 import org.collectionspace.services.common.security.UnauthorizedException;
59 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
60 import org.collectionspace.services.common.query.IQueryManager;
61 import org.collectionspace.services.contact.ContactResource;
62 import org.collectionspace.services.contact.ContactsCommon;
63 import org.collectionspace.services.contact.ContactsCommonList;
64 import org.collectionspace.services.contact.ContactJAXBSchema;
65 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
66 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
68 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
69 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
70 import org.jboss.resteasy.util.HttpResponseCodes;
72 import org.nuxeo.ecm.core.api.DocumentModel;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
78 * The Class PersonAuthorityResource.
80 @Path("/personauthorities")
81 @Consumes("multipart/mixed")
82 @Produces("multipart/mixed")
83 public class PersonAuthorityResource extends
84 AbstractMultiPartCollectionSpaceResourceImpl {
86 /** The Constant personAuthorityServiceName. */
87 private final static String personAuthorityServiceName = "personauthorities";
89 /** The Constant personServiceName. */
90 private final static String personServiceName = "persons";
93 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
94 //FIXME retrieve client type from configuration
95 /** The Constant CLIENT_TYPE. */
96 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
98 /** The contact resource. */
99 private ContactResource contactResource = new ContactResource();
102 * Instantiates a new person authority resource.
104 public PersonAuthorityResource() {
109 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
112 protected String getVersionString() {
113 /** The last change revision. */
114 final String lastChangeRevision = "$LastChangedRevision$";
115 return lastChangeRevision;
119 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
122 public String getServiceName() {
123 return personAuthorityServiceName;
127 public Class<PersonauthoritiesCommon> getCommonPartClass() {
128 return PersonauthoritiesCommon.class;
132 * Gets the item service name.
134 * @return the item service name
136 public String getItemServiceName() {
137 return personServiceName;
141 * Gets the contact service name.
143 * @return the contact service name
145 public String getContactServiceName() {
146 return contactResource.getServiceName();
150 * Creates the item document handler.
153 * @param inAuthority the in authority
155 * @return the document handler
157 * @throws Exception the exception
159 private DocumentHandler createItemDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx,
160 String inAuthority) throws Exception {
161 PersonDocumentModelHandler docHandler = (PersonDocumentModelHandler)createDocumentHandler(ctx,
162 ctx.getCommonPartLabel(getItemServiceName()),
163 PersonsCommon.class);
164 docHandler.setInAuthority(inAuthority);
170 * Creates the contact document handler.
173 * @param inAuthority the in authority
174 * @param inItem the in item
176 * @return the document handler
178 * @throws Exception the exception
180 private DocumentHandler createContactDocumentHandler(
181 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
182 String inItem) throws Exception {
183 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(ctx,
184 ctx.getCommonPartLabel(getContactServiceName()),
185 ContactsCommon.class);
186 docHandler.setInAuthority(inAuthority);
187 docHandler.setInItem(inItem);
193 * Creates the person authority.
195 * @param input the input
197 * @return the response
200 public Response createPersonAuthority(MultipartInput input) {
202 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
203 DocumentHandler handler = createDocumentHandler(ctx);
204 String csid = getRepositoryClient(ctx).create(ctx, handler);
205 //personAuthorityObject.setCsid(csid);
206 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
207 path.path("" + csid);
208 Response response = Response.created(path.build()).build();
210 } catch (BadRequestException bre) {
211 Response response = Response.status(
212 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
213 throw new WebApplicationException(response);
214 } catch (UnauthorizedException ue) {
215 Response response = Response.status(
216 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
217 throw new WebApplicationException(response);
218 } catch (Exception e) {
219 if (logger.isDebugEnabled()) {
220 logger.debug("Caught exception in createPersonAuthority", e);
222 Response response = Response.status(
223 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
224 throw new WebApplicationException(response);
229 * Gets the person authority by name.
231 * @param specifier the specifier
233 * @return the person authority by name
236 @Path("urn:cspace:name({specifier})")
237 public MultipartOutput getPersonAuthorityByName(@PathParam("specifier") String specifier) {
238 if (specifier == null) {
239 logger.error("getPersonAuthority: missing name!");
240 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
241 "get failed on PersonAuthority (missing specifier)").type(
242 "text/plain").build();
243 throw new WebApplicationException(response);
246 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
247 ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+
249 // We only get a single doc - if there are multiple,
250 // it is an error in use.
252 if (logger.isDebugEnabled()) {
253 logger.debug("getPersonAuthority with name=" + specifier);
255 MultipartOutput result = null;
257 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
258 DocumentHandler handler = createDocumentHandler(ctx);
259 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
260 handler.setDocumentFilter(myFilter);
261 getRepositoryClient(ctx).get(ctx, handler);
262 result = (MultipartOutput) ctx.getOutput();
263 } catch (UnauthorizedException ue) {
264 Response response = Response.status(
265 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
266 throw new WebApplicationException(response);
267 } catch (DocumentNotFoundException dnfe) {
268 if (logger.isDebugEnabled()) {
269 logger.debug("getPersonAuthority", dnfe);
271 Response response = Response.status(Response.Status.NOT_FOUND).entity(
272 "Get failed on PersonAuthority spec=" + specifier).type(
273 "text/plain").build();
274 throw new WebApplicationException(response);
275 } catch (Exception e) {
276 if (logger.isDebugEnabled()) {
277 logger.debug("getPersonAuthority", e);
279 Response response = Response.status(
280 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
281 throw new WebApplicationException(response);
283 if (result == null) {
284 Response response = Response.status(Response.Status.NOT_FOUND).entity(
285 "Get failed, the requested PersonAuthority spec:" + specifier + ": was not found.").type(
286 "text/plain").build();
287 throw new WebApplicationException(response);
293 * Gets the entities referencing this Person instance. The service type
294 * can be passed as a query param "type", and must match a configured type
295 * for the service bindings. If not set, the type defaults to
296 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
299 * @param csid the parent csid
300 * @param itemcsid the person csid
303 * @return the info for the referencing objects
306 @Path("{csid}/items/{itemcsid}/refObjs")
307 @Produces("application/xml") //FIXME: REM do this for CSPACE-1079 in Org authority.
308 public AuthorityRefDocList getReferencingObjects(
309 @PathParam("csid") String parentcsid,
310 @PathParam("itemcsid") String itemcsid,
311 @Context UriInfo ui) {
312 AuthorityRefDocList authRefDocList = null;
313 if (logger.isDebugEnabled()) {
314 logger.debug("getReferencingObjects with parentcsid="
315 + parentcsid + " and itemcsid=" + itemcsid);
317 if (parentcsid == null || "".equals(parentcsid)
318 || itemcsid == null || "".equals(itemcsid)) {
319 logger.error("getPerson: missing parentcsid or itemcsid!");
320 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
321 "get failed on Person with parentcsid="
322 + parentcsid + " and itemcsid=" + itemcsid).type(
323 "text/plain").build();
324 throw new WebApplicationException(response);
327 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
328 // Note that we have to create the service context for the Items, not the main service
329 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
331 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
332 RepositoryClient repoClient = getRepositoryClient(ctx);
333 DocumentFilter myFilter = handler.getDocumentFilter();
334 String serviceType = ServiceBindingUtils.SERVICE_TYPE_PROCEDURE;
335 List<String> list = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
337 serviceType = list.get(0);
339 DocumentWrapper<DocumentModel> docWrapper = repoClient.getDoc(ctx, itemcsid);
340 DocumentModel docModel = docWrapper.getWrappedObject();
341 String refName = (String)docModel.getPropertyValue(PersonJAXBSchema.REF_NAME);
343 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(ctx,
347 myFilter.getPageSize(), myFilter.getStartPage(), true /*computeTotal*/ );
348 } catch (UnauthorizedException ue) {
349 Response response = Response.status(
350 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
351 throw new WebApplicationException(response);
352 } catch (DocumentNotFoundException dnfe) {
353 if (logger.isDebugEnabled()) {
354 logger.debug("getReferencingObjects", dnfe);
356 Response response = Response.status(Response.Status.NOT_FOUND).entity(
357 "GetReferencingObjects failed with parentcsid="
358 + parentcsid + " and itemcsid=" + itemcsid).type(
359 "text/plain").build();
360 throw new WebApplicationException(response);
361 } catch (Exception e) { // Includes DocumentException
362 if (logger.isDebugEnabled()) {
363 logger.debug("GetReferencingObjects", e);
365 Response response = Response.status(
366 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
367 throw new WebApplicationException(response);
369 if (authRefDocList == null) {
370 Response response = Response.status(Response.Status.NOT_FOUND).entity(
371 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
372 "text/plain").build();
373 throw new WebApplicationException(response);
375 return authRefDocList;
380 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
382 logger.error("getPersonAuthority: missing csid!");
383 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
384 "get failed on PersonAuthority csid=" + csid).type(
385 "text/plain").build();
386 throw new WebApplicationException(response);
388 if (logger.isDebugEnabled()) {
389 logger.debug("getPersonAuthority with path(id)=" + csid);
391 MultipartOutput result = null;
393 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
394 DocumentHandler handler = createDocumentHandler(ctx);
395 getRepositoryClient(ctx).get(ctx, csid, handler);
396 result = (MultipartOutput) ctx.getOutput();
397 } catch (UnauthorizedException ue) {
398 Response response = Response.status(
399 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
400 throw new WebApplicationException(response);
401 } catch (DocumentNotFoundException dnfe) {
402 if (logger.isDebugEnabled()) {
403 logger.debug("getPersonAuthority", dnfe);
405 Response response = Response.status(Response.Status.NOT_FOUND).entity(
406 "Get failed on PersonAuthority csid=" + csid).type(
407 "text/plain").build();
408 throw new WebApplicationException(response);
409 } catch (Exception e) {
410 if (logger.isDebugEnabled()) {
411 logger.debug("getPersonAuthority", e);
413 Response response = Response.status(
414 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
415 throw new WebApplicationException(response);
417 if (result == null) {
418 Response response = Response.status(Response.Status.NOT_FOUND).entity(
419 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
420 "text/plain").build();
421 throw new WebApplicationException(response);
427 * Gets the person authority list.
431 * @return the person authority list
434 @Produces("application/xml")
435 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
436 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
438 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
439 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
440 DocumentHandler handler = createDocumentHandler(ctx);
441 String nameQ = queryParams.getFirst("refName");
443 handler.getDocumentFilter().setWhereClause("personauthorities_common:refName='" + nameQ + "'");
445 getRepositoryClient(ctx).getFiltered(ctx, handler);
446 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
447 } catch (UnauthorizedException ue) {
448 Response response = Response.status(
449 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
450 throw new WebApplicationException(response);
451 } catch (Exception e) {
452 if (logger.isDebugEnabled()) {
453 logger.debug("Caught exception in getPersonAuthorityList", e);
455 Response response = Response.status(
456 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
457 throw new WebApplicationException(response);
459 return personAuthorityObjectList;
463 * Update person authority.
465 * @param csid the csid
466 * @param theUpdate the the update
468 * @return the multipart output
472 public MultipartOutput updatePersonAuthority(
473 @PathParam("csid") String csid,
474 MultipartInput theUpdate) {
475 if (logger.isDebugEnabled()) {
476 logger.debug("updatePersonAuthority with csid=" + csid);
478 if (csid == null || "".equals(csid)) {
479 logger.error("updatePersonAuthority: missing csid!");
480 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
481 "update failed on PersonAuthority csid=" + csid).type(
482 "text/plain").build();
483 throw new WebApplicationException(response);
485 MultipartOutput result = null;
487 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
488 DocumentHandler handler = createDocumentHandler(ctx);
489 getRepositoryClient(ctx).update(ctx, csid, handler);
490 result = (MultipartOutput) ctx.getOutput();
491 } catch (BadRequestException bre) {
492 Response response = Response.status(
493 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
494 throw new WebApplicationException(response);
495 } catch (UnauthorizedException ue) {
496 Response response = Response.status(
497 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
498 throw new WebApplicationException(response);
499 } catch (DocumentNotFoundException dnfe) {
500 if (logger.isDebugEnabled()) {
501 logger.debug("caugth exception in updatePersonAuthority", dnfe);
503 Response response = Response.status(Response.Status.NOT_FOUND).entity(
504 "Update failed on PersonAuthority csid=" + csid).type(
505 "text/plain").build();
506 throw new WebApplicationException(response);
507 } catch (Exception e) {
508 Response response = Response.status(
509 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
510 throw new WebApplicationException(response);
516 * Delete person authority.
518 * @param csid the csid
520 * @return the response
524 public Response deletePersonAuthority(@PathParam("csid") String csid) {
526 if (logger.isDebugEnabled()) {
527 logger.debug("deletePersonAuthority with csid=" + csid);
529 if (csid == null || "".equals(csid)) {
530 logger.error("deletePersonAuthority: missing csid!");
531 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
532 "delete failed on PersonAuthority csid=" + csid).type(
533 "text/plain").build();
534 throw new WebApplicationException(response);
537 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
538 getRepositoryClient(ctx).delete(ctx, csid);
539 return Response.status(HttpResponseCodes.SC_OK).build();
540 } catch (UnauthorizedException ue) {
541 Response response = Response.status(
542 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
543 throw new WebApplicationException(response);
544 } catch (DocumentNotFoundException dnfe) {
545 if (logger.isDebugEnabled()) {
546 logger.debug("caught exception in deletePersonAuthority", dnfe);
548 Response response = Response.status(Response.Status.NOT_FOUND).entity(
549 "Delete failed on PersonAuthority csid=" + csid).type(
550 "text/plain").build();
551 throw new WebApplicationException(response);
552 } catch (Exception e) {
553 Response response = Response.status(
554 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
555 throw new WebApplicationException(response);
561 * ***********************************************************************
562 * Person parts - this is a sub-resource of PersonAuthority
563 * ***********************************************************************.
565 * @param parentcsid the parentcsid
566 * @param input the input
567 * @return the response
570 @Path("{csid}/items")
571 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
573 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(), input);
574 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
575 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
576 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
577 path.path(parentcsid + "/items/" + itemcsid);
578 Response response = Response.created(path.build()).build();
580 } catch (BadRequestException bre) {
581 Response response = Response.status(
582 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
583 throw new WebApplicationException(response);
584 } catch (UnauthorizedException ue) {
585 Response response = Response.status(
586 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
587 throw new WebApplicationException(response);
588 } catch (Exception e) {
589 if (logger.isDebugEnabled()) {
590 logger.debug("Caught exception in createPerson", e);
592 Response response = Response.status(
593 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
594 throw new WebApplicationException(response);
601 * @param parentcsid the parentcsid
602 * @param itemcsid the itemcsid
607 @Path("{csid}/items/{itemcsid}")
608 public MultipartOutput getPerson(
609 @PathParam("csid") String parentcsid,
610 @PathParam("itemcsid") String itemcsid) {
611 if (logger.isDebugEnabled()) {
612 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
614 if (parentcsid == null || "".equals(parentcsid)
615 || itemcsid == null || "".equals(itemcsid)) {
616 logger.error("getPerson: missing parentcsid or itemcsid!");
617 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
618 "get failed on Person with parentcsid="
619 + parentcsid + " and itemcsid=" + itemcsid).type(
620 "text/plain").build();
621 throw new WebApplicationException(response);
623 MultipartOutput result = null;
625 // Note that we have to create the service context for the Items, not the main service
626 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
627 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
628 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
629 // TODO should we assert that the item is in the passed personAuthority?
630 result = (MultipartOutput) ctx.getOutput();
631 } catch (UnauthorizedException ue) {
632 Response response = Response.status(
633 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
634 throw new WebApplicationException(response);
635 } catch (DocumentNotFoundException dnfe) {
636 if (logger.isDebugEnabled()) {
637 logger.debug("getPerson", dnfe);
639 Response response = Response.status(Response.Status.NOT_FOUND).entity(
640 "Get failed on Person csid=" + itemcsid).type(
641 "text/plain").build();
642 throw new WebApplicationException(response);
643 } catch (Exception e) {
644 if (logger.isDebugEnabled()) {
645 logger.debug("getPerson", e);
647 Response response = Response.status(
648 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
649 throw new WebApplicationException(response);
651 if (result == null) {
652 Response response = Response.status(Response.Status.NOT_FOUND).entity(
653 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
654 "text/plain").build();
655 throw new WebApplicationException(response);
661 * Gets the person list.
663 * @param parentcsid the parentcsid
664 * @param partialTerm the partial term
667 * @return the person list
670 @Path("{csid}/items")
671 @Produces("application/xml")
672 public PersonsCommonList getPersonList(
673 @PathParam("csid") String parentcsid,
674 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
675 @Context UriInfo ui) {
676 PersonsCommonList personObjectList = new PersonsCommonList();
678 // Note that docType defaults to the ServiceName, so we're fine with that.
679 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
680 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
682 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
683 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
684 handler.getDocumentFilter().setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
685 PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
687 // AND persons_common:displayName LIKE '%partialTerm%'
688 if (partialTerm != null && !partialTerm.isEmpty()) {
689 String ptClause = "AND " +
690 PersonJAXBSchema.PERSONS_COMMON + ":" +
691 PersonJAXBSchema.DISPLAY_NAME +
693 "'%" + partialTerm + "%'";
694 // handler.getDocumentFilter().appendWhereClause(ptClause, IQueryManager.SEARCH_QUALIFIER_AND);
695 handler.getDocumentFilter().appendWhereClause(ptClause, "");
698 getRepositoryClient(ctx).getFiltered(ctx, handler);
699 personObjectList = (PersonsCommonList) handler.getCommonPartList();
700 } catch (UnauthorizedException ue) {
701 Response response = Response.status(
702 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
703 throw new WebApplicationException(response);
704 } catch (Exception e) {
705 if (logger.isDebugEnabled()) {
706 logger.debug("Caught exception in getPersonList", e);
708 Response response = Response.status(
709 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
710 throw new WebApplicationException(response);
712 return personObjectList;
717 * Gets the person list by auth name.
719 * @param parentSpecifier the parent specifier
720 * @param partialTerm the partial term
723 * @return the person list by auth name
726 @Path("urn:cspace:name({specifier})/items")
727 @Produces("application/xml")
728 public PersonsCommonList getPersonListByAuthName(
729 @PathParam("specifier") String parentSpecifier,
730 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
731 @Context UriInfo ui) {
732 PersonsCommonList personObjectList = new PersonsCommonList();
735 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
736 ":" + PersonAuthorityJAXBSchema.DISPLAY_NAME+
737 "='" + parentSpecifier+"'";
738 // Need to get an Authority by name
739 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
740 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
742 getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
743 ctx = createServiceContext(getItemServiceName(), queryParams);
744 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
746 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
747 handler.getDocumentFilter().setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
748 PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
750 // AND persons_common:displayName LIKE '%partialTerm%'
751 if (partialTerm != null && !partialTerm.isEmpty()) {
752 String ptClause = "AND " +
753 PersonJAXBSchema.PERSONS_COMMON + ":" +
754 PersonJAXBSchema.DISPLAY_NAME +
756 "'%" + partialTerm + "%'";
757 // handler.getDocumentFilter().appendWhereClause(ptClause, IQueryManager.SEARCH_QUALIFIER_AND);
758 handler.getDocumentFilter().appendWhereClause(ptClause, "");
761 getRepositoryClient(ctx).getFiltered(ctx, handler);
762 personObjectList = (PersonsCommonList) handler.getCommonPartList();
763 } catch (UnauthorizedException ue) {
764 Response response = Response.status(
765 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
766 throw new WebApplicationException(response);
767 } catch (Exception e) {
768 if (logger.isDebugEnabled()) {
769 logger.debug("Caught exception in getPersonList", e);
771 Response response = Response.status(
772 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
773 throw new WebApplicationException(response);
775 return personObjectList;
781 * @param parentcsid the parentcsid
782 * @param itemcsid the itemcsid
783 * @param theUpdate the the update
785 * @return the multipart output
788 @Path("{csid}/items/{itemcsid}")
789 public MultipartOutput updatePerson(
790 @PathParam("csid") String parentcsid,
791 @PathParam("itemcsid") String itemcsid,
792 MultipartInput theUpdate) {
793 if (logger.isDebugEnabled()) {
794 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
796 if (parentcsid == null || "".equals(parentcsid)) {
797 logger.error("updatePerson: missing csid!");
798 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
799 "update failed on Person parentcsid=" + parentcsid).type(
800 "text/plain").build();
801 throw new WebApplicationException(response);
803 if (itemcsid == null || "".equals(itemcsid)) {
804 logger.error("updatePerson: missing itemcsid!");
805 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
806 "update failed on Person=" + itemcsid).type(
807 "text/plain").build();
808 throw new WebApplicationException(response);
810 MultipartOutput result = null;
812 // Note that we have to create the service context for the Items, not the main service
813 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
815 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
816 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
817 result = (MultipartOutput) ctx.getOutput();
818 } catch (BadRequestException bre) {
819 Response response = Response.status(
820 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
821 throw new WebApplicationException(response);
822 } catch (UnauthorizedException ue) {
823 Response response = Response.status(
824 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
825 throw new WebApplicationException(response);
826 } catch (DocumentNotFoundException dnfe) {
827 if (logger.isDebugEnabled()) {
828 logger.debug("caught exception in updatePerson", dnfe);
830 Response response = Response.status(Response.Status.NOT_FOUND).entity(
831 "Update failed on Person csid=" + itemcsid).type(
832 "text/plain").build();
833 throw new WebApplicationException(response);
834 } catch (Exception e) {
835 Response response = Response.status(
836 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
837 throw new WebApplicationException(response);
845 * @param parentcsid the parentcsid
846 * @param itemcsid the itemcsid
848 * @return the response
851 @Path("{csid}/items/{itemcsid}")
852 public Response deletePerson(
853 @PathParam("csid") String parentcsid,
854 @PathParam("itemcsid") String itemcsid) {
855 if (logger.isDebugEnabled()) {
856 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
858 if (parentcsid == null || "".equals(parentcsid)) {
859 logger.error("deletePerson: missing csid!");
860 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
861 "delete failed on Person parentcsid=" + parentcsid).type(
862 "text/plain").build();
863 throw new WebApplicationException(response);
865 if (itemcsid == null || "".equals(itemcsid)) {
866 logger.error("deletePerson: missing itemcsid!");
867 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
868 "delete failed on Person=" + itemcsid).type(
869 "text/plain").build();
870 throw new WebApplicationException(response);
873 // Note that we have to create the service context for the Items, not the main service
874 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
875 getRepositoryClient(ctx).delete(ctx, itemcsid);
876 return Response.status(HttpResponseCodes.SC_OK).build();
877 } catch (UnauthorizedException ue) {
878 Response response = Response.status(
879 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
880 throw new WebApplicationException(response);
881 } catch (DocumentNotFoundException dnfe) {
882 if (logger.isDebugEnabled()) {
883 logger.debug("caught exception in deletePerson", dnfe);
885 Response response = Response.status(Response.Status.NOT_FOUND).entity(
886 "Delete failed on Person itemcsid=" + itemcsid).type(
887 "text/plain").build();
888 throw new WebApplicationException(response);
889 } catch (Exception e) {
890 Response response = Response.status(
891 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
892 throw new WebApplicationException(response);
898 * ***********************************************************************
899 * Contact parts - this is a sub-resource of Person (or "item")
900 * ***********************************************************************.
902 * @param parentcsid the parentcsid
903 * @param itemcsid the itemcsid
904 * @param input the input
905 * @return the response
908 @Path("{parentcsid}/items/{itemcsid}/contacts")
909 public Response createContact(
910 @PathParam("parentcsid") String parentcsid,
911 @PathParam("itemcsid") String itemcsid,
912 MultipartInput input) {
914 // Note that we have to create the service context and document
915 // handler for the Contact service, not the main service.
916 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
917 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
918 String csid = getRepositoryClient(ctx).create(ctx, handler);
919 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
920 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
921 Response response = Response.created(path.build()).build();
923 } catch (BadRequestException bre) {
924 Response response = Response.status(
925 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
926 throw new WebApplicationException(response);
927 } catch (UnauthorizedException ue) {
928 Response response = Response.status(
929 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
930 throw new WebApplicationException(response);
931 } catch (Exception e) {
932 if (logger.isDebugEnabled()) {
933 logger.debug("Caught exception in createContact", e);
935 Response response = Response.status(
936 Response.Status.INTERNAL_SERVER_ERROR)
937 .entity("Attempt to create Contact failed.")
938 .type("text/plain").build();
939 throw new WebApplicationException(response);
945 * Gets the contact list.
947 * @param parentcsid the parentcsid
948 * @param itemcsid the itemcsid
951 * @return the contact list
954 @Produces({"application/xml"})
955 @Path("{parentcsid}/items/{itemcsid}/contacts/")
956 public ContactsCommonList getContactList(
957 @PathParam("parentcsid") String parentcsid,
958 @PathParam("itemcsid") String itemcsid,
959 @Context UriInfo ui) {
960 ContactsCommonList contactObjectList = new ContactsCommonList();
962 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
963 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
965 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
966 handler.getDocumentFilter().setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
967 ContactJAXBSchema.IN_AUTHORITY +
968 "='" + parentcsid + "'" +
970 ContactJAXBSchema.CONTACTS_COMMON + ":" +
971 ContactJAXBSchema.IN_ITEM +
972 "='" + itemcsid + "'" +
973 " AND ecm:isProxy = 0");
974 getRepositoryClient(ctx).getFiltered(ctx, handler);
975 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
976 } catch (UnauthorizedException ue) {
977 Response response = Response.status(
978 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
979 throw new WebApplicationException(response);
980 } catch (Exception e) {
981 if (logger.isDebugEnabled()) {
982 logger.debug("Caught exception in getContactsList", e);
984 Response response = Response.status(
985 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
986 throw new WebApplicationException(response);
988 return contactObjectList;
994 * @param parentcsid the parentcsid
995 * @param itemcsid the itemcsid
996 * @param csid the csid
998 * @return the contact
1001 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1002 public MultipartOutput getContact(
1003 @PathParam("parentcsid") String parentcsid,
1004 @PathParam("itemcsid") String itemcsid,
1005 @PathParam("csid") String csid) {
1006 MultipartOutput result = null;
1007 if (logger.isDebugEnabled()) {
1008 logger.debug("getContact with parentCsid=" + parentcsid +
1009 " itemcsid=" + itemcsid + " csid=" + csid);
1012 // Note that we have to create the service context and document
1013 // handler for the Contact service, not the main service.
1014 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1015 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1016 getRepositoryClient(ctx).get(ctx, csid, handler);
1017 result = (MultipartOutput) ctx.getOutput();
1018 } catch (UnauthorizedException ue) {
1019 Response response = Response.status(
1020 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
1021 throw new WebApplicationException(response);
1022 } catch (DocumentNotFoundException dnfe) {
1023 if (logger.isDebugEnabled()) {
1024 logger.debug("getContact", dnfe);
1026 Response response = Response.status(Response.Status.NOT_FOUND)
1027 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1028 .type("text/plain").build();
1029 throw new WebApplicationException(response);
1030 } catch (Exception e) {
1031 if (logger.isDebugEnabled()) {
1032 logger.debug("getContact", e);
1034 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
1035 .entity("Get contact failed")
1036 .type("text/plain").build();
1037 throw new WebApplicationException(response);
1039 if (result == null) {
1040 Response response = Response.status(Response.Status.NOT_FOUND)
1041 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1042 .type("text/plain").build();
1043 throw new WebApplicationException(response);
1052 * @param parentcsid the parentcsid
1053 * @param itemcsid the itemcsid
1054 * @param csid the csid
1055 * @param theUpdate the the update
1057 * @return the multipart output
1060 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1061 public MultipartOutput updateContact(
1062 @PathParam("parentcsid") String parentcsid,
1063 @PathParam("itemcsid") String itemcsid,
1064 @PathParam("csid") String csid,
1065 MultipartInput theUpdate) {
1066 if (logger.isDebugEnabled()) {
1067 logger.debug("updateContact with parentcsid=" + parentcsid +
1068 " itemcsid=" + itemcsid + " csid=" + csid);
1070 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1071 logger.error("updateContact: missing csid!");
1072 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1073 "update failed on Contact parentcsid=" + parentcsid).type(
1074 "text/plain").build();
1075 throw new WebApplicationException(response);
1077 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1078 logger.error("updateContact: missing itemcsid!");
1079 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1080 "update failed on Contact=" + itemcsid).type(
1081 "text/plain").build();
1082 throw new WebApplicationException(response);
1084 if (csid == null || csid.trim().isEmpty()) {
1085 logger.error("updateContact: missing csid!");
1086 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1087 "update failed on Contact=" + csid).type(
1088 "text/plain").build();
1089 throw new WebApplicationException(response);
1091 MultipartOutput result = null;
1093 // Note that we have to create the service context and document
1094 // handler for the Contact service, not the main service.
1095 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
1097 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1098 getRepositoryClient(ctx).update(ctx, csid, handler);
1099 result = (MultipartOutput) ctx.getOutput();
1100 } catch (BadRequestException bre) {
1101 Response response = Response.status(
1102 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1103 throw new WebApplicationException(response);
1104 } catch (UnauthorizedException ue) {
1105 Response response = Response.status(
1106 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1107 throw new WebApplicationException(response);
1108 } catch (DocumentNotFoundException dnfe) {
1109 if (logger.isDebugEnabled()) {
1110 logger.debug("caught exception in updateContact", dnfe);
1112 Response response = Response.status(Response.Status.NOT_FOUND).entity(
1113 "Update failed on Contact csid=" + itemcsid).type(
1114 "text/plain").build();
1115 throw new WebApplicationException(response);
1116 } catch (Exception e) {
1117 Response response = Response.status(
1118 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1119 throw new WebApplicationException(response);
1127 * @param parentcsid the parentcsid
1128 * @param itemcsid the itemcsid
1129 * @param csid the csid
1131 * @return the response
1134 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1135 public Response deleteContact(
1136 @PathParam("parentcsid") String parentcsid,
1137 @PathParam("itemcsid") String itemcsid,
1138 @PathParam("csid") String csid) {
1139 if (logger.isDebugEnabled()) {
1140 logger.debug("deleteContact with parentCsid=" + parentcsid +
1141 " itemcsid=" + itemcsid + " csid=" + csid);
1143 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1144 logger.error("deleteContact: missing parentcsid!");
1145 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1146 "delete contact failed on parentcsid=" + parentcsid).type(
1147 "text/plain").build();
1148 throw new WebApplicationException(response);
1150 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1151 logger.error("deleteContact: missing itemcsid!");
1152 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1153 "delete contact failed on itemcsid=" + itemcsid).type(
1154 "text/plain").build();
1155 throw new WebApplicationException(response);
1157 if (csid == null || csid.trim().isEmpty()) {
1158 logger.error("deleteContact: missing csid!");
1159 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1160 "delete contact failed on csid=" + csid).type(
1161 "text/plain").build();
1162 throw new WebApplicationException(response);
1165 // Note that we have to create the service context for the
1166 // Contact service, not the main service.
1167 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1168 getRepositoryClient(ctx).delete(ctx, csid);
1169 return Response.status(HttpResponseCodes.SC_OK).build();
1170 } catch (UnauthorizedException ue) {
1171 Response response = Response.status(
1172 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1173 throw new WebApplicationException(response);
1174 } catch (DocumentNotFoundException dnfe) {
1175 if (logger.isDebugEnabled()) {
1176 logger.debug("Caught exception in deleteContact", dnfe);
1178 Response response = Response.status(Response.Status.NOT_FOUND)
1179 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1180 .type("text/plain").build();
1181 throw new WebApplicationException(response);
1182 } catch (Exception e) {
1183 Response response = Response.status(
1184 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1185 throw new WebApplicationException(response);