]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
10ea216f9dbb3348aed4fd9bdb4b1e74a29fb8a8
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California, Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.organization.nuxeo;
25
26 import java.util.List;
27 import java.util.regex.Pattern;
28 import org.collectionspace.services.common.api.Tools;
29 import org.collectionspace.services.common.document.InvalidDocumentException;
30 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
31 import org.collectionspace.services.organization.OrgTermGroup;
32 import org.collectionspace.services.organization.OrgTermGroupList;
33 import org.collectionspace.services.organization.OrganizationsCommon;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * OrganizationValidatorHandler
39  * 
40  * Validates data supplied when attempting to create and/or update Organization records.
41  */
42 public class OrganizationValidatorHandler extends ValidatorHandlerImpl {
43
44     final Logger logger = LoggerFactory.getLogger(OrganizationValidatorHandler.class);
45     // 'Bad pattern' for shortIdentifiers matches any non-word characters
46     private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]"); //.matcher(input).matches()
47     private static final String VALIDATION_ERROR = "The record payload was invalid. See log file for more details.";
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 HAS_AN_EMPTY_TERM_ERROR =
53             "Each term group in an authority item must contain "
54             + "a non-empty term name or "
55             + "a non-empty term display name.";
56     boolean invalid = false;
57     String msg = "";
58
59     @Override
60     protected Class getCommonPartClass() {
61         return OrganizationsCommon.class;
62     }
63
64     @Override
65     protected void handleCreate() throws InvalidDocumentException {
66         OrganizationsCommon person = (OrganizationsCommon) getCommonPart();
67         // No guarantee that there is a common part in every post/update.
68         if (person != null) {
69             try {
70                 String shortId = person.getShortIdentifier();
71                 if (shortId != null) {
72                     CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
73                 }
74                 CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_ERROR);
75                 CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_ERROR);
76             } catch (AssertionError e) {
77                 if (logger.isErrorEnabled()) {
78                     logger.error(e.getMessage(), e);
79                 }
80                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
81             }
82         }
83     }
84
85     @Override
86     protected void handleGet() throws InvalidDocumentException {
87     }
88
89     @Override
90     protected void handleGetAll() throws InvalidDocumentException {
91     }
92
93     @Override
94     protected void handleUpdate() throws InvalidDocumentException {
95         OrganizationsCommon person = (OrganizationsCommon) getCommonPart();
96         // No guarantee that there is a common part in every post/update.
97         if (person != null) {
98             try {
99                 // shortIdentifier is among a set of fields that are
100                 // prevented from being changed on an update, and thus
101                 // we don't need to check its value here.
102                 CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_ERROR);
103                 CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_ERROR);
104             } catch (AssertionError e) {
105                 if (logger.isErrorEnabled()) {
106                     logger.error(e.getMessage(), e);
107                 }
108                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
109             }
110         }
111     }
112
113     @Override
114     protected void handleDelete() throws InvalidDocumentException {
115     }
116
117     private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
118         // Check whether any characters match the 'bad' pattern
119         if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
120             return false;
121         }
122         return true;
123     }
124
125     private boolean containsAtLeastOneTerm(OrganizationsCommon person) {
126         OrgTermGroupList termGroupList = person.getOrgTermGroupList();
127         if (termGroupList == null) {
128             return false;
129         }
130         List<OrgTermGroup> termGroups = termGroupList.getOrgTermGroup();
131         if ((termGroups == null) || (termGroups.size() == 0)) {
132             return false;
133         }
134         return true;
135     }
136
137     private boolean allTermsContainNameOrDisplayName(OrganizationsCommon person) {
138         OrgTermGroupList termGroupList = person.getOrgTermGroupList();
139         List<OrgTermGroup> termGroups = termGroupList.getOrgTermGroup();
140         for (OrgTermGroup termGroup : termGroups) {
141             if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) {
142                 return false;
143             }
144         }
145         return true;
146     }
147 }