/**
   * Gobal Action, this action prepares to show glAcctDefaultList.JSP. The form Allows the operator
   * to select the account to be modified or click add or delete.
   */
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws javax.servlet.ServletException, java.io.IOException {
    // AppLog.trace("ShowGlAccountDefaultList action doPerfrom");
    ActionErrors errors = new ActionErrors();
    HttpSession session = request.getSession();
    DbUserSession sessionUser = SessionHelpers.getUserSession(request);
    if (sessionUser == null) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.invalid"));
    }
    try {
      // Set collections in session
      SessionHelpers.setGlAccountDefaultListInSession(request);
    } catch (PersistenceException pe) {
      logger.error("Persistence Exception in ShowGlAccountDefaultList.doPerform. " + pe);
      errors.add(
          ActionErrors.GLOBAL_ERROR, new ActionError("error.PersistenceException", pe.getCause()));
    } catch (Exception pe) {
      logger.error("Exception in  ShowGlAccountDefaultList.doPerform. ", pe);
      errors.add(
          ActionErrors.GLOBAL_ERROR, new ActionError("error.GeneralException", pe.getMessage()));
    }
    // Check for any errors so far
    if (!errors.isEmpty()) {
      saveErrors(request, errors);
    }

    return mapping.findForward("showGlAcctDefaultListJsp");
  }
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws javax.servlet.ServletException, java.io.IOException {

    ActionErrors errors = new ActionErrors();
    HttpSession session = request.getSession();
    DbUserSession sessionUser = SessionHelpers.getUserSession(request);
    DatabaseTransaction t = null;
    DbSpeedData[] dbSpeedData = null;
    DbLocation[] dbLocation = null;
    LocaleDTO userlocale = null;

    MiscCashReceiptsForm miscCashReceipts = new MiscCashReceiptsForm();
    java.util.ArrayList locationList = new java.util.ArrayList();
    java.util.ArrayList glDescriptionList = new java.util.ArrayList();
    java.util.ArrayList cashAcctList = new java.util.ArrayList();
    java.util.ArrayList payMethodList = new java.util.ArrayList();
    java.util.ArrayList pleaseSelect = new java.util.ArrayList();

    if (sessionUser == null) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.invalid"));
    }

    // Database Access Logic
    try {
      t = (DatabaseTransaction) DatabaseTransaction.getTransaction(sessionUser);

      // Set the list of receipt types in session.
      SessionHelpers.setReceiptTypesInSession(request);

      // Populate the locationList collection
      dbLocation = FdmsDb.getInstance().getLocationsForRegion(t, sessionUser.getRegion());
      // AppLog.trace("DbLocation list length = " +dbLocation.length);
      for (int i = 0; i < dbLocation.length; i++) {
        String listValue = String.valueOf(dbLocation[i].getId());
        String listLabel = dbLocation[i].getName();
        locationList.add(new OptionsList(listValue, listLabel));
      }

      // Populate the glDescriptionList collection
      dbSpeedData =
          FdmsDb.getInstance()
              .getSpeedData(sessionUser.getDbLookup(), sessionUser.getRegion(), "REVTYPE");
      // AppLog.trace("DbSpeedData for REVTYPE list length = " +dbSpeedData.length);
      for (int i = 0; i < dbSpeedData.length; i++) {
        String listValue = CsvTable.getField(dbSpeedData[i].getData(), 2);
        String listLabel = CsvTable.getField(dbSpeedData[i].getData(), 1);
        glDescriptionList.add(new OptionsList(listValue, listLabel));
      }

      // Populate the cashAcctList collection
      dbSpeedData =
          FdmsDb.getInstance()
              .getSpeedData(sessionUser.getDbLookup(), sessionUser.getRegion(), "CASHTYPE");
      // AppLog.trace("DbSpeedData for CASHTYPE list length = " +dbSpeedData.length);
      for (int i = 0; i < dbSpeedData.length; i++) {
        String listValue = CsvTable.getField(dbSpeedData[i].getData(), 2);
        String listLabel = CsvTable.getField(dbSpeedData[i].getData(), 1);
        cashAcctList.add(new OptionsList(listValue, listLabel));
      }

      // Populate the payMethodList", payMethodList)
      userlocale =
          FdmsDb.getInstance().getLocale(sessionUser.getDbLookup(), sessionUser.getRegion());
      dbSpeedData =
          FdmsDb.getInstance()
              .getSpeedData(sessionUser.getDbLookup(), sessionUser.getRegion(), "PAYMETHOD");
      // AppLog.trace("DbSpeedData for PAYMETHOD list length = " +dbSpeedData.length);
      for (int i = 0; i < dbSpeedData.length; i++) {
        if (dbSpeedData[i].getData() != null && dbSpeedData[i].getData().length() > 1) {
          String listValue = dbSpeedData[i].getData().substring(0, 2);
          String listLabel = dbSpeedData[i].getData();
          payMethodList.add(new OptionsList(listValue, listLabel));
        }
      }

      // Form Defaults
      miscCashReceipts.setSubmitButton("");
      miscCashReceipts.setAmountOfTran("0");
      miscCashReceipts.setDateOfTran(FormatDate.getCurrentDateFormatedMMDDYYYY());
      miscCashReceipts.setFormId("None");
      miscCashReceipts.setReceiptNumber(String.valueOf(userlocale.getNextReceiptNo()));

      pleaseSelect.add(new OptionsList("", "--Select--"));
      // AppLog.trace("Finished setting miscCashReceipts form bean");

    } catch (PersistenceException pe) {
      logger.error("Persistence Exception in ShowMiscCashReceipts.doPerform. " + pe);
      errors.add(
          ActionErrors.GLOBAL_ERROR, new ActionError("error.PersistenceException", pe.getCause()));
    } catch (Exception pe) {
      logger.error("Exception in ShowMiscCashReceipts.doPerform. ", pe);
      errors.add(
          ActionErrors.GLOBAL_ERROR, new ActionError("error.GeneralException", pe.getMessage()));
    } finally {
      if (t != null) t.closeConnection();
    }

    // Set Form Bean Into Scope
    session.setAttribute("miscCashReceipts", miscCashReceipts);
    session.setAttribute("locationList", locationList);
    session.setAttribute("glDescriptionList", glDescriptionList);
    session.setAttribute("cashAcctList", cashAcctList);
    session.setAttribute("payMethodList", payMethodList);
    session.setAttribute("pleaseSelect", pleaseSelect);
    // AppLog.trace("Setting miscCashReceipts form bean and collection arrays into session scope.");

    ActionForward actionForward = mapping.findForward("miscCashReceipts");

    if (!errors.isEmpty()) {
      // AppLog.info("ShowMiscCahsReceipts invoking forward mapping getInput().");
      saveErrors(request, errors);
      actionForward = new ActionForward(mapping.getInput());
    }

    return actionForward;
  }
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm actionForm,
      HttpServletRequest request,
      HttpServletResponse response)
      throws javax.servlet.ServletException, java.io.IOException {

    formErrors = new ArrayList();
    MiscCashReceiptsForm form = (MiscCashReceiptsForm) actionForm;
    ActionErrors errors = new ActionErrors();
    HttpSession session = request.getSession();
    DbUserSession sessionUser = SessionHelpers.getUserSession(request);
    DatabaseTransaction t = null;
    DbHistory dbHistory = null;

    if (form.getSubmitButton() != null && form.getSubmitButton().equals("exit")) {
      ActionForward actionForward = mapping.findForward("financial");
      return actionForward;
    }

    try {
      t = (DatabaseTransaction) DatabaseTransaction.getTransaction(sessionUser);
      // AppLog.trace("ProcessMiscCashReceipts submit ="+form.getSubmitButton());

      // --- HANDLE PRINTING A RECEIPT ---
      if (form.getFormId() != null
          && form.getFormId().trim().length() > 0
          && (!form.getFormId().equals("None"))) {
        // AppLog.trace("ProcessMiscCashReceipts printing receipt form: "+form.getFormId());
        if (FormatNumber.parseInteger(form.getFormId()) < 1) {
          // AppLog.error("ProcessMiscCashReceipts - No receipt type selected.");
          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.tables.noselect"));
          formErrors.add("formId");
          t.closeConnection();
          return (new ActionForward(mapping.getInput()));
        }
      }

      if (form.getSubmitButton().equals("save")) {
        dbHistory = new DbHistory();
        dbHistory.setNew();
        validateData(form, errors);
        if (errors.isEmpty()) {
          setHistory(t, sessionUser, dbHistory, form, errors);
          if (errors.isEmpty()) {
            t.save();
          } else {
            // AppLog.criticalError("Exception in ProcessMiscCashReceipts.setHistory.");
          }
        } else {
          // AppLog.trace("Validation Errors in ProcessMiscCashReceipts; returning to
          // MiscCashReceipts form.");
        }
      }

      // --- HANDLE PRINTING A RECEIPT ---
      if (errors.isEmpty() && FormatNumber.parseInteger(form.getFormId()) > 0) {

        String crystalFlag =
            UtilSingleton.getInstance()
                .getProperty(sessionUser.getConfigID(), "CrystalServer.useReportingService");
        String pageName = null;

        if (crystalFlag != null && "true".equals(crystalFlag)) {
          CrystalReportManagerBean crystalServerReport =
              new CrystalReportManagerBean(sessionUser.getConfigID());
          pageName =
              crystalServerReport.printReport(
                  sessionUser,
                  FormatNumber.parseInteger(form.getFormId()),
                  "",
                  "",
                  null,
                  "",
                  Integer.toString(dbHistory.getId()),
                  true);
        } else {
          ExportReport crystal = new ExportReport();
          crystal.setRecordIdSelParam(dbHistory.getId()); // selection parameter
          pageName =
              crystal.printForm(
                  sessionUser,
                  FormatNumber.parseInteger(form.getFormId()),
                  "",
                  "",
                  null,
                  "",
                  request,
                  response,
                  servlet.getServletContext());
        }

        form = new MiscCashReceiptsForm();
        setNewForm(request, sessionUser, session, form, errors);
        form.setPreviewFile(pageName);
        // AppLog.trace("Setting miscCashReceipts collection arrays into session scope.");
        session.setAttribute("miscCashReceipts", form);
        ActionForward actionForward = mapping.findForward("showMiscCashReceipts");
        return actionForward;
      }

    } catch (PersistenceException pe) {
      logger.error("Persistence Exception in ProcessMiscCashReceipts.doPerform. " + pe);
      errors.add(
          ActionErrors.GLOBAL_ERROR, new ActionError("error.PersistenceException", pe.getCause()));
    } catch (Exception pe) {
      logger.error("Exception in ProcessMiscCashReceipts.doPerform. ", pe);
      errors.add(
          ActionErrors.GLOBAL_ERROR, new ActionError("error.GeneralException", pe.getMessage()));
    } finally {
      if (t != null) {
        t.closeConnection();
      }
    }

    // Action Forward Logic

    ActionForward actionForward = mapping.findForward("showMiscCashReceiptsGlobal");

    if (!errors.isEmpty()) {
      // AppLog.info("ProcessMiscCashReceipts Invoking forward mapping getInput() ");
      saveErrors(request, errors);
      request.setAttribute("formErrors", formErrors);
      actionForward = new ActionForward(mapping.getInput());
    }

    logger.debug("Leaving ProcessMiscCashReceipts.");
    return actionForward;
  }