forked from apache/fineract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-2111: Performance issue on Retrieve Loan API - Transactions
- Loading branch information
Showing
21 changed files
with
331 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
...pache/fineract/portfolio/loanaccount/service/LoanChargePaidByReadPlatformServiceImpl.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
...n/java/org/apache/fineract/portfolio/loanaccount/service/LoanChargePaidByReadService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* 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.service; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.TypedQuery; | ||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.JoinType; | ||
import jakarta.persistence.criteria.Order; | ||
import jakarta.persistence.criteria.Path; | ||
import jakarta.persistence.criteria.Root; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.fineract.portfolio.loanaccount.data.LoanChargePaidByData; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanChargePaidBy; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; | ||
import org.apache.fineract.portfolio.loanaccount.mapper.LoanChargePaidByMapper; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class LoanChargePaidByReadService { | ||
|
||
private final EntityManager entityManager; | ||
private final LoanChargePaidByMapper loanChargePaidByMapper; | ||
|
||
public List<LoanChargePaidByData> fetchLoanChargesPaidByDataTransactionId(Long transactionId) { | ||
final List<Long> transactionIds = Arrays.asList(transactionId); | ||
return fetchLoanChargesPaidByTransactionId(transactionIds).stream().map(loanChargePaidByMapper::map).toList(); | ||
} | ||
|
||
public List<LoanChargePaidByData> fetchLoanChargesPaidByDataTransactionId(final List<Long> transactionIds) { | ||
return fetchLoanChargesPaidByTransactionId(transactionIds).stream().map(loanChargePaidByMapper::map).toList(); | ||
} | ||
|
||
public List<LoanChargePaidBy> fetchLoanChargesPaidByTransactionId(final List<Long> transactionIds) { | ||
|
||
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); | ||
final CriteriaQuery<LoanChargePaidBy> query = cb.createQuery(LoanChargePaidBy.class); | ||
|
||
final Root<LoanChargePaidBy> root = query.from(LoanChargePaidBy.class); | ||
root.fetch("loanTransaction", JoinType.INNER); | ||
final Path<LoanTransaction> loanTransaction = root.join("loanTransaction", JoinType.INNER); | ||
|
||
query.select(root).where(loanTransaction.get("id").in(transactionIds)); | ||
|
||
final List<Order> orders = new ArrayList<>(); | ||
orders.add(cb.desc(root.get("id"))); | ||
query.orderBy(orders); | ||
|
||
final TypedQuery<LoanChargePaidBy> queryToExecute = entityManager.createQuery(query); | ||
return queryToExecute.getResultList(); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
...in/java/org/apache/fineract/portfolio/loanaccount/service/LoanTransactionReadService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* 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.service; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.TypedQuery; | ||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.JoinType; | ||
import jakarta.persistence.criteria.Order; | ||
import jakarta.persistence.criteria.Path; | ||
import jakarta.persistence.criteria.Predicate; | ||
import jakarta.persistence.criteria.Root; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.fineract.portfolio.loanaccount.domain.Loan; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class LoanTransactionReadService { | ||
|
||
private final EntityManager entityManager; | ||
|
||
public List<LoanTransaction> fetchLoanTransactionsByType(final Long loanId, final String externalId, final Integer transactionType) { | ||
final List<Integer> transactionTypes = new ArrayList<>(); | ||
transactionTypes.add(transactionType); | ||
return fetchLoanTransactionsByTypes(loanId, externalId, transactionTypes); | ||
} | ||
|
||
public List<LoanTransaction> fetchLoanTransactionsByTypes(final Long loanId, final String externalId, | ||
final List<Integer> transactionTypes) { | ||
|
||
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); | ||
final CriteriaQuery<LoanTransaction> query = cb.createQuery(LoanTransaction.class); | ||
|
||
final Root<LoanTransaction> root = query.from(LoanTransaction.class); | ||
root.fetch("loan", JoinType.INNER); | ||
final Path<Loan> loan = root.join("loan", JoinType.INNER); | ||
|
||
Predicate loanPredicate = cb.equal(loan.get("id"), loanId); | ||
if (externalId != null) { | ||
loanPredicate = cb.equal(loan.get("externalId"), externalId); | ||
} | ||
|
||
query.select(root) | ||
.where(cb.and(loanPredicate, root.get("typeOf").in(transactionTypes), cb.equal(root.get("reversed"), Boolean.FALSE))); | ||
|
||
final List<Order> orders = new ArrayList<>(); | ||
orders.add(cb.desc(root.get("dateOf"))); | ||
orders.add(cb.desc(root.get("createdDate"))); | ||
orders.add(cb.desc(root.get("id"))); | ||
query.orderBy(orders); | ||
|
||
final TypedQuery<LoanTransaction> queryToExecute = entityManager.createQuery(query); | ||
return queryToExecute.getResultList(); | ||
} | ||
|
||
} |
Oops, something went wrong.