ProcessDefinitionImpl currentProcessDefinitionImpl = processInstance.getProcessDefinition();
DeploymentCache deploymentCache = Context
.getProcessEngineConfiguration()
.getDeploymentCache();
ProcessDefinitionEntity currentProcessDefinition;
if (currentProcessDefinitionImpl instanceof ProcessDefinitionEntity) {
currentProcessDefinition = (ProcessDefinitionEntity) currentProcessDefinitionImpl;
} else {
currentProcessDefinition = deploymentCache.findDeployedProcessDefinitionById(currentProcessDefinitionImpl.getId());
}
ProcessDefinitionEntity newProcessDefinition = deploymentCache
.findDeployedProcessDefinitionByKeyAndVersion(currentProcessDefinition.getKey(), processDefinitionVersion);
validateAndSwitchVersionOfExecution(commandContext, processInstance, newProcessDefinition);
// switch the historic process instance to the new process definition version
HistoricProcessInstanceManager historicProcessInstanceManager = commandContext.getHistoricProcessInstanceManager();
if (historicProcessInstanceManager.isHistoryEnabled()) {
HistoricProcessInstanceEntity historicProcessInstance = historicProcessInstanceManager.findHistoricProcessInstance(processInstanceId);
historicProcessInstance.setProcessDefinitionId(newProcessDefinition.getId());
}
// switch all sub-executions of the process instance to the new process definition version
List<ExecutionEntity> childExecutions = executionManager
.findChildExecutionsByProcessInstanceId(processInstanceId);
for (ExecutionEntity executionEntity : childExecutions) {
validateAndSwitchVersionOfExecution(commandContext, executionEntity, newProcessDefinition);
}
// switch all jobs to the new process definition version
List<JobEntity> jobs = commandContext.getJobManager().findJobsByExecutionId(processInstanceId);
List<JobDefinitionEntity> currentJobDefinitions =
commandContext.getJobDefinitionManager().findByProcessDefinitionId(currentProcessDefinition.getId());
List<JobDefinitionEntity> newVersionJobDefinitions =
commandContext.getJobDefinitionManager().findByProcessDefinitionId(newProcessDefinition.getId());
Map<String, String> jobDefinitionMapping = getJobDefinitionMapping(currentJobDefinitions, newVersionJobDefinitions);
for (JobEntity jobEntity : jobs) {
switchVersionOfJob(jobEntity, newProcessDefinition, jobDefinitionMapping);
}