]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2066c5c1134ad1ee34453323b116ae5b2e94704c
[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]"); //.matcher(input).matches()
48     private static final String VALIDATION_ERROR = "The record payload was invalid. See log file for more details.";
49     private static final String SHORT_ID_BAD_CHARS_MSG =
50             "shortIdentifier must only contain standard word characters";
51     private static final String HAS_NO_TERMS_MSG =
52             "Authority items must contain at least one term.";
53     private static final String HAS_AN_EMPTY_TERM_MSG =
54             "Each term group in an authority item must contain "
55             + "a non-empty term name or "
56             + "a non-empty term display name.";
57
58     @Override
59     protected Class getCommonPartClass() {
60         return PersonsCommon.class;
61     }
62
63     @Override
64     protected void handleCreate() throws InvalidDocumentException {
65         PersonsCommon person = (PersonsCommon) getCommonPart();
66         // No guarantee that there is a common part in every post/update.
67         if (person != null) {
68             try {
69                 String shortId = person.getShortIdentifier();
70                 if (shortId != null) {
71                     CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_MSG);
72                 }
73                 CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_MSG);
74                 CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_MSG);
75             } catch (AssertionError e) {
76                 if (logger.isErrorEnabled()) {
77                     logger.error(e.getMessage(), e);
78                 }
79                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
80             }
81         }
82     }
83
84     @Override
85     protected void handleGet() throws InvalidDocumentException {
86     }
87
88     @Override
89     protected void handleGetAll() throws InvalidDocumentException {
90     }
91
92     @Override
93     protected void handleUpdate() throws InvalidDocumentException {
94         PersonsCommon person = (PersonsCommon) getCommonPart();
95         // No guarantee that there is a common part in every post/update.
96         if (person != null) {
97             try {
98                 // shortIdentifier is among a set of fields that are
99                 // prevented from being changed on an update, and thus
100                 // we don't need to check its value here.
101                 CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_MSG);
102                 CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_MSG);
103             } catch (AssertionError e) {
104                 if (logger.isErrorEnabled()) {
105                     logger.error(e.getMessage(), e);
106                 }
107                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
108             }
109         }
110     }
111
112     @Override
113     protected void handleDelete() throws InvalidDocumentException {
114     }
115
116     private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
117         // Check whether any characters match the 'bad' pattern
118         if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
119             return false;
120         }
121         return true;
122     }
123
124     private boolean containsAtLeastOneTerm(PersonsCommon person) {
125         PersonTermGroupList termGroupList = person.getPersonTermGroupList();
126         if (termGroupList == null) {
127             return false;
128         }
129         List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
130         if ((termGroups == null) || (termGroups.isEmpty())) {
131             return false;
132         }
133         return true;
134     }
135
136     private boolean allTermsContainNameOrDisplayName(PersonsCommon person) {
137         PersonTermGroupList termGroupList = person.getPersonTermGroupList();
138         List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
139         for (PersonTermGroup termGroup : termGroups) {
140             if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) {
141                 return false;
142             }
143         }
144         return true;
145     }
146 }