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