@Override
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    PurchasingAccountsPayableFormBase baseForm = (PurchasingAccountsPayableFormBase) form;

    ActionForward fwd = super.execute(mapping, form, request, response);

    AccountsPayableDocumentBase document = (AccountsPayableDocumentBase) baseForm.getDocument();
    boolean foundAccountExpiredWarning = false;
    for (int i = 0; i < KNSGlobalVariables.getMessageList().size(); i++) {
      if (StringUtils.equals(
          KNSGlobalVariables.getMessageList().get(i).getErrorKey(),
          PurapKeyConstants.MESSAGE_CLOSED_OR_EXPIRED_ACCOUNTS_REPLACED)) {
        foundAccountExpiredWarning = true;
      }
    }

    if (!foundAccountExpiredWarning) {
      SpringContext.getBean(AccountsPayableService.class)
          .generateExpiredOrClosedAccountWarning(document);
    }

    return fwd;
  }
  /**
   * @see
   *     org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#route(org.apache.struts.action.ActionMapping,
   *     org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   */
  @Override
  public ActionForward route(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    AccountsPayableFormBase apForm = (AccountsPayableFormBase) form;

    // set the last update user id
    AccountsPayableDocumentBase document = (AccountsPayableDocumentBase) apForm.getDocument();
    document.setLastActionPerformedByPersonId(
        GlobalVariables.getUserSession().getPerson().getPrincipalId());

    // if form is not yet calculated, return and prompt user to calculate
    if (requiresCaculate(apForm)) {
      GlobalVariables.getMessageMap()
          .putError(
              KFSConstants.DOCUMENT_ERRORS, PurapKeyConstants.ERROR_APPROVE_REQUIRES_CALCULATE);
      return mapping.findForward(KFSConstants.MAPPING_BASIC);
    }

    // recalculate
    customCalculate((AccountsPayableDocument) apForm.getDocument());

    // route
    ActionForward forward = super.route(mapping, form, request, response);

    // if successful, then redirect back to init
    boolean successMessageFound = false;
    MessageList messageList = KNSGlobalVariables.getMessageList();
    for (int i = 0; i < messageList.size(); i++) {
      if (StringUtils.equals(
          messageList.get(i).getErrorKey(), RiceKeyConstants.MESSAGE_ROUTE_SUCCESSFUL)) {
        successMessageFound = true;
        break;
      }
    }

    if (successMessageFound) {
      String basePath =
          SpringContext.getBean(ConfigurationService.class)
              .getPropertyValueAsString(KFSConstants.APPLICATION_URL_KEY);

      Properties parameters = new Properties();
      parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.DOC_HANDLER_METHOD);
      parameters.put(KFSConstants.PARAMETER_COMMAND, "initiate");
      parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, apForm.getDocTypeName());

      String lookupUrl =
          UrlFactory.parameterizeUrl(
              basePath + "/" + "purap" + this.getActionName() + ".do", parameters);
      forward = new ActionForward(lookupUrl, true);
    }

    return forward;
  }
  /**
   * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
   */
  @Override
  public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
    Map parameters = super.getParameters();
    String errorList;

    if (parameters.containsKey(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM)) {
      String[] actionSuccessRequestParm =
          (String[]) parameters.get(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM);
      Boolean actionSuccess =
          (Boolean)
              (new BooleanFormatter()).convertFromPresentationFormat(actionSuccessRequestParm[0]);

      if (actionSuccess != null) {

        if (!actionSuccess) {

          // if the action performed on batch was not successful we get the error message list and
          // add them to
          // GlobalVariables errorMap
          if (parameters.containsKey(PdpParameterConstants.ERROR_KEY_LIST_PARAM)) {
            String[] errorListParam =
                (String[]) parameters.get(PdpParameterConstants.ERROR_KEY_LIST_PARAM);
            errorList = errorListParam[0];
            if (StringUtils.isNotEmpty(errorList)) {
              String[] errorMsgs =
                  StringUtils.split(errorList, PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR);
              for (String error : errorMsgs) {
                if (StringUtils.isNotEmpty(error)) {
                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, error);
                }
              }
            }
          }
        } else {
          if (parameters.containsKey(PdpParameterConstants.MESSAGE_PARAM)) {
            String[] messageRequestParm =
                (String[]) parameters.get(PdpParameterConstants.MESSAGE_PARAM);
            String message = messageRequestParm[0];
            KNSGlobalVariables.getMessageList().add(message);
          }
        }
      }
    }

    List results = super.getSearchResults(fieldValues);
    return results;
  }
 /** Checks for messages in GlobalVariables and places list in request attribute. */
 private void saveMessages(HttpServletRequest request) {
   if (!KNSGlobalVariables.getMessageList().isEmpty()) {
     request.setAttribute(
         KRADConstants.GLOBAL_MESSAGES, KNSGlobalVariables.getMessageList().toActionMessages());
   }
 }
  protected ActionForward askQuestionsAndPerformReopenPurchaseOrder(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    LOG.debug("askQuestionsAndPerformDocumentAction started.");
    KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
    AccountsPayableDocumentBase apDoc =
        (AccountsPayableDocumentBase) kualiDocumentFormBase.getDocument();
    Object question = request.getParameter(KFSConstants.QUESTION_INST_ATTRIBUTE_NAME);
    String questionType = PODocumentsStrings.REOPEN_PO_QUESTION;
    String confirmType = PODocumentsStrings.CONFIRM_REOPEN_QUESTION;
    String messageType = PurapKeyConstants.PURCHASE_ORDER_MESSAGE_REOPEN_DOCUMENT;
    String operation = "Reopen ";

    try {
      ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);

      // Start in logic for confirming the proposed operation.
      if (ObjectUtils.isNull(question)) {
        String key =
            kualiConfiguration.getPropertyValueAsString(
                PurapKeyConstants.PURCHASE_ORDER_QUESTION_DOCUMENT);
        String message = StringUtils.replace(key, "{0}", operation);
        return this.performQuestionWithoutInput(
            mapping,
            form,
            request,
            response,
            questionType,
            message,
            KFSConstants.CONFIRMATION_QUESTION,
            questionType,
            "");
      } else {
        Object buttonClicked = request.getParameter(KFSConstants.QUESTION_CLICKED_BUTTON);
        if (question.equals(questionType) && buttonClicked.equals(ConfirmationQuestion.NO)) {
          // If 'No' is the button clicked, just reload the doc
          return mapping.findForward(KFSConstants.MAPPING_BASIC);
        } else if (question.equals(confirmType)
            && buttonClicked.equals(SingleConfirmationQuestion.OK)) {
          // This is the case when the user clicks on "OK" in the end; redirect to the preq doc
          return mapping.findForward(KFSConstants.MAPPING_BASIC);
        }
      }

      PurchaseOrderDocument po = apDoc.getPurchaseOrderDocument();
      if (!po.isPendingActionIndicator()
          && PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED.equals(
              po.getApplicationDocumentStatus())) {
        /*
         * Below if-else code block calls PurchaseOrderService methods that will throw ValidationException objects if errors
         * occur during any process in the attempt to perform its actions. Assume, if these return successfully, that the
         * PurchaseOrderDocument object returned from each is the newly created document and that all actions in the method
         * were run correctly. NOTE: IF BELOW IF-ELSE IS EDITED THE NEW METHODS CALLED MUST THROW ValidationException OBJECT
         * IF AN ERROR IS ADDED TO THE GlobalVariables
         */
        po = initiateReopenPurchaseOrder(po, kualiDocumentFormBase.getAnnotation());

        if (!GlobalVariables.getMessageMap().hasNoErrors()) {
          throw new ValidationException("errors occurred during new PO creation");
        }

        if (StringUtils.isNotEmpty(messageType)) {
          KNSGlobalVariables.getMessageList().add(messageType);
        }
        return this.performQuestionWithoutInput(
            mapping,
            form,
            request,
            response,
            confirmType,
            kualiConfiguration.getPropertyValueAsString(messageType),
            PODocumentsStrings.SINGLE_CONFIRMATION_QUESTION,
            questionType,
            "");
      } else {
        return this.performQuestionWithoutInput(
            mapping,
            form,
            request,
            response,
            confirmType,
            "Unable to reopen the PO at this time due to the incorrect PO status or a pending PO change document.",
            PODocumentsStrings.SINGLE_CONFIRMATION_QUESTION,
            questionType,
            "");
      }

    } catch (ValidationException ve) {
      throw ve;
    }
  }