/**
   * 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;
  }
  @Clusterable(onMaster = true)
  @Override
  public void cleanUpBackgroundTasks() throws SystemException {
    List<BackgroundTask> backgroundTasks =
        backgroundTaskPersistence.findByStatus(BackgroundTaskConstants.STATUS_IN_PROGRESS);

    for (BackgroundTask backgroundTask : backgroundTasks) {
      backgroundTask.setStatus(BackgroundTaskConstants.STATUS_FAILED);

      cleanUpBackgroundTask(backgroundTask, BackgroundTaskConstants.STATUS_FAILED);
    }
  }
  @Override
  public BackgroundTask addBackgroundTask(
      long userId,
      long groupId,
      String name,
      String[] servletContextNames,
      Class<?> taskExecutorClass,
      Map<String, Serializable> taskContextMap,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    User user = userPersistence.findByPrimaryKey(userId);
    Date now = new Date();

    final long backgroundTaskId = counterLocalService.increment();

    BackgroundTask backgroundTask = backgroundTaskPersistence.create(backgroundTaskId);

    backgroundTask.setCompanyId(user.getCompanyId());
    backgroundTask.setCreateDate(serviceContext.getCreateDate(now));
    backgroundTask.setGroupId(groupId);
    backgroundTask.setModifiedDate(serviceContext.getModifiedDate(now));
    backgroundTask.setUserId(userId);
    backgroundTask.setUserName(user.getFullName());
    backgroundTask.setName(name);
    backgroundTask.setServletContextNames(StringUtil.merge(servletContextNames));
    backgroundTask.setTaskExecutorClassName(taskExecutorClass.getName());

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

      backgroundTask.setTaskContext(taskContext);
    }

    backgroundTask.setStatus(BackgroundTaskConstants.STATUS_NEW);

    backgroundTaskPersistence.update(backgroundTask);

    TransactionCommitCallbackRegistryUtil.registerCallback(
        new Callable<Void>() {

          @Override
          public Void call() throws Exception {
            backgroundTaskLocalService.triggerBackgroundTask(backgroundTaskId);

            return null;
          }
        });

    return backgroundTask;
  }
  @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;
  }