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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.concept.nuxeo;
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;
38 * ConceptValidatorHandler
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.
44 public class ConceptValidatorHandler extends ValidatorHandlerImpl {
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.";
58 protected Class getCommonPartClass() {
59 return ConceptsCommon.class;
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) {
68 String shortId = concept.getShortIdentifier();
69 if (shortId != null) {
70 CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
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);
78 throw new InvalidDocumentException(e.getMessage(), e);
84 protected void handleGet() throws InvalidDocumentException {
88 protected void handleGetAll() throws InvalidDocumentException {
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) {
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);
106 throw new InvalidDocumentException(e.getMessage(), e);
112 protected void handleDelete() throws InvalidDocumentException {
115 private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
116 // Check whether any characters match the 'bad' pattern
117 if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
123 private boolean containsAtLeastOneTerm(ConceptsCommon concept) {
124 ConceptTermGroupList termGroupList = concept.getConceptTermGroupList();
125 if (termGroupList == null) {
128 List<ConceptTermGroup> termGroups = termGroupList.getConceptTermGroup();
129 if ((termGroups == null) || (termGroups.isEmpty())){
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())) {