/**
   * Makes service calls to rebuild the report control and sub-fund or object code select lists if
   * needed.
   *
   * @param principalName - current user requesting the report
   * @param buildHelper - contains the current and requested build states
   * @param reportSelectMode - indicates whether the report takes a sub-fund or object code select
   *     list
   */
  protected void buildControlLists(
      String principalName,
      Integer universityFiscalYear,
      ReportControlListBuildHelper buildHelper,
      ReportSelectMode reportSelectMode) {
    BudgetReportsControlListService budgetReportsControlListService =
        SpringContext.getBean(BudgetReportsControlListService.class);

    if (buildHelper.isBuildNeeded()) {
      String[] pointOfViewFields = buildHelper.getRequestedState().getPointOfView().split("[-]");
      budgetReportsControlListService.updateReportsControlList(
          principalName,
          universityFiscalYear,
          pointOfViewFields[0],
          pointOfViewFields[1],
          buildHelper.getRequestedState().getBuildMode());

      if (ReportSelectMode.SUBFUND.equals(reportSelectMode)) {
        budgetReportsControlListService.updateReportSubFundGroupSelectList(principalName);
      } else if (ReportSelectMode.OBJECT_CODE.equals(reportSelectMode)
          || ReportSelectMode.REASON.equals(reportSelectMode)) {
        budgetReportsControlListService.updateReportObjectCodeSelectList(principalName);
      }

      buildHelper.requestBuildComplete();
      GlobalVariables.getUserSession()
          .addObject(BCConstants.Report.CONTROL_BUILD_HELPER_SESSION_NAME, buildHelper);
    }
  }
  /**
   * Called from org select or account listing. Checks for needed control list build, makes call to
   * build control list if necessary, and forwards to subfund or object code select page.
   */
  public ActionForward start(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    OrganizationReportSelectionForm organizationReportSelectionForm =
        (OrganizationReportSelectionForm) form;
    String principalName = GlobalVariables.getUserSession().getPerson().getPrincipalId();

    // retrieve report mode to determine how control list should be built and what select screen
    // should be rendered
    BudgetConstructionReportMode reportMode =
        BudgetConstructionReportMode.getBudgetConstructionReportModeByName(
            organizationReportSelectionForm.getReportMode());
    if (reportMode == null) {
      LOG.error(
          "Invalid report mode passed to report select action: "
              + organizationReportSelectionForm.getReportMode());
      throw new RuntimeException(
          "Invalid report mode passed to report select action: "
              + organizationReportSelectionForm.getReportMode());
    }

    // retrieve build helper to determine if a control list rebuild is needed
    ReportControlListBuildHelper buildHelper =
        (ReportControlListBuildHelper)
            GlobalVariables.getUserSession()
                .retrieveObject(BCConstants.Report.CONTROL_BUILD_HELPER_SESSION_NAME);
    if (buildHelper == null) {
      // session timeout, need to rebuild build request
      buildHelper = new ReportControlListBuildHelper();

      Collection<BudgetConstructionPullup> selectedOrganizations =
          SpringContext.getBean(BudgetReportsControlListService.class)
              .retrieveSelectedOrganziations(principalName);
      buildHelper.addBuildRequest(
          organizationReportSelectionForm.getCurrentPointOfViewKeyCode(),
          selectedOrganizations,
          reportMode.reportBuildMode);
      GlobalVariables.getUserSession()
          .addObject(BCConstants.Report.CONTROL_BUILD_HELPER_SESSION_NAME, buildHelper);
    }

    // do list builds
    buildControlLists(
        principalName,
        organizationReportSelectionForm.getUniversityFiscalYear(),
        buildHelper,
        reportMode.reportSelectMode);

    // a few reports go just against the account control table, therefore we are ready to run the
    // report
    if (ReportSelectMode.ACCOUNT.equals(reportMode.reportSelectMode)) {
      // fixed null point exception of operationgModeTitle.
      organizationReportSelectionForm.setOperatingModeTitle(
          BCConstants.Report.NONE_SELECTION_TITLE);
      return performReport(mapping, form, request, response);
    }

    // setup action form
    if (ReportSelectMode.SUBFUND.equals(reportMode.reportSelectMode)) {
      organizationReportSelectionForm.setSubFundPickList(
          (List)
              SpringContext.getBean(BudgetReportsControlListService.class)
                  .retrieveSubFundList(principalName));
      organizationReportSelectionForm.setOperatingModeTitle(
          BCConstants.Report.SUB_FUND_SELECTION_TITLE);
    } else if (ReportSelectMode.OBJECT_CODE.equals(reportMode.reportSelectMode)
        || ReportSelectMode.REASON.equals(reportMode.reportSelectMode)) {
      organizationReportSelectionForm.setObjectCodePickList(
          (List)
              SpringContext.getBean(BudgetReportsControlListService.class)
                  .retrieveObjectCodeList(principalName));
      organizationReportSelectionForm.setOperatingModeTitle(
          BCConstants.Report.OBJECT_CODE_SELECTION_TITLE);
      organizationReportSelectionForm
          .getBudgetConstructionReportThresholdSettings()
          .setLockThreshold(reportMode.lockThreshold);
    }

    return mapping.findForward(KFSConstants.MAPPING_BASIC);
  }