From 88dc888f7b971b5b5487406dfcae05fbe351f5cc Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Tue, 4 Oct 2011 16:56:51 +0000 Subject: [PATCH] CSPACE-3178,CSPACE-2215: Payloads to create authority or voculabulary resources now no longer require short identifiers. Short identifiers will be generated from display names, consistent with handling of vocabulary or authority item payloads. Created utility method to isolate logic around identifier generation, to facilitate future work on CSPACE-2215. --- .../nuxeo/AuthorityDocumentModelHandler.java | 12 ++++ .../nuxeo/AuthorityIdentifierUtils.java | 67 +++++++++++++++++++ .../AuthorityItemDocumentModelHandler.java | 27 +------- .../nuxeo/OrgAuthorityValidatorHandler.java | 12 ++-- .../PersonAuthorityValidatorHandler.java | 12 ++-- 5 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityIdentifierUtils.java diff --git a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java index 8d923fd10..9235008b3 100644 --- a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java +++ b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java @@ -26,6 +26,7 @@ package org.collectionspace.services.common.vocabulary.nuxeo; import java.util.Map; import org.collectionspace.services.common.api.RefName; +import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.document.DocumentWrapper; import org.collectionspace.services.common.service.ObjectPartType; @@ -75,9 +76,20 @@ public abstract class AuthorityDocumentModelHandler // CSPACE-3178: // Uncomment once debugged and App layer is read to integrate // Experimenting with this uncommented now ... + handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName); updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178 } + private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception { + String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER); + String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME); + String shortDisplayName = ""; + if (Tools.isEmpty(shortIdentifier)) { + String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName); + docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier); + } + } + protected void updateRefnameForAuthority(DocumentWrapper wrapDoc, String schemaName) throws Exception { DocumentModel docModel = wrapDoc.getWrappedObject(); String suppliedRefName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.REF_NAME); diff --git a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityIdentifierUtils.java b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityIdentifierUtils.java new file mode 100644 index 000000000..6389a5164 --- /dev/null +++ b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityIdentifierUtils.java @@ -0,0 +1,67 @@ +/** + * 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. + */ +package org.collectionspace.services.common.vocabulary.nuxeo; + +import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.api.Tools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * AuthorityIdentifierUtils + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class AuthorityIdentifierUtils { + + private final Logger logger = LoggerFactory.getLogger(AuthorityIdentifierUtils.class); + + // CSPACE-2215 + // FIXME: Consider replacing this with a different algorithm, perhaps one + // that combines stems of each word token in the displayname. + // FIXME: Verify uniqueness before returning the generated short identifier. + // FIXME: Consider using a hash of the display name, rather than a timestamp, + // when it is necessary to add a suffix for uniqueness. + protected static String generateShortIdentifierFromDisplayName(String displayName, String shortDisplayName) { + String generatedShortIdentifier = ""; + if (Tools.notEmpty(displayName)) { + generatedShortIdentifier = displayName + '-' + Tools.now().toString(); + } else if (Tools.notEmpty(shortDisplayName)) { + generatedShortIdentifier = shortDisplayName + '-' + Tools.now().toString(); + } + // Ensure that the short identifier consists only of word chars. + if (Tools.notEmpty(generatedShortIdentifier)) { + generatedShortIdentifier = generatedShortIdentifier.replaceAll("[^\\w]", ""); + } + // Fallback if we can't generate a short identifier from the displayname(s). + if (generatedShortIdentifier.isEmpty()) { + generatedShortIdentifier = java.util.UUID.randomUUID().toString(); + } + return generatedShortIdentifier; + } + + +} diff --git a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 4a8ea3ad4..8696ea6f0 100644 --- a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -42,7 +42,6 @@ import org.collectionspace.services.common.repository.RepositoryClientFactory; import org.collectionspace.services.common.service.ObjectPartType; import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema; import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema; -import org.collectionspace.services.common.vocabulary.AuthorityResource; import org.collectionspace.services.nuxeo.client.java.DocHandlerBase; import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.collectionspace.services.relation.RelationResource; @@ -138,35 +137,11 @@ public abstract class AuthorityItemDocumentModelHandler // Do nothing on exception. Some vocabulary schemas may not include a short display name. } if (Tools.isEmpty(shortIdentifier)) { - String generatedShortIdentifier = generateShortIdentifierFromDisplayName(displayName, shortDisplayName); + String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName); docModel.setProperty(schemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier); } } - // CSPACE-2215 - // FIXME: Consider replacing this with a different algorithm, perhaps one - // that combines stems of each word token in the displayname. - // FIXME: Verify uniqueness before returning the generated short identifier. - // FIXME: Consider using a hash of the display name, rather than a timestamp, - // when it is necessary to add a suffix for uniqueness. - private String generateShortIdentifierFromDisplayName(String displayName, String shortDisplayName) { - String generatedShortIdentifier = ""; - if (Tools.notEmpty(displayName)) { - generatedShortIdentifier = displayName + '-' + Tools.now().toString(); - } else if (Tools.notEmpty(shortDisplayName)) { - generatedShortIdentifier = shortDisplayName + '-' + Tools.now().toString(); - } - // Ensure that the short identifier consists only of word chars. - if (Tools.notEmpty(generatedShortIdentifier)) { - generatedShortIdentifier = generatedShortIdentifier.replaceAll("[^\\w]", ""); - } - // Fallback if we can't generate a short identifier from the displayname(s). - if (generatedShortIdentifier.isEmpty()) { - generatedShortIdentifier = java.util.UUID.randomUUID().toString(); - } - return generatedShortIdentifier; - } - protected void updateRefnameForAuthorityItem(DocumentWrapper wrapDoc, String schemaName, String authorityRefBaseName) throws Exception { diff --git a/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrgAuthorityValidatorHandler.java b/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrgAuthorityValidatorHandler.java index 09a4aa0bc..867baf1ab 100644 --- a/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrgAuthorityValidatorHandler.java +++ b/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrgAuthorityValidatorHandler.java @@ -63,13 +63,11 @@ public class OrgAuthorityValidatorHandler implements ValidatorHandler { // Create-specific validation here if (action.equals(Action.CREATE)) { String shortId = organizationAuth.getShortIdentifier(); - if (shortId == null) { - invalid = true; - msg += "shortIdentifier must be non-null"; - } else if (shortId.trim().isEmpty()) { - invalid = true; - msg += "shortIdentifier must have a non-empty value"; - } else if (shortIdBadPattern.matcher(shortId).find()) { + // 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"; } diff --git a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonAuthorityValidatorHandler.java b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonAuthorityValidatorHandler.java index 48c4ff7c6..6fb6ac67d 100644 --- a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonAuthorityValidatorHandler.java +++ b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonAuthorityValidatorHandler.java @@ -64,13 +64,11 @@ public class PersonAuthorityValidatorHandler implements ValidatorHandler { // Validation specific to creates or updates if (action.equals(Action.CREATE)) { String shortId = personAuth.getShortIdentifier(); - if (shortId == null) { - invalid = true; - msg += "shortIdentifier must be non-null"; - } else if (shortId.trim().isEmpty()) { - invalid = true; - msg += "shortIdentifier must have a non-empty value"; - } else if (shortIdBadPattern.matcher(shortId).find()) { + // 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"; } -- 2.47.3