From f0c318aa6f3c7ea8f941dcb60c6f016f20a68c31 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Mon, 7 May 2012 16:50:39 -0700 Subject: [PATCH] CSPACE-5137: Modified Location validator handler to reflect repeatable term info group. Minor modifications to class comments in four authorities validator handlers. --- .../nuxeo/LocationValidatorHandler.java | 6 +- .../nuxeo/OrganizationValidatorHandler.java | 4 +- .../person/nuxeo/PersonValidatorHandler.java | 7 +- .../place/nuxeo/PlaceValidatorHandler.java | 181 ++++++++++-------- 4 files changed, 114 insertions(+), 84 deletions(-) diff --git a/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationValidatorHandler.java b/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationValidatorHandler.java index 0c6345969..ac1e52f0d 100644 --- a/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationValidatorHandler.java +++ b/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationValidatorHandler.java @@ -6,7 +6,7 @@ * http://www.collectionspace.org * http://wiki.collectionspace.org - * Copyright 2009 University of California at Berkeley + * Copyright 2009 University of California, Berkeley * Licensed under the Educational Community License (ECL), Version 2.0. * You may not use this file except in compliance with this License. @@ -37,7 +37,9 @@ import org.slf4j.LoggerFactory; /** * LocationValidatorHandler * - * Validates data supplied when attempting to create and/or update Location records. + * Performs validation when making requests related to Location records. + * As an example, you can modify this class to customize validation of + * payloads supplied in requests to create and/or update records. */ public class LocationValidatorHandler extends ValidatorHandlerImpl { diff --git a/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java b/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java index 524e2ee71..36b4acadd 100644 --- a/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java +++ b/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java @@ -37,7 +37,9 @@ import org.slf4j.LoggerFactory; /** * OrganizationValidatorHandler * - * Validates data supplied when attempting to create and/or update Organization records. + * Performs validation when making requests related to Organization records. + * As an example, you can modify this class to customize validation of + * payloads supplied in requests to create and/or update records. */ public class OrganizationValidatorHandler extends ValidatorHandlerImpl { diff --git a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java index 19ba74b1a..2066c5c11 100644 --- a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java +++ b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java @@ -36,10 +36,9 @@ import org.slf4j.LoggerFactory; /** * PersonValidatorHandler * - * Validates data supplied when attempting to create and/or update Person - * records. - * - * $LastChangedRevision: $ $LastChangedDate: $ + * Performs validation when making requests related to Person records. + * As an example, you can modify this class to customize validation of + * payloads supplied in requests to create and/or update records. */ public class PersonValidatorHandler extends ValidatorHandlerImpl { diff --git a/services/place/service/src/main/java/org/collectionspace/services/place/nuxeo/PlaceValidatorHandler.java b/services/place/service/src/main/java/org/collectionspace/services/place/nuxeo/PlaceValidatorHandler.java index bbe1f568c..d5c2c5d87 100644 --- a/services/place/service/src/main/java/org/collectionspace/services/place/nuxeo/PlaceValidatorHandler.java +++ b/services/place/service/src/main/java/org/collectionspace/services/place/nuxeo/PlaceValidatorHandler.java @@ -6,7 +6,7 @@ * http://www.collectionspace.org * http://wiki.collectionspace.org - * Copyright 2009 University of California at Berkeley + * Copyright 2009 University of California, Berkeley * Licensed under the Educational Community License (ECL), Version 2.0. * You may not use this file except in compliance with this License. @@ -20,103 +20,130 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - *//** - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - - * http://www.collectionspace.org - * http://wiki.collectionspace.org - - * Copyright 2009 University of California at Berkeley - - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - - * You may obtain a copy of the ECL 2.0 License at - - * https://source.collectionspace.org/collection-space/LICENSE.txt - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. */ + package org.collectionspace.services.place.nuxeo; +import java.util.List; import java.util.regex.Pattern; - -import org.collectionspace.services.place.PlacesCommon; -import org.collectionspace.services.common.context.MultipartServiceContext; -import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.document.DocumentHandler.Action; +import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.document.InvalidDocumentException; -import org.collectionspace.services.common.document.ValidatorHandler; +import org.collectionspace.services.common.document.ValidatorHandlerImpl; +import org.collectionspace.services.place.PlaceTermGroup; +import org.collectionspace.services.place.PlaceTermGroupList; +import org.collectionspace.services.place.PlacesCommon; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * @author + * PlaceValidatorHandler + * + * Performs validation when making requests related to Place records. + * As an example, you can modify this class to customize validation of + * payloads supplied in requests to create and/or update records. */ -public class PlaceValidatorHandler implements ValidatorHandler { +public class PlaceValidatorHandler extends ValidatorHandlerImpl { final Logger logger = LoggerFactory.getLogger(PlaceValidatorHandler.class); - private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches() + // 'Bad pattern' for shortIdentifiers matches any non-word characters + private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]"); //.matcher(input).matches() + private static final String VALIDATION_ERROR = "The record payload was invalid. See log file for more details."; + private static final String SHORT_ID_BAD_CHARS_ERROR = + "shortIdentifier must only contain standard word characters"; + private static final String HAS_NO_TERMS_ERROR = + "Authority items must contain at least one term."; + private static final String HAS_AN_EMPTY_TERM_ERROR = + "Each term group in an authority item must contain " + + "a non-empty term name or " + + "a non-empty term display name."; @Override - public void validate(Action action, ServiceContext ctx) - throws InvalidDocumentException { - if(logger.isDebugEnabled()) { - logger.debug("validate() action=" + action.name()); - } + protected Class getCommonPartClass() { + return PlacesCommon.class; + } - // Bail out if the validation action is for delete. - if (action.equals(Action.DELETE)) { - return; - } - - try { - MultipartServiceContext mctx = (MultipartServiceContext) ctx; - PlacesCommon place = (PlacesCommon) mctx.getInputPart(mctx.getCommonPartLabel(), - PlacesCommon.class); - String msg = ""; - boolean invalid = false; - - // Validation occurring on both creates and updates - String displayName = place.getDisplayName(); - if (!place.isDisplayNameComputed() && ((displayName == null) || displayName.trim().isEmpty())) { - invalid = true; - msg += "displayName must be non-null and non-blank if displayNameComputed is false"; + @Override + protected void handleCreate() throws InvalidDocumentException { + PlacesCommon place = (PlacesCommon) getCommonPart(); + // No guarantee that there is a common part in every post/update. + if (place != null) { + try { + String shortId = place.getShortIdentifier(); + if (shortId != null) { + CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR); + } + CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR); + CS_ASSERT(allTermsContainNameOrDisplayName(place), HAS_AN_EMPTY_TERM_ERROR); + } catch (AssertionError e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); + } + throw new InvalidDocumentException(VALIDATION_ERROR, e); } + } + } - // Validation specific to creates or updates - if (action.equals(Action.CREATE)) { - String shortId = place.getShortIdentifier(); - // Per CSPACE-2215, shortIdentifier values that are null (missing) - // oe the empty string are now legally accepted in create payloads. - // In either of those cases, a short identifier will be synthesized from - // a display name or supplied in another manner. - if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) { - invalid = true; - msg += "shortIdentifier must only contain standard word characters"; + @Override + protected void handleGet() throws InvalidDocumentException { + } + + @Override + protected void handleGetAll() throws InvalidDocumentException { + } + + @Override + protected void handleUpdate() throws InvalidDocumentException { + PlacesCommon place = (PlacesCommon) getCommonPart(); + // No guarantee that there is a common part in every post/update. + if (place != null) { + try { + // shortIdentifier is among a set of fields that are + // prevented from being changed on an update, and thus + // we don't need to check its value here. + CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR); + CS_ASSERT(allTermsContainNameOrDisplayName(place), HAS_AN_EMPTY_TERM_ERROR); + } catch (AssertionError e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); } - } else if (action.equals(Action.UPDATE)) { + throw new InvalidDocumentException(VALIDATION_ERROR, e); } + } + } - if (invalid) { - logger.error(msg); - throw new InvalidDocumentException(msg); + @Override + protected void handleDelete() throws InvalidDocumentException { + } + + private boolean shortIdentifierContainsOnlyValidChars(String shortId) { + // Check whether any characters match the 'bad' pattern + if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) { + return false; + } + return true; + } + + private boolean containsAtLeastOneTerm(PlacesCommon place) { + PlaceTermGroupList termGroupList = place.getPlaceTermGroupList(); + if (termGroupList == null) { + return false; + } + List termGroups = termGroupList.getPlaceTermGroup(); + if ((termGroups == null) || (termGroups.isEmpty())){ + return false; + } + return true; + } + + private boolean allTermsContainNameOrDisplayName(PlacesCommon place) { + PlaceTermGroupList termGroupList = place.getPlaceTermGroupList(); + List termGroups = termGroupList.getPlaceTermGroup(); + for (PlaceTermGroup termGroup : termGroups) { + if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) { + return false; } - } catch (InvalidDocumentException ide) { - throw ide; - } catch (Exception e) { - throw new InvalidDocumentException(e); } + return true; } } + -- 2.47.3