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 SHORT_ID_BAD_CHARS_ERROR =
51 "shortIdentifier must only contain standard word characters";
52 private static final String HAS_NO_TERMS_ERROR =
53 "Authority items must contain at least one term.";
54 private static final String TERM_HAS_EMPTY_DISPLAYNAME_ERROR =
55 "Each term in an authority item must contain "
56 + "a non-empty display name.";
59 protected Class getCommonPartClass() {
60 return PlacesCommon.class;
64 protected void handleCreate() throws InvalidDocumentException {
65 PlacesCommon place = (PlacesCommon) getCommonPart();
66 // No guarantee that there is a common part in every post/update.
69 String shortId = place.getShortIdentifier();
70 if (shortId != null) {
71 CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
73 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
74 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
75 } catch (AssertionError e) {
76 if (logger.isErrorEnabled()) {
77 logger.error(e.getMessage(), e);
79 throw new InvalidDocumentException(e.getMessage(), e);
85 protected void handleGet() throws InvalidDocumentException {
89 protected void handleGetAll() throws InvalidDocumentException {
93 protected void handleUpdate() throws InvalidDocumentException {
94 PlacesCommon place = (PlacesCommon) getCommonPart();
95 // No guarantee that there is a common part in every post/update.
98 // shortIdentifier is among a set of fields that are
99 // prevented from being changed on an update, and thus
100 // we don't need to check its value here.
101 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
102 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
103 } catch (AssertionError e) {
104 if (logger.isErrorEnabled()) {
105 logger.error(e.getMessage(), e);
107 throw new InvalidDocumentException(e.getMessage(), e);
113 protected void handleDelete() throws InvalidDocumentException {
116 private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
117 // Check whether any characters match the 'bad' pattern
118 if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
124 private boolean containsAtLeastOneTerm(PlacesCommon place) {
125 PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
126 if (termGroupList == null) {
129 List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
130 if ((termGroups == null) || (termGroups.isEmpty())){
136 private boolean allTermsContainDisplayName(PlacesCommon place) {
137 PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
138 List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
139 for (PlaceTermGroup termGroup : termGroups) {
140 if (Tools.isBlank(termGroup.getTermDisplayName())) {