/**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static BackgroundTask toModel(BackgroundTaskSoap soapModel) {
    if (soapModel == null) {
      return null;
    }

    BackgroundTask model = new BackgroundTaskImpl();

    model.setMvccVersion(soapModel.getMvccVersion());
    model.setBackgroundTaskId(soapModel.getBackgroundTaskId());
    model.setGroupId(soapModel.getGroupId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setUserId(soapModel.getUserId());
    model.setUserName(soapModel.getUserName());
    model.setCreateDate(soapModel.getCreateDate());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setName(soapModel.getName());
    model.setServletContextNames(soapModel.getServletContextNames());
    model.setTaskExecutorClassName(soapModel.getTaskExecutorClassName());
    model.setTaskContext(soapModel.getTaskContext());
    model.setCompleted(soapModel.getCompleted());
    model.setCompletionDate(soapModel.getCompletionDate());
    model.setStatus(soapModel.getStatus());
    model.setStatusMessage(soapModel.getStatusMessage());

    return model;
  }
  @Override
  public BackgroundTask amendBackgroundTask(
      long backgroundTaskId,
      Map<String, Serializable> taskContextMap,
      int status,
      String statusMessage,
      ServiceContext serviceContext)
      throws SystemException {

    Date now = new Date();

    BackgroundTask backgroundTask = backgroundTaskPersistence.fetchByPrimaryKey(backgroundTaskId);

    if (backgroundTask == null) {
      return null;
    }

    backgroundTask.setModifiedDate(serviceContext.getModifiedDate(now));

    if (taskContextMap != null) {
      String taskContext = JSONFactoryUtil.serialize(taskContextMap);

      backgroundTask.setTaskContext(taskContext);
    }

    if ((status == BackgroundTaskConstants.STATUS_FAILED)
        || (status == BackgroundTaskConstants.STATUS_SUCCESSFUL)) {

      backgroundTask.setCompleted(true);
      backgroundTask.setCompletionDate(now);
    }

    backgroundTask.setStatus(status);

    if (Validator.isNotNull(statusMessage)) {
      backgroundTask.setStatusMessage(statusMessage);
    }

    backgroundTaskPersistence.update(backgroundTask);

    return backgroundTask;
  }