Skip to content

Commit

Permalink
Merge branch 'almerico-call-activiti' into origin/dev-miguelruiz-ACTI…
Browse files Browse the repository at this point in the history
…VITI-3310-variable-mapping-logic
  • Loading branch information
almerico committed Jul 9, 2019
2 parents fb956a5 + bdb5a48 commit d1b5408
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 354 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* 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.
Expand All @@ -12,12 +12,15 @@
*/
package org.activiti.runtime.api.impl;

import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.impl.bpmn.behavior.CallActivityBehavior;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.bpmn.parser.factory.ActivityBehaviorFactory;
import org.activiti.engine.impl.bpmn.parser.factory.DefaultActivityBehaviorFactory;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.spring.process.ProcessExtensionService;
import org.apache.commons.lang3.StringUtils;

/**
* Default implementation of the {@link ActivityBehaviorFactory}. Used when no custom {@link ActivityBehaviorFactory} is injected on the {@link ProcessEngineConfigurationImpl}.
Expand All @@ -30,14 +33,27 @@ public DefaultActivityBehaviorFactoryMapping(ProcessExtensionService processExte
super();
this.processExtensionService = processExtensionService;
}


@Override
public UserTaskActivityBehavior createUserTaskActivityBehavior(UserTask userTask) {
return new DefaultUserTaskBehavior(userTask,
new VariablesMappingProvider(processExtensionService));
new VariablesMappingProvider(processExtensionService));
}

@Override
public CallActivityBehavior createCallActivityBehavior(CallActivity callActivity) {

String expressionRegex = "\\$+\\{+.+\\}";
CallActivityBehavior callActivityBehaviour = null;
if (StringUtils.isNotEmpty(callActivity.getCalledElement()) && callActivity.getCalledElement().matches(expressionRegex)) {
callActivityBehaviour = new DefaultCallActivityBehavior(expressionManager.createExpression(callActivity.getCalledElement()),
callActivity.getMapExceptions(), new VariablesMappingProvider(processExtensionService));
} else {
callActivityBehaviour = new DefaultCallActivityBehavior(callActivity.getCalledElement(),
callActivity.getMapExceptions(), new VariablesMappingProvider(processExtensionService));
}
return callActivityBehaviour;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* 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.activiti.runtime.api.impl;

import java.util.List;
import java.util.Map;

import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.MapExceptionEntry;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.impl.bpmn.behavior.CallActivityBehavior;
import org.activiti.engine.impl.interceptor.CommandContext;

public class DefaultCallActivityBehavior extends CallActivityBehavior {

VariablesMappingProvider mappingProvider;

public DefaultCallActivityBehavior(String processDefinitionKey, List<MapExceptionEntry> mapExceptions,VariablesMappingProvider mappingProvider) {
super(processDefinitionKey,mapExceptions);
this.mappingProvider=mappingProvider;
}

public DefaultCallActivityBehavior(Expression processDefinitionExpression, List<MapExceptionEntry> mapExceptions,VariablesMappingProvider mappingProvider) {
super(processDefinitionExpression,mapExceptions);
this.mappingProvider=mappingProvider;
}

@Override
protected Map<String, Object> getInboundVariables(DelegateExecution execution) {
return mappingProvider.calculateInputVariables(execution);
}

@Override
protected Map<String, Object> getOutBoundVariables(CommandContext commandContext,
DelegateExecution execution,
Map<String, Object> taskCompleteVariables) {

return mappingProvider.calculateOutPutVariables(commandContext.getProcessEngineConfiguration().isCopyVariablesToLocalForTasks(),
execution.getProcessDefinitionId(),
execution.getCurrentActivityId(),
taskCompleteVariables);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,40 @@ public VariablesMappingProvider(ProcessExtensionService processExtensionService)
public Object calculateMappedValue(Mapping inputMapping,
DelegateExecution execution,
ProcessExtensionModel extensions) {

if (inputMapping != null) {
if (Mapping.SourceMappingType.VALUE.equals(inputMapping.getType())) {
return inputMapping.getValue();
} else {
if (Mapping.SourceMappingType.VARIABLE.equals(inputMapping.getType())) {
String name = inputMapping.getValue().toString();
//This is extra check

org.activiti.spring.process.model.VariableDefinition processVariableDefinition = extensions.getExtensions().getPropertyByName(name);
if (processVariableDefinition != null) {
return execution.getVariable(processVariableDefinition.getName());
}
//We may agree that modeller will check everything
//In this case we may use simply:
//return execution.getVariable(name);

}
//We have to check if this is needed?
else {
} else {
if (Mapping.SourceMappingType.STATIC_VALUE.equals(inputMapping.getType())) {
return inputMapping.getValue();
}
return inputMapping.getValue();
}
}

}
}
return null;
}

public Map<String, Object> calculateInputVariables(DelegateExecution execution) {

Map<String, Object> inboundVariables = null;
Map<String, Object> inboundVariables = new HashMap<>();
boolean copyAllVariables = true;
ProcessExtensionModel extensions = processExtensionService.getExtensionsForId(execution.getProcessDefinitionId());

if (extensions.getExtensions().isTaskElementPresentInMappingSection(execution.getCurrentActivityId())) {

ProcessVariablesMapping processVariablesMapping = extensions.getExtensions().getMappingForFlowElement(execution.getCurrentActivityId());
extensions.getExtensions().isTaskElementPresentInMappingSection(execution.getCurrentActivityId());

Map<String, Mapping> inputMappings = processVariablesMapping.getInputs();
if (!inputMappings.isEmpty()) {
inboundVariables = new HashMap<>();
Expand All @@ -85,82 +82,81 @@ public Map<String, Object> calculateInputVariables(DelegateExecution execution)
value);
}
}
}
else {
} else {
copyAllVariables = false;
}
}
//Nothing found - put all process variables if Task is empty if task is not empty
if (inboundVariables == null) {
if (inboundVariables.isEmpty()) {
if (copyAllVariables) {
inboundVariables = new HashMap<>(execution.getVariables());
} else {
inboundVariables = new HashMap<>();
}
}

return inboundVariables;
}

public Object calculateOutPutMappedValue(Mapping mapping,
Map<String, Object> activitiCompleteVariables) {

if (mapping != null) {
if (Mapping.SourceMappingType.VALUE.equals(mapping.getType())) {

return mapping.getValue();
} else {
if (Mapping.SourceMappingType.VARIABLE.equals(mapping.getType())) {
String name = mapping.getValue().toString();

return activitiCompleteVariables != null ?
activitiCompleteVariables.get(name) :
null;
activitiCompleteVariables.get(name) :
null;
}
}

}
return null;
}

public Map<String, Object> calculateOutPutVariables(boolean defaultCopyAllVariables,
String processDefinitionId,
String processDefinitionId,
String activityId,
Map<String, Object> activitiCompleteVariables) {
Map<String, Object> outboundVariables = new HashMap<>();


Map<String, Object> outboundVariables = new HashMap<>();
boolean copyAllVariables = true;
if (activitiCompleteVariables != null && !activitiCompleteVariables.isEmpty()) {
ProcessExtensionModel extensions = processExtensionService.getExtensionsForId(processDefinitionId);
if (extensions != null) {

ProcessExtensionModel extensions = processExtensionService.getExtensionsForId(processDefinitionId);

if (extensions.getExtensions().isTaskElementPresentInMappingSection(activityId)) {

ProcessVariablesMapping processVariablesMapping = extensions.getExtensions().getMappingForFlowElement(activityId);
if (processVariablesMapping != null) {
Map<String, Mapping> outputMappings = processVariablesMapping.getOutputs();

if (!outputMappings.isEmpty()) {

for (Map.Entry<String, Mapping> mapping : outputMappings.entrySet()) {

String name = mapping.getKey();

//Check that we have this process variables in extensions
//TO DO: can we create a process variable if it is not defined in extension file?
org.activiti.spring.process.model.VariableDefinition processVariableDefinition = extensions.getExtensions().getPropertyByName(name);
if (processVariableDefinition != null) {
outboundVariables.put(name, calculateOutPutMappedValue(mapping.getValue(),
activitiCompleteVariables));
}
}
Map<String, Mapping> outputMappings = processVariablesMapping.getOutputs();

if (!outputMappings.isEmpty()) {

for (Map.Entry<String, Mapping> mapping : outputMappings.entrySet()) {

String name = mapping.getKey();

org.activiti.spring.process.model.VariableDefinition processVariableDefinition = extensions.getExtensions().getPropertyByName(name);

if (processVariableDefinition != null) {
outboundVariables.put(name, calculateOutPutMappedValue(mapping.getValue(),
activitiCompleteVariables));
}
}

}

} else {
copyAllVariables = false;
}
}

//Nothing found - put all completeVariables
if (outboundVariables.isEmpty() && defaultCopyAllVariables) {
outboundVariables = new HashMap<>(activitiCompleteVariables);

if (outboundVariables.isEmpty()) {
if (copyAllVariables) {
outboundVariables = new HashMap<>(activitiCompleteVariables);
}
}
}

return outboundVariables;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.bpmn.helper.TaskVariableCopier;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.delegate.SubProcessActivityBehavior;
import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -118,6 +121,9 @@ public void execute(DelegateExecution execution) {
}
}

variables=calculateVariables(execution,subProcess);


// copy process variables
for (IOParameter ioParameter : callActivity.getInParameters()) {
Object value = null;
Expand Down Expand Up @@ -147,7 +153,7 @@ public void execute(DelegateExecution execution) {

public void completing(DelegateExecution execution, DelegateExecution subProcessInstance) throws Exception {
// only data. no control flow available on this execution.

CommandContext commandContext = Context.getCommandContext();
ExpressionManager expressionManager = Context.getProcessEngineConfiguration().getExpressionManager();

// copy process variables
Expand All @@ -164,6 +170,12 @@ public void completing(DelegateExecution execution, DelegateExecution subProcess
}
execution.setVariable(ioParameter.getTarget(), value);
}
Map<String, Object> outboundVariables = getOutBoundVariables(commandContext,
execution,subProcessInstance.getVariables());
if (outboundVariables != null) {
execution.getParent().setVariables(outboundVariables);
}

}

public void completed(DelegateExecution execution) throws Exception {
Expand Down Expand Up @@ -204,4 +216,27 @@ public void setProcessDefinitonKey(String processDefinitonKey) {
public String getProcessDefinitonKey() {
return processDefinitonKey;
}

protected Map<String, Object> calculateVariables(DelegateExecution execution,
// CommandContext commandContext,
Process subProcess) {
Map<String, Object> inboundVars = getInboundVariables(execution);
return inboundVars;
}

protected Map<String, Object> getInboundVariables(DelegateExecution execution) {
return null;
}


protected Map<String, Object> getOutBoundVariables(CommandContext commandContext,
DelegateExecution execution,
Map<String, Object> taskVariables) {

if(commandContext.getProcessEngineConfiguration().isCopyVariablesToLocalForTasks()){
return taskVariables;
}
return null;
}

}
Loading

0 comments on commit d1b5408

Please sign in to comment.