Skip to content

Commit

Permalink
Add support to enable event sub process start event for process insta…
Browse files Browse the repository at this point in the history
…nce migration
  • Loading branch information
tijsrademakers committed Apr 22, 2024
1 parent a0c0530 commit 261f1f5
Show file tree
Hide file tree
Showing 28 changed files with 1,063 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public ChangeActivityStateCmd(ChangeActivityStateBuilderImpl changeActivityState

@Override
public Void execute(CommandContext commandContext) {
if (changeActivityStateBuilder.getMoveExecutionIdList().size() == 0 && changeActivityStateBuilder.getMoveActivityIdList().size() == 0) {
throw new FlowableIllegalArgumentException("No move execution or activity ids provided");
if (changeActivityStateBuilder.getMoveExecutionIdList().isEmpty() && changeActivityStateBuilder.getMoveActivityIdList().isEmpty()
&& changeActivityStateBuilder.getEnableActivityIdList().isEmpty()) {

throw new FlowableIllegalArgumentException("No move execution or activity ids or enable activity ids provided");

} else if (changeActivityStateBuilder.getMoveActivityIdList().size() > 0 && changeActivityStateBuilder.getProcessInstanceId() == null) {
} else if ((!changeActivityStateBuilder.getMoveActivityIdList().isEmpty() || !changeActivityStateBuilder.getEnableActivityIdList().isEmpty()) && changeActivityStateBuilder.getProcessInstanceId() == null) {
throw new FlowableIllegalArgumentException("Process instance id is required");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntityManager;
import org.flowable.engine.impl.runtime.ChangeActivityStateBuilderImpl;
import org.flowable.engine.impl.runtime.EnableActivityIdContainer;
import org.flowable.engine.impl.runtime.MoveActivityIdContainer;
import org.flowable.engine.impl.runtime.MoveExecutionIdContainer;
import org.flowable.engine.impl.util.CommandContextUtil;
Expand Down Expand Up @@ -243,6 +244,18 @@ public List<MoveExecutionEntityContainer> resolveMoveExecutionEntityContainers(C

return moveExecutionEntityContainerList;
}

public List<EnableActivityContainer> resolveEnableActivityContainers(ChangeActivityStateBuilderImpl changeActivityStateBuilder) {
List<EnableActivityContainer> enableActivityContainerList = new ArrayList<>();
if (!changeActivityStateBuilder.getEnableActivityIdList().isEmpty()) {
for (EnableActivityIdContainer enableActivityIdContainer : changeActivityStateBuilder.getEnableActivityIdList()) {
EnableActivityContainer enableActivityContainer = new EnableActivityContainer(enableActivityIdContainer.getActivityIds());
enableActivityContainerList.add(enableActivityContainer);
}
}

return enableActivityContainerList;
}

protected ExecutionEntity resolveActiveExecution(String executionId, CommandContext commandContext) {
ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
Expand Down Expand Up @@ -409,7 +422,6 @@ protected FlowElement resolveFlowElementFromBpmnModel(BpmnModel bpmnModel, Strin
//-- Move container preparation section end

protected void doMoveExecutionState(ProcessInstanceChangeState processInstanceChangeState, CommandContext commandContext) {

ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
Map<String, List<ExecutionEntity>> activeEmbeddedSubProcesses = resolveActiveEmbeddedSubProcesses(processInstanceChangeState.getProcessInstanceId(), commandContext);
processInstanceChangeState.setProcessInstanceActiveEmbeddedExecutions(activeEmbeddedSubProcesses);
Expand Down Expand Up @@ -554,6 +566,31 @@ protected void doMoveExecutionState(ProcessInstanceChangeState processInstanceCh
}

processPendingEventSubProcessesStartEvents(processInstanceChangeState, commandContext);

for (EnableActivityContainer enableActivityContainer : processInstanceChangeState.getEnableActivityContainers()) {
if (enableActivityContainer.getActivityIds() != null && !enableActivityContainer.getActivityIds().isEmpty()) {
BpmnModel bpmnModel = null;
ExecutionEntity parentExecution = executionEntityManager.findById(processInstanceChangeState.getProcessInstanceId());
if (processInstanceChangeState.getProcessDefinitionToMigrateTo() != null) {
bpmnModel = ProcessDefinitionUtil.getBpmnModel(processInstanceChangeState.getProcessDefinitionToMigrateTo().getId());

} else {
bpmnModel = ProcessDefinitionUtil.getBpmnModel(parentExecution.getProcessDefinitionId());
}

ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
ProcessInstanceHelper processInstanceHelper = processEngineConfiguration.getProcessInstanceHelper();

for (String enableActivityId : enableActivityContainer.getActivityIds()) {
FlowElement enableFlowElement = bpmnModel.getFlowElement(enableActivityId);
if (enableFlowElement == null) {
throw new FlowableException("could not find element for activity id " + enableActivityId);
}

processInstanceHelper.processEventSubProcessStartEvent(enableFlowElement, parentExecution, processEngineConfiguration, commandContext);
}
}
}
}

protected void processPendingEventSubProcessesStartEvents(ProcessInstanceChangeState processInstanceChangeState, CommandContext commandContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ public class DefaultDynamicStateManager extends AbstractDynamicStateManager impl
public void moveExecutionState(ChangeActivityStateBuilderImpl changeActivityStateBuilder, CommandContext commandContext) {
List<MoveExecutionEntityContainer> moveExecutionEntityContainerList = resolveMoveExecutionEntityContainers(changeActivityStateBuilder,
changeActivityStateBuilder.getProcessInstanceVariables(), commandContext);
List<ExecutionEntity> executions = moveExecutionEntityContainerList.iterator().next().getExecutions();
String processInstanceId = executions.iterator().next().getProcessInstanceId();
List<ExecutionEntity> executions = null;
if (!moveExecutionEntityContainerList.isEmpty()) {
executions = moveExecutionEntityContainerList.iterator().next().getExecutions();
}

List<EnableActivityContainer> enableActivityContainerList = resolveEnableActivityContainers(changeActivityStateBuilder);
String processInstanceId = null;
if (executions != null && !executions.isEmpty()) {
processInstanceId = executions.iterator().next().getProcessInstanceId();
} else {
processInstanceId = changeActivityStateBuilder.getProcessInstanceId();
}

ProcessInstanceChangeState processInstanceChangeState = new ProcessInstanceChangeState()
.setProcessInstanceId(processInstanceId)
.setMoveExecutionEntityContainers(moveExecutionEntityContainerList)
.setEnableActivityContainers(enableActivityContainerList)
.setLocalVariables(changeActivityStateBuilder.getLocalVariables())
.setProcessInstanceVariables(changeActivityStateBuilder.getProcessInstanceVariables());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* 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.engine.impl.dynamic;

import java.util.List;

public class EnableActivityContainer {

protected List<String> activityIds;

public EnableActivityContainer(List<String> activityIds) {
this.activityIds = activityIds;
}

public List<String> getActivityIds() {
return activityIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ProcessInstanceChangeState {
protected Map<String, Map<String, Object>> localVariables = new HashMap<>();
protected Map<String, List<ExecutionEntity>> processInstanceActiveEmbeddedExecutions;
protected List<MoveExecutionEntityContainer> moveExecutionEntityContainers;
protected List<EnableActivityContainer> enableActivityContainers;
protected HashMap<String, ExecutionEntity> createdEmbeddedSubProcess = new HashMap<>();
protected HashMap<String, ExecutionEntity> createdMultiInstanceRootExecution = new HashMap<>();
protected HashMap<StartEvent, ExecutionEntity> pendingEventSubProcessesStartEvents = new HashMap<>();
Expand Down Expand Up @@ -80,6 +81,15 @@ public ProcessInstanceChangeState setLocalVariables(Map<String, Map<String, Obje
return this;
}

public List<EnableActivityContainer> getEnableActivityContainers() {
return enableActivityContainers;
}

public ProcessInstanceChangeState setEnableActivityContainers(List<EnableActivityContainer> enableActivityContainers) {
this.enableActivityContainers = enableActivityContainers;
return this;
}

public List<MoveExecutionEntityContainer> getMoveExecutionEntityContainers() {
return moveExecutionEntityContainers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.ProcessMigrationService;
import org.flowable.engine.migration.ActivityMigrationMapping;
import org.flowable.engine.migration.EnableActivityMapping;
import org.flowable.engine.migration.ProcessInstanceMigrationBuilder;
import org.flowable.engine.migration.ProcessInstanceMigrationDocument;
import org.flowable.engine.migration.ProcessInstanceMigrationValidationResult;
Expand Down Expand Up @@ -114,6 +115,12 @@ public ProcessInstanceMigrationBuilder addActivityMigrationMapping(ActivityMigra
this.migrationDocumentBuilder.addActivityMigrationMapping(mapping);
return this;
}

@Override
public ProcessInstanceMigrationBuilder addEnableEventSubProcessStartEvent(EnableActivityMapping mapping) {
this.migrationDocumentBuilder.addEnableActivityMapping(mapping);
return this;
}

@Override
public ProcessInstanceMigrationBuilder withProcessInstanceVariable(String variableName, Object variableValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.migration.ActivityMigrationMapping;
import org.flowable.engine.migration.EnableActivityMapping;
import org.flowable.engine.migration.ProcessInstanceMigrationDocument;
import org.flowable.engine.migration.ProcessInstanceMigrationDocumentBuilder;
import org.flowable.engine.migration.Script;
Expand All @@ -35,6 +36,7 @@ public class ProcessInstanceMigrationDocumentBuilderImpl implements ProcessInsta
protected Integer migrateToProcessDefinitionVersion;
protected String migrateToProcessDefinitionTenantId;
protected List<ActivityMigrationMapping> activityMigrationMappings = new ArrayList<>();
protected List<EnableActivityMapping> enableActivityMappings = new ArrayList<>();
protected Map<String, Object> processInstanceVariables = new HashMap<>();
protected Script preUpgradeScript;
protected String preUpgradeJavaDelegate;
Expand Down Expand Up @@ -109,6 +111,18 @@ public ProcessInstanceMigrationDocumentBuilder addActivityMigrationMapping(Activ
this.activityMigrationMappings.add(activityMigrationMapping);
return this;
}

@Override
public ProcessInstanceMigrationDocumentBuilder addEnableActivityMappings(List<EnableActivityMapping> enableActivityMappings) {
this.enableActivityMappings.addAll(enableActivityMappings);
return this;
}

@Override
public ProcessInstanceMigrationDocumentBuilder addEnableActivityMapping(EnableActivityMapping enableActivityMapping) {
this.enableActivityMappings.add(enableActivityMapping);
return this;
}

@Override
public ProcessInstanceMigrationDocumentBuilder addProcessInstanceVariable(String variableName, Object variableValue) {
Expand Down Expand Up @@ -156,6 +170,7 @@ public ProcessInstanceMigrationDocument build() {
document.setPostUpgradeJavaDelegateExpression(postUpgradeJavaDelegateExpression);
}
document.setActivityMigrationMappings(activityMigrationMappings);
document.setEnableActivityMappings(enableActivityMappings);
document.setProcessInstanceVariables(processInstanceVariables);

return document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.migration.ActivityMigrationMapping;
import org.flowable.engine.migration.EnableActivityMapping;
import org.flowable.engine.migration.ProcessInstanceMigrationDocument;
import org.flowable.engine.migration.ProcessInstanceMigrationDocumentConverter;
import org.flowable.engine.migration.Script;
Expand All @@ -40,6 +41,7 @@ public class ProcessInstanceMigrationDocumentImpl implements ProcessInstanceMigr
protected Integer migrateToProcessDefinitionVersion;
protected String migrateToProcessDefinitionTenantId;
protected List<ActivityMigrationMapping> activityMigrationMappings;
protected List<EnableActivityMapping> enableActivityMappings;
protected Map<String, Map<String, Object>> activitiesLocalVariables;
protected Map<String, Object> processInstanceVariables;
protected Script preUpgradeScript;
Expand Down Expand Up @@ -199,6 +201,10 @@ public void setActivityMigrationMappings(List<ActivityMigrationMapping> activity
throw new FlowableException("From activity '" + Arrays.toString(duplicates.toArray()) + "' is mapped more than once");
}
}

public void setEnableActivityMappings(List<EnableActivityMapping> enableActivityMappings) {
this.enableActivityMappings = enableActivityMappings;
}

protected static List<String> findDuplicatedFromActivityIds(List<ActivityMigrationMapping> activityMigrationMappings) {
//Frequency Map
Expand Down Expand Up @@ -245,6 +251,11 @@ protected static Map<String, Map<String, Object>> buildActivitiesLocalVariablesM
public List<ActivityMigrationMapping> getActivityMigrationMappings() {
return activityMigrationMappings;
}

@Override
public List<EnableActivityMapping> getEnableActivityMappings() {
return enableActivityMappings;
}

@Override
public Map<String, Map<String, Object>> getActivitiesLocalVariables() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.flowable.engine.impl.delegate.ActivityBehaviorInvocation;
import org.flowable.engine.impl.delegate.invocation.JavaDelegateInvocation;
import org.flowable.engine.impl.dynamic.AbstractDynamicStateManager;
import org.flowable.engine.impl.dynamic.EnableActivityContainer;
import org.flowable.engine.impl.dynamic.MoveExecutionEntityContainer;
import org.flowable.engine.impl.dynamic.ProcessInstanceChangeState;
import org.flowable.engine.impl.history.HistoryManager;
Expand All @@ -73,6 +74,7 @@
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.engine.migration.ActivityMigrationMapping;
import org.flowable.engine.migration.EnableActivityMapping;
import org.flowable.engine.migration.ProcessInstanceBatchMigrationResult;
import org.flowable.engine.migration.ProcessInstanceMigrationCallback;
import org.flowable.engine.migration.ProcessInstanceMigrationDocument;
Expand Down Expand Up @@ -436,11 +438,20 @@ protected void doMigrateProcessInstance(ProcessInstance processInstance, Process
builder, document.getProcessInstanceVariables(), commandContext);
moveExecutionEntityContainerList.addAll(moveExecutionEntityContainers);
}

List<EnableActivityContainer> enableActivityContainerList = new ArrayList<>();
if (!document.getEnableActivityMappings().isEmpty()) {
for (EnableActivityMapping enableActivityMapping : document.getEnableActivityMappings()) {
EnableActivityContainer enableActivityContainer = new EnableActivityContainer(Collections.singletonList(enableActivityMapping.getActivityId()));
enableActivityContainerList.add(enableActivityContainer);
}
}

ProcessInstanceChangeState processInstanceChangeState = new ProcessInstanceChangeState()
.setProcessInstanceId(processInstance.getId())
.setProcessDefinitionToMigrateTo(procDefToMigrateTo)
.setMoveExecutionEntityContainers(moveExecutionEntityContainerList)
.setEnableActivityContainers(enableActivityContainerList)
.setProcessInstanceVariables(document.getProcessInstanceVariables())
.setLocalVariables(document.getActivitiesLocalVariables());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ChangeActivityStateBuilderImpl implements ChangeActivityStateBuilde
protected String processInstanceId;
protected List<MoveExecutionIdContainer> moveExecutionIdList = new ArrayList<>();
protected List<MoveActivityIdContainer> moveActivityIdList = new ArrayList<>();
protected List<EnableActivityIdContainer> enableActivityIdList = new ArrayList<>();
protected Map<String, Object> processVariables = new HashMap<>();
protected Map<String, Map<String, Object>> localVariables = new HashMap<>();

Expand Down Expand Up @@ -170,6 +171,13 @@ public ChangeActivityStateBuilder moveSingleActivityIdToSubProcessInstanceActivi
moveActivityIdList.add(moveActivityIdsContainer);
return this;
}

@Override
public ChangeActivityStateBuilder enableEventSubProcessStartEvent(String eventSubProcessStartEventId) {
EnableActivityIdContainer enableActivityIdContainer = new EnableActivityIdContainer(eventSubProcessStartEventId);
enableActivityIdList.add(enableActivityIdContainer);
return this;
}

@Override
public ChangeActivityStateBuilder processVariable(String processVariableName, Object processVariableValue) {
Expand Down Expand Up @@ -238,6 +246,10 @@ public List<MoveActivityIdContainer> getMoveActivityIdList() {
return moveActivityIdList;
}

public List<EnableActivityIdContainer> getEnableActivityIdList() {
return enableActivityIdList;
}

public Map<String, Object> getProcessInstanceVariables() {
return processVariables;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* 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.engine.impl.runtime;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author Tijs Rademakers
*/
public class EnableActivityIdContainer {

protected List<String> activityIds;

public EnableActivityIdContainer(String singleActivityId) {
this.activityIds = Collections.singletonList(singleActivityId);
}

public EnableActivityIdContainer(List<String> activityIds) {
this.activityIds = activityIds;
}

public List<String> getActivityIds() {
return Optional.ofNullable(activityIds).orElse(Collections.emptyList());
}
}
Loading

0 comments on commit 261f1f5

Please sign in to comment.