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.person.PersonsCommon;
29 import org.collectionspace.services.common.context.MultipartServiceContext;
30 import org.collectionspace.services.common.context.ServiceContext;
31 import org.collectionspace.services.common.document.DocumentHandler.Action;
32 import org.collectionspace.services.common.document.InvalidDocumentException;
33 import org.collectionspace.services.common.document.ValidatorHandler;
34 import org.collectionspace.services.person.PersonTermGroup;
35 import org.collectionspace.services.person.PersonTermGroupList;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * PersonValidatorHandler
42 * Validates data supplied when attempting to create and/or update Person
45 * $LastChangedRevision: $ $LastChangedDate: $
47 public class PersonValidatorHandler implements ValidatorHandler {
49 final Logger logger = LoggerFactory.getLogger(PersonValidatorHandler.class);
50 private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
53 public void validate(Action action, ServiceContext ctx)
54 throws InvalidDocumentException {
55 if (logger.isDebugEnabled()) {
56 logger.debug("validate() action=" + action.name());
59 // Bail out if the validation action is for delete.
60 if (action.equals(Action.DELETE)) {
65 MultipartServiceContext mctx = (MultipartServiceContext) ctx;
66 PersonsCommon person = (PersonsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
69 boolean invalid = false;
71 if (person != null) { // No guarantee that there is a common part in every post/update.
73 // Validation occurring on both creates and updates
76 * String displayName = person.getDisplayName(); if
77 * (!person.isDisplayNameComputed() && ((displayName == null) ||
78 * displayName.trim().isEmpty())) { invalid = true; msg +=
79 * "displayName must be non-null and non-blank if
80 * displayNameComputed is false!"; }
84 if (!containsAtLeastOneTerm(person)) {
86 msg += "Authority items must contain at least one term.";
89 if (!allTermsContainNameOrDisplayName(person)) {
91 msg += "Each term group in an authority item must contain "
92 + "a non-empty term name or "
93 + "a non-empty term display name.";
96 // Validation specific to creates or updates
97 if (action.equals(Action.CREATE)) {
99 // shortIdentifier value must contain only word characters
100 String shortId = person.getShortIdentifier();
101 if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
103 msg += "shortIdentifier must only contain standard word characters";
106 // Note: Per CSPACE-2215, shortIdentifier values that are null (missing)
107 // or the empty string are now legally accepted in create payloads.
108 // In either of those cases, a short identifier will be synthesized from
109 // a display name or supplied in another manner.
111 } else if (action.equals(Action.UPDATE)) {
117 throw new InvalidDocumentException(msg);
119 } catch (InvalidDocumentException ide) {
121 } catch (Exception e) {
122 throw new InvalidDocumentException(e);
126 private boolean containsAtLeastOneTerm(PersonsCommon person) {
127 PersonTermGroupList termGroupList = person.getPersonTermGroupList();
128 if (termGroupList == null) {
131 List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
132 if ((termGroups == null) || (termGroups.size() == 0)) {
138 private boolean allTermsContainNameOrDisplayName(PersonsCommon person) {
139 PersonTermGroupList termGroupList = person.getPersonTermGroupList();
140 List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
141 for (PersonTermGroup termGroup : termGroups) {
142 if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName()) ){