]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
65e245ce30b7c74868145725a23959898cfbda1a
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org http://wiki.collectionspace.org
7  *
8  * Copyright 2009 University of California at Berkeley
9  *
10  * Licensed under the Educational Community License (ECL), Version 2.0. You may
11  * not use this file except in compliance with this License.
12  *
13  * You may obtain a copy of the ECL 2.0 License at
14  *
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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
21  * the License.
22  */
23 package org.collectionspace.services.person.nuxeo;
24
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;
35
36 /**
37  * PersonValidatorHandler
38  *
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.
42  */
43 public class PersonValidatorHandler extends ValidatorHandlerImpl {
44
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.";
55
56     @Override
57     protected Class getCommonPartClass() {
58         return PersonsCommon.class;
59     }
60
61     @Override
62     protected void handleCreate() throws InvalidDocumentException {
63         PersonsCommon person = (PersonsCommon) getCommonPart();
64         // No guarantee that there is a common part in every post/update.
65         if (person != null) {
66             try {
67                 String shortId = person.getShortIdentifier();
68                 if (shortId != null) {
69                     CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
70                 }
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);
76                 }
77                 throw new InvalidDocumentException(e.getMessage(), e);
78             }
79         }
80     }
81
82     @Override
83     protected void handleGet() throws InvalidDocumentException {
84     }
85
86     @Override
87     protected void handleGetAll() throws InvalidDocumentException {
88     }
89
90     @Override
91     protected void handleUpdate() throws InvalidDocumentException {
92         PersonsCommon person = (PersonsCommon) getCommonPart();
93         // No guarantee that there is a common part in every post/update.
94         if (person != null) {
95             try {
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);
104                 }
105                 throw new InvalidDocumentException(e.getMessage(), e);
106             }
107         }
108     }
109
110     @Override
111     protected void handleDelete() throws InvalidDocumentException {
112     }
113
114     private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
115         // Check whether any characters match the 'bad' pattern
116         if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
117             return false;
118         }
119         return true;
120     }
121
122     private boolean containsAtLeastOneTerm(PersonsCommon person) {
123         PersonTermGroupList termGroupList = person.getPersonTermGroupList();
124         if (termGroupList == null) {
125             return false;
126         }
127         List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
128         if ((termGroups == null) || (termGroups.isEmpty())) {
129             return false;
130         }
131         return true;
132     }
133
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())) {
139                 return false;
140             }
141         }
142         return true;
143     }
144 }