Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRES_NEW;

import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.cob.domain.LoanAccountLock;
import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;
import org.apache.fineract.cob.exceptions.LoanReadException;
import org.apache.fineract.cob.loan.LoanLockingService;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.infrastructure.core.serialization.ThrowableSerialization;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
Expand All @@ -46,7 +45,7 @@
@RequiredArgsConstructor
public abstract class AbstractLoanItemListener {

private final LoanAccountLockRepository accountLockRepository;
private final LoanLockingService loanLockingService;

private final TransactionTemplate transactionTemplate;

Expand All @@ -57,9 +56,10 @@ private void updateAccountLockWithError(List<Long> loanIds, String msg, Throwabl
@Override
protected void doInTransactionWithoutResult(@NotNull TransactionStatus status) {
for (Long loanId : loanIds) {
Optional<LoanAccountLock> loanAccountLock = accountLockRepository.findByLoanIdAndLockOwner(loanId, getLockOwner());
loanAccountLock.ifPresent(
accountLock -> accountLock.setError(String.format(msg, loanId), ThrowableSerialization.serialize(e)));
LoanAccountLock loanAccountLock = loanLockingService.findByLoanIdAndLockOwner(loanId, getLockOwner());
if (loanAccountLock != null) {
loanAccountLock.setError(String.format(msg, loanId), ThrowableSerialization.serialize(e));
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package org.apache.fineract.cob.listener;

import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;
import org.apache.fineract.cob.loan.LoanLockingService;
import org.springframework.transaction.support.TransactionTemplate;

public class ChunkProcessingLoanItemListener extends AbstractLoanItemListener {

public ChunkProcessingLoanItemListener(LoanAccountLockRepository accountLockRepository, TransactionTemplate transactionTemplate) {
super(accountLockRepository, transactionTemplate);
public ChunkProcessingLoanItemListener(LoanLockingService loanLockingService, TransactionTemplate transactionTemplate) {
super(loanLockingService, transactionTemplate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package org.apache.fineract.cob.listener;

import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;
import org.apache.fineract.cob.loan.LoanLockingService;
import org.springframework.transaction.support.TransactionTemplate;

public class InlineCOBLoanItemListener extends AbstractLoanItemListener {

public InlineCOBLoanItemListener(LoanAccountLockRepository accountLockRepository, TransactionTemplate transactionTemplate) {
super(accountLockRepository, transactionTemplate);
public InlineCOBLoanItemListener(LoanLockingService loanLockingService, TransactionTemplate transactionTemplate) {
super(loanLockingService, transactionTemplate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public abstract class AbstractLoanItemReader implements ItemReader<Loan> {

protected final LoanRepository loanRepository;

@Setter(AccessLevel.PROTECTED)
private List<Long> alreadyLockedOrProcessedAccounts;
@Setter(AccessLevel.PROTECTED)
private List<Long> remainingData;
private Long loanId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
Expand All @@ -32,14 +31,14 @@
@RequiredArgsConstructor
public abstract class AbstractLoanItemWriter extends RepositoryItemWriter<Loan> {

private final LoanAccountLockRepository accountLockRepository;
private final LoanLockingService loanLockingService;

@Override
public void write(@NotNull List<? extends Loan> items) throws Exception {
if (!items.isEmpty()) {
super.write(items);
List<Long> loanIds = items.stream().map(AbstractPersistableCustom::getId).toList();
accountLockRepository.deleteByLoanIdInAndLockOwner(loanIds, getLockOwner());
loanLockingService.deleteByLoanIdInAndLockOwner(loanIds, getLockOwner());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.cob.data.LoanCOBParameter;
import org.apache.fineract.cob.domain.LoanAccountLock;
import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.jdbc.core.JdbcTemplate;

@Slf4j
@RequiredArgsConstructor
public class ApplyLoanLockTasklet implements Tasklet {

private final LoanAccountLockRepository accountLockRepository;
private final FineractProperties fineractProperties;
private final JdbcTemplate jdbcTemplate;
private final LoanLockingService loanLockingService;
private final RetrieveLoanIdService retrieveLoanIdService;

@Override
Expand All @@ -64,9 +60,9 @@ public RepeatStatus execute(@NotNull StepContribution contribution, @NotNull Chu
loanIds = new ArrayList<>(
retrieveLoanIdService.retrieveAllNonClosedLoansByLastClosedBusinessDateAndMinAndMaxLoanId(loanCOBParameter));
}
List<List<Long>> loanIdPartitions = Lists.partition(loanIds, fineractProperties.getQuery().getInClauseParameterSizeLimit());
List<List<Long>> loanIdPartitions = Lists.partition(loanIds, getInClauseParameterSizeLimit());
List<LoanAccountLock> accountLocks = new ArrayList<>();
loanIdPartitions.forEach(loanIdPartition -> accountLocks.addAll(accountLockRepository.findAllByLoanIdIn(loanIdPartition)));
loanIdPartitions.forEach(loanIdPartition -> accountLocks.addAll(loanLockingService.findAllByLoanIdIn(loanIdPartition)));

Map<Long, LoanAccountLock> alreadySoftLockedAccountsMap = accountLocks.stream()
.filter(e -> LockOwner.LOAN_COB_PARTITIONING.equals(e.getLockOwner()))
Expand All @@ -77,7 +73,7 @@ public RepeatStatus execute(@NotNull StepContribution contribution, @NotNull Chu

List<Long> toBeProcessedLoanIds = new ArrayList<>(alreadySoftLockedAccountsMap.keySet());

upgradeToHardLock(toBeProcessedLoanIds);
loanLockingService.upgradeLock(toBeProcessedLoanIds, LockOwner.LOAN_COB_CHUNK_PROCESSING);

toBeProcessedLoanIds.addAll(alreadyLockedByChunkProcessingAccountIds);
List<Long> alreadyLockedByInlineCOBOrProcessedLoanIds = new ArrayList<>(loanIds);
Expand All @@ -88,16 +84,6 @@ public RepeatStatus execute(@NotNull StepContribution contribution, @NotNull Chu
return RepeatStatus.FINISHED;
}

private void upgradeToHardLock(List<Long> accountsToLock) {
jdbcTemplate.batchUpdate("""
UPDATE m_loan_account_locks SET version= version + 1, lock_owner = ?, lock_placed_on = ? WHERE loan_id = ?
""", accountsToLock, getInClauseParameterSizeLimit(), (ps, id) -> {
ps.setString(1, LockOwner.LOAN_COB_CHUNK_PROCESSING.name());
ps.setObject(2, DateUtils.getOffsetDateTimeOfTenant());
ps.setLong(3, id);
});
}

private int getInClauseParameterSizeLimit() {
return fineractProperties.getQuery().getInClauseParameterSizeLimit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
*/
package org.apache.fineract.cob.loan;

import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;

public class InlineCOBLoanItemWriter extends AbstractLoanItemWriter {

public InlineCOBLoanItemWriter(LoanAccountLockRepository accountLockRepository) {
super(accountLockRepository);
public InlineCOBLoanItemWriter(LoanLockingService loanLockingService) {
super(loanLockingService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.infrastructure.springbatch.PropertyService;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
Expand All @@ -44,7 +43,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@EnableBatchIntegration
Expand Down Expand Up @@ -76,10 +74,7 @@ public class LoanCOBManagerConfiguration {
@Autowired
private CustomJobParameterResolver customJobParameterResolver;
@Autowired
private LoanRepository loanRepository;

@Autowired
private JdbcTemplate jdbcTemplate;
private LoanLockingService loanLockingService;

@Bean
@JobScope
Expand Down Expand Up @@ -123,7 +118,7 @@ public LoanIdParameterTasklet loanIdParameterTasklet() {
@Bean
@JobScope
public LockLoanTasklet lockLoanTasklet() {
return new LockLoanTasklet(jdbcTemplate);
return new LockLoanTasklet(loanLockingService);
}

@Bean
Expand All @@ -135,7 +130,7 @@ public ResolveLoanCOBCustomJobParametersTasklet resolveCustomJobParametersTaskle
@Bean
@JobScope
public StayedLockedLoansTasklet stayedLockedTasklet() {
return new StayedLockedLoansTasklet(businessEventNotifierService, loanRepository);
return new StayedLockedLoansTasklet(businessEventNotifierService, retrieveLoanIdService);
}

@Bean(name = "loanCOBJob")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.fineract.cob.COBBusinessStepService;
import org.apache.fineract.cob.common.InitialisationTasklet;
import org.apache.fineract.cob.common.ResetContextTasklet;
import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.listener.ChunkProcessingLoanItemListener;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.jobs.service.JobName;
Expand All @@ -41,7 +40,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;

@Configuration
Expand All @@ -62,8 +60,6 @@ public class LoanCOBWorkerConfiguration {
@Autowired
private COBBusinessStepService cobBusinessStepService;
@Autowired
private LoanAccountLockRepository accountLockRepository;
@Autowired
private AppUserRepositoryWrapper userRepository;
@Autowired
private TransactionTemplate transactionTemplate;
Expand All @@ -73,7 +69,7 @@ public class LoanCOBWorkerConfiguration {
@Autowired
private FineractProperties fineractProperties;
@Autowired
private JdbcTemplate jdbcTemplate;
private LoanLockingService loanLockingService;

@Bean(name = LoanCOBConstant.LOAN_COB_WORKER_STEP)
public Step loanCOBWorkerStep() {
Expand Down Expand Up @@ -120,12 +116,12 @@ public InitialisationTasklet initialiseContext() {

@Bean
public ChunkProcessingLoanItemListener loanItemListener() {
return new ChunkProcessingLoanItemListener(accountLockRepository, transactionTemplate);
return new ChunkProcessingLoanItemListener(loanLockingService, transactionTemplate);
}

@Bean
public ApplyLoanLockTasklet applyLock() {
return new ApplyLoanLockTasklet(accountLockRepository, fineractProperties, jdbcTemplate, retrieveLoanIdService);
return new ApplyLoanLockTasklet(fineractProperties, loanLockingService, retrieveLoanIdService);
}

@Bean
Expand All @@ -148,7 +144,7 @@ public LoanItemProcessor cobWorkerItemProcessor() {
@Bean
@StepScope
public LoanItemWriter cobWorkerItemWriter() {
LoanItemWriter repositoryItemWriter = new LoanItemWriter(accountLockRepository);
LoanItemWriter repositoryItemWriter = new LoanItemWriter(loanLockingService);
repositoryItemWriter.setRepository(loanRepository);
return repositoryItemWriter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class LoanInlineCOBConfig {
private TransactionTemplate transactionTemplate;
@Autowired
private CustomJobParameterRepository loanIdListRepository;
@Autowired
private LoanLockingService loanLockingService;

@Bean
public InlineLoanCOBBuildExecutionContextTasklet inlineLoanCOBBuildExecutionContextTasklet() {
Expand Down Expand Up @@ -103,14 +105,14 @@ public Step inlineCOBResetContextStep() {

@Bean
public InlineCOBLoanItemWriter inlineCobWorkerItemWriter() {
InlineCOBLoanItemWriter repositoryItemWriter = new InlineCOBLoanItemWriter(accountLockRepository);
InlineCOBLoanItemWriter repositoryItemWriter = new InlineCOBLoanItemWriter(loanLockingService);
repositoryItemWriter.setRepository(loanRepository);
return repositoryItemWriter;
}

@Bean
public InlineCOBLoanItemListener inlineCobLoanItemListener() {
return new InlineCOBLoanItemListener(accountLockRepository, transactionTemplate);
return new InlineCOBLoanItemListener(loanLockingService, transactionTemplate);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
*/
package org.apache.fineract.cob.loan;

import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.cob.domain.LockOwner;

public class LoanItemWriter extends AbstractLoanItemWriter {

public LoanItemWriter(LoanAccountLockRepository accountLockRepository) {
super(accountLockRepository);
public LoanItemWriter(LoanLockingService loanLockingService) {
super(loanLockingService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.cob.loan;

import org.apache.fineract.cob.domain.LoanAccountLockRepository;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class LoanLockingConfiguration {

@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private FineractProperties fineractProperties;
@Autowired
private LoanAccountLockRepository loanAccountLockRepository;

@Bean
@ConditionalOnMissingBean
public LoanLockingService retrieveLoanLockingService() {
return new LoanLockingServiceImpl(jdbcTemplate, fineractProperties, loanAccountLockRepository);
}
}
Loading