Skip to content

Commit

Permalink
Add support for input variables for script tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Feb 17, 2025
1 parent bb50957 commit df9fec8
Show file tree
Hide file tree
Showing 33 changed files with 719 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public interface BpmnXMLConstants {
public static final String ATTRIBUTE_TASK_SCRIPT_RESULTVARIABLE = "resultVariable";
public static final String ATTRIBUTE_TASK_SCRIPT_SKIP_EXPRESSION = "skipExpression";
public static final String ATTRIBUTE_TASK_SCRIPT_AUTO_STORE_VARIABLE = "autoStoreVariables";
public static final String ATTRIBUTE_TASK_SCRIPT_DO_NOT_INCLUDE_VARIABLES = "doNotIncludeVariables";

public static final String ATTRIBUTE_TASK_SERVICE_CLASS = "class";
public static final String ATTRIBUTE_TASK_SERVICE_EXPRESSION = "expression";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.converter.child.BaseChildElementParser;
import org.flowable.bpmn.converter.child.InParameterParser;
import org.flowable.bpmn.converter.child.ScriptTextParser;
import org.flowable.bpmn.converter.util.BpmnXMLUtil;
import org.flowable.bpmn.model.BaseElement;
Expand All @@ -39,14 +40,17 @@ public class ScriptTaskXMLConverter extends BaseBpmnXMLConverter {
new ExtensionAttribute(ATTRIBUTE_TASK_SCRIPT_RESULTVARIABLE),
new ExtensionAttribute(ATTRIBUTE_TASK_SERVICE_RESULT_VARIABLE_NAME),
new ExtensionAttribute(ATTRIBUTE_TASK_SCRIPT_SKIP_EXPRESSION),
new ExtensionAttribute(ATTRIBUTE_TASK_SCRIPT_AUTO_STORE_VARIABLE)
new ExtensionAttribute(ATTRIBUTE_TASK_SCRIPT_AUTO_STORE_VARIABLE),
new ExtensionAttribute(ATTRIBUTE_TASK_SCRIPT_DO_NOT_INCLUDE_VARIABLES)
);

protected Map<String, BaseChildElementParser> childParserMap = new HashMap<>();

public ScriptTaskXMLConverter() {
ScriptTextParser scriptTextParser = new ScriptTextParser();
childParserMap.put(scriptTextParser.getElementName(), scriptTextParser);
InParameterParser inParameterParser = new InParameterParser();
childParserMap.put(inParameterParser.getElementName(), inParameterParser);
}

@Override
Expand Down Expand Up @@ -78,6 +82,8 @@ protected BaseElement convertXMLToElement(XMLStreamReader xtr, BpmnModel model)
scriptTask.setAutoStoreVariables(Boolean.valueOf(autoStoreVariables));
}

scriptTask.setDoNotIncludeVariables(Boolean.parseBoolean(BpmnXMLUtil.getAttributeValue(ATTRIBUTE_TASK_SCRIPT_DO_NOT_INCLUDE_VARIABLES, xtr)));

BpmnXMLUtil.addCustomAttributes(xtr, scriptTask, defaultElementAttributes, defaultActivityAttributes, defaultScriptTaskAttributes);

parseChildElements(getXMLElementName(), scriptTask, childParserMap, model, xtr);
Expand All @@ -93,11 +99,23 @@ protected void writeAdditionalAttributes(BaseElement element, BpmnModel model, X
writeQualifiedAttribute(ATTRIBUTE_TASK_SCRIPT_RESULTVARIABLE, scriptTask.getResultVariable(), xtw);
writeQualifiedAttribute(ATTRIBUTE_TASK_SCRIPT_SKIP_EXPRESSION, scriptTask.getSkipExpression(), xtw);
writeQualifiedAttribute(ATTRIBUTE_TASK_SCRIPT_AUTO_STORE_VARIABLE, String.valueOf(scriptTask.isAutoStoreVariables()), xtw);
if (scriptTask.isDoNotIncludeVariables()) {
writeQualifiedAttribute(ATTRIBUTE_TASK_SCRIPT_DO_NOT_INCLUDE_VARIABLES, "true", xtw);
}

BpmnXMLUtil.writeCustomAttributes(scriptTask.getAttributes().values(), xtw, defaultElementAttributes,
defaultActivityAttributes, defaultScriptTaskAttributes);
}

@Override
protected boolean writeExtensionChildElements(BaseElement element, boolean didWriteExtensionStartElement, XMLStreamWriter xtw) throws Exception {
ScriptTask scriptTask = (ScriptTask) element;
didWriteExtensionStartElement = super.writeExtensionChildElements(element, didWriteExtensionStartElement, xtw);
didWriteExtensionStartElement = BpmnXMLUtil.writeIOParameters(ELEMENT_IN_PARAMETERS,
scriptTask.getInParameters(), didWriteExtensionStartElement, xtw);
return didWriteExtensionStartElement;
}

@Override
protected void writeAdditionalChildElements(BaseElement element, BpmnModel model, XMLStreamWriter xtw) throws Exception {
ScriptTask scriptTask = (ScriptTask) element;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.flowable.editor.language.xml;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.IOParameter;
import org.flowable.bpmn.model.ScriptTask;
import org.flowable.editor.language.xml.util.BpmnXmlConverterTest;

/**
* @author Filip Hrisafov
*/
class ScriptTaskConverterTest {

@BpmnXmlConverterTest("scriptTask/script-task-input-parameters.xml")
void scriptTaskWithInputParameters(BpmnModel model) {
FlowElement element = model.getMainProcess().getFlowElement("script1");
assertThat(element)
.isInstanceOfSatisfying(ScriptTask.class, scriptTask -> {
assertThat(scriptTask.isDoNotIncludeVariables()).isFalse();
assertThat(scriptTask.getInParameters())
.extracting(IOParameter::getSource, IOParameter::getSourceExpression, IOParameter::getTarget)
.containsExactlyInAnyOrder(
tuple("aVar", null, "targetAVar"),
tuple(null, "${'test'}", "targetVar")
);
});

}

@BpmnXmlConverterTest("scriptTask/script-task-do-not-include-variables.xml")
void scriptTaskWithDoNotIncludeVariables(BpmnModel model) {
FlowElement element = model.getMainProcess().getFlowElement("script1");
assertThat(element)
.isInstanceOfSatisfying(ScriptTask.class, scriptTask -> {
assertThat(scriptTask.isDoNotIncludeVariables()).isTrue();
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ 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.
-->

<definitions id="definitions"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">

<process id="testInputVariables">

<startEvent id="theStart"/>
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="script1"/>

<scriptTask id="script1" scriptFormat="JavaScript" flowable:doNotIncludeVariables="true">
<script><![CDATA[
var sum = a + b;
execution.setVariable("sum", sum);
]]></script>
</scriptTask>
<sequenceFlow id="flow2" sourceRef="script1" targetRef="theEnd"/>

<endEvent id="theEnd"/>

</process>

</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ 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.
-->

<definitions id="definitions"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">

<process id="testInputVariables">

<startEvent id="theStart"/>
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="script1"/>

<scriptTask id="script1" scriptFormat="JavaScript">
<extensionElements>
<flowable:in source="aVar" target="targetAVar"/>
<flowable:in sourceExpression="${'test'}" target="targetVar"/>
</extensionElements>
<script><![CDATA[
var sum = a + b;
execution.setVariable("sum", sum);
]]></script>
</scriptTask>
<sequenceFlow id="flow2" sourceRef="script1" targetRef="theEnd"/>
<endEvent id="theEnd"/>

</process>

</definitions>
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
*/
package org.flowable.bpmn.model;

import java.util.ArrayList;
import java.util.List;

/**
* @author Tijs Rademakers
* @author Joram Barrez
*/
public class ScriptTask extends Task {
public class ScriptTask extends Task implements HasInParameters {

protected String scriptFormat;
protected String script;
protected String resultVariable;
protected String skipExpression;
protected boolean autoStoreVariables; // see https://activiti.atlassian.net/browse/ACT-1626

protected boolean doNotIncludeVariables = false;
protected List<IOParameter> inParameters;

public String getScriptFormat() {
return scriptFormat;
}
Expand Down Expand Up @@ -64,6 +70,32 @@ public void setAutoStoreVariables(boolean autoStoreVariables) {
this.autoStoreVariables = autoStoreVariables;
}

public boolean isDoNotIncludeVariables() {
return doNotIncludeVariables;
}

public void setDoNotIncludeVariables(boolean doNotIncludeVariables) {
this.doNotIncludeVariables = doNotIncludeVariables;
}

@Override
public List<IOParameter> getInParameters() {
return inParameters;
}

@Override
public void addInParameter(IOParameter inParameter) {
if (inParameters == null) {
inParameters = new ArrayList<>();
}
inParameters.add(inParameter);
}

@Override
public void setInParameters(List<IOParameter> inParameters) {
this.inParameters = inParameters;
}

@Override
public ScriptTask clone() {
ScriptTask clone = new ScriptTask();
Expand All @@ -78,5 +110,14 @@ public void setValues(ScriptTask otherElement) {
setResultVariable(otherElement.getResultVariable());
setSkipExpression(otherElement.getSkipExpression());
setAutoStoreVariables(otherElement.isAutoStoreVariables());
setDoNotIncludeVariables(otherElement.isDoNotIncludeVariables());

inParameters = null;
if (otherElement.getInParameters() != null && !otherElement.getInParameters().isEmpty()) {
inParameters = new ArrayList<>();
for (IOParameter parameter : otherElement.getInParameters()) {
inParameters.add(parameter.clone());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public interface CmmnXmlConstants {
String ATTRIBUTE_REPETITION_ELEMENT_INDEX_VARIABLE_NAME = "elementIndexVariable";

String ATTRIBUTE_TASK_SCRIPT_AUTO_STORE_VARIABLE = "autoStoreVariables";
String ATTRIBUTE_TASK_SCRIPT_DO_NOT_INCLUDE_VARIABLES = "doNotIncludeVariables";

String ATTRIBUTE_EVENT_LISTENER_TYPE = "eventType"; // Note that this is the same as ELEMENT_EVENT_TYPE. We can't change this (backwards compatibility)
String ATTRIBUTE_EVENT_LISTENER_AVAILABLE_CONDITION = "availableCondition";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ protected Task convertToScriptTask(XMLStreamReader xtr) {
scriptTask.setAutoStoreVariables(Boolean.valueOf(autoStoreVariables));
}

scriptTask.setDoNotIncludeVariables(Boolean.parseBoolean(
xtr.getAttributeValue(CmmnXmlConstants.FLOWABLE_EXTENSIONS_NAMESPACE, CmmnXmlConstants.ATTRIBUTE_TASK_SCRIPT_DO_NOT_INCLUDE_VARIABLES)));

return scriptTask;
}

Expand Down
Loading

0 comments on commit df9fec8

Please sign in to comment.