@SuppressWarnings({ "unchecked", "rawtypes" })
public void setReplacedBy(InterpretableExecution replacedBy) {
this.replacedBy = (ExecutionEntity) replacedBy;
CommandContext commandContext = Context.getCommandContext();
DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
// update the related tasks
List<TaskEntity> allTasks = new ArrayList<TaskEntity>();
allTasks.addAll(getTasks());
List<TaskEntity> cachedTasks = dbSqlSession.findInCache(TaskEntity.class);
for (TaskEntity cachedTask : cachedTasks) {
if (cachedTask.getExecutionId().equals(this.getId())) {
allTasks.add(cachedTask);
}
}
for (TaskEntity task: allTasks) {
task.setExecutionId(replacedBy.getId());
task.setExecution(this.replacedBy);
// update the related local task variables
List<VariableInstanceEntity> variables = (List) commandContext
.getVariableInstanceEntityManager()
.findVariableInstancesByTaskId(task.getId());
for (VariableInstanceEntity variable : variables) {
variable.setExecution(this.replacedBy);
}
this.replacedBy.addTask(task);
}
// All tasks have been moved to 'replacedBy', safe to clear the list
this.tasks.clear();
tasks = dbSqlSession.findInCache(TaskEntity.class);
for (TaskEntity task: tasks) {
if (id.equals(task.getExecutionId())) {
task.setExecutionId(replacedBy.getId());
}
}
// update the related jobs
List<JobEntity> jobs = getJobs();
for (JobEntity job: jobs) {
job.setExecution((ExecutionEntity) replacedBy);
}
// update the related event subscriptions
List<EventSubscriptionEntity> eventSubscriptions = getEventSubscriptions();
for (EventSubscriptionEntity subscriptionEntity: eventSubscriptions) {
subscriptionEntity.setExecution((ExecutionEntity) replacedBy);
}
// update the related process variables
List<VariableInstanceEntity> variables = (List) commandContext
.getVariableInstanceEntityManager()
.findVariableInstancesByExecutionId(id);
for (VariableInstanceEntity variable: variables) {
variable.setExecutionId(replacedBy.getId());
}
variables = dbSqlSession.findInCache(VariableInstanceEntity.class);
for (VariableInstanceEntity variable: variables) {
if (id.equals(variable.getExecutionId())) {
variable.setExecutionId(replacedBy.getId());
}
}