How to Restart a Workflow From the Task Associated With a Given Status

I need to create a workflow that lets the user retrieve an entity from the database, review it, and finally approve or discard it. If the user approves the entity, the application just updates its status to APPROVED, otherwise if the user discards the entity, the application updates its status to DISCARDED.

When a new entity gets stored into the database, its status is default to DRAFT. When the user starts reviewing the entity, the status is then set to IN_REVIEW – a review might last several days.

To implement the workflow I defined the following JavaDelegate classes:

public class ReviewEntityTask implements JavaDelegate { 
    public void execute(DelegateExecution execution) {
        // set entity status to IN_REVIEW and update DB
    }
}

public class ApproveEntityTask implements JavaDelegate { 
    public void execute(DelegateExecution execution) {
        // set entity status to APPROVED and update DB
    }
}

public class DiscardEntityTask implements JavaDelegate { 
    public void execute(DelegateExecution execution) {
        // set entity status to DISCARDED and update DB
    }
}

Then, looking at the official documentation, the workflow should be setup like this:

@Service
public class MyWorkflowService {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    public void startProcess(Article article) {
        Map<String, Object> variables = new HashMap<>();
        ...
        runtimeService.startProcessInstanceByKey("entityReview", variables);
    }

    public void updateOnReview(final MyEntity myEntity) {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("review", myEntity.getStauts == "IN_REVIEW");
        taskService.complete(myEntity.getId(), variables);
    }

    public void updateOnApprove(final MyEntity myEntity) {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("approved", myEntity.getStauts == "APPROVED");
        taskService.complete(myEntity.getId(), variables);
    }

    public void updateOnDiscard(final MyEntity myEntity) {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("discarded", myEntity.getStauts == "DISCARDED");
        taskService.complete(myEntity.getId(), variables);
    }
}

Before running any task, I need to call runtimeService.startProcessInstanceByKey to start the process. So my question is: how do I restart the workflow from a given [previous] status? For instance, let’s suppose today I start the review by invoking updateOnReview (status goes to IN_REVIW) … and in two days I’m going to approve it by invoking updateOnApprove (status goes to APPROVED), how should I invoke runtimeService.startProcessInstanceByKey to start the workflow from the task I left previously (i.e. the one associated with status IN_REVIEW)? Thanks.

Perhaps I’m missing something here, but in general you would like a workflow instance to model the entire lifecycle of the entity without having to restart the workflow. It sounds like you need a task that waits for someone to perform a review task, in which case your entity would transition to IN_REVIEW status. The workflow could then wait for someone to review it, in which the entity could be marked as either APPROVED or DISCARDED depending on their decision. This allows the workflow to hold state about the entity, such as it’s id and so forth. You could start up a workflow instance with some initial state by setting variables in the map provided to the startProcessInstanceByKey method call if you need to.