@Override
 public void resumed(ProcessEvent event) {
   taskEntity.setState(TaskState.RUNNING.name());
   taskEntity.setMessage(toString(event.getTask()));
   roundProgression(event);
   updateTask(taskEntity);
 }
 @Override
 public void started(ProcessEvent event) {
   taskEntity.setState(TaskState.RUNNING.name());
   taskEntity.setDateStart(System.currentTimeMillis());
   taskEntity.setMessage(toString(event.getTask()));
   roundProgression(event);
   updateTask(taskEntity);
 }
    @Override
    public void failed(ProcessEvent event) {
      taskEntity.setState(TaskState.FAILED.name());
      taskEntity.setDateEnd(System.currentTimeMillis());

      final Exception exception = event.getException();
      final String exceptionStr = printException(exception);
      taskEntity.setMessage(toString(event.getTask()) + " cause : " + exceptionStr);
      // taskEntity.setProgress((double) event.getProgress());
      updateTask(taskEntity);
    }
  @Override
  public synchronized void jobToBeExecuted(JobExecutionContext jec) {
    if (processBusiness == null) {
      this.processBusiness = SpringHelper.getBean(IProcessBusiness.class);
    }

    final Job job = jec.getJobInstance();
    if (!(job instanceof ProcessJob)) return;

    // attach a listener on the process
    final ProcessJob pj = (ProcessJob) job;
    final ProcessJobDetail detail = (ProcessJobDetail) jec.getJobDetail();
    final QuartzTask quartzTask =
        (QuartzTask) detail.getJobDataMap().get(QuartzJobListener.PROPERTY_TASK);
    final String quartzTaskId = quartzTask.getId();

    final Task taskEntity = new Task();
    taskEntity.setIdentifier(UUID.randomUUID().toString());
    taskEntity.setState(TaskState.PENDING.name());
    taskEntity.setTaskParameterId(quartzTask.getTaskParameterId());
    taskEntity.setOwner(quartzTask.getUserId());
    taskEntity.setType(""); // TODO
    processBusiness.addTask(taskEntity);

    final ProcessListener listener =
        new StateListener(taskEntity.getIdentifier(), quartzTask.getTitle());
    pj.addListener(listener);
    LOGGER.log(Level.INFO, "Run task " + taskEntity.getIdentifier());
  }
 /**
  * Round event progression value to {@link #ROUND_SCALE} before set to taskEntity object.
  *
  * @param event ProcessEvent
  */
 private void roundProgression(ProcessEvent event) {
   if (!Float.isNaN(event.getProgress()) && !Float.isInfinite(event.getProgress())) {
     BigDecimal progress = new BigDecimal(event.getProgress());
     progress = progress.setScale(ROUND_SCALE, BigDecimal.ROUND_HALF_UP);
     taskEntity.setProgress(progress.doubleValue());
   }
 }
    @Override
    public void progressing(ProcessEvent event) {
      taskEntity.setState(TaskState.RUNNING.name());
      taskEntity.setMessage(toString(event.getTask()));
      roundProgression(event);

      ParameterValueGroup output = event.getOutput();
      if (output != null) {
        try {
          taskEntity.setTaskOutput(ParamUtilities.writeParameterJSON(output));
        } catch (JsonProcessingException e) {
          LOGGER.log(Level.WARNING, "Process output serialization failed", e);
        }
      }

      if (event.getException() != null) {
        warnings.add(event);
      }

      updateTask(taskEntity);
    }
    @Override
    public void completed(ProcessEvent event) {
      taskEntity.setDateEnd(System.currentTimeMillis());
      taskEntity.setMessage(toString(event.getTask()));
      roundProgression(event);

      ParameterValueGroup output = event.getOutput();
      if (output != null) {
        try {
          taskEntity.setTaskOutput(ParamUtilities.writeParameterJSON(output));
        } catch (JsonProcessingException e) {
          LOGGER.log(Level.WARNING, "Process output serialization failed", e);
        }
      }

      // If a warning occurred, send exception to the user.
      if (!warnings.isEmpty()) {
        taskEntity.setState(TaskState.WARNING.name());
        taskEntity.setMessage(processWarningMessage());
      } else {
        taskEntity.setState(TaskState.SUCCEED.name());
      }

      updateTask(taskEntity);
    }
    private void updateTask(Task taskEntity) {
      if (processBusiness == null) {
        this.processBusiness = SpringHelper.getBean(IProcessBusiness.class);
      }
      // update in database
      processBusiness.updateTask(taskEntity);

      // send event
      final TaskStatusDTO taskStatus = new TaskStatusDTO();
      taskStatus.setId(taskEntity.getIdentifier());
      taskStatus.setTaskId(taskEntity.getTaskParameterId());
      taskStatus.setTitle(title);
      taskStatus.setStatus(taskEntity.getState());
      taskStatus.setMessage(taskEntity.getMessage());
      taskStatus.setPercent(taskEntity.getProgress().floatValue());
      taskStatus.setStart(taskEntity.getDateStart());
      taskStatus.setEnd(taskEntity.getDateEnd());
      taskStatus.setOutput(taskEntity.getTaskOutput());

      SpringHelper.sendEvent(taskStatus);
    }