Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FINERACT-2022: Rewrite account domain, services and jobs using QueryDSL. #3854

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
6 changes: 6 additions & 0 deletions fineract-core/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,10 @@ dependencies {
implementation(
project(path: ':fineract-avro-schemas')
)
implementation('com.querydsl:querydsl-core:5.1.0',
'com.querydsl:querydsl-jpa:5.1.0:jakarta')
annotationProcessor(
'com.querydsl:querydsl-apt:5.1.0:jakarta',
'jakarta.persistence:jakarta.persistence-api:3.1.0',
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static BigDecimal getBigDecimalDefaultToNullIfZero(final ResultSet rs, fi
return defaultToNullIfZero(value);
}

private static BigDecimal defaultToNullIfZero(final BigDecimal value) {
public static BigDecimal defaultToNullIfZero(final BigDecimal value) {
BigDecimal result = value;
if (value != null && BigDecimal.ZERO.compareTo(value) == 0) {
result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public <E> Page<E> fetchPage(final JdbcTemplate jt, final String sqlFetchRows, f
return new Page<>(items, totalFilteredRecords);
}

public <E> Page<E> createPageFromItems(final List<E> items, final Long filteredRecords) {
return new Page<>(items, filteredRecords.intValue());
}

public <E> Page<Long> fetchPage(JdbcTemplate jdbcTemplate, String sql, Class<Long> type) {
final List<Long> items = jdbcTemplate.queryForList(sql, type);

Expand Down
6 changes: 6 additions & 0 deletions fineract-loan/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ dependencies {
implementation('org.eclipse.persistence:org.eclipse.persistence.jpa') {
exclude group: 'org.eclipse.persistence', module: 'jakarta.persistence'
}
implementation('com.querydsl:querydsl-core:5.1.0',
'com.querydsl:querydsl-jpa:5.1.0:jakarta')
annotationProcessor(
'com.querydsl:querydsl-apt:5.1.0:jakarta',
'jakarta.persistence:jakarta.persistence-api:3.1.0',
)
// testCompile dependencies are ONLY used in src/test, not src/main.
// Do NOT repeat dependencies which are ALREADY in implementation or runtimeOnly!
//
Expand Down
7 changes: 7 additions & 0 deletions fineract-provider/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ dependencies {

implementation 'io.github.classgraph:classgraph'

implementation('com.querydsl:querydsl-core:5.1.0',
'com.querydsl:querydsl-jpa:5.1.0:jakarta')
annotationProcessor(
'com.querydsl:querydsl-apt:5.1.0:jakarta',
'jakarta.persistence:jakarta.persistence-api:3.1.0',
)

// testCompile dependencies are ONLY used in src/test, not src/main.
// Do NOT repeat dependencies which are ALREADY in implementation or runtimeOnly!
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

public interface AccountAssociationsRepository
extends JpaRepository<AccountAssociations, Long>, JpaSpecificationExecutor<AccountAssociations> {

@Query("select aa from AccountAssociations aa where aa.loanAccount.id= :loanId and aa.associationType = :associationType")
AccountAssociations findByLoanIdAndType(@Param("loanId") Long loanId, @Param("associationType") Integer accountAssociationType);

@Query("select aa from AccountAssociations aa where aa.savingsAccount.id= :savingsId and aa.associationType = :associationType")
AccountAssociations findBySavingsIdAndType(@Param("savingsId") Long savingsId,
@Param("associationType") Integer accountAssociationType);
}
public interface AccountAssociationsRepository extends JpaRepository<AccountAssociations, Long>,
JpaSpecificationExecutor<AccountAssociations>, QuerydslPredicateExecutor<AccountAssociations> {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,9 @@
*/
package org.apache.fineract.portfolio.account.domain;

import java.util.Collection;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

public interface AccountTransferRepository
extends JpaRepository<AccountTransferTransaction, Long>, JpaSpecificationExecutor<AccountTransferTransaction> {

@Query("select att from AccountTransferTransaction att where att.accountTransferDetails.fromLoanAccount.id= :accountNumber and att.reversed=false")
List<AccountTransferTransaction> findByFromLoanId(@Param("accountNumber") Long accountNumber);

@Query("select att from AccountTransferTransaction att where (att.accountTransferDetails.fromLoanAccount.id= :accountNumber or att.accountTransferDetails.toLoanAccount.id=:accountNumber) and att.reversed=false order by att.id desc")
List<AccountTransferTransaction> findAllByLoanId(@Param("accountNumber") Long accountNumber);

@Query("select att from AccountTransferTransaction att where att.toLoanTransaction.id= :loanTransactionId and att.reversed=false")
AccountTransferTransaction findByToLoanTransactionId(@Param("loanTransactionId") Long loanTransactionId);

@Query("select att from AccountTransferTransaction att where att.fromLoanTransaction.id IN :loanTransactions and att.reversed=false")
List<AccountTransferTransaction> findByFromLoanTransactions(@Param("loanTransactions") Collection<Long> loanTransactions);
}
public interface AccountTransferRepository extends JpaRepository<AccountTransferTransaction, Long>,
JpaSpecificationExecutor<AccountTransferTransaction>, QuerydslPredicateExecutor<AccountTransferTransaction> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 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.account.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "m_account_transfer_standing_instructions_history")
public class AccountTransferStandingInstructionsHistory extends AbstractPersistableCustom {

@ManyToOne
@JoinColumn(name = "standing_instruction_id")
private AccountTransferStandingInstruction accountTransferStandingInstruction;

@Column(name = "status", length = 20)
private String status;

@Column(name = "execution_time")
private LocalDateTime executionTime;

@Column(name = "amount", scale = 6, precision = 19)
private BigDecimal amount;

@Column(name = "error_log", length = 500)
private String errorLog;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,9 @@
*/
package org.apache.fineract.portfolio.account.domain;

import java.util.Collection;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.savings.domain.SavingsAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

public interface StandingInstructionRepository
extends JpaRepository<AccountTransferStandingInstruction, Long>, JpaSpecificationExecutor<AccountTransferStandingInstruction> {

String FIND_BY_LOAN_AND_STATUS_QUERY = "select accountTransferStandingInstruction "
+ "from AccountTransferStandingInstruction accountTransferStandingInstruction "
+ "where accountTransferStandingInstruction.status = :status "
+ "and (accountTransferStandingInstruction.accountTransferDetails.toLoanAccount = :loan "
+ "or accountTransferStandingInstruction.accountTransferDetails.fromLoanAccount = :loan)";

String FIND_BY_SAVINGS_AND_STATUS_QUERY = "select accountTransferStandingInstruction "
+ "from AccountTransferStandingInstruction accountTransferStandingInstruction "
+ "where accountTransferStandingInstruction.status = :status "
+ "and (accountTransferStandingInstruction.accountTransferDetails.toSavingsAccount = :savingsAccount "
+ "or accountTransferStandingInstruction.accountTransferDetails.fromSavingsAccount = :savingsAccount)";

@Query(FIND_BY_LOAN_AND_STATUS_QUERY)
Collection<AccountTransferStandingInstruction> findByLoanAccountAndStatus(@Param("loan") Loan loan, @Param("status") Integer status);

@Query(FIND_BY_SAVINGS_AND_STATUS_QUERY)
Collection<AccountTransferStandingInstruction> findBySavingsAccountAndStatus(@Param("savingsAccount") SavingsAccount savingsAccount,
@Param("status") Integer status);
}
public interface StandingInstructionRepository extends JpaRepository<AccountTransferStandingInstruction, Long>,
JpaSpecificationExecutor<AccountTransferStandingInstruction>, QuerydslPredicateExecutor<AccountTransferStandingInstruction> {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
package org.apache.fineract.portfolio.account.jobs.executestandinginstructions;

import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
import jakarta.persistence.EntityManager;
import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.portfolio.account.domain.StandingInstructionRepository;
import org.apache.fineract.portfolio.account.service.AccountTransfersWritePlatformService;
import org.apache.fineract.portfolio.account.service.StandingInstructionReadPlatformService;
import org.springframework.batch.core.Job;
Expand All @@ -31,7 +32,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
Expand All @@ -44,11 +44,11 @@ public class ExecuteStandingInstructionsConfig {
@Autowired
private StandingInstructionReadPlatformService standingInstructionReadPlatformService;
@Autowired
private JdbcTemplate jdbcTemplate;
private AccountTransfersWritePlatformService accountTransfersWritePlatformService;
@Autowired
private DatabaseSpecificSQLGenerator sqlGenerator;
private EntityManager entityManager;
@Autowired
private AccountTransfersWritePlatformService accountTransfersWritePlatformService;
private StandingInstructionRepository standingInstructionRepository;

@Bean
protected Step executeStandingInstructionsStep() {
Expand All @@ -64,7 +64,7 @@ public Job executeStandingInstructionsJob() {

@Bean
public ExecuteStandingInstructionsTasklet executeStandingInstructionsTasklet() {
return new ExecuteStandingInstructionsTasklet(standingInstructionReadPlatformService, jdbcTemplate, sqlGenerator,
accountTransfersWritePlatformService);
return new ExecuteStandingInstructionsTasklet(standingInstructionReadPlatformService, accountTransfersWritePlatformService,
entityManager, standingInstructionRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/
package org.apache.fineract.portfolio.account.jobs.executestandinginstructions;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -29,12 +32,14 @@
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformServiceUnavailableException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.apache.fineract.portfolio.account.data.AccountTransferDTO;
import org.apache.fineract.portfolio.account.data.StandingInstructionData;
import org.apache.fineract.portfolio.account.data.StandingInstructionDuesData;
import org.apache.fineract.portfolio.account.domain.AccountTransferRecurrenceType;
import org.apache.fineract.portfolio.account.domain.AccountTransferStandingInstructionsHistory;
import org.apache.fineract.portfolio.account.domain.QAccountTransferStandingInstruction;
import org.apache.fineract.portfolio.account.domain.StandingInstructionRepository;
import org.apache.fineract.portfolio.account.domain.StandingInstructionStatus;
import org.apache.fineract.portfolio.account.domain.StandingInstructionType;
import org.apache.fineract.portfolio.account.service.AccountTransfersWritePlatformService;
Expand All @@ -48,19 +53,21 @@
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
public class ExecuteStandingInstructionsTasklet implements Tasklet {

private final StandingInstructionReadPlatformService standingInstructionReadPlatformService;
private final JdbcTemplate jdbcTemplate;
private final DatabaseSpecificSQLGenerator sqlGenerator;
private final AccountTransfersWritePlatformService accountTransfersWritePlatformService;
private final EntityManager entityManager;
private final StandingInstructionRepository standingInstructionRepository;

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws JobExecutionException {
Collection<StandingInstructionData> instructionData = standingInstructionReadPlatformService
.retrieveAll(StandingInstructionStatus.ACTIVE.getValue());
List<Throwable> errors = new ArrayList<>();
Expand Down Expand Up @@ -113,8 +120,12 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
final boolean transferCompleted = transferAmount(errors, accountTransferDTO, data.getId());

if (transferCompleted) {
final String updateQuery = "UPDATE m_account_transfer_standing_instructions SET last_run_date = ? where id = ?";
jdbcTemplate.update(updateQuery, transactionDate, data.getId());
final JPAQueryFactory queryFactory = new JPAQueryFactory(this.entityManager);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this whole logic into an AccountTransferStandingInstruction repository. We can change our repository interfaces to an impl where queryDSL is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

final QAccountTransferStandingInstruction qAccountTransferStandingInstruction = QAccountTransferStandingInstruction.accountTransferStandingInstruction;

queryFactory.update(qAccountTransferStandingInstruction)
.set(qAccountTransferStandingInstruction.latsRunDate, transactionDate)
.where(qAccountTransferStandingInstruction.id.eq(data.getId())).execute();
}

}
Expand All @@ -127,10 +138,8 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon

private boolean transferAmount(final List<Throwable> errors, final AccountTransferDTO accountTransferDTO, final Long instructionId) {
boolean transferCompleted = true;
StringBuilder errorLog = new StringBuilder();
StringBuilder updateQuery = new StringBuilder(
"INSERT INTO m_account_transfer_standing_instructions_history (standing_instruction_id, " + sqlGenerator.escape("status")
+ ", amount,execution_time, error_log) VALUES (");
final StringBuilder errorLog = new StringBuilder();

try {
accountTransfersWritePlatformService.transferFunds(accountTransferDTO);
} catch (final PlatformApiDataValidationException e) {
Expand All @@ -151,17 +160,21 @@ private boolean transferAmount(final List<Throwable> errors, final AccountTransf
errorLog.append("Exception while trasfering funds ").append(e.getMessage());

}
updateQuery.append(instructionId).append(",");
if (errorLog.length() > 0) {

if (!errorLog.isEmpty()) {
transferCompleted = false;
updateQuery.append("'failed'").append(",");
} else {
updateQuery.append("'success'").append(",");
}
updateQuery.append(accountTransferDTO.getTransactionAmount().doubleValue());
updateQuery.append(", now(),");
updateQuery.append("'").append(errorLog).append("')");
jdbcTemplate.update(updateQuery.toString());

final AccountTransferStandingInstructionsHistory newHistory = new AccountTransferStandingInstructionsHistory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this whole logic into a repository

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

newHistory.setAccountTransferStandingInstruction(standingInstructionRepository.getReferenceById(instructionId));
newHistory.setStatus(transferCompleted ? "success" : "failed");
newHistory.setAmount(accountTransferDTO.getTransactionAmount());
newHistory.setExecutionTime(LocalDateTime.now(DateUtils.getDateTimeZoneOfTenant()));
newHistory.setErrorLog(String.valueOf(errorLog));

entityManager.persist(newHistory);
entityManager.flush();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need explicit flush.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You right, done


return transferCompleted;
}

Expand Down
Loading