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.vocabulary.nuxeo;
26 import java.util.regex.Pattern;
28 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
29 import org.collectionspace.services.common.context.MultipartServiceContext;
30 import org.collectionspace.services.common.context.ServiceContext;
31 import org.collectionspace.services.common.document.DocumentHandler.Action;
32 import org.collectionspace.services.common.document.InvalidDocumentException;
33 import org.collectionspace.services.common.document.ValidatorHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * VocabularyItemValidatorHandler
40 * Validates data supplied when attempting to create and/or update VocabularyItem records.
42 * $LastChangedRevision: $
45 public class VocabularyItemValidatorHandler implements ValidatorHandler {
47 final Logger logger = LoggerFactory.getLogger(VocabularyItemValidatorHandler.class);
48 private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
51 public void validate(Action action, ServiceContext ctx)
52 throws InvalidDocumentException {
53 if (logger.isDebugEnabled()) {
54 logger.debug("validate() action=" + action.name());
57 // Bail out if the validation action is for delete.
58 if (action.equals(Action.DELETE)) {
63 MultipartServiceContext mctx = (MultipartServiceContext) ctx;
64 VocabularyitemsCommon vocabItem = (VocabularyitemsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
65 VocabularyitemsCommon.class);
67 boolean invalid = false;
69 // Validation occurring on both creates and updates
70 String displayName = vocabItem.getDisplayName();
71 if ((displayName == null) || (displayName.trim().length() < 2)) {
73 msg += "displayName must be non-null and contain at least 2 non-whitespace characters";
76 // Validation specific to creates or updates
77 if (action.equals(Action.CREATE)) {
78 String shortId = vocabItem.getShortIdentifier();
79 // Per CSPACE-2215, shortIdentifier values that are null (missing)
80 // oe the empty string are now legally accepted in create payloads.
81 // In either of those cases, a short identifier will be synthesized from
82 // a display name or supplied in another manner.
83 if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
85 msg += "shortIdentifier must only contain standard word characters";
87 } else if (action.equals(Action.UPDATE)) {
92 throw new InvalidDocumentException(msg);
94 } catch (InvalidDocumentException ide) {
96 } catch (Exception e) {
97 throw new InvalidDocumentException(e);