/*
   * (non-Javadoc)
   *
   * @see com.htm.TaskParentInterface#exit(java.lang.String)
   */
  public void exit(String tiid) throws HumanTaskManagerException {
    try {
      /* Start transaction for creating a task instance */
      dataAccessProvider.beginTx();
      log.debug("Exit task instance - Trying to exit task instance '" + tiid + "'");
      this.authorizationManager.authorizeTaskParentAction(getCurrentUser(), tiid, EActions.EXIT);
      ITaskInstance taskInstance = dataAccessProvider.getTaskInstance(tiid);
      String oldState = taskInstance.getStatus().toString();

      // TODO for skipped, completed, faulted task instances an individual
      // error message should
      // be created. Currently only an invalidarguemntexception is thrown
      if (taskInstance.isExpired()) {
        String errorMsg =
            "The task instance '"
                + taskInstance.getId()
                + "' can not be exited "
                + "because it expired on "
                + Utilities.formatTimestamp(taskInstance.getExpirationTime());
        log.error(errorMsg);
        throw new InvalidOperationException(errorMsg);
      }

      taskInstance.setStatus(ETaskInstanceState.EXITED);

      // Audit
      if (Configuration.isLoggingEnabled()) {
        IAuditLogger auditLogger = AuditFactory.newInstance();
        TaskInstanceView taskInstanceView = new TaskInstanceView(taskInstance);
        AuditAction action =
            new AuditAction(
                EActions.EXIT.toString(),
                taskInstanceView,
                taskInstanceView.getStatus(),
                oldState,
                SessionUtils.getCurrentUser());
        auditLogger.logAction(action);
      }
      dataAccessProvider.commitTx();
    } catch (HumanTaskManagerException e) {
      dataAccessProvider.rollbackTx();
      throw e;

    } finally {
      dataAccessProvider.close();
    }
  }