FINERACT-2326: Enhanced loan retrieval API to avoid some of the N+1 queries when loading summaries

This commit is contained in:
Arnold Galovics
2025-08-24 22:03:56 +02:00
committed by Arnold Gálovics
parent 2a29b1ca0e
commit 9ef79f86a0
18 changed files with 262 additions and 46 deletions
@@ -1,34 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.service;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public final class StreamUtil {
private StreamUtil() {}
public static <A, B> Collector<A, ?, B> foldLeft(final B init, final BiFunction<? super B, ? super A, ? extends B> f) {
return Collectors.collectingAndThen(Collectors.reducing(Function.<B>identity(), a -> b -> f.apply(b, a), Function::andThen),
endo -> endo.apply(init));
}
}
@@ -27,7 +27,6 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.api.ApiParameterHelper;
import org.apache.fineract.infrastructure.core.service.StreamUtil;
import org.apache.fineract.infrastructure.dataqueries.api.RunreportsApiResource;
import org.apache.fineract.infrastructure.dataqueries.data.ReportExportType;
import org.apache.fineract.infrastructure.dataqueries.service.export.DatatableReportExportService;
@@ -35,6 +34,7 @@ import org.apache.fineract.infrastructure.dataqueries.service.export.ResponseHol
import org.apache.fineract.infrastructure.report.annotation.ReportService;
import org.apache.fineract.infrastructure.report.service.AbstractReportingProcessService;
import org.apache.fineract.infrastructure.security.service.SqlValidator;
import org.apache.fineract.util.StreamUtil;
import org.springframework.stereotype.Service;
@Service
@@ -18,6 +18,7 @@
*/
package org.apache.fineract.portfolio.loanaccount.api;
import static java.util.Collections.emptyList;
import static org.apache.fineract.portfolio.loanproduct.service.LoanEnumerations.interestType;
import com.google.gson.JsonElement;
@@ -130,7 +131,9 @@ import org.apache.fineract.portfolio.loanaccount.data.LoanApprovalData;
import org.apache.fineract.portfolio.loanaccount.data.LoanApprovedAmountHistoryData;
import org.apache.fineract.portfolio.loanaccount.data.LoanChargeData;
import org.apache.fineract.portfolio.loanaccount.data.LoanCollateralManagementData;
import org.apache.fineract.portfolio.loanaccount.data.LoanSummaryData;
import org.apache.fineract.portfolio.loanaccount.data.LoanTermVariationsData;
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionBalanceWithLoanId;
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionData;
import org.apache.fineract.portfolio.loanaccount.data.PaidInAdvanceData;
import org.apache.fineract.portfolio.loanaccount.data.RepaymentScheduleRelatedLoanData;
@@ -505,22 +508,37 @@ public class LoansApiResource {
final Page<LoanAccountData> loanBasicDetails = this.loanReadPlatformService.retrieveAll(searchParameters);
final Set<String> associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters());
if (associationParameters.contains(DataTableApiConstant.summaryAssociateParamName)) {
List<Long> loanIds = loanBasicDetails.getPageItems().stream().map(LoanAccountData::getId).toList();
Map<Long, List<DisbursementData>> disbursementDataByLoanIds = loanReadPlatformService.retrieveLoanDisbursementDetails(loanIds);
Map<Long, List<LoanTransactionRepaymentPeriodData>> repaymentPeriodDataByLoanIds = loanCapitalizedIncomeBalanceRepository
.findRepaymentPeriodDataByLoanIds(loanIds);
Map<Long, List<LoanTransactionBalanceWithLoanId>> loanTransactionBalancesByLoanIds = loanSummaryBalancesRepository
.retrieveLoanSummaryBalancesByTransactionType(loanIds, LoanApiConstants.LOAN_SUMMARY_TRANSACTION_TYPES);
loanBasicDetails.getPageItems().forEach(i -> {
if (i.getSummary() != null) {
Collection<DisbursementData> disbursementData = this.loanReadPlatformService.retrieveLoanDisbursementDetails(i.getId());
List<LoanTransactionRepaymentPeriodData> capitalizedIncomeData = this.loanCapitalizedIncomeBalanceRepository
.findRepaymentPeriodDataByLoanId(i.getId());
final RepaymentScheduleRelatedLoanData repaymentScheduleRelatedData = new RepaymentScheduleRelatedLoanData(
Long loanId = i.getId();
List<DisbursementData> disbursementData = disbursementDataByLoanIds.getOrDefault(loanId, emptyList());
List<LoanTransactionRepaymentPeriodData> capitalizedIncomeData = repaymentPeriodDataByLoanIds.getOrDefault(loanId,
emptyList());
List<LoanTransactionBalanceWithLoanId> loanTransactionBalances = loanTransactionBalancesByLoanIds.getOrDefault(loanId,
emptyList());
RepaymentScheduleRelatedLoanData repaymentScheduleRelatedData = new RepaymentScheduleRelatedLoanData(
i.getTimeline().getExpectedDisbursementDate(), i.getTimeline().getActualDisbursementDate(), i.getCurrency(),
i.getPrincipal(), i.getInArrearsTolerance(), i.getFeeChargesAtDisbursementCharged());
final LoanScheduleData repaymentSchedule = this.loanReadPlatformService.retrieveRepaymentSchedule(i.getId(),
LoanScheduleData repaymentSchedule = loanReadPlatformService.retrieveRepaymentSchedule(loanId,
repaymentScheduleRelatedData, disbursementData, capitalizedIncomeData, i.isInterestRecalculationEnabled(),
LoanScheduleType.fromEnumOptionData(i.getLoanScheduleType()));
LoanSummaryDataProvider loanSummaryDataProvider = loanSummaryProviderDelegate
.resolveLoanSummaryDataProvider(i.getTransactionProcessingStrategyCode());
i.setSummary(loanSummaryDataProvider.withTransactionAmountsSummary(i.getId(), i.getSummary(), repaymentSchedule,
loanSummaryBalancesRepository.retrieveLoanSummaryBalancesByTransactionType(i.getId(),
LoanApiConstants.LOAN_SUMMARY_TRANSACTION_TYPES)));
LoanSummaryData summaryData = loanSummaryDataProvider.withTransactionAmountsSummary(loanId, i.getSummary(),
repaymentSchedule, loanTransactionBalances);
i.setSummary(summaryData);
}
});
}
@@ -637,8 +637,8 @@ public class LoanScheduleAssembler {
.getAsBigDecimal();
}
BigDecimal waivedChargeAmount = null;
disbursementDatas.add(new DisbursementData(null, expectedDisbursementDate, null, principal, netDisbursalAmount, null,
null, waivedChargeAmount));
disbursementDatas.add(new DisbursementData(null, null, expectedDisbursementDate, null, principal, netDisbursalAmount,
null, null, waivedChargeAmount));
i++;
} while (i < disbursementDataArray.size());
}
@@ -34,6 +34,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@@ -1408,7 +1409,6 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
Integer loanTermInDays = 0;
Set<Long> disbursementPeriodIds = new HashSet<>();
while (rs.next()) {
final Integer period = JdbcSupport.getInteger(rs, "period");
LocalDate fromDate = JdbcSupport.getLocalDate(rs, "fromDate");
final LocalDate dueDate = JdbcSupport.getLocalDate(rs, "dueDate");
@@ -1941,10 +1941,16 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
@Override
public Collection<DisbursementData> retrieveLoanDisbursementDetails(final Long loanId) {
return retrieveLoanDisbursementDetails(List.of(loanId)).getOrDefault(loanId, Collections.emptyList());
}
@Override
public Map<Long, List<DisbursementData>> retrieveLoanDisbursementDetails(final List<Long> loanIds) {
Object[] parameters = sqlGenerator.inParametersFor(loanIds);
final LoanDisbursementDetailMapper rm = new LoanDisbursementDetailMapper(sqlGenerator);
final String sql = "select " + rm.schema()
+ " where dd.loan_id=? and dd.is_reversed=false group by dd.id, lc.amount_waived_derived order by dd.expected_disburse_date,dd.disbursedon_date,dd.id";
return this.jdbcTemplate.query(sql, rm, loanId); // NOSONAR
final String sql = "select " + rm.schema() + " where " + sqlGenerator.in("dd.loan_id", loanIds)
+ " and dd.is_reversed=false group by dd.id, lc.amount_waived_derived order by dd.expected_disburse_date,dd.disbursedon_date,dd.id";
return this.jdbcTemplate.query(sql, rm, parameters).stream().collect(Collectors.groupingBy(DisbursementData::getLoanId)); // NOSONAR
}
private static final class LoanDisbursementDetailMapper implements RowMapper<DisbursementData> {
@@ -1956,7 +1962,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
}
public String schema() {
return "dd.id as id,dd.expected_disburse_date as expectedDisbursementdate, dd.disbursedon_date as actualDisbursementdate,dd.principal as principal,dd.net_disbursal_amount as netDisbursalAmount,sum(lc.amount) chargeAmount, lc.amount_waived_derived waivedAmount, "
return "dd.id as id, dd.loan_id as loanId, dd.expected_disburse_date as expectedDisbursementdate, dd.disbursedon_date as actualDisbursementdate,dd.principal as principal,dd.net_disbursal_amount as netDisbursalAmount,sum(lc.amount) chargeAmount, lc.amount_waived_derived waivedAmount, "
+ sqlGenerator.groupConcat("lc.id") + " loanChargeId "
+ "from m_loan l inner join m_loan_disbursement_detail dd on dd.loan_id = l.id left join m_loan_tranche_disbursement_charge tdc on tdc.disbursement_detail_id=dd.id "
+ "left join m_loan_charge lc on lc.id=tdc.loan_charge_id and lc.is_active=true";
@@ -1965,6 +1971,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
@Override
public DisbursementData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = rs.getLong("id");
final Long loanId = rs.getLong("loanId");
final LocalDate expectedDisbursementdate = JdbcSupport.getLocalDate(rs, "expectedDisbursementdate");
final LocalDate actualDisbursementdate = JdbcSupport.getLocalDate(rs, "actualDisbursementdate");
final BigDecimal principal = rs.getBigDecimal("principal");
@@ -1975,8 +1982,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
if (chargeAmount != null && waivedAmount != null) {
chargeAmount = chargeAmount.subtract(waivedAmount);
}
return new DisbursementData(id, expectedDisbursementdate, actualDisbursementdate, principal, netDisbursalAmount, loanChargeId,
chargeAmount, waivedAmount);
return new DisbursementData(id, loanId, expectedDisbursementdate, actualDisbursementdate, principal, netDisbursalAmount,
loanChargeId, chargeAmount, waivedAmount);
}
}