2 * This document is a part of the source code and related artifacts for
3 * CollectionSpace, an open source collections management system for museums and
4 * related institutions:
6 * http://www.collectionspace.org http://wiki.collectionspace.org
8 * Copyright 2009 University of California at Berkeley
10 * Licensed under the Educational Community License (ECL), Version 2.0. You may
11 * not use this file except in compliance with this License.
13 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20 * License for the specific language governing permissions and limitations under
23 package org.collectionspace.services.person.nuxeo;
25 import java.util.List;
26 import java.util.regex.Pattern;
27 import org.collectionspace.services.common.api.Tools;
28 import org.collectionspace.services.common.document.InvalidDocumentException;
29 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
30 import org.collectionspace.services.person.PersonTermGroup;
31 import org.collectionspace.services.person.PersonTermGroupList;
32 import org.collectionspace.services.person.PersonsCommon;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * PersonValidatorHandler
39 * Performs validation when making requests related to Person records.
40 * As an example, you can modify this class to customize validation of
41 * payloads supplied in requests to create and/or update records.
43 public class PersonValidatorHandler extends ValidatorHandlerImpl {
45 final Logger logger = LoggerFactory.getLogger(PersonValidatorHandler.class);
46 // 'Bad pattern' for shortIdentifiers matches any non-word characters
47 private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]");
48 private static final String SHORT_ID_BAD_CHARS_ERROR =
49 "shortIdentifier must only contain standard word characters";
50 private static final String HAS_NO_TERMS_ERROR =
51 "Authority items must contain at least one term.";
52 private static final String TERM_HAS_EMPTY_DISPLAYNAME_ERROR =
53 "Each term group in an authority item must contain "
54 + "a non-empty display name.";
57 protected Class getCommonPartClass() {
58 return PersonsCommon.class;
62 protected void handleCreate() throws InvalidDocumentException {
63 PersonsCommon person = (PersonsCommon) getCommonPart();
64 // No guarantee that there is a common part in every post/update.
67 String shortId = person.getShortIdentifier();
68 if (shortId != null) {
69 CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
71 CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_ERROR);
72 CS_ASSERT(allTermsContainDisplayName(person), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
73 } catch (AssertionError e) {
74 if (logger.isErrorEnabled()) {
75 logger.error(e.getMessage(), e);
77 throw new InvalidDocumentException(e.getMessage(), e);
83 protected void handleGet() throws InvalidDocumentException {
87 protected void handleGetAll() throws InvalidDocumentException {
91 protected void handleUpdate() throws InvalidDocumentException {
92 PersonsCommon person = (PersonsCommon) getCommonPart();
93 // No guarantee that there is a common part in every post/update.
96 // shortIdentifier is among a set of fields that are
97 // prevented from being changed on an update, and thus
98 // we don't need to check its value here.
99 CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_ERROR);
100 CS_ASSERT(allTermsContainDisplayName(person), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
101 } catch (AssertionError e) {
102 if (logger.isErrorEnabled()) {
103 logger.error(e.getMessage(), e);
105 throw new InvalidDocumentException(e.getMessage(), e);
111 protected void handleDelete() throws InvalidDocumentException {
114 private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
115 // Check whether any characters match the 'bad' pattern
116 if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
122 private boolean containsAtLeastOneTerm(PersonsCommon person) {
123 PersonTermGroupList termGroupList = person.getPersonTermGroupList();
124 if (termGroupList == null) {
127 List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
128 if ((termGroups == null) || (termGroups.isEmpty())) {
134 private boolean allTermsContainDisplayName(PersonsCommon person) {
135 PersonTermGroupList termGroupList = person.getPersonTermGroupList();
136 List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
137 for (PersonTermGroup termGroup : termGroups) {
138 if (Tools.isBlank(termGroup.getTermDisplayName())) {