/**
   * 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;
  }
  /**
   * 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);
      }
    }
  }