@Override
  public void dispatch (TaskExecution aTask) {
    SimpleTaskExecution switchTask = SimpleTaskExecution.of(aTask);
    switchTask.setStartTime(new Date ());
    switchTask.setStatus(TaskStatus.STARTED);
    taskExecutionRepo.merge(switchTask);
    Accessor selectedCase = resolveCase(aTask);
    if(selectedCase.containsKey("tasks")) {
      List<MapObject> tasks = selectedCase.getList("tasks", MapObject.class,Collections.emptyList());
      if(tasks.size() > 0) {
        MapObject task = tasks.get(0);
        SimpleTaskExecution execution = SimpleTaskExecution.of(task);
        execution.setId(UUIDGenerator.generate());
        execution.setStatus(TaskStatus.CREATED);
        execution.setCreateTime(new Date());
        execution.setTaskNumber(1);
        execution.setJobId(switchTask.getJobId());
        execution.setParentId(switchTask.getId());
        execution.setPriority(switchTask.getPriority());
        MapContext context = new MapContext (contextRepository.peek(switchTask.getId()));
        contextRepository.push(execution.getId(), context);
        TaskExecution evaluatedExecution = taskEvaluator.evaluate(execution, context);
        taskExecutionRepo.create(evaluatedExecution);
        taskDispatcher.dispatch(evaluatedExecution);
      }
      else  {
        SimpleTaskExecution completion = SimpleTaskExecution.of(aTask);
        completion.setStartTime(new Date());
        completion.setEndTime(new Date());
        completion.setExecutionTime(0);
        messageBroker.send(Queues.COMPLETIONS, completion);
      }
    }
    else {
      SimpleTaskExecution completion = SimpleTaskExecution.of(aTask);
      completion.setStartTime(new Date());
      completion.setEndTime(new Date());
      completion.setExecutionTime(0);
      completion.setOutput(selectedCase.get("value"));
      messageBroker.send(Queues.COMPLETIONS, completion);
    }
  }
@author Arik Cohen
@since Jun 3, 2017
@see SwitchTaskCompletionHandler
src/main/java/com/creactiviti/piper/core/task/SwitchTaskDispatcher.java