From cbabf8e5455fe77c494ecfa2a3394a378b02ac55 Mon Sep 17 00:00:00 2001 From: Sara Aspery Date: Tue, 27 Jun 2017 12:48:59 +0100 Subject: [PATCH] updated serialId in new RecordMissingMetadataException --- .../requests/gscore/api/RecordsAPI.java | 10 ++ .../action/impl/DeclareRecordAction.java | 28 +--- .../RecordMissingMetadataException.java | 63 +++++++++ .../record/RecordServiceImpl.java | 132 ++++++++++-------- .../api/records/RecordsEntityResource.java | 10 +- 5 files changed, 165 insertions(+), 78 deletions(-) create mode 100644 rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordMissingMetadataException.java diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/gscore/api/RecordsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/gscore/api/RecordsAPI.java index e39c8034be..6864754edb 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/gscore/api/RecordsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/gscore/api/RecordsAPI.java @@ -139,6 +139,16 @@ public class RecordsAPI extends RMModelRequest )); } + /** + * see {@link #completeRecord(String, String) + */ + public Record completeRecord(String recordId) throws Exception + { + mandatoryString("recordId", recordId); + + return completeRecord(recordId, EMPTY); + } + /** * Complete the record recordId * diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java index c468f109ac..2042701e27 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java @@ -27,10 +27,8 @@ package org.alfresco.module.org_alfresco_module_rm.action.impl; -import java.util.HashMap; -import java.util.Map; - import org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase; +import org.alfresco.module.org_alfresco_module_rm.record.RecordMissingMetadataException; import org.alfresco.module.org_alfresco_module_rm.record.RecordService; import org.alfresco.repo.action.executer.ActionExecuterAbstractBase; import org.alfresco.repo.node.integrity.IntegrityException; @@ -74,27 +72,15 @@ public class DeclareRecordAction extends RMActionExecuterAbstractBase { recordService.complete(actionedUponNodeRef); } + catch (RecordMissingMetadataException e) + { + action.setParameterValue(ActionExecuterAbstractBase.PARAM_RESULT, MISSING_PROPERTIES); + } catch (IntegrityException e) { - Map errorMap = getErrorMessageMap(); - if (e.getMsgId().equals(errorMap.get(MISSING_PROPERTIES))) - { - action.setParameterValue(ActionExecuterAbstractBase.PARAM_RESULT, MISSING_PROPERTIES); - } + // IntegrityException is deliberately ignored here, there should be no action taken } } - - /** - * TODO: needs to be properties file - * Temporary Helper method to get the map of complete record error message keys with associated message text - * @return errorMap - */ - private Map getErrorMessageMap() { - Map errorMap = new HashMap<>(); - errorMap.put("missingProperties", "The record has missing mandatory properties."); - errorMap.put("alreadyCompleted", "The record is already completed."); - errorMap.put("unsuitableNode", "The node is not a record or the record does not exist or is frozen."); - return errorMap; - } + } diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordMissingMetadataException.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordMissingMetadataException.java new file mode 100644 index 0000000000..74fbacd567 --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordMissingMetadataException.java @@ -0,0 +1,63 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ + +package org.alfresco.module.org_alfresco_module_rm.record; + +import java.io.Serializable; + +import org.alfresco.error.AlfrescoRuntimeException; + +/** + * Record missing metadata exception class + * + * @author Sara Aspery + * @since 2.6 + */ +public class RecordMissingMetadataException extends AlfrescoRuntimeException +{ + private static final long serialVersionUID = 3437687270967611234L; + + public RecordMissingMetadataException(String msgId, Throwable cause) + { + super(msgId, cause); + } + + public RecordMissingMetadataException(String msgId, Object[] msgParams, Throwable cause) + { + super(msgId, msgParams, cause); + } + + public RecordMissingMetadataException(String msgId, Object[] msgParams) + { + super(msgId, msgParams); + } + + public RecordMissingMetadataException(String msgId) + { + super(msgId); + } +} diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordServiceImpl.java index 3dc1c547b9..f6c42d8f85 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordServiceImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/record/RecordServiceImpl.java @@ -1992,7 +1992,8 @@ public class RecordServiceImpl extends BaseBehaviourBean return null; } }); - } finally + } + finally { enablePropertyEditableCheck(); } @@ -2005,37 +2006,87 @@ public class RecordServiceImpl extends BaseBehaviourBean * @throws Exception if node not valid for completion */ private void validateForCompletion(NodeRef nodeRef) { - if (nodeService.exists(nodeRef) && isRecord(nodeRef) && !freezeService.isFrozen(nodeRef)) + if (!nodeService.exists(nodeRef)) { - if (!isDeclared(nodeRef)) - { - // if the record is newly created make sure the record identifier is set before completing the record - Set newRecords = transactionalResourceHelper.getSet(RecordServiceImpl.KEY_NEW_RECORDS); - if (newRecords.contains(nodeRef)) - { - generateRecordIdentifier(nodeService, identifierService, nodeRef); - } + logError(nodeRef); + throw new IntegrityException("The record does not exist.", null); + } + + if (!isRecord(nodeRef)) + { + logError(nodeRef); + throw new IntegrityException("The node is not a record.", null); + } + + if (freezeService.isFrozen(nodeRef)) + { + logError(nodeRef); + throw new IntegrityException("The record is frozen.", null); + } - List missingProperties = new ArrayList<>(5); - // Aspect not already defined - check mandatory properties then add - if (checkMandatoryPropertiesEnabled && !mandatoryPropertiesSet(nodeRef, missingProperties)) - { - LOGGER.debug(buildMissingPropertiesErrorString(missingProperties)); - throw new IntegrityException("The record has missing mandatory properties.", null); - } - } else - { - throw new IntegrityException("The record is already completed.", null); - } - } else + if (isDeclared(nodeRef)) { - if (LOGGER.isWarnEnabled()) + throw new IntegrityException("The record is already completed.", null); + } + + // if the record is newly created make sure the record identifier is set before completing the record + Set newRecords = transactionalResourceHelper.getSet(RecordServiceImpl.KEY_NEW_RECORDS); + if (newRecords.contains(nodeRef)) + { + generateRecordIdentifier(nodeService, identifierService, nodeRef); + } + + // Validate that all mandatory properties, if any, are present + List missingProperties = new ArrayList<>(5); + // Aspect not already defined - check mandatory properties then add + if (checkMandatoryPropertiesEnabled) + { + Map nodeRefProps = nodeService.getProperties(nodeRef); + QName nodeRefType = nodeService.getType(nodeRef); + + // check for missing mandatory metadata from type definitions + TypeDefinition typeDef = dictionaryService.getType(nodeRefType); + checkDefinitionMandatoryPropsSet(typeDef, nodeRefProps, missingProperties); + + // check for missing mandatory metadata from aspect definitions + Set aspects = nodeService.getAspects(nodeRef); + for (QName aspect : aspects) { - LOGGER.warn(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, nodeRef.toString())); + AspectDefinition aspectDef = dictionaryService.getAspect(aspect); + checkDefinitionMandatoryPropsSet(aspectDef, nodeRefProps, missingProperties); + } + + // check for missing mandatory metadata from custom aspect definitions + QName customAspect = getCustomAspectImpl(nodeRefType); + AspectDefinition aspectDef = dictionaryService.getAspect(customAspect); + checkDefinitionMandatoryPropsSet(aspectDef, nodeRefProps, missingProperties); + + if (!missingProperties.isEmpty()) + { + LOGGER.debug(buildMissingPropertiesErrorString(missingProperties)); + throw new RecordMissingMetadataException("The record has missing mandatory properties."); } - throw new IntegrityException("The node is not a record or the record does not exist or is frozen.", null); } } + + /** + * Helper method to log a warning to the log file + * + * @param nodeRef node for which error occurred + */ + private void logError(NodeRef nodeRef) { + if (LOGGER.isWarnEnabled()) + { + LOGGER.warn(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, nodeRef.toString())); + } + } + + /** + * Helper method to build single string containing list of missing properties + * + * @param missingProperties list of missing properties + * @return String of missing properties + */ private String buildMissingPropertiesErrorString(List missingProperties) { StringBuilder builder = new StringBuilder(255); @@ -2048,37 +2099,6 @@ public class RecordServiceImpl extends BaseBehaviourBean return builder.toString(); } - /** - * Helper method to check whether all the mandatory properties of the node have been set - * - * @param nodeRef node reference - * @return boolean true if all mandatory properties are set, false otherwise - */ - private boolean mandatoryPropertiesSet(final NodeRef nodeRef, final List missingProperties) - { - Map nodeRefProps = nodeService.getProperties(nodeRef); - QName nodeRefType = nodeService.getType(nodeRef); - - // check for missing mandatory metadata from type definitions - TypeDefinition typeDef = dictionaryService.getType(nodeRefType); - checkDefinitionMandatoryPropsSet(typeDef, nodeRefProps, missingProperties); - - // check for missing mandatory metadata from aspect definitions - Set aspects = nodeService.getAspects(nodeRef); - for (QName aspect : aspects) - { - AspectDefinition aspectDef = dictionaryService.getAspect(aspect); - checkDefinitionMandatoryPropsSet(aspectDef, nodeRefProps, missingProperties); - } - - // check for missing mandatory metadata from custom aspect definitions - QName customAspect = getCustomAspectImpl(nodeRefType); - AspectDefinition aspectDef = dictionaryService.getAspect(customAspect); - checkDefinitionMandatoryPropsSet(aspectDef, nodeRefProps, missingProperties); - - return missingProperties.isEmpty(); - } - /** * Helper method to check whether all the definition mandatory properties of the node have been set * diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java index fe77d89ccc..daf676ecdb 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java @@ -31,6 +31,7 @@ import static org.alfresco.module.org_alfresco_module_rm.util.RMParameterCheck.c import static org.alfresco.util.ParameterCheck.mandatory; import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; +import org.alfresco.module.org_alfresco_module_rm.record.RecordMissingMetadataException; import org.alfresco.module.org_alfresco_module_rm.record.RecordService; import org.alfresco.repo.activities.ActivityType; import org.alfresco.repo.node.integrity.IntegrityException; @@ -241,7 +242,14 @@ public class RecordsEntityResource implements BinaryResourceAction.Read, NodeRef record = apiUtils.validateRecord(recordId); // Complete the record - recordService.complete(record); + try + { + recordService.complete(record); + } + catch (RecordMissingMetadataException e) + { + throw new IntegrityException("The record has missing mandatory properties.", null); + } // return record state FileInfo info = fileFolderService.getFileInfo(record);