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.authorityref.AuthorityRefList;
51 //import org.collectionspace.services.common.context.MultipartServiceContext;
52 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
53 //import org.collectionspace.services.common.context.MultipartServiceContextImpl;
54 import org.collectionspace.services.common.context.ServiceBindingUtils;
55 import org.collectionspace.services.common.context.ServiceContext;
56 import org.collectionspace.services.common.document.BadRequestException;
57 //import org.collectionspace.services.common.document.DocumentException;
58 import org.collectionspace.services.common.document.DocumentFilter;
59 import org.collectionspace.services.common.document.DocumentHandler;
60 import org.collectionspace.services.common.document.DocumentNotFoundException;
61 import org.collectionspace.services.common.document.DocumentWrapper;
62 import org.collectionspace.services.common.repository.RepositoryClient;
63 import org.collectionspace.services.common.security.UnauthorizedException;
64 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
65 //import org.collectionspace.services.common.vocabulary.RefNameUtils;
66 import org.collectionspace.services.common.query.IQueryManager;
67 import org.collectionspace.services.contact.ContactResource;
68 import org.collectionspace.services.contact.ContactsCommon;
69 import org.collectionspace.services.contact.ContactsCommonList;
70 import org.collectionspace.services.contact.ContactJAXBSchema;
71 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
72 //import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
73 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
74 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
75 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
76 import org.jboss.resteasy.util.HttpResponseCodes;
77 import org.nuxeo.ecm.core.api.DocumentModel;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
82 * The Class PersonAuthorityResource.
84 @Path("/personauthorities")
85 @Consumes("multipart/mixed")
86 @Produces("multipart/mixed")
87 public class PersonAuthorityResource extends
88 AbstractMultiPartCollectionSpaceResourceImpl {
90 /** The Constant personAuthorityServiceName. */
91 private final static String personAuthorityServiceName = "personauthorities";
93 /** The Constant personServiceName. */
94 private final static String personServiceName = "persons";
97 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
98 //FIXME retrieve client type from configuration
99 /** The Constant CLIENT_TYPE. */
100 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
102 /** The contact resource. */
103 private ContactResource contactResource = new ContactResource();
106 * Instantiates a new person authority resource.
108 public PersonAuthorityResource() {
113 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
116 protected String getVersionString() {
117 /** The last change revision. */
118 final String lastChangeRevision = "$LastChangedRevision$";
119 return lastChangeRevision;
123 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
126 public String getServiceName() {
127 return personAuthorityServiceName;
131 public Class<PersonauthoritiesCommon> getCommonPartClass() {
132 return PersonauthoritiesCommon.class;
136 * Gets the item service name.
138 * @return the item service name
140 public String getItemServiceName() {
141 return personServiceName;
145 * Gets the contact service name.
147 * @return the contact service name
149 public String getContactServiceName() {
150 return contactResource.getServiceName();
154 // public DocumentHandler createDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx) throws Exception {
155 // DocumentHandler docHandler = ctx.getDocumentHandler();
156 // if (ctx.getInput() != null) {
157 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
158 // if (obj != null) {
159 // docHandler.setCommonPart((PersonauthoritiesCommon) obj);
162 // return docHandler;
166 * Creates the item document handler.
169 * @param inAuthority the in authority
171 * @return the document handler
173 * @throws Exception the exception
175 private DocumentHandler createItemDocumentHandler(ServiceContext<MultipartInput, MultipartOutput> ctx,
176 String inAuthority) throws Exception {
177 PersonDocumentModelHandler docHandler = (PersonDocumentModelHandler)createDocumentHandler(ctx,
178 ctx.getCommonPartLabel(getItemServiceName()),
179 PersonsCommon.class);
180 docHandler.setInAuthority(inAuthority);
186 * Creates the contact document handler.
189 * @param inAuthority the in authority
190 * @param inItem the in item
192 * @return the document handler
194 * @throws Exception the exception
196 private DocumentHandler createContactDocumentHandler(
197 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
198 String inItem) throws Exception {
199 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(ctx,
200 ctx.getCommonPartLabel(getContactServiceName()),
201 ContactsCommon.class);
202 docHandler.setInAuthority(inAuthority);
203 docHandler.setInItem(inItem);
205 // DocumentHandler docHandler = ctx.getDocumentHandler();
206 // // Set the inAuthority and inItem values, which specify the
207 // // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
208 // // (e.g. Person, Organization) with which the Contact is associated.
209 // ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
210 // ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
211 // if (ctx.getInput() != null) {
212 // Object obj = ((MultipartServiceContext) ctx)
213 // .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
214 // ContactsCommon.class);
215 // if (obj != null) {
216 // docHandler.setCommonPart((ContactsCommon) obj);
223 * Creates the person authority.
225 * @param input the input
227 * @return the response
230 public Response createPersonAuthority(MultipartInput input) {
232 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
233 DocumentHandler handler = createDocumentHandler(ctx);
234 String csid = getRepositoryClient(ctx).create(ctx, handler);
235 //personAuthorityObject.setCsid(csid);
236 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
237 path.path("" + csid);
238 Response response = Response.created(path.build()).build();
240 } catch (BadRequestException bre) {
241 Response response = Response.status(
242 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
243 throw new WebApplicationException(response);
244 } catch (UnauthorizedException ue) {
245 Response response = Response.status(
246 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
247 throw new WebApplicationException(response);
248 } catch (Exception e) {
249 if (logger.isDebugEnabled()) {
250 logger.debug("Caught exception in createPersonAuthority", e);
252 Response response = Response.status(
253 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
254 throw new WebApplicationException(response);
259 * Gets the person authority by name.
261 * @param specifier the specifier
263 * @return the person authority by name
266 @Path("urn:cspace:name({specifier})")
267 public MultipartOutput getPersonAuthorityByName(@PathParam("specifier") String specifier) {
268 //REM: String idValue = null;
269 if (specifier == null) {
270 logger.error("getPersonAuthority: missing name!");
271 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
272 "get failed on PersonAuthority (missing specifier)").type(
273 "text/plain").build();
274 throw new WebApplicationException(response);
277 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
278 ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+
280 // We only get a single doc - if there are multiple,
281 // it is an error in use.
283 if (logger.isDebugEnabled()) {
284 logger.debug("getPersonAuthority with name=" + specifier);
286 MultipartOutput result = null;
288 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
289 DocumentHandler handler = createDocumentHandler(ctx);
290 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
291 handler.setDocumentFilter(myFilter);
292 getRepositoryClient(ctx).get(ctx, handler);
293 result = (MultipartOutput) ctx.getOutput();
294 } catch (UnauthorizedException ue) {
295 Response response = Response.status(
296 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
297 throw new WebApplicationException(response);
298 } catch (DocumentNotFoundException dnfe) {
299 if (logger.isDebugEnabled()) {
300 logger.debug("getPersonAuthority", dnfe);
302 Response response = Response.status(Response.Status.NOT_FOUND).entity(
303 "Get failed on PersonAuthority spec=" + specifier).type(
304 "text/plain").build();
305 throw new WebApplicationException(response);
306 } catch (Exception e) {
307 if (logger.isDebugEnabled()) {
308 logger.debug("getPersonAuthority", e);
310 Response response = Response.status(
311 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
312 throw new WebApplicationException(response);
314 if (result == null) {
315 Response response = Response.status(Response.Status.NOT_FOUND).entity(
316 "Get failed, the requested PersonAuthority spec:" + specifier + ": was not found.").type(
317 "text/plain").build();
318 throw new WebApplicationException(response);
324 * Gets the entities referencing this Person instance. The service type
325 * can be passed as a query param "type", and must match a configured type
326 * for the service bindings. If not set, the type defaults to
327 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
330 * @param csid the parent csid
331 * @param itemcsid the person csid
334 * @return the info for the referencing objects
337 @Path("{csid}/items/{itemcsid}/refObjs")
338 @Produces("application/xml") //FIXME: REM do this for CSPACE-1079 in Org authority.
339 public AuthorityRefDocList getReferencingObjects(
340 @PathParam("csid") String parentcsid,
341 @PathParam("itemcsid") String itemcsid,
342 @Context UriInfo ui) {
343 AuthorityRefDocList authRefDocList = null;
344 if (logger.isDebugEnabled()) {
345 logger.debug("getReferencingObjects with parentcsid="
346 + parentcsid + " and itemcsid=" + itemcsid);
348 if (parentcsid == null || "".equals(parentcsid)
349 || itemcsid == null || "".equals(itemcsid)) {
350 logger.error("getPerson: missing parentcsid or itemcsid!");
351 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
352 "get failed on Person with parentcsid="
353 + parentcsid + " and itemcsid=" + itemcsid).type(
354 "text/plain").build();
355 throw new WebApplicationException(response);
358 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
359 // Note that we have to create the service context for the Items, not the main service
360 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
362 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
363 RepositoryClient repoClient = getRepositoryClient(ctx);
364 DocumentFilter myFilter = handler.getDocumentFilter();
365 String serviceType = ServiceBindingUtils.SERVICE_TYPE_PROCEDURE;
366 List<String> list = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
368 serviceType = list.get(0);
370 DocumentWrapper<DocumentModel> docWrapper = repoClient.getDoc(ctx, itemcsid);
371 DocumentModel docModel = docWrapper.getWrappedObject();
372 String refName = (String)docModel.getPropertyValue(PersonJAXBSchema.REF_NAME);
374 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(ctx,
378 myFilter.getPageSize(), myFilter.getStartPage(), true /*computeTotal*/ );
379 } catch (UnauthorizedException ue) {
380 Response response = Response.status(
381 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
382 throw new WebApplicationException(response);
383 } catch (DocumentNotFoundException dnfe) {
384 if (logger.isDebugEnabled()) {
385 logger.debug("getReferencingObjects", dnfe);
387 Response response = Response.status(Response.Status.NOT_FOUND).entity(
388 "GetReferencingObjects failed with parentcsid="
389 + parentcsid + " and itemcsid=" + itemcsid).type(
390 "text/plain").build();
391 throw new WebApplicationException(response);
392 } catch (Exception e) { // Includes DocumentException
393 if (logger.isDebugEnabled()) {
394 logger.debug("GetReferencingObjects", e);
396 Response response = Response.status(
397 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
398 throw new WebApplicationException(response);
400 if (authRefDocList == null) {
401 Response response = Response.status(Response.Status.NOT_FOUND).entity(
402 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
403 "text/plain").build();
404 throw new WebApplicationException(response);
406 return authRefDocList;
411 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
413 logger.error("getPersonAuthority: missing csid!");
414 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
415 "get failed on PersonAuthority csid=" + csid).type(
416 "text/plain").build();
417 throw new WebApplicationException(response);
419 if (logger.isDebugEnabled()) {
420 logger.debug("getPersonAuthority with path(id)=" + csid);
422 MultipartOutput result = null;
424 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
425 DocumentHandler handler = createDocumentHandler(ctx);
426 getRepositoryClient(ctx).get(ctx, csid, handler);
427 result = (MultipartOutput) ctx.getOutput();
428 } catch (UnauthorizedException ue) {
429 Response response = Response.status(
430 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
431 throw new WebApplicationException(response);
432 } catch (DocumentNotFoundException dnfe) {
433 if (logger.isDebugEnabled()) {
434 logger.debug("getPersonAuthority", dnfe);
436 Response response = Response.status(Response.Status.NOT_FOUND).entity(
437 "Get failed on PersonAuthority csid=" + csid).type(
438 "text/plain").build();
439 throw new WebApplicationException(response);
440 } catch (Exception e) {
441 if (logger.isDebugEnabled()) {
442 logger.debug("getPersonAuthority", e);
444 Response response = Response.status(
445 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
446 throw new WebApplicationException(response);
448 if (result == null) {
449 Response response = Response.status(Response.Status.NOT_FOUND).entity(
450 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
451 "text/plain").build();
452 throw new WebApplicationException(response);
458 * Gets the person authority list.
462 * @return the person authority list
465 @Produces("application/xml")
466 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
467 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
469 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
470 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
471 DocumentHandler handler = createDocumentHandler(ctx);
472 String nameQ = queryParams.getFirst("refName");
474 handler.getDocumentFilter().setWhereClause("personauthorities_common:refName='" + nameQ + "'");
476 getRepositoryClient(ctx).getFiltered(ctx, handler);
477 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
478 } catch (UnauthorizedException ue) {
479 Response response = Response.status(
480 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
481 throw new WebApplicationException(response);
482 } catch (Exception e) {
483 if (logger.isDebugEnabled()) {
484 logger.debug("Caught exception in getPersonAuthorityList", e);
486 Response response = Response.status(
487 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
488 throw new WebApplicationException(response);
490 return personAuthorityObjectList;
494 * Update person authority.
496 * @param csid the csid
497 * @param theUpdate the the update
499 * @return the multipart output
503 public MultipartOutput updatePersonAuthority(
504 @PathParam("csid") String csid,
505 MultipartInput theUpdate) {
506 if (logger.isDebugEnabled()) {
507 logger.debug("updatePersonAuthority with csid=" + csid);
509 if (csid == null || "".equals(csid)) {
510 logger.error("updatePersonAuthority: missing csid!");
511 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
512 "update failed on PersonAuthority csid=" + csid).type(
513 "text/plain").build();
514 throw new WebApplicationException(response);
516 MultipartOutput result = null;
518 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
519 DocumentHandler handler = createDocumentHandler(ctx);
520 getRepositoryClient(ctx).update(ctx, csid, handler);
521 result = (MultipartOutput) ctx.getOutput();
522 } catch (BadRequestException bre) {
523 Response response = Response.status(
524 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
525 throw new WebApplicationException(response);
526 } catch (UnauthorizedException ue) {
527 Response response = Response.status(
528 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
529 throw new WebApplicationException(response);
530 } catch (DocumentNotFoundException dnfe) {
531 if (logger.isDebugEnabled()) {
532 logger.debug("caugth exception in updatePersonAuthority", dnfe);
534 Response response = Response.status(Response.Status.NOT_FOUND).entity(
535 "Update failed on PersonAuthority csid=" + csid).type(
536 "text/plain").build();
537 throw new WebApplicationException(response);
538 } catch (Exception e) {
539 Response response = Response.status(
540 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
541 throw new WebApplicationException(response);
547 * Delete person authority.
549 * @param csid the csid
551 * @return the response
555 public Response deletePersonAuthority(@PathParam("csid") String csid) {
557 if (logger.isDebugEnabled()) {
558 logger.debug("deletePersonAuthority with csid=" + csid);
560 if (csid == null || "".equals(csid)) {
561 logger.error("deletePersonAuthority: missing csid!");
562 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
563 "delete failed on PersonAuthority csid=" + csid).type(
564 "text/plain").build();
565 throw new WebApplicationException(response);
568 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
569 getRepositoryClient(ctx).delete(ctx, csid);
570 return Response.status(HttpResponseCodes.SC_OK).build();
571 } catch (UnauthorizedException ue) {
572 Response response = Response.status(
573 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
574 throw new WebApplicationException(response);
575 } catch (DocumentNotFoundException dnfe) {
576 if (logger.isDebugEnabled()) {
577 logger.debug("caught exception in deletePersonAuthority", dnfe);
579 Response response = Response.status(Response.Status.NOT_FOUND).entity(
580 "Delete failed on PersonAuthority csid=" + csid).type(
581 "text/plain").build();
582 throw new WebApplicationException(response);
583 } catch (Exception e) {
584 Response response = Response.status(
585 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
586 throw new WebApplicationException(response);
592 * ***********************************************************************
593 * Person parts - this is a sub-resource of PersonAuthority
594 * ***********************************************************************.
596 * @param parentcsid the parentcsid
597 * @param input the input
598 * @return the response
601 @Path("{csid}/items")
602 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
604 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(), input);
605 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
606 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
607 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
608 path.path(parentcsid + "/items/" + itemcsid);
609 Response response = Response.created(path.build()).build();
611 } catch (BadRequestException bre) {
612 Response response = Response.status(
613 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
614 throw new WebApplicationException(response);
615 } catch (UnauthorizedException ue) {
616 Response response = Response.status(
617 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
618 throw new WebApplicationException(response);
619 } catch (Exception e) {
620 if (logger.isDebugEnabled()) {
621 logger.debug("Caught exception in createPerson", e);
623 Response response = Response.status(
624 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
625 throw new WebApplicationException(response);
632 * @param parentcsid the parentcsid
633 * @param itemcsid the itemcsid
638 @Path("{csid}/items/{itemcsid}")
639 public MultipartOutput getPerson(
640 @PathParam("csid") String parentcsid,
641 @PathParam("itemcsid") String itemcsid) {
642 if (logger.isDebugEnabled()) {
643 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
645 if (parentcsid == null || "".equals(parentcsid)
646 || itemcsid == null || "".equals(itemcsid)) {
647 logger.error("getPerson: missing parentcsid or itemcsid!");
648 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
649 "get failed on Person with parentcsid="
650 + parentcsid + " and itemcsid=" + itemcsid).type(
651 "text/plain").build();
652 throw new WebApplicationException(response);
654 MultipartOutput result = null;
656 // Note that we have to create the service context for the Items, not the main service
657 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
658 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
659 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
660 // TODO should we assert that the item is in the passed personAuthority?
661 result = (MultipartOutput) ctx.getOutput();
662 } catch (UnauthorizedException ue) {
663 Response response = Response.status(
664 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
665 throw new WebApplicationException(response);
666 } catch (DocumentNotFoundException dnfe) {
667 if (logger.isDebugEnabled()) {
668 logger.debug("getPerson", dnfe);
670 Response response = Response.status(Response.Status.NOT_FOUND).entity(
671 "Get failed on Person csid=" + itemcsid).type(
672 "text/plain").build();
673 throw new WebApplicationException(response);
674 } catch (Exception e) {
675 if (logger.isDebugEnabled()) {
676 logger.debug("getPerson", e);
678 Response response = Response.status(
679 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
680 throw new WebApplicationException(response);
682 if (result == null) {
683 Response response = Response.status(Response.Status.NOT_FOUND).entity(
684 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
685 "text/plain").build();
686 throw new WebApplicationException(response);
692 * Gets the person list.
694 * @param parentcsid the parentcsid
695 * @param partialTerm the partial term
698 * @return the person list
701 @Path("{csid}/items")
702 @Produces("application/xml")
703 public PersonsCommonList getPersonList(
704 @PathParam("csid") String parentcsid,
705 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
706 @Context UriInfo ui) {
707 PersonsCommonList personObjectList = new PersonsCommonList();
709 // Note that docType defaults to the ServiceName, so we're fine with that.
710 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
711 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
713 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
714 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
715 handler.getDocumentFilter().setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
716 PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
718 // AND persons_common:displayName LIKE '%partialTerm%'
719 if (partialTerm != null && !partialTerm.isEmpty()) {
720 String ptClause = "AND " +
721 PersonJAXBSchema.PERSONS_COMMON + ":" +
722 PersonJAXBSchema.DISPLAY_NAME +
724 "'%" + partialTerm + "%'";
725 handler.getDocumentFilter().appendWhereClause(ptClause);
727 getRepositoryClient(ctx).getFiltered(ctx, handler);
728 personObjectList = (PersonsCommonList) handler.getCommonPartList();
729 } catch (UnauthorizedException ue) {
730 Response response = Response.status(
731 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
732 throw new WebApplicationException(response);
733 } catch (Exception e) {
734 if (logger.isDebugEnabled()) {
735 logger.debug("Caught exception in getPersonList", e);
737 Response response = Response.status(
738 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
739 throw new WebApplicationException(response);
741 return personObjectList;
746 * Gets the person list by auth name.
748 * @param parentSpecifier the parent specifier
749 * @param partialTerm the partial term
752 * @return the person list by auth name
755 @Path("urn:cspace:name({specifier})/items")
756 @Produces("application/xml")
757 public PersonsCommonList getPersonListByAuthName(
758 @PathParam("specifier") String parentSpecifier,
759 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
760 @Context UriInfo ui) {
761 PersonsCommonList personObjectList = new PersonsCommonList();
764 PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+
765 ":" + PersonAuthorityJAXBSchema.DISPLAY_NAME+
766 "='" + parentSpecifier+"'";
767 // Need to get an Authority by name
768 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
769 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
771 getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
772 ctx = createServiceContext(getItemServiceName(), queryParams);
773 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
775 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
776 handler.getDocumentFilter().setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
777 PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
779 // AND persons_common:displayName LIKE '%partialTerm%'
780 if (partialTerm != null && !partialTerm.isEmpty()) {
781 String ptClause = "AND " +
782 PersonJAXBSchema.PERSONS_COMMON + ":" +
783 PersonJAXBSchema.DISPLAY_NAME +
785 "'%" + partialTerm + "%'";
786 handler.getDocumentFilter().appendWhereClause(ptClause);
788 getRepositoryClient(ctx).getFiltered(ctx, handler);
789 personObjectList = (PersonsCommonList) handler.getCommonPartList();
790 } catch (UnauthorizedException ue) {
791 Response response = Response.status(
792 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
793 throw new WebApplicationException(response);
794 } catch (Exception e) {
795 if (logger.isDebugEnabled()) {
796 logger.debug("Caught exception in getPersonList", e);
798 Response response = Response.status(
799 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
800 throw new WebApplicationException(response);
802 return personObjectList;
808 * @param parentcsid the parentcsid
809 * @param itemcsid the itemcsid
810 * @param theUpdate the the update
812 * @return the multipart output
815 @Path("{csid}/items/{itemcsid}")
816 public MultipartOutput updatePerson(
817 @PathParam("csid") String parentcsid,
818 @PathParam("itemcsid") String itemcsid,
819 MultipartInput theUpdate) {
820 if (logger.isDebugEnabled()) {
821 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
823 if (parentcsid == null || "".equals(parentcsid)) {
824 logger.error("updatePerson: missing csid!");
825 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
826 "update failed on Person parentcsid=" + parentcsid).type(
827 "text/plain").build();
828 throw new WebApplicationException(response);
830 if (itemcsid == null || "".equals(itemcsid)) {
831 logger.error("updatePerson: missing itemcsid!");
832 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
833 "update failed on Person=" + itemcsid).type(
834 "text/plain").build();
835 throw new WebApplicationException(response);
837 MultipartOutput result = null;
839 // Note that we have to create the service context for the Items, not the main service
840 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
842 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
843 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
844 result = (MultipartOutput) ctx.getOutput();
845 } catch (BadRequestException bre) {
846 Response response = Response.status(
847 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
848 throw new WebApplicationException(response);
849 } catch (UnauthorizedException ue) {
850 Response response = Response.status(
851 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
852 throw new WebApplicationException(response);
853 } catch (DocumentNotFoundException dnfe) {
854 if (logger.isDebugEnabled()) {
855 logger.debug("caught exception in updatePerson", dnfe);
857 Response response = Response.status(Response.Status.NOT_FOUND).entity(
858 "Update failed on Person csid=" + itemcsid).type(
859 "text/plain").build();
860 throw new WebApplicationException(response);
861 } catch (Exception e) {
862 Response response = Response.status(
863 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
864 throw new WebApplicationException(response);
872 * @param parentcsid the parentcsid
873 * @param itemcsid the itemcsid
875 * @return the response
878 @Path("{csid}/items/{itemcsid}")
879 public Response deletePerson(
880 @PathParam("csid") String parentcsid,
881 @PathParam("itemcsid") String itemcsid) {
882 if (logger.isDebugEnabled()) {
883 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
885 if (parentcsid == null || "".equals(parentcsid)) {
886 logger.error("deletePerson: missing csid!");
887 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
888 "delete failed on Person parentcsid=" + parentcsid).type(
889 "text/plain").build();
890 throw new WebApplicationException(response);
892 if (itemcsid == null || "".equals(itemcsid)) {
893 logger.error("deletePerson: missing itemcsid!");
894 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
895 "delete failed on Person=" + itemcsid).type(
896 "text/plain").build();
897 throw new WebApplicationException(response);
900 // Note that we have to create the service context for the Items, not the main service
901 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
902 getRepositoryClient(ctx).delete(ctx, itemcsid);
903 return Response.status(HttpResponseCodes.SC_OK).build();
904 } catch (UnauthorizedException ue) {
905 Response response = Response.status(
906 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
907 throw new WebApplicationException(response);
908 } catch (DocumentNotFoundException dnfe) {
909 if (logger.isDebugEnabled()) {
910 logger.debug("caught exception in deletePerson", dnfe);
912 Response response = Response.status(Response.Status.NOT_FOUND).entity(
913 "Delete failed on Person itemcsid=" + itemcsid).type(
914 "text/plain").build();
915 throw new WebApplicationException(response);
916 } catch (Exception e) {
917 Response response = Response.status(
918 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
919 throw new WebApplicationException(response);
925 * ***********************************************************************
926 * Contact parts - this is a sub-resource of Person (or "item")
927 * ***********************************************************************.
929 * @param parentcsid the parentcsid
930 * @param itemcsid the itemcsid
931 * @param input the input
932 * @return the response
935 @Path("{parentcsid}/items/{itemcsid}/contacts")
936 public Response createContact(
937 @PathParam("parentcsid") String parentcsid,
938 @PathParam("itemcsid") String itemcsid,
939 MultipartInput input) {
941 // Note that we have to create the service context and document
942 // handler for the Contact service, not the main service.
943 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
944 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
945 String csid = getRepositoryClient(ctx).create(ctx, handler);
946 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
947 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
948 Response response = Response.created(path.build()).build();
950 } catch (BadRequestException bre) {
951 Response response = Response.status(
952 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
953 throw new WebApplicationException(response);
954 } catch (UnauthorizedException ue) {
955 Response response = Response.status(
956 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
957 throw new WebApplicationException(response);
958 } catch (Exception e) {
959 if (logger.isDebugEnabled()) {
960 logger.debug("Caught exception in createContact", e);
962 Response response = Response.status(
963 Response.Status.INTERNAL_SERVER_ERROR)
964 .entity("Attempt to create Contact failed.")
965 .type("text/plain").build();
966 throw new WebApplicationException(response);
972 * Gets the contact list.
974 * @param parentcsid the parentcsid
975 * @param itemcsid the itemcsid
978 * @return the contact list
981 @Produces({"application/xml"})
982 @Path("{parentcsid}/items/{itemcsid}/contacts/")
983 public ContactsCommonList getContactList(
984 @PathParam("parentcsid") String parentcsid,
985 @PathParam("itemcsid") String itemcsid,
986 @Context UriInfo ui) {
987 ContactsCommonList contactObjectList = new ContactsCommonList();
989 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
990 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
992 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
993 handler.getDocumentFilter().setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
994 ContactJAXBSchema.IN_AUTHORITY +
995 "='" + parentcsid + "'" +
997 ContactJAXBSchema.CONTACTS_COMMON + ":" +
998 ContactJAXBSchema.IN_ITEM +
999 "='" + itemcsid + "'" +
1000 " AND ecm:isProxy = 0");
1001 getRepositoryClient(ctx).getFiltered(ctx, handler);
1002 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
1003 } catch (UnauthorizedException ue) {
1004 Response response = Response.status(
1005 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
1006 throw new WebApplicationException(response);
1007 } catch (Exception e) {
1008 if (logger.isDebugEnabled()) {
1009 logger.debug("Caught exception in getContactsList", e);
1011 Response response = Response.status(
1012 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
1013 throw new WebApplicationException(response);
1015 return contactObjectList;
1021 * @param parentcsid the parentcsid
1022 * @param itemcsid the itemcsid
1023 * @param csid the csid
1025 * @return the contact
1028 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1029 public MultipartOutput getContact(
1030 @PathParam("parentcsid") String parentcsid,
1031 @PathParam("itemcsid") String itemcsid,
1032 @PathParam("csid") String csid) {
1033 MultipartOutput result = null;
1034 if (logger.isDebugEnabled()) {
1035 logger.debug("getContact with parentCsid=" + parentcsid +
1036 " itemcsid=" + itemcsid + " csid=" + csid);
1039 // Note that we have to create the service context and document
1040 // handler for the Contact service, not the main service.
1041 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1042 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1043 getRepositoryClient(ctx).get(ctx, csid, handler);
1044 result = (MultipartOutput) ctx.getOutput();
1045 } catch (UnauthorizedException ue) {
1046 Response response = Response.status(
1047 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
1048 throw new WebApplicationException(response);
1049 } catch (DocumentNotFoundException dnfe) {
1050 if (logger.isDebugEnabled()) {
1051 logger.debug("getContact", dnfe);
1053 Response response = Response.status(Response.Status.NOT_FOUND)
1054 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1055 .type("text/plain").build();
1056 throw new WebApplicationException(response);
1057 } catch (Exception e) {
1058 if (logger.isDebugEnabled()) {
1059 logger.debug("getContact", e);
1061 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
1062 .entity("Get contact failed")
1063 .type("text/plain").build();
1064 throw new WebApplicationException(response);
1066 if (result == null) {
1067 Response response = Response.status(Response.Status.NOT_FOUND)
1068 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1069 .type("text/plain").build();
1070 throw new WebApplicationException(response);
1079 * @param parentcsid the parentcsid
1080 * @param itemcsid the itemcsid
1081 * @param csid the csid
1082 * @param theUpdate the the update
1084 * @return the multipart output
1087 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1088 public MultipartOutput updateContact(
1089 @PathParam("parentcsid") String parentcsid,
1090 @PathParam("itemcsid") String itemcsid,
1091 @PathParam("csid") String csid,
1092 MultipartInput theUpdate) {
1093 if (logger.isDebugEnabled()) {
1094 logger.debug("updateContact with parentcsid=" + parentcsid +
1095 " itemcsid=" + itemcsid + " csid=" + csid);
1097 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1098 logger.error("updateContact: missing csid!");
1099 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1100 "update failed on Contact parentcsid=" + parentcsid).type(
1101 "text/plain").build();
1102 throw new WebApplicationException(response);
1104 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1105 logger.error("updateContact: missing itemcsid!");
1106 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1107 "update failed on Contact=" + itemcsid).type(
1108 "text/plain").build();
1109 throw new WebApplicationException(response);
1111 if (csid == null || csid.trim().isEmpty()) {
1112 logger.error("updateContact: missing csid!");
1113 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1114 "update failed on Contact=" + csid).type(
1115 "text/plain").build();
1116 throw new WebApplicationException(response);
1118 MultipartOutput result = null;
1120 // Note that we have to create the service context and document
1121 // handler for the Contact service, not the main service.
1122 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
1124 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1125 getRepositoryClient(ctx).update(ctx, csid, handler);
1126 result = (MultipartOutput) ctx.getOutput();
1127 } catch (BadRequestException bre) {
1128 Response response = Response.status(
1129 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1130 throw new WebApplicationException(response);
1131 } catch (UnauthorizedException ue) {
1132 Response response = Response.status(
1133 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1134 throw new WebApplicationException(response);
1135 } catch (DocumentNotFoundException dnfe) {
1136 if (logger.isDebugEnabled()) {
1137 logger.debug("caught exception in updateContact", dnfe);
1139 Response response = Response.status(Response.Status.NOT_FOUND).entity(
1140 "Update failed on Contact csid=" + itemcsid).type(
1141 "text/plain").build();
1142 throw new WebApplicationException(response);
1143 } catch (Exception e) {
1144 Response response = Response.status(
1145 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1146 throw new WebApplicationException(response);
1154 * @param parentcsid the parentcsid
1155 * @param itemcsid the itemcsid
1156 * @param csid the csid
1158 * @return the response
1161 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1162 public Response deleteContact(
1163 @PathParam("parentcsid") String parentcsid,
1164 @PathParam("itemcsid") String itemcsid,
1165 @PathParam("csid") String csid) {
1166 if (logger.isDebugEnabled()) {
1167 logger.debug("deleteContact with parentCsid=" + parentcsid +
1168 " itemcsid=" + itemcsid + " csid=" + csid);
1170 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1171 logger.error("deleteContact: missing parentcsid!");
1172 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1173 "delete contact failed on parentcsid=" + parentcsid).type(
1174 "text/plain").build();
1175 throw new WebApplicationException(response);
1177 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1178 logger.error("deleteContact: missing itemcsid!");
1179 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1180 "delete contact failed on itemcsid=" + itemcsid).type(
1181 "text/plain").build();
1182 throw new WebApplicationException(response);
1184 if (csid == null || csid.trim().isEmpty()) {
1185 logger.error("deleteContact: missing csid!");
1186 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1187 "delete contact failed on csid=" + csid).type(
1188 "text/plain").build();
1189 throw new WebApplicationException(response);
1192 // Note that we have to create the service context for the
1193 // Contact service, not the main service.
1194 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1195 getRepositoryClient(ctx).delete(ctx, csid);
1196 return Response.status(HttpResponseCodes.SC_OK).build();
1197 } catch (UnauthorizedException ue) {
1198 Response response = Response.status(
1199 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1200 throw new WebApplicationException(response);
1201 } catch (DocumentNotFoundException dnfe) {
1202 if (logger.isDebugEnabled()) {
1203 logger.debug("Caught exception in deleteContact", dnfe);
1205 Response response = Response.status(Response.Status.NOT_FOUND)
1206 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1207 .type("text/plain").build();
1208 throw new WebApplicationException(response);
1209 } catch (Exception e) {
1210 Response response = Response.status(
1211 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1212 throw new WebApplicationException(response);