]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8f5d342f03a3ac59f952051c5e6e6d4cd422400d
[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 at 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.concept.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.concept.ConceptTermGroup;
32 import org.collectionspace.services.concept.ConceptTermGroupList;
33 import org.collectionspace.services.concept.ConceptsCommon;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * ConceptValidatorHandler
39  * 
40  * Performs validation when making requests related to Concept records.
41  * As an example, you can modify this class to customize validation of
42  * payloads supplied in requests to create and/or update records.
43  */
44 public class ConceptValidatorHandler extends ValidatorHandlerImpl {
45
46     final Logger logger = LoggerFactory.getLogger(ConceptValidatorHandler.class);
47     // 'Bad pattern' for shortIdentifiers matches any non-word characters
48     private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]");
49     private static final String SHORT_ID_BAD_CHARS_ERROR =
50             "shortIdentifier must only contain standard word characters";
51     private static final String HAS_NO_TERMS_ERROR =
52             "Authority items must contain at least one term.";
53     private static final String TERM_HAS_EMPTY_DISPLAYNAME_ERROR =
54             "Each term group in an authority item must contain "
55             + "a non-empty display name.";
56
57     @Override
58     protected Class getCommonPartClass() {
59         return ConceptsCommon.class;
60     }
61
62     @Override
63     protected void handleCreate() throws InvalidDocumentException {
64         ConceptsCommon concept = (ConceptsCommon) getCommonPart();
65         // No guarantee that there is a common part in every post/update.
66         if (concept != null) {
67             try {
68                 String shortId = concept.getShortIdentifier();
69                 if (shortId != null) {
70                     CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
71                 }
72                 CS_ASSERT(containsAtLeastOneTerm(concept), HAS_NO_TERMS_ERROR);
73                 CS_ASSERT(allTermsContainDisplayName(concept), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
74             } catch (AssertionError e) {
75                 if (logger.isErrorEnabled()) {
76                     logger.error(e.getMessage(), e);
77                 }
78                 throw new InvalidDocumentException(e.getMessage(), e);
79             }
80         }
81     }
82
83     @Override
84     protected void handleGet() throws InvalidDocumentException {
85     }
86
87     @Override
88     protected void handleGetAll() throws InvalidDocumentException {
89     }
90
91     @Override
92     protected void handleUpdate() throws InvalidDocumentException {
93         ConceptsCommon concept = (ConceptsCommon) getCommonPart();
94         // No guarantee that there is a common part in every post/update.
95         if (concept != null) {
96             try {
97                 // shortIdentifier is among a set of fields that are
98                 // prevented from being changed on an update, and thus
99                 // we don't need to check its value here.
100                 CS_ASSERT(containsAtLeastOneTerm(concept), HAS_NO_TERMS_ERROR);
101                 CS_ASSERT(allTermsContainDisplayName(concept), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
102             } catch (AssertionError e) {
103                 if (logger.isErrorEnabled()) {
104                     logger.error(e.getMessage(), e);
105                 }
106                 throw new InvalidDocumentException(e.getMessage(), e);
107             }
108         }
109     }
110
111     @Override
112     protected void handleDelete() throws InvalidDocumentException {
113     }
114
115     private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
116         // Check whether any characters match the 'bad' pattern
117         if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
118             return false;
119         }
120         return true;
121     }
122
123     private boolean containsAtLeastOneTerm(ConceptsCommon concept) {
124         ConceptTermGroupList termGroupList = concept.getConceptTermGroupList();
125         if (termGroupList == null) {
126             return false;
127         }
128         List<ConceptTermGroup> termGroups = termGroupList.getConceptTermGroup();
129         if ((termGroups == null) || (termGroups.isEmpty())){ 
130             return false;
131         }
132         return true;
133     }
134
135     private boolean allTermsContainDisplayName(ConceptsCommon concept) {
136         ConceptTermGroupList termGroupList = concept.getConceptTermGroupList();
137         List<ConceptTermGroup> termGroups = termGroupList.getConceptTermGroup();
138         for (ConceptTermGroup termGroup : termGroups) {
139             if (Tools.isBlank(termGroup.getTermDisplayName())) {
140                 return false;
141             }
142         }
143         return true;
144     }
145 }