Hello,

I have a process where I have 3 sequential user task (something like Task 1
-> Task 2 -> Task 3). So, to validate the Task 3, I have to validate the
Task 1, then the Task 2.

My goal is to be able to go back to the previous task without modifying my
process definition. To do so, I found a way thanks to the need of another
Activiti user in [1]. But, it seems that his previous tasks are not
"finished" whereas mine, once validated, disappear from the ACT_RU_TASK
table but can be found in the ACT_HI_TASKINST.

So, I implemented a Command like he did but I don't manage to make my
execution to point to the previous task. Algorithm should be something like:

- Retrieve the current task
- Get the process instance of the current task
- Get the historic tasks of the process instance
- From the list of the historic tasks, deduce the previous one
- Create a new task from the previous historic task
- Make the execution to point on this new task

Here is my code:

public class MoveTokenCmd implements Command<Void> {

    protected String fromTaskId;


    public MoveTokenCmd(String fromTaskId) {
        this.fromTaskId = fromTaskId;

    }

    public Void execute(CommandContext commandContext) {
        HistoricTaskInstanceEntity currentUserTaskEntity =
commandContext.getHistoricTaskInstanceEntityManager()
                .findHistoricTaskInstanceById(fromTaskId);
        ExecutionEntity currentExecution =
commandContext.getExecutionEntityManager()
                .findExecutionById(currentUserTaskEntity.getExecutionId());

        // Get process Instance
        HistoricProcessInstanceEntity historicProcessInstanceEntity =
commandContext
                .getHistoricProcessInstanceEntityManager()

.findHistoricProcessInstance(currentUserTaskEntity.getProcessInstanceId());

        HistoricTaskInstanceQueryImpl historicTaskInstanceQuery = new
HistoricTaskInstanceQueryImpl();

historicTaskInstanceQuery.processInstanceId(historicProcessInstanceEntity.getId()).orderByExecutionId().desc();
        List<HistoricTaskInstance> historicTaskInstances =
commandContext.getHistoricTaskInstanceEntityManager()

.findHistoricTaskInstancesByQueryCriteria(historicTaskInstanceQuery);

        int index = 0;
        for (HistoricTaskInstance historicTaskInstance :
historicTaskInstances) {
            if
(historicTaskInstance.getId().equals(currentUserTaskEntity.getId())) {
                break;
            }
            index++;
        }

        if (index > 0) {
            HistoricTaskInstance previousTask =
historicTaskInstances.get(index - 1);

            TaskEntity newTaskEntity =
createTaskFromHistoricTask(previousTask, commandContext);
            currentExecution.addTask(newTaskEntity);

            commandContext.getTaskEntityManager().insert(newTaskEntity);


AtomicOperation.TRANSITION_CREATE_SCOPE.execute(currentExecution);
        } else {
            // TODO: find the last task of the previous process instance
        }

        // To overcome the "Task cannot be deleted because is part of a
running
        // process"
        TaskEntity currentUserTask =
commandContext.getTaskEntityManager().findTaskById(fromTaskId);
        if (currentUserTask != null) {
            currentUserTask.setExecutionId(null);

commandContext.getTaskEntityManager().deleteTask(currentUserTask, "jumped
to another task", true);
        }

        return null;
    }

    private TaskEntity createTaskFromHistoricTask(HistoricTaskInstance
historicTaskInstance,
            CommandContext commandContext) {

        TaskEntity newTaskEntity = new TaskEntity();


newTaskEntity.setProcessDefinitionId(historicTaskInstance.getProcessDefinitionId());
        newTaskEntity.setName(historicTaskInstance.getName());

newTaskEntity.setTaskDefinitionKey(historicTaskInstance.getTaskDefinitionKey());

newTaskEntity.setProcessInstanceId(historicTaskInstance.getExecutionId());
        newTaskEntity.setExecutionId(historicTaskInstance.getExecutionId());

        return newTaskEntity;
    }

Can you help me, please?

Regards,

Thomas

[1] https://community.alfresco.com/thread/224300-user-task-rollback
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to