/**
   * Update Bank EFT Status.
   *
   * @param disbursements DisbursementDTO
   */
  public void updateBankEFTStatus(Collection<DisbursementDTO> disbursements) {
    for (DisbursementDTO disbursementDTOParam : disbursements) {
      DisbursementPartyDTO disbursementPartyDTO = disbursementDTOParam.getDisbursementParty();
      boolean preNoteInd = false;
      /*
       * Use MinimalPartyPojo to retrieve all other data. Note: context is
       * for getting address by context, we won't be using the address
       * data anyway
       */
      if (disbursementPartyDTO == null) {
        return;
      }
      // 12/17/2013 @GR - Performance Tuning - Fetch MinimalParty only for PreNotes
      /*MinimalPartyRequestDTO minimalPartyRequestDTO = new MinimalPartyRequestDTO();
      minimalPartyRequestDTO.setPartyId(disbursementPartyDTO
      		.getPartyIdPayeePrimary());
      /*
       * Need to set something, it's only used for fetching address, which
       * we don't care about
       *
      minimalPartyRequestDTO.setContextType("");
      MinimalPartyDTO minParty = MuleServiceFactory.getService(
      		PartyService.class).retrieveMinimalParty(
      		minimalPartyRequestDTO);
      minParty.setContext(getExportResourceManager().getContext());*/
      Collection<BankAccountDTO> bankAccounts =
          retrievePartyBankAccountInformation(disbursementPartyDTO.getPartyIdPayeePrimary());
      for (BankAccountDTO bankAccountDTO : bankAccounts) {

        // Trying to avoid multiple saves to the same Party since was producing Party Merge
        // Optimistic Locking Exception
        if (!DisbursementConstants.EFT_SENT_STAUTS.equals(bankAccountDTO.getEFTStatusCode())
            && bankAccountDTO.ispreNoteIndicator()) {
          preNoteInd = true;
          bankAccountDTO.setEFTStatusCode(DisbursementConstants.EFT_SENT_STAUTS);
          bankAccountDTO.setEFTStatusEffDate(DateUtility.getSystemDateTime());
        }
      }
      if (preNoteInd) {
        MinimalPartyRequestDTO minimalPartyRequestDTO = new MinimalPartyRequestDTO();
        minimalPartyRequestDTO.setPartyId(disbursementPartyDTO.getPartyIdPayeePrimary());
        /*
         * Need to set something, it's only used for fetching address, which
         * we don't care about
         */
        minimalPartyRequestDTO.setContextType("");
        MinimalPartyDTO minParty =
            MuleServiceFactory.getService(PartyService.class)
                .retrieveMinimalParty(minimalPartyRequestDTO);
        minParty.setBankInformations(bankAccounts);
        MuleServiceFactory.getService(PartyService.class).saveMinimalParty(minParty);
      }
    }
  }
  /**
   * Fetch disbursements only for context types supported currently by external payment processor.
   *
   * @param dtoParams Input collection of DisbursementDTO
   * @return collection of processed DisbursementImportSummaryDTOs
   */
  private Collection<DisbursementDTO> fetchValidDisbursements(
      Collection<DisbursementDTO> dtoParams) {
    FinancialTransactionDTO financialTransactionDTO = null;
    Collection<FinancialTransactionDTO> ftdtos = null;
    Collection<DisbursementDTO> validDisbursements = new ArrayList<DisbursementDTO>();
    for (DisbursementDTO dtoParam : dtoParams) {
      ftdtos = new ArrayList<FinancialTransactionDTO>();
      // fetching ftps for disbursement
      if (dtoParam
          .getFinancialAgreementTypeCode()
          .equals(FinancialsConstants.FINANCIAL_AGREEMENT_TYPE_PLCY)) {
        Collection<FTPDisbursementDTO> fTPDisbursements = dtoParam.getFtpDisbursements();
        // fetching ftps for context type agency and billing as export
        // flow support only those two types currently.
        for (FTPDisbursementDTO fTPDisbursementDTO : fTPDisbursements) {
          financialTransactionDTO =
              MuleServiceFactory.getService(FinancialsService.class)
                  .retrieveFinancialTransaction(
                      fTPDisbursementDTO.getFtpId(), dtoParam.getFinancialAgreementTypeCode());

          String contextType = financialTransactionDTO.getFinancialContext().getContextType();
          getExportResourceManager().setContext(contextType);
          if (FinancialsConstants.AGREEMENT_TYPE_BILLING.equals(contextType)
              || FinancialsConstants.AGREEMENT_TYPE_AGENCY.equals(contextType)) {
            ftdtos.add(financialTransactionDTO);
            validDisbursements.add(dtoParam);
          } else {
            // logging info messages if disbursement is not of type
            // billing or agency.
            logger.info(
                "Processor doesn't support for type - {} " + "and Context for Disbursement ID = {}",
                contextType,
                dtoParam.getRecordId());
          }
        }
        if (!ftdtos.isEmpty()) {
          getExportResourceManager().getDsbFtpMap().put(dtoParam.getRecordId(), ftdtos);
        }
      } else if (dtoParam
          .getFinancialAgreementTypeCode()
          .equals(FinancialsConstants.FINANCIAL_AGREEMENT_TYPE_CLAIM)) {
        getExportResourceManager().setContext(FinancialsConstants.FINANCIAL_AGREEMENT_TYPE_CLAIM);

        // Collection<PaymentClaimInfo> claimCollection =
        // MuleServiceFactory.getService(ClaimFinancialsProxyService.class).retrieveClaimInfo(dtoParam.getRecordId());
        // paymentClaimCollection = new ArrayList<PaymentClaimInfo>();
        // for (PaymentClaimInfo paymentClaimInfo : claimCollection) {
        // paymentClaimCollection.add(paymentClaimInfo);
        // }
        validDisbursements.add(dtoParam);
        // if (!paymentClaimCollection.isEmpty()){
        // getExportResourceManager().getClaimDsbMap().put(dtoParam.getRecordId(),
        // paymentClaimCollection);
        // }
      }
    }
    return validDisbursements;
  }
  private boolean disbursementAlreadyIssued(DisbursementDTO disbursement) {

    boolean issued = false;
    for (DisbursementStatusDTO status : disbursement.getDisbursementStatuses()) {
      if (DisbursementConstants.DISB_STATUS_TYPE_CODE_ISSUED_PAYMENT.equals(
          status.getDisbursementStatusType())) {
        issued = true;
        break;
      }
    }
    return issued;
  }
  private boolean updateManualCheckDisbursementStatus(DisbursementDTO disbursement) {

    boolean updated = false;

    if ((DisbursementConstants.PAY_PAYEE_CHECK.equals(
            disbursement.getDisbursementMethodPaymentCode()))
        && (disbursement.isManualCheckIndicator())) {

      Collection<DisbursementStatusDTO> disbursementStatusList =
          disbursement.getDisbursementStatuses();
      for (DisbursementStatusDTO disbursementStatus : disbursementStatusList) {
        if (DisbursementConstants.DISB_STATUS_TYPE_CODE_ISSUED_PAYMENT.equals(
                disbursementStatus.getDisbursementStatusType())
            && (DisbursementConstants.DISB_STATUS_REASON_CODE_MANUAL_PAYMENT.equals(
                disbursementStatus.getDisbursementStatusReason()))) {
          disbursementStatus.setDisbursementStatusReason(
              DisbursementConstants.DISB_STATUS_REASON_CODE_SENT_TO_PAYMENT_PROCESSOR);
          DTOUtils.saveEntityWithAssociations(lds, disbursement);
          updated = true;
        }
      }
    }
    return updated;
  }
  /**
   * The method should be invoked before collection of disbursement DTO has been saved within
   * staging database.
   *
   * @param dtos Input collection of DisbursementDTO
   * @return collection of DisbursementDTO processed successfully.
   */
  public Collection<DisbursementDTO> preProcess(Collection<DisbursementDTO> dtos) {
    /*
     * Disbursement State of "Incomplete" and Disbursement
     * Status of "Incomplete Payment" that have not been sent to Payment
     * Processor for processing and a Disbursement Date less than or equal
     * to the Business Date is eligible for processing.
     */

    Collection<DisbursementDTO> validDisbursement = new ArrayList<DisbursementDTO>();
    if (dtos != null && dtos.size() > 0) {
      logger.debug(
          "Process Started. [ClientExportPaymentHelper] Validate disbursement to be exported.");
      Date businessDate =
          MuleServiceFactory.getService(DateService.class)
              .getBusinessDate(1L, BusinessDateType.BUSINESS);

      for (DisbursementDTO dto : dtos) {

        if (DisbursementConstants.DISB_STATE_TYPE_CODE_INCOMPLETE.equalsIgnoreCase(
                dto.getStateCode())
            && DisbursementConstants.DISB_STATUS_TYPE_CODE_INCOMPLETE_PAYMENT.equalsIgnoreCase(
                dto.getStatusCode())
            && (DateUtility.compareDate(dto.getEffectiveDate(), businessDate) <= 0)
            && (dto.getEndDate() == null
                || DateUtility.compareDate(dto.getEndDate(), businessDate) >= 0)) {
          validDisbursement.add(dto);

        } else if (DisbursementConstants.DISB_STATE_TYPE_CODE_OPEN.equalsIgnoreCase(
                dto.getStateCode())
            && DisbursementConstants.DISB_STATUS_TYPE_CODE_PAYMENT_STOPPED_NOT_REISSUED
                .equalsIgnoreCase(dto.getStatusCode())
            && (DateUtility.compareDate(dto.getEffectiveDate(), businessDate) <= 0)
            && (dto.getEndDate() == null
                || DateUtility.compareDate(dto.getEndDate(), businessDate) >= 0)) {
          validDisbursement.add(dto);
        } else if (DisbursementConstants.DISB_STATE_TYPE_CODE_OPEN.equalsIgnoreCase(
                dto.getStateCode())
            && DisbursementConstants.DISB_STATUS_TYPE_CODE_ISSUED_PAYMENT.equalsIgnoreCase(
                dto.getStatusCode())
            && (dto.getDisbursementStatuses().size() == 1)
            && (DateUtility.compareDate(dto.getEffectiveDate(), businessDate) <= 0)
            && (dto.getEndDate() == null
                || DateUtility.compareDate(dto.getEndDate(), businessDate) >= 0)) {
          validDisbursement.add(dto);
        } else if (DisbursementConstants.DISB_STATE_TYPE_CODE_COMPLETE.equalsIgnoreCase(
                dto.getStateCode())
            && DisbursementConstants.DISB_STATUS_TYPE_CODE_PAYMENT_VOIDED_NOT_REISSUED
                .equalsIgnoreCase(dto.getStatusCode())
            && (DateUtility.compareDate(dto.getEffectiveDate(), businessDate) <= 0)
            && (dto.getEndDate() == null
                || DateUtility.compareDate(dto.getEndDate(), businessDate) >= 0)) {
          validDisbursement.add(dto);
        } else {
          logger.debug(
              "[ClientExportPaymentHelper] Criteria Not Met for Disbursement ID = {} ",
              dto.getRecordId());
        }
      }
      // Flush all context to re initialize sequence generator.
      ExportPaymentSequenceGenHelper.flushAllContext();
    }
    return validDisbursement;
  }
  /**
   * Update Disbursement Status.
   *
   * @param disbursements DisbursementDTO collection
   */
  public void updateDisbursementStatus(Collection<DisbursementDTO> disbursements) {
    for (DisbursementDTO dto : disbursements) {
      Collection<DisbursementStatusDTO> statusColl = dto.getDisbursementStatuses();

      /* This logic is updating the existing man_pay record, This should be no different from regular status update, expire the existing record
       * and create a new record for DISB_STATUS_REASON_CODE_SENT_TO_PAYMENT_PROCESSOR
       * if (updateManualCheckDisbursementStatus(dto)) {
       continue;
      }*/
      DisbursementStatusDTO voidedDisbStatus = null;
      for (DisbursementStatusDTO disbursementStatusDTO : statusColl) {
        if (disbursementStatusDTO.getEndDateTime() == null) {
          disbursementStatusDTO.setEndDateTime(DateUtility.getSystemDateTime());
        }
        // Check for voids and stop pay no issue statuses
        if (disbursementStatusDTO
                .getDisbursementStatusType()
                .equals(DisbursementConstants.DISB_STATUS_TYPE_CODE_PAYMENT_VOIDED_NOT_REISSUED)
            || disbursementStatusDTO
                .getDisbursementStatusType()
                .equals(DisbursementConstants.DISB_STATUS_TYPE_CODE_PAYMENT_STOPPED_NOT_REISSUED)) {
          voidedDisbStatus = disbursementStatusDTO;
        }
      }

      DisbursementStatusDTO currentStatus = new DisbursementStatusDTO();

      if (voidedDisbStatus != null) {
        // If voided copy the current codes for the new status record.
        currentStatus.setDisbursementStatusType(voidedDisbStatus.getDisbursementStatusType());
        currentStatus.setDisbursementState(voidedDisbStatus.getDisbursementState());
      } else if ((dto.isManualCheckIndicator())
          || (dto.getFinancialAgreementTypeCode().equals("dsb")
              && dto.getDisbursementTypeCode().equals("prenote"))) {
        currentStatus.setDisbursementStatusType(
            DisbursementConstants.DISB_STATUS_TYPE_CODE_ISSUED_PAYMENT);
        currentStatus.setDisbursementState(DisbursementConstants.DISB_STATE_TYPE_CODE_OPEN);
      } else {
        currentStatus.setDisbursementStatusType(
            DisbursementConstants.DISB_STATUS_TYPE_CODE_AWAITING_PAYMENT);
        currentStatus.setDisbursementState(DisbursementConstants.DISB_STATE_TYPE_CODE_INCOMPLETE);
      }

      currentStatus.setDisbursementStatusReason(
          DisbursementConstants.DISB_STATUS_REASON_CODE_SENT_TO_PAYMENT_PROCESSOR);
      currentStatus.setEffectiveDateTime(DateUtility.getSystemDateTime());

      /**
       * AP Export Performance Fix - @GR - 03/30/2014 - Save just the Disbursement Status record.
       */
      // dto.getDisbursementStatuses().add(currentStatus);
      statusColl.add(currentStatus);
      // DTOUtils.saveEntityWithAssociations(lds, dto);
      /*
       * Save the DisbursementStatusBO along with all its
       * associations from the values in DTO
       */
      // fetch DisbursementBO
      DisbursementBO disbursement = getDisbursementDao().retrieveDisbursement(dto.getRecordId());
      /*
       * Instantiate the custom mapper for
       * DisbursementStatusBO
       */
      DisbursementStatusDTOMapper disbStatusDTOMapper =
          new DisbursementStatusDTOMapper(disbursement);

      for (DisbursementStatusDTO disbstatus : statusColl) {
        DTOUtils.saveEntityWithAssociations(
            lds, disbstatus, DisbursementStatusBO.class, disbStatusDTOMapper, true);
      }
    }
  }