From 312c3e14f719b5588d813aac9eacf53dc1808a7f Mon Sep 17 00:00:00 2001 From: Ana Bozianu Date: Fri, 4 Nov 2016 15:14:11 +0200 Subject: [PATCH] RM-4296 - centralized validation --- .../model/BaseBehaviourBean.java | 30 +++++++++++++++++++ .../model/rma/type/FilePlanType.java | 21 +++---------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/BaseBehaviourBean.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/BaseBehaviourBean.java index d2d21414d4..80eaf57274 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/BaseBehaviourBean.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/BaseBehaviourBean.java @@ -27,16 +27,22 @@ package org.alfresco.module.org_alfresco_module_rm.model; +import java.security.InvalidParameterException; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.module.org_alfresco_module_rm.util.ServiceBaseImpl; import org.alfresco.repo.policy.BehaviourFilter; import org.alfresco.repo.policy.annotation.BehaviourRegistry; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.namespace.QName; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.google.common.collect.Sets; + /** * Convenient base class for behaviour beans. * @@ -87,4 +93,28 @@ public abstract class BaseBehaviourBean extends ServiceBaseImpl return behaviours.get(name); } + /** + * Helper method that checks if the newly created child association complies with the RM rules + * @param parent the parent node + * @param childType the child node + * @param acceptedUniqueChildType a list of node types that are accepted as children of the provided parent only once + * @param acceptedMultipleChildType a list of node types that are accepted as children of the provided parent multiple times + * @throws InvalidParameterException if the child association doesn't comply with the RM rules + */ + protected void validateNewChildAssociation(NodeRef parent, NodeRef child, List acceptedUniqueChildType, List acceptedMultipleChildType) throws InvalidParameterException + { + QName childType = getInternalNodeService().getType(child); + if(acceptedUniqueChildType.contains(childType)) + { + // check the user is not trying to create multiple children of a type that is only accepted once + if(nodeService.getChildAssocs(parent, Sets.newHashSet(childType)).size() > 1) + { + throw new InvalidParameterException("Operation failed. Multiple children of this type are not allowed."); + } + } + else if(!acceptedMultipleChildType.contains(childType)) + { + throw new InvalidParameterException("Operation failed. Children of type " + childType + " are not allowed"); + } + } } diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/type/FilePlanType.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/type/FilePlanType.java index a25f6f9e11..841a1db57b 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/type/FilePlanType.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/type/FilePlanType.java @@ -66,6 +66,9 @@ public class FilePlanType extends BaseBehaviourBean NodeServicePolicies.OnCreateNodePolicy, NodeServicePolicies.OnDeleteNodePolicy { + private static List ACCEPTED_UNIQUE_CHILD_TYPES = Arrays.asList(TYPE_HOLD_CONTAINER, TYPE_TRANSFER_CONTAINER, TYPE_UNFILED_RECORD_CONTAINER); + private static List ACCEPTED_NON_UNIQUE_CHILD_TYPES = Arrays.asList(TYPE_RECORD_CATEGORY); + /** file plan service */ private FilePlanService filePlanService; @@ -155,29 +158,13 @@ public class FilePlanType extends BaseBehaviourBean // ensure we only add categories and special containers as fileplan children NodeRef child = childAssocRef.getChildRef(); NodeRef parent = childAssocRef.getParentRef(); - if (!getFilePlanService().isFilePlan(parent)) { return; } // list of the accepted types of fileplan children - List acceptedUniqueChildType = Arrays.asList(TYPE_HOLD_CONTAINER, TYPE_TRANSFER_CONTAINER, TYPE_UNFILED_RECORD_CONTAINER); - List acceptedMultipleChildType = Arrays.asList(TYPE_RECORD_CATEGORY); - - QName childType = getInternalNodeService().getType(child); - if(acceptedUniqueChildType.contains(childType)) - { - // check the user is not trying to create multiple children of a type that is only accepted once - if(nodeService.getChildAssocs(parent, Sets.newHashSet(childType)).size() > 1) - { - throw new InvalidParameterException("Operation failed. The fileplan already has a child of type " + childType + ". Multiple children of this type are not allowed."); - } - } - else if(!acceptedMultipleChildType.contains(childType)) - { - throw new InvalidParameterException("Operation failed. You can only place categories and special containers in the root of the file plan."); - } + validateNewChildAssociation(parent, child, ACCEPTED_UNIQUE_CHILD_TYPES, ACCEPTED_NON_UNIQUE_CHILD_TYPES); } /**