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.location.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.location.LocTermGroup;
32 import org.collectionspace.services.location.LocTermGroupList;
33 import org.collectionspace.services.location.LocationsCommon;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * LocationValidatorHandler
40 * Validates data supplied when attempting to create and/or update Location records.
42 public class LocationValidatorHandler extends ValidatorHandlerImpl {
44 final Logger logger = LoggerFactory.getLogger(LocationValidatorHandler.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.";
58 protected Class getCommonPartClass() {
59 return LocationsCommon.class;
63 protected void handleCreate() throws InvalidDocumentException {
64 LocationsCommon location = (LocationsCommon) getCommonPart();
65 // No guarantee that there is a common part in every post/update.
66 if (location != null) {
68 String shortId = location.getShortIdentifier();
69 if (shortId != null) {
70 CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
72 CS_ASSERT(containsAtLeastOneTerm(location), HAS_NO_TERMS_ERROR);
73 CS_ASSERT(allTermsContainNameOrDisplayName(location), HAS_AN_EMPTY_TERM_ERROR);
74 } catch (AssertionError e) {
75 if (logger.isErrorEnabled()) {
76 logger.error(e.getMessage(), e);
78 throw new InvalidDocumentException(VALIDATION_ERROR, e);
84 protected void handleGet() throws InvalidDocumentException {
88 protected void handleGetAll() throws InvalidDocumentException {
92 protected void handleUpdate() throws InvalidDocumentException {
93 LocationsCommon location = (LocationsCommon) getCommonPart();
94 // No guarantee that there is a common part in every post/update.
95 if (location != 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(location), HAS_NO_TERMS_ERROR);
101 CS_ASSERT(allTermsContainNameOrDisplayName(location), HAS_AN_EMPTY_TERM_ERROR);
102 } catch (AssertionError e) {
103 if (logger.isErrorEnabled()) {
104 logger.error(e.getMessage(), e);
106 throw new InvalidDocumentException(VALIDATION_ERROR, 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(LocationsCommon person) {
124 LocTermGroupList termGroupList = person.getLocTermGroupList();
125 if (termGroupList == null) {
128 List<LocTermGroup> termGroups = termGroupList.getLocTermGroup();
129 if ((termGroups == null) || (termGroups.isEmpty())) {
135 private boolean allTermsContainNameOrDisplayName(LocationsCommon person) {
136 LocTermGroupList termGroupList = person.getLocTermGroupList();
137 List<LocTermGroup> termGroups = termGroupList.getLocTermGroup();
138 for (LocTermGroup termGroup : termGroups) {
139 if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) {