/**
   * @param loanProductId
   * @param paymentTypeToGLAccountMappers
   * @return
   */
  private List<PaymentTypeToGLAccountMapper> fetchPaymentTypeToFundSourceMappings(
      final PortfolioProductType portfolioProductType, final Long loanProductId) {
    final ProductToGLAccountMappingMapper rm = new ProductToGLAccountMappingMapper();
    final String sql = "select " + rm.schema() + " and product_id = ? and payment_type is not null";

    final List<Map<String, Object>> paymentTypeToFundSourceMappingsList =
        this.jdbcTemplate.query(
            sql, rm, new Object[] {portfolioProductType.getValue(), loanProductId});

    List<PaymentTypeToGLAccountMapper> paymentTypeToGLAccountMappers = null;
    for (final Map<String, Object> productToGLAccountMap : paymentTypeToFundSourceMappingsList) {
      if (paymentTypeToGLAccountMappers == null) {
        paymentTypeToGLAccountMappers = new ArrayList<>();
      }
      final Long paymentTypeId = (Long) productToGLAccountMap.get("paymentTypeId");
      final String paymentTypeValue = (String) productToGLAccountMap.get("paymentTypeValue");
      final CodeValueData codeValueData = CodeValueData.instance(paymentTypeId, paymentTypeValue);
      final Long glAccountId = (Long) productToGLAccountMap.get("glAccountId");
      final String glAccountName = (String) productToGLAccountMap.get("glAccountName");
      final String glCode = (String) productToGLAccountMap.get("glCode");
      final GLAccountData gLAccountData = new GLAccountData(glAccountId, glAccountName, glCode);

      final PaymentTypeToGLAccountMapper paymentTypeToGLAccountMapper =
          new PaymentTypeToGLAccountMapper(codeValueData, gLAccountData);
      paymentTypeToGLAccountMappers.add(paymentTypeToGLAccountMapper);
    }
    return paymentTypeToGLAccountMappers;
  }
  private List<ChargeToGLAccountMapper> fetchChargeToIncomeAccountMappings(
      final PortfolioProductType portfolioProductType,
      final Long loanProductId,
      final boolean penalty) {
    final ProductToGLAccountMappingMapper rm = new ProductToGLAccountMappingMapper();
    String sql =
        "select "
            + rm.schema()
            + " and product_id = ? and mapping.charge_id is not null and charge.is_penalty=";
    if (penalty) {
      sql = sql + " 1";
    } else {
      sql = sql + " 0";
    }

    final List<Map<String, Object>> chargeToFundSourceMappingsList =
        this.jdbcTemplate.query(
            sql, rm, new Object[] {portfolioProductType.getValue(), loanProductId});
    List<ChargeToGLAccountMapper> chargeToGLAccountMappers = null;
    for (final Map<String, Object> chargeToIncomeAccountMap : chargeToFundSourceMappingsList) {
      if (chargeToGLAccountMappers == null) {
        chargeToGLAccountMappers = new ArrayList<>();
      }
      final Long glAccountId = (Long) chargeToIncomeAccountMap.get("glAccountId");
      final String glAccountName = (String) chargeToIncomeAccountMap.get("glAccountName");
      final String glCode = (String) chargeToIncomeAccountMap.get("glCode");
      final GLAccountData gLAccountData = new GLAccountData(glAccountId, glAccountName, glCode);
      final Long chargeId = (Long) chargeToIncomeAccountMap.get("chargeId");
      final String chargeName = (String) chargeToIncomeAccountMap.get("chargeName");
      final Boolean penalty1 = (Boolean) chargeToIncomeAccountMap.get("penalty");
      final ChargeData chargeData = ChargeData.lookup(chargeId, chargeName, penalty1);
      final ChargeToGLAccountMapper chargeToGLAccountMapper =
          new ChargeToGLAccountMapper(chargeData, gLAccountData);
      chargeToGLAccountMappers.add(chargeToGLAccountMapper);
    }
    return chargeToGLAccountMappers;
  }
    @Override
    public AutoPostingData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum)
        throws SQLException {

      final Long id = rs.getLong("id");
      final String name = rs.getString("name");
      final String description = rs.getString("description");
      final Long officeId = JdbcSupport.getLong(rs, "officeId");
      final String officeName = rs.getString("officeName");
      final int productTypeEnum = rs.getInt("productTypeEnum");
      final Long productId = JdbcSupport.getLong(rs, "productId");
      final Long chargeId = JdbcSupport.getLong(rs, "chargeId");
      final Long accountingRuleId = JdbcSupport.getLong(rs, "accountingRuleId");
      final Long eventId = JdbcSupport.getLong(rs, "eventId");
      final Long eventAttributeId = JdbcSupport.getLong(rs, "eventAttributeId");
      final Long debitAccountId = JdbcSupport.getLong(rs, "debitAccountId");
      final Long creditAccountId = JdbcSupport.getLong(rs, "creditAccountId");
      final String savingProductName = rs.getString("savingProductName");
      final String loanProductName = rs.getString("loanProductName");
      final String chargeName = rs.getString("chargeName");
      final String eventName = rs.getString("eventName");
      final String eventAttributeName = rs.getString("eventAttributeName");
      final boolean chargeIsPenalty = rs.getBoolean("isPenalty");

      OfficeData officeData = null;
      if (officeId != null) {
        officeData = OfficeData.dropdown(officeId, officeName, null);
      }

      SavingsProductData savingsProductData = null;
      LoanProductData loanProductData = null;

      PortfolioProductType portfolioProductType = PortfolioProductType.fromInt(productTypeEnum);
      EnumOptionData productType =
          AccountingEnumerations.portfolioProductType(portfolioProductType);
      if (portfolioProductType.isLoanProduct() && productId != null) {
        savingsProductData = SavingsProductData.lookup(productId, savingProductName);
      } else if (portfolioProductType.isSavingProduct() && productId != null) {
        loanProductData = LoanProductData.lookup(productId, loanProductName);
      }

      ChargeData chargeData = null;
      if (chargeId != null) {
        chargeData = ChargeData.lookup(chargeId, chargeName, chargeIsPenalty);
      }

      CodeData event = CodeData.instance(eventId, eventName, true);

      CodeValueData eventAttribute = null;
      if (eventAttributeId != null) {
        eventAttribute = CodeValueData.instance(eventAttributeId, eventAttributeName);
      }

      AccountingRuleData accountingRuleData =
          new AccountingRuleData(accountingRuleId, debitAccountId, creditAccountId);

      return new AutoPostingData(
          id,
          name,
          description,
          officeData,
          productType,
          loanProductData,
          savingsProductData,
          chargeData,
          event,
          eventAttribute,
          accountingRuleData);
    }