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, 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.
25 package org.collectionspace.services.place.nuxeo;
27 import java.util.List;
28 import java.util.regex.Pattern;
29 import org.collectionspace.services.common.api.Tools;
30 import org.collectionspace.services.common.document.InvalidDocumentException;
31 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
32 import org.collectionspace.services.place.PlaceTermGroup;
33 import org.collectionspace.services.place.PlaceTermGroupList;
34 import org.collectionspace.services.place.PlacesCommon;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * PlaceValidatorHandler
41 * Performs validation when making requests related to Place records.
42 * As an example, you can modify this class to customize validation of
43 * payloads supplied in requests to create and/or update records.
45 public class PlaceValidatorHandler extends ValidatorHandlerImpl {
47 final Logger logger = LoggerFactory.getLogger(PlaceValidatorHandler.class);
48 // 'Bad pattern' for shortIdentifiers matches any non-word characters
49 private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]");
50 private static final String VALIDATION_ERROR =
51 "The record payload was invalid. See log file for more details.";
52 private static final String SHORT_ID_BAD_CHARS_ERROR =
53 "shortIdentifier must only contain standard word characters";
54 private static final String HAS_NO_TERMS_ERROR =
55 "Authority items must contain at least one term.";
56 private static final String TERM_HAS_EMPTY_DISPLAYNAME_ERROR =
57 "Each term group in an authority item must contain "
58 + "a non-empty term display name.";
61 protected Class getCommonPartClass() {
62 return PlacesCommon.class;
66 protected void handleCreate() throws InvalidDocumentException {
67 PlacesCommon place = (PlacesCommon) getCommonPart();
68 // No guarantee that there is a common part in every post/update.
71 String shortId = place.getShortIdentifier();
72 if (shortId != null) {
73 CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
75 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
76 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
77 } catch (AssertionError e) {
78 if (logger.isErrorEnabled()) {
79 logger.error(e.getMessage(), e);
81 throw new InvalidDocumentException(VALIDATION_ERROR, e);
87 protected void handleGet() throws InvalidDocumentException {
91 protected void handleGetAll() throws InvalidDocumentException {
95 protected void handleUpdate() throws InvalidDocumentException {
96 PlacesCommon place = (PlacesCommon) getCommonPart();
97 // No guarantee that there is a common part in every post/update.
100 // shortIdentifier is among a set of fields that are
101 // prevented from being changed on an update, and thus
102 // we don't need to check its value here.
103 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
104 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
105 } catch (AssertionError e) {
106 if (logger.isErrorEnabled()) {
107 logger.error(e.getMessage(), e);
109 throw new InvalidDocumentException(VALIDATION_ERROR, e);
115 protected void handleDelete() throws InvalidDocumentException {
118 private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
119 // Check whether any characters match the 'bad' pattern
120 if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
126 private boolean containsAtLeastOneTerm(PlacesCommon place) {
127 PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
128 if (termGroupList == null) {
131 List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
132 if ((termGroups == null) || (termGroups.isEmpty())){
138 private boolean allTermsContainDisplayName(PlacesCommon place) {
139 PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
140 List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
141 for (PlaceTermGroup termGroup : termGroups) {
142 if (Tools.isBlank(termGroup.getTermDisplayName())) {