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.organization.nuxeo;
26 import java.util.regex.Pattern;
27 import org.collectionspace.services.organization.OrganizationsCommon;
28 import org.collectionspace.services.common.context.MultipartServiceContext;
29 import org.collectionspace.services.common.context.ServiceContext;
30 import org.collectionspace.services.common.document.DocumentHandler.Action;
31 import org.collectionspace.services.common.document.InvalidDocumentException;
32 import org.collectionspace.services.common.document.ValidatorHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * OrganizationValidatorHandler
39 * Validates data supplied when attempting to create and/or update Organization records.
41 * $LastChangedRevision$
44 public class OrganizationValidatorHandler implements ValidatorHandler {
46 final Logger logger = LoggerFactory.getLogger(OrganizationValidatorHandler.class);
47 private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
50 public void validate(Action action, ServiceContext ctx)
51 throws InvalidDocumentException {
52 if (logger.isDebugEnabled()) {
53 logger.debug("validate() action=" + action.name());
56 // Bail out if the validation action is for delete.
57 if (action.equals(Action.DELETE)) {
62 MultipartServiceContext mctx = (MultipartServiceContext) ctx;
63 OrganizationsCommon org = (OrganizationsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
64 OrganizationsCommon.class);
66 boolean invalid = false;
68 // Validation occurring on both creates and updates
69 String displayName = org.getDisplayName();
70 if (!org.isDisplayNameComputed() && ((displayName == null) || displayName.trim().isEmpty())) {
72 msg += "displayName must be non-null and non-blank if displayNameComputed is false";
75 // Validation specific to creates or updates
76 if (action.equals(Action.CREATE)) {
77 String shortId = org.getShortIdentifier();
78 // Per CSPACE-2215, shortIdentifier values that are null (missing)
79 // or the empty string are now legally accepted in create payloads.
80 // In either of those cases, a short identifier will be synthesized from
81 // a display name or supplied in another manner.
82 if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
84 msg += "shortIdentifier must only contain standard word characters";
86 } else if (action.equals(Action.UPDATE)) {
91 throw new InvalidDocumentException(msg);
93 } catch (InvalidDocumentException ide) {
95 } catch (Exception e) {
96 throw new InvalidDocumentException(e);