-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
9047c88
443e513
6b376c1
c267bfd
d99eadb
a260e28
8116875
3eee9db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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<>(); | ||
|
@@ -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); | ||
final QAccountTransferStandingInstruction qAccountTransferStandingInstruction = QAccountTransferStandingInstruction.accountTransferStandingInstruction; | ||
|
||
queryFactory.update(qAccountTransferStandingInstruction) | ||
.set(qAccountTransferStandingInstruction.latsRunDate, transactionDate) | ||
.where(qAccountTransferStandingInstruction.id.eq(data.getId())).execute(); | ||
} | ||
|
||
} | ||
|
@@ -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) { | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this whole logic into a repository There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think we need explicit flush. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You right, done |
||
|
||
return transferCompleted; | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done