FINERACT-2326: Enhanced loan retrieval API to avoid some of the N+1 queries when loading summaries
This commit is contained in:
committed by
Arnold Gálovics
parent
2a29b1ca0e
commit
9ef79f86a0
+1
@@ -177,6 +177,7 @@ public final class ErrorHandler {
|
|||||||
msg = defaultMsg == null ? cause.getMessage() : defaultMsg;
|
msg = defaultMsg == null ? cause.getMessage() : defaultMsg;
|
||||||
if (nre instanceof NonTransientDataAccessException) {
|
if (nre instanceof NonTransientDataAccessException) {
|
||||||
msgCode = msgCode == null ? codePfx + ".data.integrity.issue" : msgCode;
|
msgCode = msgCode == null ? codePfx + ".data.integrity.issue" : msgCode;
|
||||||
|
log.warn("Handled exception is", nre);
|
||||||
return new PlatformDataIntegrityException(msgCode, msg, param, args);
|
return new PlatformDataIntegrityException(msgCode, msg, param, args);
|
||||||
} else if (cause instanceof OptimisticLockException) {
|
} else if (cause instanceof OptimisticLockException) {
|
||||||
return (RuntimeException) cause;
|
return (RuntimeException) cause;
|
||||||
|
|||||||
+70
-6
@@ -22,28 +22,29 @@ import static java.lang.String.format;
|
|||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.fineract.infrastructure.core.service.DateUtils;
|
import org.apache.fineract.infrastructure.core.service.DateUtils;
|
||||||
import org.apache.fineract.infrastructure.dataqueries.data.ResultsetColumnHeaderData;
|
import org.apache.fineract.infrastructure.dataqueries.data.ResultsetColumnHeaderData;
|
||||||
import org.apache.logging.log4j.util.Strings;
|
import org.apache.logging.log4j.util.Strings;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceUtils;
|
||||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class DatabaseSpecificSQLGenerator {
|
public class DatabaseSpecificSQLGenerator {
|
||||||
|
|
||||||
private final DatabaseTypeResolver databaseTypeResolver;
|
private final DatabaseTypeResolver databaseTypeResolver;
|
||||||
|
private final RoutingDataSource dataSource;
|
||||||
public static final String SELECT_CLAUSE = "SELECT %s";
|
public static final String SELECT_CLAUSE = "SELECT %s";
|
||||||
|
public static final int IN_CLAUSE_MAX_PARAMS = 10_000;
|
||||||
@Autowired
|
|
||||||
public DatabaseSpecificSQLGenerator(DatabaseTypeResolver databaseTypeResolver) {
|
|
||||||
this.databaseTypeResolver = databaseTypeResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DatabaseType getDialect() {
|
public DatabaseType getDialect() {
|
||||||
return databaseTypeResolver.databaseType();
|
return databaseTypeResolver.databaseType();
|
||||||
@@ -298,4 +299,67 @@ public class DatabaseSpecificSQLGenerator {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds an SQL fragment for filtering a column by a list of IDs in a dialect-specific way.
|
||||||
|
* <p>
|
||||||
|
* For PostgreSQL:
|
||||||
|
* <ul>
|
||||||
|
* <li>Returns a fragment using {@code = ANY (?)}, where the single {@code ?} is bound to a SQL array.</li>
|
||||||
|
* <li>This avoids the PostgreSQL limit of 65,535 bind parameters, since all IDs are passed as one array
|
||||||
|
* parameter.</li>
|
||||||
|
* </ul>
|
||||||
|
* For MySQL:
|
||||||
|
* <ul>
|
||||||
|
* <li>Returns a fragment using {@code IN (?, ?, ...)}, expanding placeholders to match the number of IDs.</li>
|
||||||
|
* <li>MySQL does not support array parameters, so each ID must be bound as an individual parameter.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param column
|
||||||
|
* the name of the column to filter on (e.g. {@code "id"})
|
||||||
|
* @param ids
|
||||||
|
* the list of IDs to include in the condition; must not be empty
|
||||||
|
* @return an SQL fragment representing the {@code IN} condition, ready to be appended to a query
|
||||||
|
*/
|
||||||
|
public String in(String column, List<Long> ids) {
|
||||||
|
return switch (getDialect()) {
|
||||||
|
case POSTGRESQL -> column + " = ANY (?)";
|
||||||
|
case MYSQL -> {
|
||||||
|
String inSql = String.join(",", Collections.nCopies(ids.size(), "?"));
|
||||||
|
yield column + " IN (" + inSql + ")";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the bind parameter values corresponding to the SQL fragment generated by {@link #in(String, List)}.
|
||||||
|
* <p>
|
||||||
|
* For PostgreSQL:
|
||||||
|
* <ul>
|
||||||
|
* <li>Returns a single-element array containing a {@link java.sql.Array} of type {@code bigint[]}.</li>
|
||||||
|
* <li>This array should be bound to the single {@code ?} placeholder in the {@code = ANY (?)} fragment.</li>
|
||||||
|
* </ul>
|
||||||
|
* For MySQL:
|
||||||
|
* <ul>
|
||||||
|
* <li>Returns an {@code Object[]} of the individual ID values, one per placeholder.</li>
|
||||||
|
* <li>This matches the expanded {@code IN (?, ?, ...)} fragment produced by {@link #in(String, List)}.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param ids
|
||||||
|
* the list of IDs to be bound; must not be empty
|
||||||
|
* @return an array of parameter values to bind in the same order as the placeholders
|
||||||
|
* @throws RuntimeException
|
||||||
|
* if PostgreSQL array creation fails due to a {@link java.sql.SQLException}
|
||||||
|
*/
|
||||||
|
public Object[] inParametersFor(List<Long> ids) {
|
||||||
|
return switch (getDialect()) {
|
||||||
|
case POSTGRESQL -> {
|
||||||
|
try {
|
||||||
|
yield new Object[] { DataSourceUtils.getConnection(dataSource).createArrayOf("bigint", ids.toArray(new Long[0])) };
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case MYSQL -> ids.toArray();
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/**
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collector that merges a stream of maps (with list values) into a single map.
|
||||||
|
* <p>
|
||||||
|
* If the same key appears in multiple maps, the lists are concatenated.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* <pre>{@code
|
||||||
|
*
|
||||||
|
* Map<Long, List<Foo>> merged = streamOfMaps.collect(StreamUtils.mergeMapsOfLists());
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param <K>
|
||||||
|
* the type of map keys
|
||||||
|
* @param <V>
|
||||||
|
* the type of elements in the value lists
|
||||||
|
* @return a collector producing a merged map with concatenated list values
|
||||||
|
*/
|
||||||
|
public static <K, V> Collector<Map<K, List<V>>, ?, Map<K, List<V>>> mergeMapsOfLists() {
|
||||||
|
return Collectors.collectingAndThen(Collectors.flatMapping((Map<K, List<V>> m) -> m.entrySet().stream(),
|
||||||
|
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (list1, list2) -> {
|
||||||
|
List<V> merged = new ArrayList<>(list1);
|
||||||
|
merged.addAll(list2);
|
||||||
|
return merged;
|
||||||
|
})), HashMap::new // ensures the result is mutable
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-1
@@ -25,7 +25,9 @@ import org.mockito.Mockito;
|
|||||||
public class DatabaseSpecificSQLGeneratorTest {
|
public class DatabaseSpecificSQLGeneratorTest {
|
||||||
|
|
||||||
private final DatabaseTypeResolver databaseTypeResolver = Mockito.mock(DatabaseTypeResolver.class);
|
private final DatabaseTypeResolver databaseTypeResolver = Mockito.mock(DatabaseTypeResolver.class);
|
||||||
private final DatabaseSpecificSQLGenerator databaseSpecificSQLGenerator = new DatabaseSpecificSQLGenerator(databaseTypeResolver);
|
private final RoutingDataSource dataSource = Mockito.mock(RoutingDataSource.class);
|
||||||
|
private final DatabaseSpecificSQLGenerator databaseSpecificSQLGenerator = new DatabaseSpecificSQLGenerator(databaseTypeResolver,
|
||||||
|
dataSource);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCountQueryResultOnEmptyString() {
|
public void testCountQueryResultOnEmptyString() {
|
||||||
|
|||||||
+2
@@ -33,6 +33,7 @@ import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanSchedul
|
|||||||
public final class DisbursementData implements LoanPrincipalRelatedDataHolder, Comparable<DisbursementData> {
|
public final class DisbursementData implements LoanPrincipalRelatedDataHolder, Comparable<DisbursementData> {
|
||||||
|
|
||||||
private final Long id;
|
private final Long id;
|
||||||
|
private final Long loanId;
|
||||||
private final LocalDate expectedDisbursementDate;
|
private final LocalDate expectedDisbursementDate;
|
||||||
private final LocalDate actualDisbursementDate;
|
private final LocalDate actualDisbursementDate;
|
||||||
private final BigDecimal principal;
|
private final BigDecimal principal;
|
||||||
@@ -61,6 +62,7 @@ public final class DisbursementData implements LoanPrincipalRelatedDataHolder, C
|
|||||||
this.note = "";
|
this.note = "";
|
||||||
this.linkAccountId = linkAccountId;
|
this.linkAccountId = linkAccountId;
|
||||||
this.id = null;
|
this.id = null;
|
||||||
|
this.loanId = null;
|
||||||
this.expectedDisbursementDate = null;
|
this.expectedDisbursementDate = null;
|
||||||
this.principal = null;
|
this.principal = null;
|
||||||
this.loanChargeId = null;
|
this.loanChargeId = null;
|
||||||
|
|||||||
+1
-1
@@ -77,7 +77,7 @@ public class RepaymentScheduleRelatedLoanData {
|
|||||||
|
|
||||||
public DisbursementData disbursementData() {
|
public DisbursementData disbursementData() {
|
||||||
BigDecimal waivedChargeAmount = null;
|
BigDecimal waivedChargeAmount = null;
|
||||||
return new DisbursementData(null, this.expectedDisbursementDate, this.actualDisbursementDate, this.principal,
|
return new DisbursementData(null, null, this.expectedDisbursementDate, this.actualDisbursementDate, this.principal,
|
||||||
this.netDisbursalAmount, null, null, waivedChargeAmount);
|
this.netDisbursalAmount, null, null, waivedChargeAmount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -122,8 +122,8 @@ public class LoanDisbursementDetails extends AbstractPersistableCustom<Long> {
|
|||||||
public DisbursementData toData() {
|
public DisbursementData toData() {
|
||||||
LocalDate expectedDisburseDate = expectedDisbursementDateAsLocalDate();
|
LocalDate expectedDisburseDate = expectedDisbursementDateAsLocalDate();
|
||||||
BigDecimal waivedChargeAmount = null;
|
BigDecimal waivedChargeAmount = null;
|
||||||
return new DisbursementData(getId(), expectedDisburseDate, this.actualDisbursementDate, this.principal, this.netDisbursalAmount,
|
return new DisbursementData(getId(), loan.getId(), expectedDisburseDate, this.actualDisbursementDate, this.principal,
|
||||||
null, null, waivedChargeAmount);
|
this.netDisbursalAmount, null, null, waivedChargeAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateActualDisbursementDate(LocalDate actualDisbursementDate) {
|
public void updateActualDisbursementDate(LocalDate actualDisbursementDate) {
|
||||||
|
|||||||
+1
-1
@@ -2030,7 +2030,7 @@ public abstract class AbstractCumulativeLoanScheduleGenerator implements LoanSch
|
|||||||
} else {
|
} else {
|
||||||
if (loanApplicationTerms.getDisbursementDatas().isEmpty()) {
|
if (loanApplicationTerms.getDisbursementDatas().isEmpty()) {
|
||||||
loanApplicationTerms.getDisbursementDatas()
|
loanApplicationTerms.getDisbursementDatas()
|
||||||
.add(new DisbursementData(1L, loanApplicationTerms.getExpectedDisbursementDate(),
|
.add(new DisbursementData(1L, null, loanApplicationTerms.getExpectedDisbursementDate(),
|
||||||
loanApplicationTerms.getExpectedDisbursementDate(), loanApplicationTerms.getPrincipal().getAmount(), null,
|
loanApplicationTerms.getExpectedDisbursementDate(), loanApplicationTerms.getPrincipal().getAmount(), null,
|
||||||
null, null, null));
|
null, null, null));
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -92,8 +92,9 @@ public class LoanMapper {
|
|||||||
actualDisbursementDate = loanDisbursementDetails.actualDisbursementDate();
|
actualDisbursementDate = loanDisbursementDetails.actualDisbursementDate();
|
||||||
}
|
}
|
||||||
BigDecimal waivedChargeAmount = null;
|
BigDecimal waivedChargeAmount = null;
|
||||||
disbursementData.add(new DisbursementData(loanDisbursementDetails.getId(), expectedDisbursementDate, actualDisbursementDate,
|
disbursementData.add(
|
||||||
loanDisbursementDetails.principal(), loan.getNetDisbursalAmount(), null, null, waivedChargeAmount));
|
new DisbursementData(loanDisbursementDetails.getId(), loan.getId(), expectedDisbursementDate, actualDisbursementDate,
|
||||||
|
loanDisbursementDetails.principal(), loan.getNetDisbursalAmount(), null, null, waivedChargeAmount));
|
||||||
}
|
}
|
||||||
|
|
||||||
return disbursementData;
|
return disbursementData;
|
||||||
|
|||||||
+3
@@ -22,6 +22,7 @@ import jakarta.validation.constraints.NotNull;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.apache.fineract.infrastructure.core.domain.ExternalId;
|
import org.apache.fineract.infrastructure.core.domain.ExternalId;
|
||||||
import org.apache.fineract.infrastructure.core.service.Page;
|
import org.apache.fineract.infrastructure.core.service.Page;
|
||||||
@@ -104,6 +105,8 @@ public interface LoanReadPlatformService {
|
|||||||
|
|
||||||
Collection<DisbursementData> retrieveLoanDisbursementDetails(Long loanId);
|
Collection<DisbursementData> retrieveLoanDisbursementDetails(Long loanId);
|
||||||
|
|
||||||
|
Map<Long, List<DisbursementData>> retrieveLoanDisbursementDetails(List<Long> loanIds);
|
||||||
|
|
||||||
DisbursementData retrieveLoanDisbursementDetail(Long loanId, Long disbursementId);
|
DisbursementData retrieveLoanDisbursementDetail(Long loanId, Long disbursementId);
|
||||||
|
|
||||||
LoanTransactionData retrieveRecoveryPaymentTemplate(Long loanId);
|
LoanTransactionData retrieveRecoveryPaymentTemplate(Long loanId);
|
||||||
|
|||||||
+1
-1
@@ -281,7 +281,7 @@ public class ProgressiveLoanScheduleGenerator implements LoanScheduleGenerator {
|
|||||||
private void prepareDisbursementsOnLoanApplicationTerms(final LoanApplicationTerms loanApplicationTerms) {
|
private void prepareDisbursementsOnLoanApplicationTerms(final LoanApplicationTerms loanApplicationTerms) {
|
||||||
if (loanApplicationTerms.getDisbursementDatas().isEmpty()) {
|
if (loanApplicationTerms.getDisbursementDatas().isEmpty()) {
|
||||||
loanApplicationTerms.getDisbursementDatas()
|
loanApplicationTerms.getDisbursementDatas()
|
||||||
.add(new DisbursementData(1L, loanApplicationTerms.getExpectedDisbursementDate(),
|
.add(new DisbursementData(1L, null, loanApplicationTerms.getExpectedDisbursementDate(),
|
||||||
loanApplicationTerms.getExpectedDisbursementDate(), loanApplicationTerms.getPrincipal().getAmount(), null, null,
|
loanApplicationTerms.getExpectedDisbursementDate(), loanApplicationTerms.getPrincipal().getAmount(), null, null,
|
||||||
null, null));
|
null, null));
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-12
@@ -16,19 +16,13 @@
|
|||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package org.apache.fineract.infrastructure.core.service;
|
package org.apache.fineract.portfolio.loanaccount.repository;
|
||||||
|
|
||||||
import java.util.function.BiFunction;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.Map;
|
||||||
import java.util.stream.Collector;
|
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepaymentPeriodData;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public final class StreamUtil {
|
public interface CustomizedLoanCapitalizedIncomeBalanceRepository {
|
||||||
|
|
||||||
private StreamUtil() {}
|
Map<Long, List<LoanTransactionRepaymentPeriodData>> findRepaymentPeriodDataByLoanIds(List<Long> loanIds);
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
/**
|
||||||
|
* 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.portfolio.loanaccount.repository;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.TypedQuery;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
|
||||||
|
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepaymentPeriodData;
|
||||||
|
import org.apache.fineract.util.StreamUtil;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CustomizedLoanCapitalizedIncomeBalanceRepositoryImpl implements CustomizedLoanCapitalizedIncomeBalanceRepository {
|
||||||
|
|
||||||
|
private final EntityManager entityManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Long, List<LoanTransactionRepaymentPeriodData>> findRepaymentPeriodDataByLoanIds(List<Long> loanIds) {
|
||||||
|
List<List<Long>> partitions = Lists.partition(loanIds, DatabaseSpecificSQLGenerator.IN_CLAUSE_MAX_PARAMS);
|
||||||
|
return partitions.stream().map(this::doFindRepaymentPeriodDataByLoanIds).collect(StreamUtil.mergeMapsOfLists());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Long, List<LoanTransactionRepaymentPeriodData>> doFindRepaymentPeriodDataByLoanIds(List<Long> loanIds) {
|
||||||
|
// making the List serializable since sometimes it's just not
|
||||||
|
// Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type
|
||||||
|
// class java.util.ImmutableCollections$SubList for parameter loanIds with expected type of
|
||||||
|
// interface java.io.Serializable from query string ...
|
||||||
|
loanIds = new ArrayList<>(loanIds);
|
||||||
|
|
||||||
|
TypedQuery<LoanTransactionRepaymentPeriodData> query = entityManager.createQuery(
|
||||||
|
LoanCapitalizedIncomeBalanceRepository.FIND_BALANCE_REPAYMENT_SCHEDULE_DATA + " WHERE lcib.loan.id IN (:loanIds)",
|
||||||
|
LoanTransactionRepaymentPeriodData.class);
|
||||||
|
query.setParameter("loanIds", loanIds);
|
||||||
|
List<LoanTransactionRepaymentPeriodData> result = query.getResultList();
|
||||||
|
return result.stream().collect(Collectors.groupingBy(LoanTransactionRepaymentPeriodData::getLoanId));
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -26,8 +26,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
public interface LoanCapitalizedIncomeBalanceRepository
|
public interface LoanCapitalizedIncomeBalanceRepository extends JpaRepository<LoanCapitalizedIncomeBalance, Long>,
|
||||||
extends JpaRepository<LoanCapitalizedIncomeBalance, Long>, JpaSpecificationExecutor<LoanCapitalizedIncomeBalance> {
|
JpaSpecificationExecutor<LoanCapitalizedIncomeBalance>, CustomizedLoanCapitalizedIncomeBalanceRepository {
|
||||||
|
|
||||||
String FIND_BALANCE_REPAYMENT_SCHEDULE_DATA = "SELECT new org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepaymentPeriodData(lcib.loanTransaction.id, lcib.loan.id, lcib.loanTransaction.dateOf, lcib.loanTransaction.reversed, lcib.amount, lcib.unrecognizedAmount, lcib.loanTransaction.feeChargesPortion) FROM LoanCapitalizedIncomeBalance lcib ";
|
String FIND_BALANCE_REPAYMENT_SCHEDULE_DATA = "SELECT new org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepaymentPeriodData(lcib.loanTransaction.id, lcib.loan.id, lcib.loanTransaction.dateOf, lcib.loanTransaction.reversed, lcib.amount, lcib.unrecognizedAmount, lcib.loanTransaction.feeChargesPortion) FROM LoanCapitalizedIncomeBalance lcib ";
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,6 @@ import java.util.Optional;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.fineract.infrastructure.core.api.ApiParameterHelper;
|
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.api.RunreportsApiResource;
|
||||||
import org.apache.fineract.infrastructure.dataqueries.data.ReportExportType;
|
import org.apache.fineract.infrastructure.dataqueries.data.ReportExportType;
|
||||||
import org.apache.fineract.infrastructure.dataqueries.service.export.DatatableReportExportService;
|
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.annotation.ReportService;
|
||||||
import org.apache.fineract.infrastructure.report.service.AbstractReportingProcessService;
|
import org.apache.fineract.infrastructure.report.service.AbstractReportingProcessService;
|
||||||
import org.apache.fineract.infrastructure.security.service.SqlValidator;
|
import org.apache.fineract.infrastructure.security.service.SqlValidator;
|
||||||
|
import org.apache.fineract.util.StreamUtil;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
|||||||
+26
-8
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.fineract.portfolio.loanaccount.api;
|
package org.apache.fineract.portfolio.loanaccount.api;
|
||||||
|
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
import static org.apache.fineract.portfolio.loanproduct.service.LoanEnumerations.interestType;
|
import static org.apache.fineract.portfolio.loanproduct.service.LoanEnumerations.interestType;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
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.LoanApprovedAmountHistoryData;
|
||||||
import org.apache.fineract.portfolio.loanaccount.data.LoanChargeData;
|
import org.apache.fineract.portfolio.loanaccount.data.LoanChargeData;
|
||||||
import org.apache.fineract.portfolio.loanaccount.data.LoanCollateralManagementData;
|
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.LoanTermVariationsData;
|
||||||
|
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionBalanceWithLoanId;
|
||||||
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionData;
|
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionData;
|
||||||
import org.apache.fineract.portfolio.loanaccount.data.PaidInAdvanceData;
|
import org.apache.fineract.portfolio.loanaccount.data.PaidInAdvanceData;
|
||||||
import org.apache.fineract.portfolio.loanaccount.data.RepaymentScheduleRelatedLoanData;
|
import org.apache.fineract.portfolio.loanaccount.data.RepaymentScheduleRelatedLoanData;
|
||||||
@@ -505,22 +508,37 @@ public class LoansApiResource {
|
|||||||
final Page<LoanAccountData> loanBasicDetails = this.loanReadPlatformService.retrieveAll(searchParameters);
|
final Page<LoanAccountData> loanBasicDetails = this.loanReadPlatformService.retrieveAll(searchParameters);
|
||||||
final Set<String> associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters());
|
final Set<String> associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters());
|
||||||
if (associationParameters.contains(DataTableApiConstant.summaryAssociateParamName)) {
|
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 -> {
|
loanBasicDetails.getPageItems().forEach(i -> {
|
||||||
if (i.getSummary() != null) {
|
if (i.getSummary() != null) {
|
||||||
Collection<DisbursementData> disbursementData = this.loanReadPlatformService.retrieveLoanDisbursementDetails(i.getId());
|
Long loanId = i.getId();
|
||||||
List<LoanTransactionRepaymentPeriodData> capitalizedIncomeData = this.loanCapitalizedIncomeBalanceRepository
|
List<DisbursementData> disbursementData = disbursementDataByLoanIds.getOrDefault(loanId, emptyList());
|
||||||
.findRepaymentPeriodDataByLoanId(i.getId());
|
List<LoanTransactionRepaymentPeriodData> capitalizedIncomeData = repaymentPeriodDataByLoanIds.getOrDefault(loanId,
|
||||||
final RepaymentScheduleRelatedLoanData repaymentScheduleRelatedData = new RepaymentScheduleRelatedLoanData(
|
emptyList());
|
||||||
|
List<LoanTransactionBalanceWithLoanId> loanTransactionBalances = loanTransactionBalancesByLoanIds.getOrDefault(loanId,
|
||||||
|
emptyList());
|
||||||
|
|
||||||
|
RepaymentScheduleRelatedLoanData repaymentScheduleRelatedData = new RepaymentScheduleRelatedLoanData(
|
||||||
i.getTimeline().getExpectedDisbursementDate(), i.getTimeline().getActualDisbursementDate(), i.getCurrency(),
|
i.getTimeline().getExpectedDisbursementDate(), i.getTimeline().getActualDisbursementDate(), i.getCurrency(),
|
||||||
i.getPrincipal(), i.getInArrearsTolerance(), i.getFeeChargesAtDisbursementCharged());
|
i.getPrincipal(), i.getInArrearsTolerance(), i.getFeeChargesAtDisbursementCharged());
|
||||||
final LoanScheduleData repaymentSchedule = this.loanReadPlatformService.retrieveRepaymentSchedule(i.getId(),
|
LoanScheduleData repaymentSchedule = loanReadPlatformService.retrieveRepaymentSchedule(loanId,
|
||||||
repaymentScheduleRelatedData, disbursementData, capitalizedIncomeData, i.isInterestRecalculationEnabled(),
|
repaymentScheduleRelatedData, disbursementData, capitalizedIncomeData, i.isInterestRecalculationEnabled(),
|
||||||
LoanScheduleType.fromEnumOptionData(i.getLoanScheduleType()));
|
LoanScheduleType.fromEnumOptionData(i.getLoanScheduleType()));
|
||||||
|
|
||||||
LoanSummaryDataProvider loanSummaryDataProvider = loanSummaryProviderDelegate
|
LoanSummaryDataProvider loanSummaryDataProvider = loanSummaryProviderDelegate
|
||||||
.resolveLoanSummaryDataProvider(i.getTransactionProcessingStrategyCode());
|
.resolveLoanSummaryDataProvider(i.getTransactionProcessingStrategyCode());
|
||||||
i.setSummary(loanSummaryDataProvider.withTransactionAmountsSummary(i.getId(), i.getSummary(), repaymentSchedule,
|
|
||||||
loanSummaryBalancesRepository.retrieveLoanSummaryBalancesByTransactionType(i.getId(),
|
LoanSummaryData summaryData = loanSummaryDataProvider.withTransactionAmountsSummary(loanId, i.getSummary(),
|
||||||
LoanApiConstants.LOAN_SUMMARY_TRANSACTION_TYPES)));
|
repaymentSchedule, loanTransactionBalances);
|
||||||
|
|
||||||
|
i.setSummary(summaryData);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -637,8 +637,8 @@ public class LoanScheduleAssembler {
|
|||||||
.getAsBigDecimal();
|
.getAsBigDecimal();
|
||||||
}
|
}
|
||||||
BigDecimal waivedChargeAmount = null;
|
BigDecimal waivedChargeAmount = null;
|
||||||
disbursementDatas.add(new DisbursementData(null, expectedDisbursementDate, null, principal, netDisbursalAmount, null,
|
disbursementDatas.add(new DisbursementData(null, null, expectedDisbursementDate, null, principal, netDisbursalAmount,
|
||||||
null, waivedChargeAmount));
|
null, null, waivedChargeAmount));
|
||||||
i++;
|
i++;
|
||||||
} while (i < disbursementDataArray.size());
|
} while (i < disbursementDataArray.size());
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-7
@@ -34,6 +34,7 @@ import java.util.Collections;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -1408,7 +1409,6 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
|
|||||||
Integer loanTermInDays = 0;
|
Integer loanTermInDays = 0;
|
||||||
Set<Long> disbursementPeriodIds = new HashSet<>();
|
Set<Long> disbursementPeriodIds = new HashSet<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
|
||||||
final Integer period = JdbcSupport.getInteger(rs, "period");
|
final Integer period = JdbcSupport.getInteger(rs, "period");
|
||||||
LocalDate fromDate = JdbcSupport.getLocalDate(rs, "fromDate");
|
LocalDate fromDate = JdbcSupport.getLocalDate(rs, "fromDate");
|
||||||
final LocalDate dueDate = JdbcSupport.getLocalDate(rs, "dueDate");
|
final LocalDate dueDate = JdbcSupport.getLocalDate(rs, "dueDate");
|
||||||
@@ -1941,10 +1941,16 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<DisbursementData> retrieveLoanDisbursementDetails(final Long loanId) {
|
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 LoanDisbursementDetailMapper rm = new LoanDisbursementDetailMapper(sqlGenerator);
|
||||||
final String sql = "select " + rm.schema()
|
final String sql = "select " + rm.schema() + " where " + sqlGenerator.in("dd.loan_id", loanIds)
|
||||||
+ " 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";
|
+ " 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
|
return this.jdbcTemplate.query(sql, rm, parameters).stream().collect(Collectors.groupingBy(DisbursementData::getLoanId)); // NOSONAR
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class LoanDisbursementDetailMapper implements RowMapper<DisbursementData> {
|
private static final class LoanDisbursementDetailMapper implements RowMapper<DisbursementData> {
|
||||||
@@ -1956,7 +1962,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String schema() {
|
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 "
|
+ 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 "
|
+ "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";
|
+ "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
|
@Override
|
||||||
public DisbursementData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
|
public DisbursementData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
|
||||||
final Long id = rs.getLong("id");
|
final Long id = rs.getLong("id");
|
||||||
|
final Long loanId = rs.getLong("loanId");
|
||||||
final LocalDate expectedDisbursementdate = JdbcSupport.getLocalDate(rs, "expectedDisbursementdate");
|
final LocalDate expectedDisbursementdate = JdbcSupport.getLocalDate(rs, "expectedDisbursementdate");
|
||||||
final LocalDate actualDisbursementdate = JdbcSupport.getLocalDate(rs, "actualDisbursementdate");
|
final LocalDate actualDisbursementdate = JdbcSupport.getLocalDate(rs, "actualDisbursementdate");
|
||||||
final BigDecimal principal = rs.getBigDecimal("principal");
|
final BigDecimal principal = rs.getBigDecimal("principal");
|
||||||
@@ -1975,8 +1982,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
|
|||||||
if (chargeAmount != null && waivedAmount != null) {
|
if (chargeAmount != null && waivedAmount != null) {
|
||||||
chargeAmount = chargeAmount.subtract(waivedAmount);
|
chargeAmount = chargeAmount.subtract(waivedAmount);
|
||||||
}
|
}
|
||||||
return new DisbursementData(id, expectedDisbursementdate, actualDisbursementdate, principal, netDisbursalAmount, loanChargeId,
|
return new DisbursementData(id, loanId, expectedDisbursementdate, actualDisbursementdate, principal, netDisbursalAmount,
|
||||||
chargeAmount, waivedAmount);
|
loanChargeId, chargeAmount, waivedAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user