diff --git a/source/java/org/alfresco/repo/workflow/activiti/ActivitiScriptNode.java b/source/java/org/alfresco/repo/workflow/activiti/ActivitiScriptNode.java index 3ee5638493..3b8326687b 100644 --- a/source/java/org/alfresco/repo/workflow/activiti/ActivitiScriptNode.java +++ b/source/java/org/alfresco/repo/workflow/activiti/ActivitiScriptNode.java @@ -2,7 +2,7 @@ * #%L * Alfresco Repository * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited + * 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 @@ -66,7 +66,7 @@ public class ActivitiScriptNode extends ScriptNode } /** - * Value converter for beanshell. Dates should be handled differenty since + * Value converter for beanshell. Dates should be handled differently since * default conversion uses top-level scope which is sometimes missing. */ private class ActivitiNodeConverter extends NodeValueConverter @@ -87,11 +87,15 @@ public class ActivitiScriptNode extends ScriptNode @Override public Serializable convertValueForScript(ServiceRegistry serviceRegistry, Scriptable theScope, QName qname, Serializable value) { - // ALF-14863: If script-node is used outside of Script-call (eg. Activiti evaluating an expression that contains variables of type ScriptNode) - // a scope should be created solely for this conversion. The scope will ALWAYS be set when value-conversion is called from the - // ScriptProcessor - ensureScopePresent(); - + // ALF-14863: If script-node is used outside of Script-call (eg. Activiti evaluating an expression that contains variables of type ScriptNode) + // a scope should be created solely for this conversion. The scope will ALWAYS be set when value-conversion is called from the + // ScriptProcessor + ensureScopePresent(); + if (theScope == null) + { + theScope = scope; + } + if (value instanceof NodeRef) { return new ActivitiScriptNode(((NodeRef)value), serviceRegistry); @@ -106,26 +110,29 @@ public class ActivitiScriptNode extends ScriptNode } } - private void ensureScopePresent() { - if(scope == null) { - // Create a scope for the value conversion. This scope will be an empty scope exposing basic Object and Function, sufficient for value-conversion. - // In case no context is active for the current thread, we can safely enter end exit one to get hold of a scope - Context ctx = Context.getCurrentContext(); - boolean closeContext = false; - if(ctx == null) - { - ctx = Context.enter(); - closeContext = true; - } - - scope = ctx.initStandardObjects(); - scope.setParentScope(null); - - if(closeContext) { - // Only an exit call should be done when context didn't exist before - Context.exit(); - } - } - } + private void ensureScopePresent() + { + if (scope == null) + { + // Create a scope for the value conversion. This scope will be an empty scope exposing basic Object and Function, sufficient for value-conversion. + // In case no context is active for the current thread, we can safely enter end exit one to get hold of a scope + Context ctx = Context.getCurrentContext(); + boolean closeContext = false; + if (ctx == null) + { + ctx = Context.enter(); + closeContext = true; + } + + scope = ctx.initStandardObjects(); + scope.setParentScope(null); + + if (closeContext) + { + // Only an exit call should be done when context didn't exist before + Context.exit(); + } + } + } } } \ No newline at end of file diff --git a/source/test-java/org/alfresco/repo/jscript/ScriptNodeTest.java b/source/test-java/org/alfresco/repo/jscript/ScriptNodeTest.java index c3b304f1c0..7e1aa7cb57 100644 --- a/source/test-java/org/alfresco/repo/jscript/ScriptNodeTest.java +++ b/source/test-java/org/alfresco/repo/jscript/ScriptNodeTest.java @@ -2,7 +2,7 @@ * #%L * Alfresco Repository * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited + * 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 @@ -53,6 +53,7 @@ import org.alfresco.repo.tenant.TenantAdminService; import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.version.VersionableAspect; +import org.alfresco.repo.workflow.activiti.ActivitiScriptNode; import org.alfresco.scripts.ScriptException; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.dictionary.DictionaryService; @@ -707,5 +708,30 @@ public class ScriptNodeTest Context.exit(); } return scope; + } + + /** + * MNT-16053: Conversion for property with multiple=true, on an Activiti script node, fails. + */ + @Test + public void testConvertMultiplePropertyForActivitiScriptNode() + { + ArrayList numbers = new ArrayList<>(); + numbers.add("Phone #1"); + numbers.add("Phone #2"); + Repository repositoryHelper = (Repository) APP_CONTEXT_INIT.getApplicationContext() + .getBean("repositoryHelper"); + NodeRef companyHome = repositoryHelper.getCompanyHome(); + + ActivitiScriptNode scriptNode = new ActivitiScriptNode(companyHome, SERVICE_REGISTRY); + try + { + // Do a conversion of a multiple property (this is a residual property, but it doesn't matter, the conversion code is the same, regardless of the property being in the model or not). + scriptNode.getValueConverter().convertValueForScript(QName.createQName("cm:phonenumbers"), numbers); + } + catch (Exception e) + { + fail("Converting multiple property for Activiti script fails with " + e); + } } }