-
Notifications
You must be signed in to change notification settings - Fork 2.3k
FINERACT-2455: Working Capital Loan COB Job #5539
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
Open
somasorosdpc
wants to merge
2
commits into
apache:develop
Choose a base branch
from
openMF:FINERACTR-2455/working-capital-cob
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,190
−536
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
101 changes: 101 additions & 0 deletions
101
fineract-cob/src/main/java/org/apache/fineract/cob/common/CommonPartitioner.java
This file contains hidden or 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,101 @@ | ||
| /** | ||
| * 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.common; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.fineract.cob.COBConstant; | ||
| import org.apache.fineract.cob.data.BusinessStepNameAndOrder; | ||
| import org.apache.fineract.cob.data.COBParameter; | ||
| import org.apache.fineract.cob.data.COBPartition; | ||
| import org.apache.fineract.cob.resolver.BusinessDateResolver; | ||
| import org.apache.fineract.cob.resolver.CatchUpFlagResolver; | ||
| import org.apache.fineract.cob.service.RetrieveIdService; | ||
| import org.springframework.batch.core.StepExecution; | ||
| import org.springframework.batch.core.launch.JobExecutionNotRunningException; | ||
| import org.springframework.batch.core.launch.JobOperator; | ||
| import org.springframework.batch.core.launch.NoSuchJobExecutionException; | ||
| import org.springframework.batch.item.ExecutionContext; | ||
| import org.springframework.util.StopWatch; | ||
|
|
||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public abstract class CommonPartitioner { | ||
|
|
||
| private final JobOperator jobOperator; | ||
| private final StepExecution stepExecution; | ||
| private final Long numberOfDays; | ||
| private final RetrieveIdService retrieveIdService; | ||
|
|
||
| public Map<String, ExecutionContext> getPartitions(int partitionSize, Set<BusinessStepNameAndOrder> cobBusinessSteps) { | ||
| if (cobBusinessSteps.isEmpty()) { | ||
| stopJobExecution(); | ||
| return Map.of(); | ||
| } | ||
| LocalDate businessDate = BusinessDateResolver.resolve(stepExecution); | ||
| boolean isCatchUp = CatchUpFlagResolver.resolve(stepExecution); | ||
| StopWatch sw = new StopWatch(); | ||
| sw.start(); | ||
| List<COBPartition> partitions = new ArrayList<>( | ||
| retrieveIdService.retrieveLoanCOBPartitions(numberOfDays, businessDate, isCatchUp, partitionSize)); | ||
| sw.stop(); | ||
| // if there is no loan to be closed, we still would like to create at least one partition | ||
|
|
||
| if (partitions.isEmpty()) { | ||
| partitions.add(new COBPartition(0L, 0L, 1L, 0L)); | ||
| } | ||
| log.info( | ||
| "{}} found {} loans to be processed as part of COB. {} partitions were created using partition size {}. RetrieveLoanCOBPartitions was executed in {} ms.", | ||
| getClass().getName(), getLoanCount(partitions), partitions.size(), partitionSize, sw.getTotalTimeMillis()); | ||
| return partitions.stream().collect(Collectors.toMap(l -> COBConstant.PARTITION_PREFIX + l.getPageNo(), | ||
| l -> createExecutionContextForPartition(cobBusinessSteps, l, businessDate, isCatchUp))); | ||
| } | ||
|
|
||
| private long getLoanCount(List<COBPartition> loanCOBPartitions) { | ||
| return loanCOBPartitions.stream().map(COBPartition::getCount).reduce(0L, Long::sum); | ||
| } | ||
|
|
||
| private ExecutionContext createExecutionContextForPartition(Set<BusinessStepNameAndOrder> cobBusinessSteps, | ||
| COBPartition loanCOBPartition, LocalDate businessDate, boolean isCatchUp) { | ||
| ExecutionContext executionContext = new ExecutionContext(); | ||
| executionContext.put(COBConstant.BUSINESS_STEPS, cobBusinessSteps); | ||
| executionContext.put(COBConstant.COB_PARAMETER, new COBParameter(loanCOBPartition.getMinId(), loanCOBPartition.getMaxId())); | ||
| executionContext.put(COBConstant.PARTITION_KEY, COBConstant.PARTITION_PREFIX + loanCOBPartition.getPageNo()); | ||
| executionContext.put(COBConstant.BUSINESS_DATE_PARAMETER_NAME, businessDate.toString()); | ||
| executionContext.put(COBConstant.IS_CATCH_UP_PARAMETER_NAME, Boolean.toString(isCatchUp)); | ||
| return executionContext; | ||
| } | ||
|
|
||
| private void stopJobExecution() { | ||
| Long jobId = stepExecution.getJobExecution().getId(); | ||
| try { | ||
| jobOperator.stop(jobId); | ||
| } catch (NoSuchJobExecutionException | JobExecutionNotRunningException e) { | ||
| log.error("There is no running execution for the given execution ID. Execution ID: {}", jobId); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
fineract-cob/src/main/java/org/apache/fineract/cob/domain/AbstractLockingService.java
This file contains hidden or 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,91 @@ | ||
| /** | ||
| * 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.domain; | ||
|
|
||
| import java.sql.PreparedStatement; | ||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType; | ||
| import org.apache.fineract.infrastructure.core.config.FineractProperties; | ||
| import org.apache.fineract.infrastructure.core.service.DateUtils; | ||
| import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public abstract class AbstractLockingService<T extends AccountLock> implements LockingService<T> { | ||
|
|
||
| private final JdbcTemplate jdbcTemplate; | ||
| private final FineractProperties fineractProperties; | ||
| private final AccountLockRepository<T> loanAccountLockRepository; | ||
|
|
||
| protected abstract String getBatchLoanLockUpgrade(); | ||
|
|
||
| protected abstract String getBatchLoanLockInsert(); | ||
|
|
||
| @Override | ||
| public void upgradeLock(List<Long> accountsToLock, LockOwner lockOwner) { | ||
| jdbcTemplate.batchUpdate(getBatchLoanLockUpgrade(), accountsToLock, getInClauseParameterSizeLimit(), (ps, id) -> { | ||
| ps.setString(1, lockOwner.name()); | ||
| ps.setObject(2, DateUtils.getAuditOffsetDateTime()); | ||
| ps.setLong(3, id); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public List<T> findAllByLoanIdIn(List<Long> loanIds) { | ||
| return loanAccountLockRepository.findAllByLoanIdIn(loanIds); | ||
| } | ||
|
|
||
| @Override | ||
| public T findByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner) { | ||
| return loanAccountLockRepository.findByLoanIdAndLockOwner(loanId, lockOwner).orElseGet(() -> { | ||
| log.warn("There is no lock for loan account with id: {}", loanId); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public List<T> findAllByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner) { | ||
| return loanAccountLockRepository.findAllByLoanIdInAndLockOwner(loanIds, lockOwner); | ||
| } | ||
|
|
||
| @Override | ||
| public void applyLock(List<Long> loanIds, LockOwner lockOwner) { | ||
| LocalDate cobBusinessDate = ThreadLocalContextUtil.getBusinessDateByType(BusinessDateType.COB_DATE); | ||
| jdbcTemplate.batchUpdate(getBatchLoanLockInsert(), loanIds, loanIds.size(), (PreparedStatement ps, Long loanId) -> { | ||
| ps.setLong(1, loanId); | ||
| ps.setLong(2, 1); | ||
| ps.setString(3, lockOwner.name()); | ||
| ps.setObject(4, DateUtils.getAuditOffsetDateTime()); | ||
| ps.setObject(5, cobBusinessDate); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner) { | ||
| loanAccountLockRepository.deleteByLoanIdInAndLockOwner(loanIds, lockOwner); | ||
| } | ||
|
|
||
| private int getInClauseParameterSizeLimit() { | ||
| return fineractProperties.getQuery().getInClauseParameterSizeLimit(); | ||
| } | ||
| } |
110 changes: 110 additions & 0 deletions
110
fineract-cob/src/main/java/org/apache/fineract/cob/domain/AccountLock.java
This file contains hidden or 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,110 @@ | ||
| /** | ||
| * 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.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.MappedSuperclass; | ||
| import jakarta.persistence.PostLoad; | ||
| import jakarta.persistence.PrePersist; | ||
| import jakarta.persistence.Transient; | ||
| import jakarta.persistence.Version; | ||
| import java.io.Serializable; | ||
| import java.time.LocalDate; | ||
| import java.time.OffsetDateTime; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import org.apache.fineract.infrastructure.core.service.DateUtils; | ||
| import org.springframework.data.domain.Persistable; | ||
|
|
||
| @Getter | ||
| @MappedSuperclass | ||
| @NoArgsConstructor | ||
| public abstract class AccountLock implements Persistable<Long>, Serializable { | ||
|
|
||
| protected static final long serialVersionUID = 2272591907035824317L; | ||
|
|
||
| @Id | ||
| @Getter | ||
| @Column(name = "loan_id", nullable = false) | ||
| protected Long loanId; | ||
|
|
||
| @Version | ||
| @Getter | ||
| @Column(name = "version") | ||
| protected Long version; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Getter | ||
| @Column(name = "lock_owner", nullable = false) | ||
| protected LockOwner lockOwner; | ||
|
|
||
| @Column(name = "lock_placed_on", nullable = false) | ||
| @Getter | ||
| protected OffsetDateTime lockPlacedOn; | ||
|
|
||
| @Column(name = "error") | ||
| @Getter | ||
| protected String error; | ||
|
|
||
| @Column(name = "stacktrace") | ||
| @Getter | ||
| protected String stacktrace; | ||
|
|
||
| @Column(name = "lock_placed_on_cob_business_date") | ||
| @Getter | ||
| protected LocalDate lockPlacedOnCobBusinessDate; | ||
|
|
||
| @Transient | ||
| @Setter(value = AccessLevel.NONE) | ||
| @Getter | ||
| protected boolean isNew = true; | ||
|
|
||
| @PrePersist | ||
| @PostLoad | ||
| void markNotNew() { | ||
| this.isNew = false; | ||
| } | ||
|
|
||
| @Override | ||
| public Long getId() { | ||
| return getLoanId(); | ||
| } | ||
|
|
||
| public AccountLock(Long loanId, LockOwner lockOwner, LocalDate lockPlacedOnCobBusinessDate) { | ||
| this.loanId = loanId; | ||
| this.lockOwner = lockOwner; | ||
| this.lockPlacedOn = DateUtils.getAuditOffsetDateTime(); | ||
| this.lockPlacedOnCobBusinessDate = lockPlacedOnCobBusinessDate; | ||
| } | ||
|
|
||
| public void setError(String errorMessage, String stacktrace) { | ||
| this.error = errorMessage; | ||
| this.stacktrace = stacktrace; | ||
| } | ||
|
|
||
| public void setNewLockOwner(LockOwner newLockOwner) { | ||
| this.lockOwner = newLockOwner; | ||
| this.lockPlacedOn = DateUtils.getAuditOffsetDateTime(); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
fineract-cob/src/main/java/org/apache/fineract/cob/domain/AccountLockRepository.java
This file contains hidden or 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,45 @@ | ||
| /** | ||
| * 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.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.repository.NoRepositoryBean; | ||
|
|
||
| @NoRepositoryBean | ||
| public interface AccountLockRepository<T extends AccountLock> { | ||
|
|
||
| Optional<T> findByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner); | ||
|
|
||
| void deleteByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner); | ||
|
|
||
| List<T> findAllByLoanIdIn(List<Long> loanIds); | ||
|
|
||
| boolean existsByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner); | ||
|
|
||
| boolean existsByLoanIdAndLockOwnerAndErrorIsNotNull(Long loanId, LockOwner lockOwner); | ||
|
|
||
| List<T> findAllByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner); | ||
|
|
||
| void removeByLockOwnerInAndErrorIsNotNullAndLockPlacedOnCobBusinessDateIsNotNull(List<LockOwner> lockOwners); | ||
|
|
||
| Page<T> findAll(Pageable loanAccountLockPage); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.