/**
   * @param principal the user principal representing the desired user
   * @param name the name of the workflow to create.
   * @param actionId the ID of the initial action
   * @param inputs the inputs to the workflow
   * @return a WorkflowVO representing the newly created workflow instance
   * @throws SystemException if an error occurs while initiaizing the workflow
   */
  public WorkflowVO initializeWorkflow(
      InfoGluePrincipal principal, String name, int actionId, Map inputs) throws SystemException {
    WorkflowVO workflowVO = null;

    try {
      Session session = null;
      net.sf.hibernate.Transaction tx = null;

      try {
        session = hibernateSessionFactory.openSession();
        tx = session.beginTransaction();

        if (getIsAccessApproved(name, principal)) {
          WorkflowFacade wf =
              new WorkflowFacade(
                  principal, name, actionId, inputs, hibernateSessionFactory, session);
          workflowVO = wf.createWorkflowVO();

          session.flush();

          tx.commit();
        } else {
          throw new Bug("You are not allowed to create " + name + " workflows.");
        }
      } catch (Exception e) {
        logger.error(
            "An error occurred when we tries to run initializeWorkflow():" + e.getMessage(), e);
        try {
          tx.rollback();
        } catch (HibernateException he) {
          logger.error(
              "An error occurred when we tries to rollback transaction:" + he.getMessage(), he);
        }
        restoreSessionFactory(e);
      } finally {
        try {
          session.close();
        } catch (HibernateException e) {
          logger.error("An error occurred when we tries to close:" + e.getMessage(), e);
        }
      }
    } catch (Exception e) {
      throw new SystemException(e);
    }

    return workflowVO;
  }
  /**
   * Invokes an action on a workflow for a given user and request
   *
   * @param principal the user principal
   * @param workflowId the ID of the desired workflow
   * @param actionId the ID of the desired action
   * @param inputs the inputs to the workflow
   * @return a WorkflowVO representing the current state of the workflow identified by workflowId
   * @throws WorkflowException if a workflow error occurs
   */
  public WorkflowVO invokeAction(
      InfoGluePrincipal principal, long workflowId, int actionId, Map inputs)
      throws WorkflowException {
    WorkflowVO workflowVO = null;

    Session session = null;
    net.sf.hibernate.Transaction tx = null;

    try {
      session = hibernateSessionFactory.openSession();

      tx = session.beginTransaction();

      WorkflowFacade wf =
          new WorkflowFacade(principal, workflowId, hibernateSessionFactory, session);
      wf.doAction(actionId, inputs);

      session.flush();

      tx.commit();
    } catch (Exception e) {
      logger.error(
          "An error occurred when we tries to execute invokeAction():" + e.getMessage(), e);
      try {
        tx.rollback();
      } catch (HibernateException he) {
        logger.error(
            "An error occurred when we tries to rollback transaction:" + he.getMessage(), he);
      }
      restoreSessionFactory(e);
    } finally {
      try {
        session.close();
      } catch (HibernateException e) {
        logger.error("An error occurred when we tries to close session:" + e.getMessage(), e);
      }
    }

    try {
      session = hibernateSessionFactory.openSession();

      tx = session.beginTransaction();

      WorkflowFacade wf =
          new WorkflowFacade(principal, workflowId, hibernateSessionFactory, session);

      workflowVO = wf.createWorkflowVO();

      session.flush();

      tx.commit();
    } catch (Exception e) {
      logger.error(
          "An error occurred when we tries to execute invokeAction():" + e.getMessage(), e);
      try {
        tx.rollback();
      } catch (HibernateException he) {
        logger.error(
            "An error occurred when we tries to rollback transaction:" + he.getMessage(), he);
      }
      restoreSessionFactory(e);
    } finally {
      try {
        session.close();
      } catch (HibernateException e) {
        logger.error("An error occurred when we tries to close session:" + e.getMessage(), e);
      }
    }

    return workflowVO;
  }