Log errors from cron jobs individually and with cause (FINERACT-858)

instead of current huge JobExecutionException message (without cause)
This commit is contained in:
Michael Vorburger
2020-03-10 17:10:01 +01:00
committed by Michael Vorburger ⛑️
parent 0c9273a11b
commit 1b1e80b6b1
16 changed files with 397 additions and 444 deletions
@@ -29,6 +29,7 @@ import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.ApiParameterError; import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder; import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
import org.apache.fineract.infrastructure.core.exception.MultiException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException; import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.portfolio.loanaccount.service.LoanAccrualPlatformService; import org.apache.fineract.portfolio.loanaccount.service.LoanAccrualPlatformService;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
@@ -52,12 +53,13 @@ public class AccrualAccountingWritePlatformServiceImpl implements AccrualAccount
public CommandProcessingResult executeLoansPeriodicAccrual(JsonCommand command) { public CommandProcessingResult executeLoansPeriodicAccrual(JsonCommand command) {
this.accountingDataValidator.validateLoanPeriodicAccrualData(command.json()); this.accountingDataValidator.validateLoanPeriodicAccrualData(command.json());
LocalDate tilldate = command.localDateValueOfParameterNamed(accrueTillParamName); LocalDate tilldate = command.localDateValueOfParameterNamed(accrueTillParamName);
String errorlog = this.loanAccrualPlatformService.addPeriodicAccruals(tilldate); try {
if (errorlog.length() > 0) { this.loanAccrualPlatformService.addPeriodicAccruals(tilldate);
} catch (MultiException e) {
final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
.resource(PERIODIC_ACCRUAL_ACCOUNTING_RESOURCE_NAME); .resource(PERIODIC_ACCRUAL_ACCOUNTING_RESOURCE_NAME);
baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(PERIODIC_ACCRUAL_ACCOUNTING_EXECUTION_ERROR_CODE, errorlog); baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(PERIODIC_ACCRUAL_ACCOUNTING_EXECUTION_ERROR_CODE, e.getMessage());
throw new PlatformApiDataValidationException(dataValidationErrors); throw new PlatformApiDataValidationException(dataValidationErrors);
} }
return CommandProcessingResult.empty(); return CommandProcessingResult.empty();
@@ -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.infrastructure.core.exception;
/**
* Exception with multiple root causes.
*
* Intended to be used in places where N operations are performed in a loop over something,
* each of which could fail, but where we don't want to fail immediately but continue, and
* then fail at end.
*
* <p>The failures MUST each be logged within the loop, as they occur; this exception is
* only thrown to propagate the failure, but will not contain and re-log the details.
*
* @author Michael Vorburger.ch <mike@vorburger.ch>
*/
public class MultiException extends Exception {
private final int n;
public MultiException(int n) {
super(n + "x failures occured here (details have been previously logged");
this.n = n;
}
public int getCausesSize() {
return n;
}
}
@@ -26,9 +26,7 @@ import org.apache.fineract.infrastructure.jobs.service.JobName;
/** /**
* Annotation that marks a method to be picked while scheduling a cron jobs. * Annotation that marks a method to be picked while scheduling a cron jobs.
*
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface CronTarget { public @interface CronTarget {
@@ -18,9 +18,11 @@
*/ */
package org.apache.fineract.infrastructure.jobs.exception; package org.apache.fineract.infrastructure.jobs.exception;
public class JobExecutionException extends Exception { import org.apache.fineract.infrastructure.core.exception.MultiException;
public JobExecutionException(final String msg) { public class JobExecutionException extends MultiException {
super(msg);
public JobExecutionException(int n) {
super(n);
} }
} }
@@ -20,27 +20,27 @@ package org.apache.fineract.infrastructure.jobs.service;
public enum JobName { public enum JobName {
UPDATE_LOAN_SUMMARY("Update loan Summary"), // UPDATE_LOAN_SUMMARY("Update loan Summary"),
UPDATE_LOAN_ARREARS_AGEING("Update Loan Arrears Ageing"), // UPDATE_LOAN_ARREARS_AGEING("Update Loan Arrears Ageing"),
UPDATE_LOAN_PAID_IN_ADVANCE("Update Loan Paid In Advance"), // UPDATE_LOAN_PAID_IN_ADVANCE("Update Loan Paid In Advance"),
APPLY_ANNUAL_FEE_FOR_SAVINGS("Apply Annual Fee For Savings"), // APPLY_ANNUAL_FEE_FOR_SAVINGS("Apply Annual Fee For Savings"),
APPLY_HOLIDAYS_TO_LOANS("Apply Holidays To Loans"), // APPLY_HOLIDAYS_TO_LOANS("Apply Holidays To Loans"),
POST_INTEREST_FOR_SAVINGS("Post Interest For Savings"), // POST_INTEREST_FOR_SAVINGS("Post Interest For Savings"),
TRANSFER_FEE_CHARGE_FOR_LOANS("Transfer Fee For Loans From Savings"), // TRANSFER_FEE_CHARGE_FOR_LOANS("Transfer Fee For Loans From Savings"),
ACCOUNTING_RUNNING_BALANCE_UPDATE("Update Accounting Running Balances"), // ACCOUNTING_RUNNING_BALANCE_UPDATE("Update Accounting Running Balances"),
PAY_DUE_SAVINGS_CHARGES("Pay Due Savings Charges"), // PAY_DUE_SAVINGS_CHARGES("Pay Due Savings Charges"),
APPLY_CHARGE_TO_OVERDUE_LOAN_INSTALLMENT("Apply penalty to overdue loans"), // APPLY_CHARGE_TO_OVERDUE_LOAN_INSTALLMENT("Apply penalty to overdue loans"),
EXECUTE_STANDING_INSTRUCTIONS("Execute Standing Instruction"), // EXECUTE_STANDING_INSTRUCTIONS("Execute Standing Instruction"),
ADD_ACCRUAL_ENTRIES("Add Accrual Transactions"), // ADD_ACCRUAL_ENTRIES("Add Accrual Transactions"),
UPDATE_NPA("Update Non Performing Assets"), // UPDATE_NPA("Update Non Performing Assets"),
UPDATE_DEPOSITS_ACCOUNT_MATURITY_DETAILS("Update Deposit Accounts Maturity details"), // UPDATE_DEPOSITS_ACCOUNT_MATURITY_DETAILS("Update Deposit Accounts Maturity details"),
TRANSFER_INTEREST_TO_SAVINGS("Transfer Interest To Savings"), // TRANSFER_INTEREST_TO_SAVINGS("Transfer Interest To Savings"),
ADD_PERIODIC_ACCRUAL_ENTRIES("Add Periodic Accrual Transactions"), // ADD_PERIODIC_ACCRUAL_ENTRIES("Add Periodic Accrual Transactions"),
RECALCULATE_INTEREST_FOR_LOAN("Recalculate Interest For Loans"), // RECALCULATE_INTEREST_FOR_LOAN("Recalculate Interest For Loans"),
GENERATE_RD_SCEHDULE("Generate Mandatory Savings Schedule"), // GENERATE_RD_SCEHDULE("Generate Mandatory Savings Schedule"),
GENERATE_LOANLOSS_PROVISIONING("Generate Loan Loss Provisioning"), // GENERATE_LOANLOSS_PROVISIONING("Generate Loan Loss Provisioning"),
POST_DIVIDENTS_FOR_SHARES("Post Dividends For Shares"), // POST_DIVIDENTS_FOR_SHARES("Post Dividends For Shares"),
UPDATE_SAVINGS_DORMANT_ACCOUNTS("Update Savings Dormant Accounts"), // UPDATE_SAVINGS_DORMANT_ACCOUNTS("Update Savings Dormant Accounts"),
ADD_PERIODIC_ACCRUAL_ENTRIES_FOR_LOANS_WITH_INCOME_POSTED_AS_TRANSACTIONS("Add Accrual Transactions For Loans With Income Posted As Transactions"), ADD_PERIODIC_ACCRUAL_ENTRIES_FOR_LOANS_WITH_INCOME_POSTED_AS_TRANSACTIONS("Add Accrual Transactions For Loans With Income Posted As Transactions"),
EXECUTE_REPORT_MAILING_JOBS("Execute Report Mailing Jobs"), EXECUTE_REPORT_MAILING_JOBS("Execute Report Mailing Jobs"),
UPDATE_SMS_OUTBOUND_WITH_CAMPAIGN_MESSAGE("Update SMS Outbound with Campaign Message"), UPDATE_SMS_OUTBOUND_WITH_CAMPAIGN_MESSAGE("Update SMS Outbound with Campaign Message"),
@@ -167,10 +167,10 @@ public class StandingInstructionWritePlatformServiceImpl implements StandingInst
AccountTransferStandingInstruction standingInstructionsForUpdate = this.standingInstructionRepository.findById(id) AccountTransferStandingInstruction standingInstructionsForUpdate = this.standingInstructionRepository.findById(id)
.orElseThrow(() -> new StandingInstructionNotFoundException(id)); .orElseThrow(() -> new StandingInstructionNotFoundException(id));
final Map<String, Object> actualChanges = standingInstructionsForUpdate.update(command); final Map<String, Object> actualChanges = standingInstructionsForUpdate.update(command);
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(id) // .withEntityId(id)
.with(actualChanges) // .with(actualChanges)
.build(); .build();
} }
@@ -182,9 +182,9 @@ public class StandingInstructionWritePlatformServiceImpl implements StandingInst
final Map<String, Object> actualChanges = new HashMap<>(); final Map<String, Object> actualChanges = new HashMap<>();
actualChanges.put(statusParamName, StandingInstructionStatus.DELETED.getValue()); actualChanges.put(statusParamName, StandingInstructionStatus.DELETED.getValue());
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder()
.withEntityId(id) // .withEntityId(id)
.with(actualChanges) // .with(actualChanges)
.build(); .build();
} }
@@ -249,14 +249,13 @@ public class StandingInstructionWritePlatformServiceImpl implements StandingInst
} }
} }
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } if (sb.length() > 0) {
logger.error("executeStandingInstructions (transferAmount) encountered failure/s: {}", sb.toString());
// This is a bit of a hack (related to https://issues.apache.org/jira/browse/FINERACT-858) and could be improved..
throw new JobExecutionException(123456789);
}
} }
/**
* @param sb
* @param accountTransferDTO
*/
private boolean transferAmount(final StringBuilder sb, final AccountTransferDTO accountTransferDTO, final Long instructionId) { private boolean transferAmount(final StringBuilder sb, final AccountTransferDTO accountTransferDTO, final Long instructionId) {
boolean transferCompleted = true; boolean transferCompleted = true;
StringBuffer errorLog = new StringBuffer(); StringBuffer errorLog = new StringBuffer();
@@ -35,6 +35,7 @@ import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder; import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
import org.apache.fineract.infrastructure.core.exception.GeneralPlatformDomainRuleException; import org.apache.fineract.infrastructure.core.exception.GeneralPlatformDomainRuleException;
import org.apache.fineract.infrastructure.core.exception.MultiException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException; import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.infrastructure.core.service.DateUtils; import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
@@ -227,10 +228,10 @@ public class LoanAccountDomainServiceJpa implements LoanAccountDomainService {
// disable all active standing orders linked to this loan if status changes to closed // disable all active standing orders linked to this loan if status changes to closed
disableStandingInstructionsLinkedToClosedLoan(loan); disableStandingInstructionsLinkedToClosedLoan(loan);
builderResult.withEntityId(newRepaymentTransaction.getId()) // builderResult.withEntityId(newRepaymentTransaction.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()); // .withGroupId(loan.getGroupId());
return newRepaymentTransaction; return newRepaymentTransaction;
} }
@@ -412,10 +413,10 @@ public class LoanAccountDomainServiceJpa implements LoanAccountDomainService {
postJournalEntries(loan, existingTransactionIds, existingReversedTransactionIds, isAccountTransfer); postJournalEntries(loan, existingTransactionIds, existingReversedTransactionIds, isAccountTransfer);
this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_REFUND, this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_REFUND,
constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, newRefundTransaction)); constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, newRefundTransaction));
builderResult.withEntityId(newRefundTransaction.getId()) // builderResult.withEntityId(newRefundTransaction.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()); // .withGroupId(loan.getGroupId());
return newRefundTransaction; return newRefundTransaction;
} }
@@ -509,10 +510,11 @@ public class LoanAccountDomainServiceJpa implements LoanAccountDomainService {
} }
if (!loanScheduleAccrualDatas.isEmpty()) { if (!loanScheduleAccrualDatas.isEmpty()) {
String error = this.loanAccrualPlatformService.addPeriodicAccruals(accruedTill, loanScheduleAccrualDatas); try {
if (error.length() > 0) { this.loanAccrualPlatformService.addPeriodicAccruals(accruedTill, loanScheduleAccrualDatas);
} catch (MultiException e) {
String globalisationMessageCode = "error.msg.accrual.exception"; String globalisationMessageCode = "error.msg.accrual.exception";
throw new GeneralPlatformDomainRuleException(globalisationMessageCode, error, error); throw new GeneralPlatformDomainRuleException(globalisationMessageCode, e.getMessage());
} }
} }
} }
@@ -604,10 +606,10 @@ public class LoanAccountDomainServiceJpa implements LoanAccountDomainService {
this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_REFUND, this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_REFUND,
constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, newRefundTransaction)); constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, newRefundTransaction));
builderResult.withEntityId(newRefundTransaction.getId()) // builderResult.withEntityId(newRefundTransaction.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()); // .withGroupId(loan.getGroupId());
return newRefundTransaction; return newRefundTransaction;
} }
@@ -19,15 +19,16 @@
package org.apache.fineract.portfolio.loanaccount.service; package org.apache.fineract.portfolio.loanaccount.service;
import java.util.Collection; import java.util.Collection;
import org.apache.fineract.infrastructure.core.exception.MultiException;
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException; import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.apache.fineract.portfolio.loanaccount.data.LoanScheduleAccrualData; import org.apache.fineract.portfolio.loanaccount.data.LoanScheduleAccrualData;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
public interface LoanAccrualPlatformService { public interface LoanAccrualPlatformService {
String addPeriodicAccruals(LocalDate tilldate); void addPeriodicAccruals(LocalDate tilldate) throws MultiException;
String addPeriodicAccruals(LocalDate tilldate, Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas); void addPeriodicAccruals(LocalDate tilldate, Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas) throws MultiException;
void addAccrualAccounting() throws JobExecutionException; void addAccrualAccounting() throws JobExecutionException;
@@ -22,17 +22,22 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.fineract.infrastructure.core.exception.MultiException;
import org.apache.fineract.infrastructure.jobs.annotation.CronTarget; import org.apache.fineract.infrastructure.jobs.annotation.CronTarget;
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException; import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.apache.fineract.infrastructure.jobs.service.JobName; import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.portfolio.loanaccount.data.LoanScheduleAccrualData; import org.apache.fineract.portfolio.loanaccount.data.LoanScheduleAccrualData;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformService { public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformService {
private final static Logger LOG = LoggerFactory.getLogger(LoanAccrualPlatformServiceImpl.class);
private final LoanReadPlatformService loanReadPlatformService; private final LoanReadPlatformService loanReadPlatformService;
private final LoanAccrualWritePlatformService loanAccrualWritePlatformService; private final LoanAccrualWritePlatformService loanAccrualWritePlatformService;
@@ -47,7 +52,6 @@ public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformServic
@CronTarget(jobName = JobName.ADD_ACCRUAL_ENTRIES) @CronTarget(jobName = JobName.ADD_ACCRUAL_ENTRIES)
public void addAccrualAccounting() throws JobExecutionException { public void addAccrualAccounting() throws JobExecutionException {
Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas = this.loanReadPlatformService.retriveScheduleAccrualData(); Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas = this.loanReadPlatformService.retriveScheduleAccrualData();
StringBuilder sb = new StringBuilder();
Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>(); Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>();
for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) { for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) {
if (loanDataMap.containsKey(accrualData.getLoanId())) { if (loanDataMap.containsKey(accrualData.getLoanId())) {
@@ -59,37 +63,36 @@ public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformServic
} }
} }
int errors = 0;
for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) { for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) {
try { try {
this.loanAccrualWritePlatformService.addAccrualAccounting(mapEntry.getKey(), mapEntry.getValue()); this.loanAccrualWritePlatformService.addAccrualAccounting(mapEntry.getKey(), mapEntry.getValue());
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; LOG.error("Failed to add accural transaction for loan {}", mapEntry.getKey(), e);
if (e.getCause() != null) { ++errors;
realCause = e.getCause();
}
sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + realCause.getMessage());
} }
} }
if (errors > 0) { throw new JobExecutionException(errors); }
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); }
} }
@Override @Override
@CronTarget(jobName = JobName.ADD_PERIODIC_ACCRUAL_ENTRIES) @CronTarget(jobName = JobName.ADD_PERIODIC_ACCRUAL_ENTRIES)
public void addPeriodicAccruals() throws JobExecutionException { public void addPeriodicAccruals() throws JobExecutionException {
String errors = addPeriodicAccruals(LocalDate.now()); try {
if (errors.length() > 0) { throw new JobExecutionException(errors); } addPeriodicAccruals(LocalDate.now());
} catch (MultiException e) {
throw new JobExecutionException(e.getCausesSize());
}
} }
@Override @Override
public String addPeriodicAccruals(final LocalDate tilldate) { public void addPeriodicAccruals(final LocalDate tilldate) throws MultiException {
Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas = this.loanReadPlatformService.retrivePeriodicAccrualData(tilldate); Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas = this.loanReadPlatformService.retrivePeriodicAccrualData(tilldate);
return addPeriodicAccruals(tilldate, loanScheduleAccrualDatas); addPeriodicAccruals(tilldate, loanScheduleAccrualDatas);
} }
@Override @Override
public String addPeriodicAccruals(final LocalDate tilldate, Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas) { public void addPeriodicAccruals(final LocalDate tilldate, Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas) throws MultiException {
StringBuilder sb = new StringBuilder();
Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>(); Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>();
for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) { for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) {
if (loanDataMap.containsKey(accrualData.getLoanId())) { if (loanDataMap.containsKey(accrualData.getLoanId())) {
@@ -101,19 +104,16 @@ public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformServic
} }
} }
int errors = 0;
for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) { for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) {
try { try {
this.loanAccrualWritePlatformService.addPeriodicAccruals(tilldate, mapEntry.getKey(), mapEntry.getValue()); this.loanAccrualWritePlatformService.addPeriodicAccruals(tilldate, mapEntry.getKey(), mapEntry.getValue());
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; LOG.error("Failed to add accural transaction for loan {}", mapEntry.getKey(), e);
if (e.getCause() != null) { ++errors;
realCause = e.getCause();
}
sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + realCause.getMessage());
} }
} }
if (errors > 0) { throw new MultiException(errors); }
return sb.toString();
} }
@Override @Override
@@ -121,19 +121,16 @@ public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformServic
public void addPeriodicAccrualsForLoansWithIncomePostedAsTransactions() throws JobExecutionException { public void addPeriodicAccrualsForLoansWithIncomePostedAsTransactions() throws JobExecutionException {
Collection<Long> loanIds = this.loanReadPlatformService.retrieveLoanIdsWithPendingIncomePostingTransactions(); Collection<Long> loanIds = this.loanReadPlatformService.retrieveLoanIdsWithPendingIncomePostingTransactions();
if(loanIds != null && loanIds.size() > 0){ if(loanIds != null && loanIds.size() > 0){
StringBuilder sb = new StringBuilder(); int errors = 0;
for (Long loanId : loanIds) { for (Long loanId : loanIds) {
try { try {
this.loanAccrualWritePlatformService.addIncomeAndAccrualTransactions(loanId); this.loanAccrualWritePlatformService.addIncomeAndAccrualTransactions(loanId);
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; LOG.error("Failed to add income and accrual transaction for loan {}", loanId, e);
if (e.getCause() != null) { ++errors;
realCause = e.getCause();
}
sb.append("failed to add income and accrual transaction for loan " + loanId + " with message " + realCause.getMessage());
} }
} }
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } if (errors > 0) { throw new JobExecutionException(errors); }
} }
} }
} }
@@ -20,7 +20,6 @@ package org.apache.fineract.portfolio.loanaccount.service;
import java.util.Map; import java.util.Map;
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException; import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.apache.fineract.organisation.office.data.OfficeData;
public interface LoanSchedularService { public interface LoanSchedularService {
@@ -29,8 +28,5 @@ public interface LoanSchedularService {
void recalculateInterest() throws JobExecutionException; void recalculateInterest() throws JobExecutionException;
void recalculateInterest(final OfficeData office, final int threadPoolSize, final int batchSize);
void recalculateInterest(@SuppressWarnings("unused") final Map<String, String> jobParameters); void recalculateInterest(@SuppressWarnings("unused") final Map<String, String> jobParameters);
} }
@@ -55,6 +55,7 @@ import org.springframework.util.CollectionUtils;
public class LoanSchedularServiceImpl implements LoanSchedularService { public class LoanSchedularServiceImpl implements LoanSchedularService {
private final static Logger logger = LoggerFactory.getLogger(LoanSchedularServiceImpl.class); private final static Logger logger = LoggerFactory.getLogger(LoanSchedularServiceImpl.class);
private final ConfigurationDomainService configurationDomainService; private final ConfigurationDomainService configurationDomainService;
private final LoanReadPlatformService loanReadPlatformService; private final LoanReadPlatformService loanReadPlatformService;
private final LoanWritePlatformService loanWritePlatformService; private final LoanWritePlatformService loanWritePlatformService;
@@ -83,7 +84,6 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
.retrieveAllLoansWithOverdueInstallments(penaltyWaitPeriodValue,backdatePenalties); .retrieveAllLoansWithOverdueInstallments(penaltyWaitPeriodValue,backdatePenalties);
if (!overdueLoanScheduledInstallments.isEmpty()) { if (!overdueLoanScheduledInstallments.isEmpty()) {
final StringBuilder sb = new StringBuilder();
final Map<Long, Collection<OverdueLoanScheduleData>> overdueScheduleData = new HashMap<>(); final Map<Long, Collection<OverdueLoanScheduleData>> overdueScheduleData = new HashMap<>();
for (final OverdueLoanScheduleData overdueInstallment : overdueLoanScheduledInstallments) { for (final OverdueLoanScheduleData overdueInstallment : overdueLoanScheduledInstallments) {
if (overdueScheduleData.containsKey(overdueInstallment.getLoanId())) { if (overdueScheduleData.containsKey(overdueInstallment.getLoanId())) {
@@ -95,6 +95,7 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
} }
} }
int numberOfErrors = 0;
for (final Long loanId : overdueScheduleData.keySet()) { for (final Long loanId : overdueScheduleData.keySet()) {
try { try {
this.loanWritePlatformService.applyOverdueChargesForLoan(loanId, overdueScheduleData.get(loanId)); this.loanWritePlatformService.applyOverdueChargesForLoan(loanId, overdueScheduleData.get(loanId));
@@ -102,28 +103,18 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
} catch (final PlatformApiDataValidationException e) { } catch (final PlatformApiDataValidationException e) {
final List<ApiParameterError> errors = e.getErrors(); final List<ApiParameterError> errors = e.getErrors();
for (final ApiParameterError error : errors) { for (final ApiParameterError error : errors) {
logger.error("Apply Charges due for overdue loans failed for account:" + loanId + " with message " logger.error("Apply Charges due for overdue loans failed for account {} with message: {}", loanId, error.getDeveloperMessage(), e);
+ error.getDeveloperMessage()); ++numberOfErrors;
sb.append("Apply Charges due for overdue loans failed for account:").append(loanId).append(" with message ")
.append(error.getDeveloperMessage());
} }
} catch (final AbstractPlatformDomainRuleException ex) { } catch (final AbstractPlatformDomainRuleException e) {
logger.error("Apply Charges due for overdue loans failed for account:" + loanId + " with message " logger.error("Apply Charges due for overdue loans failed for account {} with message: {}", loanId, e.getDefaultUserMessage(), e);
+ ex.getDefaultUserMessage()); ++numberOfErrors;
sb.append("Apply Charges due for overdue loans failed for account:").append(loanId).append(" with message ")
.append(ex.getDefaultUserMessage());
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; logger.error("Apply Charges due for overdue loans failed for account {}", loanId, e);
if (e.getCause() != null) { ++numberOfErrors;
realCause = e.getCause();
}
logger.error("Apply Charges due for overdue loans failed for account:" + loanId + " with message "
+ realCause.getMessage());
sb.append("Apply Charges due for overdue loans failed for account:").append(loanId).append(" with message ")
.append(realCause.getMessage());
} }
} }
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } if (numberOfErrors > 0) { throw new JobExecutionException(numberOfErrors); }
} }
} }
@@ -138,64 +129,44 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
.fetchLoansForInterestRecalculation(); .fetchLoansForInterestRecalculation();
int i = 0; int i = 0;
if (!loanIds.isEmpty()) { if (!loanIds.isEmpty()) {
final StringBuilder sb = new StringBuilder(); int errors = 0;
for (Long loanId : loanIds) { for (Long loanId : loanIds) {
logger.info("Loan ID " + loanId); logger.info("recalculateInterest: Loan ID = {}", loanId);
Integer numberOfRetries = 0; Integer numberOfRetries = 0;
while (numberOfRetries <= maxNumberOfRetries) { while (numberOfRetries <= maxNumberOfRetries) {
try { try {
this.loanWritePlatformService this.loanWritePlatformService.recalculateInterest(loanId);
.recalculateInterest(loanId);
numberOfRetries = maxNumberOfRetries + 1; numberOfRetries = maxNumberOfRetries + 1;
} catch (CannotAcquireLockException } catch (CannotAcquireLockException
| ObjectOptimisticLockingFailureException exception) { | ObjectOptimisticLockingFailureException exception) {
logger.info("Recalulate interest job has been retried " logger.info("Recalulate interest job has been retried {} time(s)", numberOfRetries);
+ numberOfRetries + " time(s)"); // Fail if the transaction has been retried for maxNumberOfRetries
/***
* Fail if the transaction has been retired for
* maxNumberOfRetries
**/
if (numberOfRetries >= maxNumberOfRetries) { if (numberOfRetries >= maxNumberOfRetries) {
logger.warn("Recalulate interest job has been retried for the max allowed attempts of " logger.error("Recalulate interest job has been retried for the max allowed attempts of {} and will be rolled back", numberOfRetries);
+ numberOfRetries ++errors;
+ " and will be rolled back");
sb.append("Recalulate interest job has been retried for the max allowed attempts of "
+ numberOfRetries
+ " and will be rolled back");
break; break;
} }
/*** // Else sleep for a random time (between 1 to 10 seconds) and continue
* Else sleep for a random time (between 1 to 10
* seconds) and continue
**/
try { try {
Random random = new Random(); Random random = new Random();
int randomNum = random int randomNum = random.nextInt(maxIntervalBetweenRetries + 1);
.nextInt(maxIntervalBetweenRetries + 1);
Thread.sleep(1000 + (randomNum * 1000)); Thread.sleep(1000 + (randomNum * 1000));
numberOfRetries = numberOfRetries + 1; numberOfRetries = numberOfRetries + 1;
} catch (InterruptedException e) { } catch (InterruptedException e) {
sb.append("Interest recalculation for loans failed " + exception.getMessage()) ; logger.error("Interest recalculation for loans retry failed due to InterruptedException", e) ;
++errors;
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; logger.error("Interest recalculation for loans failed for account {}", loanId, e);
if (e.getCause() != null) {
realCause = e.getCause();
}
logger.error("Interest recalculation for loans failed for account:" + loanId + " with message "
+ realCause.getMessage());
sb.append("Interest recalculation for loans failed for account:").append(loanId).append(" with message ")
.append(realCause.getMessage());
numberOfRetries = maxNumberOfRetries + 1; numberOfRetries = maxNumberOfRetries + 1;
++errors;
} }
i++; i++;
} }
logger.info("Loans count " + i); logger.info("recalculateInterest: Loans count {}", i);
}
if (sb.length() > 0) {
throw new JobExecutionException(sb.toString());
} }
if (errors > 0) { throw new JobExecutionException(errors); }
} }
} }
@@ -205,20 +176,21 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
public void recalculateInterest(Map<String, String> jobParameters) { public void recalculateInterest(Map<String, String> jobParameters) {
// gets the officeId // gets the officeId
final String officeId = jobParameters.get("officeId"); final String officeId = jobParameters.get("officeId");
logger.info(officeId); logger.info("recalculateInterest: officeId={}", officeId);
Long officeIdLong = Long.valueOf(officeId); Long officeIdLong = Long.valueOf(officeId);
// gets the Office object // gets the Office object
final OfficeData office = this.officeReadPlatformService.retrieveOffice(officeIdLong); final OfficeData office = this.officeReadPlatformService.retrieveOffice(officeIdLong);
if(office == null) if(office == null) {
throw new OfficeNotFoundException(officeIdLong); throw new OfficeNotFoundException(officeIdLong);
}
final int threadPoolSize=Integer.parseInt(jobParameters.get("thread-pool-size")); final int threadPoolSize=Integer.parseInt(jobParameters.get("thread-pool-size"));
final int batchSize=Integer.parseInt(jobParameters.get("batch-size")); final int batchSize=Integer.parseInt(jobParameters.get("batch-size"));
recalculateInterest(office,threadPoolSize,batchSize); recalculateInterest(office,threadPoolSize,batchSize);
} }
@Override private void recalculateInterest(OfficeData office, int threadPoolSize, int batchSize) {
public void recalculateInterest(OfficeData office, int threadPoolSize, int batchSize) {
final int pageSize = batchSize * threadPoolSize; final int pageSize = batchSize * threadPoolSize;
// initialise the executor service with fetched configurations // initialise the executor service with fetched configurations
@@ -227,17 +199,15 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
Long maxLoanIdInList = 0L; Long maxLoanIdInList = 0L;
final String officeHierarchy = office.getHierarchy() + "%"; final String officeHierarchy = office.getHierarchy() + "%";
//Get the loanIds from service // get the loanIds from service
List<Long> loanIds = Collections.synchronizedList(this.loanReadPlatformService List<Long> loanIds = Collections.synchronizedList(this.loanReadPlatformService
.fetchLoansForInterestRecalculation(pageSize, maxLoanIdInList, officeHierarchy)); .fetchLoansForInterestRecalculation(pageSize, maxLoanIdInList, officeHierarchy));
// gets the loanIds data set iteratively and call addAccuruals for that paginated dataset // gets the loanIds data set iteratively and call addAccuruals for that paginated dataset
do { do {
int totalFilteredRecords = loanIds.size(); int totalFilteredRecords = loanIds.size();
logger.info("Starting accrual - total filtered records - " + totalFilteredRecords); logger.info("Starting accrual - total filtered records - " + totalFilteredRecords);
recalculateInterest(loanIds, threadPoolSize, batchSize, recalculateInterest(loanIds, threadPoolSize, batchSize, executorService);
executorService);
maxLoanIdInList+= pageSize+1; maxLoanIdInList+= pageSize+1;
loanIds = Collections.synchronizedList(this.loanReadPlatformService loanIds = Collections.synchronizedList(this.loanReadPlatformService
.fetchLoansForInterestRecalculation(pageSize, maxLoanIdInList, officeHierarchy)); .fetchLoansForInterestRecalculation(pageSize, maxLoanIdInList, officeHierarchy));
@@ -249,18 +219,18 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
private void recalculateInterest(List<Long> loanIds, private void recalculateInterest(List<Long> loanIds,
int threadPoolSize, int batchSize, final ExecutorService executorService) { int threadPoolSize, int batchSize, final ExecutorService executorService) {
//StringBuilder sb = new StringBuilder();
List<Callable<Object>> posters = new ArrayList<Callable<Object>>(); List<Callable<Void>> posters = new ArrayList<>();
int fromIndex = 0; int fromIndex = 0;
// get the size of current paginated dataset // get the size of current paginated dataset
int size = loanIds.size(); int size = loanIds.size();
// calculate the batch size // calculate the batch size
double toGetCeilValue = (double) (size / threadPoolSize); double toGetCeilValue = size / threadPoolSize;
batchSize = (int) Math.ceil(toGetCeilValue); batchSize = (int) Math.ceil(toGetCeilValue);
if(batchSize == 0) if(batchSize == 0) {
return; return;
}
int toIndex = (batchSize > size - 1)? size : batchSize ; int toIndex = (batchSize > size - 1)? size : batchSize ;
while(toIndex < size && loanIds.get(toIndex - 1).equals(loanIds.get(toIndex))) { while(toIndex < size && loanIds.get(toIndex - 1).equals(loanIds.get(toIndex))) {
@@ -274,11 +244,13 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
RecalculateInterestPoster poster = (RecalculateInterestPoster) this.applicationContext.getBean("recalculateInterestPoster"); RecalculateInterestPoster poster = (RecalculateInterestPoster) this.applicationContext.getBean("recalculateInterestPoster");
poster.setLoanIds(subList); poster.setLoanIds(subList);
poster.setLoanWritePlatformService(loanWritePlatformService); poster.setLoanWritePlatformService(loanWritePlatformService);
posters.add(Executors.callable(poster)); posters.add(poster);
if(lastBatch) if(lastBatch) {
break; break;
if(toIndex + batchSize > size - 1) }
if(toIndex + batchSize > size - 1) {
lastBatch = true; lastBatch = true;
}
fromIndex = fromIndex + (toIndex - fromIndex); fromIndex = fromIndex + (toIndex - fromIndex);
toIndex = (toIndex + batchSize > size - 1)? size : toIndex + batchSize; toIndex = (toIndex + batchSize > size - 1)? size : toIndex + batchSize;
while(toIndex < size && loanIds.get(toIndex - 1).equals(loanIds.get(toIndex))) { while(toIndex < size && loanIds.get(toIndex - 1).equals(loanIds.get(toIndex))) {
@@ -287,14 +259,15 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
} }
try { try {
List<Future<Object>> responses = executorService.invokeAll(posters); List<Future<Void>> responses = executorService.invokeAll(posters);
checkCompletion(responses); checkCompletion(responses);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
logger.error("Interrupted while recalculateInterest", e1); logger.error("Interrupted while recalculateInterest", e1);
} }
} }
// break the lists into sub lists // break the lists into sub lists
public <T> List<T> safeSubList(List<T> list, int fromIndex, int toIndex) { private <T> List<T> safeSubList(List<T> list, int fromIndex, int toIndex) {
int size = list.size(); int size = list.size();
if (fromIndex >= size || toIndex <= 0 || fromIndex >= toIndex) { if (fromIndex >= size || toIndex <= 0 || fromIndex >= toIndex) {
return Collections.emptyList(); return Collections.emptyList();
@@ -305,30 +278,28 @@ public class LoanSchedularServiceImpl implements LoanSchedularService {
return list.subList(fromIndex, toIndex); return list.subList(fromIndex, toIndex);
} }
// checks the execution of task by each thread in the executor service // checks the execution of task by each thread in the executor service
private void checkCompletion(List<Future<Object>> responses) { private void checkCompletion(List<Future<Void>> responses) {
try { try {
for(Future f : responses) { for(Future<Void> f : responses) {
f.get(); f.get();
} }
boolean allThreadsExecuted = false; boolean allThreadsExecuted = false;
int noOfThreadsExecuted = 0; int noOfThreadsExecuted = 0;
for (Future<Object> future : responses) { for (Future<Void> future : responses) {
if (future.isDone()) { if (future.isDone()) {
noOfThreadsExecuted++; noOfThreadsExecuted++;
} }
} }
allThreadsExecuted = noOfThreadsExecuted == responses.size(); allThreadsExecuted = noOfThreadsExecuted == responses.size();
if(!allThreadsExecuted) if(!allThreadsExecuted) {
logger.error("All threads could not execute."); logger.error("All threads could not execute.");
}
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
logger.error("Interrupted while posting IR entries", e1); logger.error("Interrupted while posting IR entries", e1);
} catch (ExecutionException e2) { } catch (ExecutionException e2) {
logger.error("Execution exception while posting IR entries", e2); logger.error("Execution exception while posting IR entries", e2);
} }
} }
} }
@@ -194,7 +194,7 @@ import org.springframework.transaction.annotation.Transactional;
@Service @Service
public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatformService { public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatformService {
private final static Logger logger = LoggerFactory.getLogger(LoanWritePlatformServiceJpaRepositoryImpl.class); private final static Logger LOG = LoggerFactory.getLogger(LoanWritePlatformServiceJpaRepositoryImpl.class);
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final LoanEventApiJsonValidator loanEventApiJsonValidator; private final LoanEventApiJsonValidator loanEventApiJsonValidator;
@@ -621,9 +621,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
final LocalDate nextPossibleRepaymentDate = null; final LocalDate nextPossibleRepaymentDate = null;
final Date rescheduledRepaymentDate = null; final Date rescheduledRepaymentDate = null;
for (int i = 0; i < disbursalCommand.length; i++) { for (final SingleDisbursalCommand singleLoanDisbursalCommand : disbursalCommand) {
final SingleDisbursalCommand singleLoanDisbursalCommand = disbursalCommand[i];
final Loan loan = this.loanAssembler.assembleFrom(singleLoanDisbursalCommand.getLoanId()); final Loan loan = this.loanAssembler.assembleFrom(singleLoanDisbursalCommand.getLoanId());
final LocalDate actualDisbursementDate = command.localDateValueOfParameterNamed("actualDisbursementDate"); final LocalDate actualDisbursementDate = command.localDateValueOfParameterNamed("actualDisbursementDate");
@@ -1008,14 +1006,14 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
} }
this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_ADJUST_TRANSACTION, entityMap); this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_ADJUST_TRANSACTION, entityMap);
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(transactionId) // .withEntityId(transactionId)
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()) // .withGroupId(loan.getGroupId())
.withLoanId(loanId) // .withLoanId(loanId)
.with(changes) // .with(changes)
.build(); .build();
} }
@@ -1095,14 +1093,14 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
this.loanAccountDomainService.recalculateAccruals(loan); this.loanAccountDomainService.recalculateAccruals(loan);
this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_WAIVE_INTEREST, this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_WAIVE_INTEREST,
constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, waiveInterestTransaction)); constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, waiveInterestTransaction));
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(waiveInterestTransaction.getId()) // .withEntityId(waiveInterestTransaction.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()) // .withGroupId(loan.getGroupId())
.withLoanId(loanId) // .withLoanId(loanId)
.with(changes) // .with(changes)
.build(); .build();
} }
@@ -1165,14 +1163,14 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
this.loanAccountDomainService.recalculateAccruals(loan); this.loanAccountDomainService.recalculateAccruals(loan);
this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_WRITTEN_OFF, this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_WRITTEN_OFF,
constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, writeoff)); constructEntityMap(BUSINESS_ENTITY.LOAN_TRANSACTION, writeoff));
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(writeoff.getId()) // .withEntityId(writeoff.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()) // .withGroupId(loan.getGroupId())
.withLoanId(loanId) // .withLoanId(loanId)
.with(changes) // .with(changes)
.build(); .build();
} }
@@ -1237,24 +1235,24 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
CommandProcessingResult result = null; CommandProcessingResult result = null;
if (possibleClosingTransaction != null) { if (possibleClosingTransaction != null) {
result = new CommandProcessingResultBuilder() // result = new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(possibleClosingTransaction.getId()) // .withEntityId(possibleClosingTransaction.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()) // .withGroupId(loan.getGroupId())
.withLoanId(loanId) // .withLoanId(loanId)
.with(changes) // .with(changes)
.build(); .build();
} else { } else {
result = new CommandProcessingResultBuilder() // result = new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(loanId) // .withEntityId(loanId)
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()) // .withGroupId(loan.getGroupId())
.withLoanId(loanId) // .withLoanId(loanId)
.with(changes) // .with(changes)
.build(); .build();
} }
@@ -1332,14 +1330,8 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
final Charge chargeDefinition = this.chargeRepository.findOneWithNotFoundDetection(chargeDefinitionId); final Charge chargeDefinition = this.chargeRepository.findOneWithNotFoundDetection(chargeDefinitionId);
if (loan.isDisbursed() && chargeDefinition.isDisbursementCharge()) { if (loan.isDisbursed() && chargeDefinition.isDisbursementCharge()) {
validateAddingNewChargeAllowed(loanDisburseDetails); // validates // validates whether any pending disbursements are available to apply this charge
// whether any validateAddingNewChargeAllowed(loanDisburseDetails);
// pending
// disbursements
// are
// available to
// apply this
// charge
} }
final List<Long> existingTransactionIds = new ArrayList<>(loan.findExistingTransactionIds()); final List<Long> existingTransactionIds = new ArrayList<>(loan.findExistingTransactionIds());
final List<Long> existingReversedTransactionIds = new ArrayList<>(loan.findExistingReversedTransactionIds()); final List<Long> existingReversedTransactionIds = new ArrayList<>(loan.findExistingReversedTransactionIds());
@@ -1410,13 +1402,13 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
} }
this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_ADD_CHARGE, this.businessEventNotifierService.notifyBusinessEventWasExecuted(BUSINESS_EVENTS.LOAN_ADD_CHARGE,
constructEntityMap(BUSINESS_ENTITY.LOAN_CHARGE, loanCharge)); constructEntityMap(BUSINESS_ENTITY.LOAN_CHARGE, loanCharge));
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) // .withCommandId(command.commandId())
.withEntityId(loanCharge.getId()) // .withEntityId(loanCharge.getId())
.withOfficeId(loan.getOfficeId()) // .withOfficeId(loan.getOfficeId())
.withClientId(loan.getClientId()) // .withClientId(loan.getClientId())
.withGroupId(loan.getGroupId()) // .withGroupId(loan.getGroupId())
.withLoanId(loanId) // .withLoanId(loanId)
.build(); .build();
} }
@@ -1782,7 +1774,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
final Collection<LoanChargeData> chargeDatas = this.loanChargeReadPlatformService.retrieveLoanChargesForFeePayment( final Collection<LoanChargeData> chargeDatas = this.loanChargeReadPlatformService.retrieveLoanChargesForFeePayment(
ChargePaymentMode.ACCOUNT_TRANSFER.getValue(), LoanStatus.ACTIVE.getValue()); ChargePaymentMode.ACCOUNT_TRANSFER.getValue(), LoanStatus.ACTIVE.getValue());
final boolean isRegularTransaction = true; final boolean isRegularTransaction = true;
final StringBuilder sb = new StringBuilder(); int errors = 0;
if (chargeDatas != null) { if (chargeDatas != null) {
for (final LoanChargeData chargeData : chargeDatas) { for (final LoanChargeData chargeData : chargeDatas) {
if (chargeData.isInstallmentFee()) { if (chargeData.isInstallmentFee()) {
@@ -1803,7 +1795,9 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
null, LoanTransactionType.CHARGE_PAYMENT.getValue(), chargeData.getId(), null, LoanTransactionType.CHARGE_PAYMENT.getValue(), chargeData.getId(),
installmentChargeData.getInstallmentNumber(), AccountTransferType.CHARGE_PAYMENT.getValue(), null, installmentChargeData.getInstallmentNumber(), AccountTransferType.CHARGE_PAYMENT.getValue(), null,
null, null, null, null, fromSavingsAccount, isRegularTransaction, isExceptionForBalanceCheck); null, null, null, null, fromSavingsAccount, isRegularTransaction, isExceptionForBalanceCheck);
transferFeeCharge(sb, accountTransferDTO); if (!transferFeeCharge(accountTransferDTO)) {
++errors;
}
} }
} }
} else if (chargeData.getDueDate() != null && !chargeData.getDueDate().isAfter(DateUtils.getLocalDateOfTenant())) { } else if (chargeData.getDueDate() != null && !chargeData.getDueDate().isAfter(DateUtils.getLocalDateOfTenant())) {
@@ -1817,27 +1811,25 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
LoanTransactionType.CHARGE_PAYMENT.getValue(), chargeData.getId(), null, LoanTransactionType.CHARGE_PAYMENT.getValue(), chargeData.getId(), null,
AccountTransferType.CHARGE_PAYMENT.getValue(), null, null, null, null, null, fromSavingsAccount, AccountTransferType.CHARGE_PAYMENT.getValue(), null, null, null, null, null, fromSavingsAccount,
isRegularTransaction, isExceptionForBalanceCheck); isRegularTransaction, isExceptionForBalanceCheck);
transferFeeCharge(sb, accountTransferDTO); if (!transferFeeCharge(accountTransferDTO)) {
++errors;
} }
} }
} }
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } }
if (errors > 0) { throw new JobExecutionException(errors); }
} }
/** private boolean transferFeeCharge(final AccountTransferDTO accountTransferDTO) {
* @param sb
* @param accountTransferDTO
*/
private void transferFeeCharge(final StringBuilder sb, final AccountTransferDTO accountTransferDTO) {
try { try {
this.accountTransfersWritePlatformService.transferFunds(accountTransferDTO); this.accountTransfersWritePlatformService.transferFunds(accountTransferDTO);
return true;
} catch (final PlatformApiDataValidationException e) { } catch (final PlatformApiDataValidationException e) {
sb.append("Validation exception while paying charge ").append(accountTransferDTO.getChargeId()).append(" for loan id:") LOG.error("Validation exception while paying charge {} for loan id {}", accountTransferDTO.getChargeId(), accountTransferDTO.getToAccountId(), e);
.append(accountTransferDTO.getToAccountId()).append("--------"); return false;
} catch (final InsufficientAccountBalanceException e) { } catch (final InsufficientAccountBalanceException e) {
sb.append("InsufficientAccountBalance Exception while paying charge ").append(accountTransferDTO.getChargeId()) LOG.error("InsufficientAccountBalanceException while paying charge {} for loan id {}", accountTransferDTO.getChargeId(), accountTransferDTO.getToAccountId(), e);
.append("for loan id:").append(accountTransferDTO.getToAccountId()).append("--------"); return false;
} }
} }
@@ -2883,7 +2875,9 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
BigDecimal overpaid = this.loanReadPlatformService.retrieveTotalPaidInAdvance(loanId).getPaidInAdvance(); BigDecimal overpaid = this.loanReadPlatformService.retrieveTotalPaidInAdvance(loanId).getPaidInAdvance();
if (overpaid == null || overpaid.equals(new BigDecimal(0)) || transactionAmount.floatValue() > overpaid.floatValue()) { if (overpaid == null || overpaid.equals(new BigDecimal(0)) || transactionAmount.floatValue() > overpaid.floatValue()) {
if (overpaid == null) overpaid = BigDecimal.ZERO; if (overpaid == null) {
overpaid = BigDecimal.ZERO;
}
throw new InvalidPaidInAdvanceAmountException(overpaid.toPlainString()); throw new InvalidPaidInAdvanceAmountException(overpaid.toPlainString());
} }
} }
@@ -20,6 +20,7 @@ package org.apache.fineract.portfolio.loanaccount.service;
import java.util.Collection; import java.util.Collection;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Callable;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil; import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException; import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -29,41 +30,25 @@ import org.springframework.dao.CannotAcquireLockException;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@Scope("prototype") @Scope("prototype")
public class RecalculateInterestPoster implements Callable<Void> {
public class RecalculateInterestPoster implements Runnable { private final static Logger logger = LoggerFactory.getLogger(RecalculateInterestPoster.class);
private final static Logger logger = LoggerFactory.getLogger(" recalculate interest poster");
private Collection<Long> loanIds; private Collection<Long> loanIds;
private LoanWritePlatformService loanWritePlatformService; private LoanWritePlatformService loanWritePlatformService;
public void setLoanIds(final Collection<Long> loanIds) { public void setLoanIds(final Collection<Long> loanIds) {
this.loanIds = loanIds; this.loanIds = loanIds;
} }
public void setLoanWritePlatformService(final LoanWritePlatformService loanWritePlatformService) { public void setLoanWritePlatformService(final LoanWritePlatformService loanWritePlatformService) {
this.loanWritePlatformService = loanWritePlatformService; this.loanWritePlatformService = loanWritePlatformService;
} }
@Override @Override
public void run() { public Void call() throws JobExecutionException {
Integer maxNumberOfRetries = ThreadLocalContextUtil.getTenant() Integer maxNumberOfRetries = ThreadLocalContextUtil.getTenant()
.getConnection().getMaxRetriesOnDeadlock(); .getConnection().getMaxRetriesOnDeadlock();
Integer maxIntervalBetweenRetries = ThreadLocalContextUtil.getTenant() Integer maxIntervalBetweenRetries = ThreadLocalContextUtil.getTenant()
@@ -71,72 +56,45 @@ public class RecalculateInterestPoster implements Runnable {
int i = 0; int i = 0;
if (!loanIds.isEmpty()) { if (!loanIds.isEmpty()) {
final StringBuilder sb = new StringBuilder(); int errors = 0;
for (Long loanId : loanIds) { for (Long loanId : loanIds) {
logger.info("Loan ID " + loanId); logger.info("Loan ID " + loanId);
Integer numberOfRetries = 0; Integer numberOfRetries = 0;
while (numberOfRetries <= maxNumberOfRetries) { while (numberOfRetries <= maxNumberOfRetries) {
try { try {
this.loanWritePlatformService this.loanWritePlatformService.recalculateInterest(loanId);
.recalculateInterest(loanId);
numberOfRetries = maxNumberOfRetries + 1; numberOfRetries = maxNumberOfRetries + 1;
} catch (CannotAcquireLockException } catch (CannotAcquireLockException
| ObjectOptimisticLockingFailureException exception) { | ObjectOptimisticLockingFailureException exception) {
logger.info("Recalulate interest job has been retried " logger.info("Recalulate interest job has been retried time(s)", numberOfRetries);
+ numberOfRetries + " time(s)"); // Fail if the transaction has been retired for maxNumberOfRetries
/***
* Fail if the transaction has been retired for
* maxNumberOfRetries
**/
if (numberOfRetries >= maxNumberOfRetries) { if (numberOfRetries >= maxNumberOfRetries) {
logger.warn("Recalulate interest job has been retried for the max allowed attempts of " logger.error("Recalulate interest job has been retried for the max allowed attempts of {} and will be rolled back", numberOfRetries);
+ numberOfRetries ++errors;
+ " and will be rolled back");
sb.append("Recalulate interest job has been retried for the max allowed attempts of "
+ numberOfRetries
+ " and will be rolled back");
break; break;
} }
/*** // Else sleep for a random time (between 1 to 10 seconds) and continue
* Else sleep for a random time (between 1 to 10
* seconds) and continue
**/
try { try {
Random random = new Random(); Random random = new Random();
int randomNum = random int randomNum = random.nextInt(maxIntervalBetweenRetries + 1);
.nextInt(maxIntervalBetweenRetries + 1);
Thread.sleep(1000 + (randomNum * 1000)); Thread.sleep(1000 + (randomNum * 1000));
numberOfRetries = numberOfRetries + 1; numberOfRetries = numberOfRetries + 1;
} catch (InterruptedException e) { } catch (InterruptedException e) {
sb.append("Interest recalculation for loans failed " + exception.getMessage()) ; logger.error("Interest recalculation for loans retry failed due to InterruptedException", e) ;
++errors;
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; logger.error("Interest recalculation for loans failed for account {}", loanId, e);
if (e.getCause() != null) {
realCause = e.getCause();
}
logger.error("Interest recalculation for loans failed for account:" + loanId + " with message " + realCause.getMessage(), e);
sb.append("Interest recalculation for loans failed for account:").append(loanId).append(" with message ")
.append(realCause.getMessage());
numberOfRetries = maxNumberOfRetries + 1; numberOfRetries = maxNumberOfRetries + 1;
++errors;
} }
i++; i++;
} }
logger.info("Loans count " + i); logger.info("Loans count " + i);
} }
if (sb.length() > 0) { if (errors > 0) { throw new JobExecutionException(errors); }
try { }
throw new JobExecutionException(sb.toString()); return null;
} catch (JobExecutionException e) {
logger.info("JobExecutionException occured :", e);
} }
}
}
}
} }
@@ -112,6 +112,8 @@ import org.apache.fineract.useradministration.domain.AppUser;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -119,6 +121,8 @@ import org.springframework.transaction.annotation.Transactional;
@Service @Service
public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements DepositAccountWritePlatformService { public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements DepositAccountWritePlatformService {
private final static Logger LOG = LoggerFactory.getLogger(DepositAccountWritePlatformServiceJpaRepositoryImpl.class);
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final SavingsAccountRepositoryWrapper savingAccountRepositoryWrapper; private final SavingsAccountRepositoryWrapper savingAccountRepositoryWrapper;
private final SavingsAccountTransactionRepository savingsAccountTransactionRepository; private final SavingsAccountTransactionRepository savingsAccountTransactionRepository;
@@ -571,21 +575,20 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo
@Override @Override
@CronTarget(jobName = JobName.TRANSFER_INTEREST_TO_SAVINGS) @CronTarget(jobName = JobName.TRANSFER_INTEREST_TO_SAVINGS)
public void transferInterestToSavings() throws JobExecutionException { public void transferInterestToSavings() throws JobExecutionException {
int errors = 0;
Collection<AccountTransferDTO> accountTrasferData = this.depositAccountReadPlatformService.retrieveDataForInterestTransfer(); Collection<AccountTransferDTO> accountTrasferData = this.depositAccountReadPlatformService.retrieveDataForInterestTransfer();
StringBuilder sb = new StringBuilder(200);
for (AccountTransferDTO accountTransferDTO : accountTrasferData) { for (AccountTransferDTO accountTransferDTO : accountTrasferData) {
try { try {
this.accountTransfersWritePlatformService.transferFunds(accountTransferDTO); this.accountTransfersWritePlatformService.transferFunds(accountTransferDTO);
} catch (final PlatformApiDataValidationException e) { } catch (final PlatformApiDataValidationException e) {
sb.append("Validation exception while trasfering Interest form ").append(accountTransferDTO.getFromAccountId()) LOG.error("Validation exception while trasfering Interest from {} to {}", accountTransferDTO.getFromAccountId(), accountTransferDTO.getToAccountId(), e);
.append(" to ").append(accountTransferDTO.getToAccountId()).append("--------"); ++errors;
} catch (final InsufficientAccountBalanceException e) { } catch (final InsufficientAccountBalanceException e) {
sb.append("InsufficientAccountBalance Exception while trasfering Interest form ") LOG.error("InsufficientAccountBalanceException while trasfering Interest from {} to {} ", accountTransferDTO.getFromAccountId(), accountTransferDTO.getToAccountId(), e);
.append(accountTransferDTO.getFromAccountId()).append(" to ").append(accountTransferDTO.getToAccountId()) ++errors;
.append("--------");
} }
} }
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } if (errors > 0) { throw new JobExecutionException(errors); }
} }
@Override @Override
@@ -689,7 +692,7 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo
.findOneByIdAndSavingsAccountId(transactionId, savingsId); .findOneByIdAndSavingsAccountId(transactionId, savingsId);
if (savingsAccountTransaction == null) { throw new SavingsAccountTransactionNotFoundException(savingsId, transactionId); } if (savingsAccountTransaction == null) { throw new SavingsAccountTransactionNotFoundException(savingsId, transactionId); }
if (!(savingsAccountTransaction.isDeposit() || savingsAccountTransaction.isWithdrawal()) || savingsAccountTransaction.isReversed()) { throw new TransactionUpdateNotAllowedException( if ((!savingsAccountTransaction.isDeposit() && !savingsAccountTransaction.isWithdrawal()) || savingsAccountTransaction.isReversed()) { throw new TransactionUpdateNotAllowedException(
savingsId, transactionId); } savingsId, transactionId); }
if (this.accountTransfersReadPlatformService.isAccountTransfer(transactionId, PortfolioAccountType.SAVINGS)) { throw new PlatformServiceUnavailableException( if (this.accountTransfersReadPlatformService.isAccountTransfer(transactionId, PortfolioAccountType.SAVINGS)) { throw new PlatformServiceUnavailableException(
@@ -18,6 +18,8 @@
*/ */
package org.apache.fineract.portfolio.savings.service; package org.apache.fineract.portfolio.savings.service;
import static org.apache.fineract.portfolio.savings.domain.SavingsAccountStatusType.ACTIVE;
import java.util.List; import java.util.List;
import org.apache.fineract.infrastructure.core.service.DateUtils; import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.jobs.annotation.CronTarget; import org.apache.fineract.infrastructure.jobs.annotation.CronTarget;
@@ -26,8 +28,9 @@ import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.portfolio.savings.domain.SavingsAccount; import org.apache.fineract.portfolio.savings.domain.SavingsAccount;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountAssembler; import org.apache.fineract.portfolio.savings.domain.SavingsAccountAssembler;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountRepositoryWrapper; import org.apache.fineract.portfolio.savings.domain.SavingsAccountRepositoryWrapper;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountStatusType;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@@ -36,6 +39,8 @@ import org.springframework.stereotype.Service;
@Service @Service
public class SavingsSchedularServiceImpl implements SavingsSchedularService { public class SavingsSchedularServiceImpl implements SavingsSchedularService {
private final static Logger LOG = LoggerFactory.getLogger(SavingsSchedularServiceImpl.class);
private final SavingsAccountAssembler savingAccountAssembler; private final SavingsAccountAssembler savingAccountAssembler;
private final SavingsAccountWritePlatformService savingsAccountWritePlatformService; private final SavingsAccountWritePlatformService savingsAccountWritePlatformService;
private final SavingsAccountReadPlatformService savingAccountReadPlatformService; private final SavingsAccountReadPlatformService savingAccountReadPlatformService;
@@ -51,63 +56,54 @@ public class SavingsSchedularServiceImpl implements SavingsSchedularService {
this.savingsAccountRepository = savingsAccountRepository; this.savingsAccountRepository = savingsAccountRepository;
} }
@CronTarget(jobName = JobName.POST_INTEREST_FOR_SAVINGS)
@Override @Override
@CronTarget(jobName = JobName.POST_INTEREST_FOR_SAVINGS)
public void postInterestForAccounts() throws JobExecutionException { public void postInterestForAccounts() throws JobExecutionException {
int page = 0; int page = 0;
Integer initialSize = 500; Integer initialSize = 500;
Integer totalPageSize = 0; Integer totalPageSize = 0;
StringBuffer sb = new StringBuffer(); int errors = 0;
do { do {
PageRequest pageRequest = PageRequest.of(page, initialSize); PageRequest pageRequest = PageRequest.of(page, initialSize);
Page<SavingsAccount> savingsAccounts = this.savingsAccountRepository.findByStatus(SavingsAccountStatusType.ACTIVE.getValue(), Page<SavingsAccount> savingsAccounts = this.savingsAccountRepository.findByStatus(ACTIVE.getValue(), pageRequest);
pageRequest);
for (SavingsAccount savingsAccount : savingsAccounts.getContent()) { for (SavingsAccount savingsAccount : savingsAccounts.getContent()) {
try { try {
this.savingAccountAssembler.assignSavingAccountHelpers(savingsAccount); this.savingAccountAssembler.assignSavingAccountHelpers(savingsAccount);
boolean postInterestAsOn = false; boolean postInterestAsOn = false;
LocalDate transactionDate = null; LocalDate transactionDate = null;
this.savingsAccountWritePlatformService.postInterest(savingsAccount, postInterestAsOn, this.savingsAccountWritePlatformService.postInterest(savingsAccount, postInterestAsOn, transactionDate);
transactionDate);
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; LOG.error("Failed to post interest for Savings with id {}", savingsAccount.getId(), e);
if (e.getCause() != null) { ++errors;
realCause = e.getCause();
}
sb.append("failed to post interest for Savings with id " + savingsAccount.getId() + " with message "
+ realCause.getMessage());
} }
} }
page++; page++;
totalPageSize = savingsAccounts.getTotalPages(); totalPageSize = savingsAccounts.getTotalPages();
} while (page < totalPageSize); } while (page < totalPageSize);
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } if (errors > 0) { throw new JobExecutionException(errors); }
} }
@CronTarget(jobName = JobName.UPDATE_SAVINGS_DORMANT_ACCOUNTS)
@Override @Override
@CronTarget(jobName = JobName.UPDATE_SAVINGS_DORMANT_ACCOUNTS)
public void updateSavingsDormancyStatus() throws JobExecutionException { public void updateSavingsDormancyStatus() throws JobExecutionException {
final LocalDate tenantLocalDate = DateUtils.getLocalDateOfTenant(); LocalDate tenantLocalDate = DateUtils.getLocalDateOfTenant();
final List<Long> savingsPendingInactive = this.savingAccountReadPlatformService List<Long> savingsPendingInactive = savingAccountReadPlatformService.retrieveSavingsIdsPendingInactive(tenantLocalDate);
.retrieveSavingsIdsPendingInactive(tenantLocalDate);
if(null != savingsPendingInactive && savingsPendingInactive.size() > 0){ if(null != savingsPendingInactive && savingsPendingInactive.size() > 0){
for(Long savingsId : savingsPendingInactive){ for(Long savingsId : savingsPendingInactive){
this.savingsAccountWritePlatformService.setSubStatusInactive(savingsId); this.savingsAccountWritePlatformService.setSubStatusInactive(savingsId);
} }
} }
final List<Long> savingsPendingDormant = this.savingAccountReadPlatformService List<Long> savingsPendingDormant = savingAccountReadPlatformService.retrieveSavingsIdsPendingDormant(tenantLocalDate);
.retrieveSavingsIdsPendingDormant(tenantLocalDate);
if(null != savingsPendingDormant && savingsPendingDormant.size() > 0){ if(null != savingsPendingDormant && savingsPendingDormant.size() > 0){
for(Long savingsId : savingsPendingDormant){ for(Long savingsId : savingsPendingDormant){
this.savingsAccountWritePlatformService.setSubStatusDormant(savingsId); this.savingsAccountWritePlatformService.setSubStatusDormant(savingsId);
} }
} }
final List<Long> savingsPendingEscheat = this.savingAccountReadPlatformService List<Long> savingsPendingEscheat = savingAccountReadPlatformService.retrieveSavingsIdsPendingEscheat(tenantLocalDate);
.retrieveSavingsIdsPendingEscheat(tenantLocalDate);
if(null != savingsPendingEscheat && savingsPendingEscheat.size() > 0){ if(null != savingsPendingEscheat && savingsPendingEscheat.size() > 0){
for(Long savingsId : savingsPendingEscheat){ for(Long savingsId : savingsPendingEscheat){
this.savingsAccountWritePlatformService.escheat(savingsId); this.savingsAccountWritePlatformService.escheat(savingsId);
@@ -62,6 +62,7 @@ import org.springframework.util.CollectionUtils;
public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService { public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService {
private final static Logger logger = LoggerFactory.getLogger(ScheduledJobRunnerServiceImpl.class); private final static Logger logger = LoggerFactory.getLogger(ScheduledJobRunnerServiceImpl.class);
private final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd"); private final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
private final DateTimeFormatter formatterWithTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); private final DateTimeFormatter formatterWithTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
@@ -238,31 +239,21 @@ public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService
@Override @Override
@CronTarget(jobName = JobName.PAY_DUE_SAVINGS_CHARGES) @CronTarget(jobName = JobName.PAY_DUE_SAVINGS_CHARGES)
public void applyDueChargesForSavings() throws JobExecutionException { public void applyDueChargesForSavings() throws JobExecutionException {
final Collection<SavingsAccountAnnualFeeData> chargesDueData = this.savingsAccountChargeReadPlatformService final Collection<SavingsAccountAnnualFeeData> chargesDueData = this.savingsAccountChargeReadPlatformService.retrieveChargesWithDue();
.retrieveChargesWithDue(); int numberOfErrors = 0;
final StringBuilder errorMsg = new StringBuilder();
for (final SavingsAccountAnnualFeeData savingsAccountReference : chargesDueData) { for (final SavingsAccountAnnualFeeData savingsAccountReference : chargesDueData) {
try { try {
this.savingsAccountWritePlatformService.applyChargeDue(savingsAccountReference.getId(), this.savingsAccountWritePlatformService.applyChargeDue(savingsAccountReference.getId(), savingsAccountReference.getAccountId());
savingsAccountReference.getAccountId());
} catch (final PlatformApiDataValidationException e) { } catch (final PlatformApiDataValidationException e) {
final List<ApiParameterError> errors = e.getErrors(); final List<ApiParameterError> errors = e.getErrors();
for (final ApiParameterError error : errors) { for (final ApiParameterError error : errors) {
logger.error("Apply Charges due for savings failed for account:" + savingsAccountReference.getAccountNo() logger.error("Apply Charges due for savings failed for account {} with message: {}", savingsAccountReference.getAccountNo(), error.getDeveloperMessage(), e);
+ " with message " + error.getDeveloperMessage()); ++numberOfErrors;
errorMsg.append("Apply Charges due for savings failed for account:").append(savingsAccountReference.getAccountNo())
.append(" with message ").append(error.getDeveloperMessage());
} }
} }
} }
logger.info("{}: Savings accounts affected by update: {}", ThreadLocalContextUtil.getTenant().getName(), chargesDueData.size());
logger.info(ThreadLocalContextUtil.getTenant().getName() + ": Savings accounts affected by update: " + chargesDueData.size()); if (numberOfErrors > 0) { throw new JobExecutionException(numberOfErrors); }
/*
* throw exception if any charge payment fails.
*/
if (errorMsg.length() > 0) { throw new JobExecutionException(errorMsg.toString()); }
} }
@Transactional @Transactional
@@ -383,12 +374,13 @@ public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService
@Override @Override
@CronTarget(jobName = JobName.POST_DIVIDENTS_FOR_SHARES) @CronTarget(jobName = JobName.POST_DIVIDENTS_FOR_SHARES)
public void postDividends() throws JobExecutionException { public void postDividends() throws JobExecutionException {
int numberOfErrors = 0;
List<Map<String, Object>> dividendDetails = this.shareAccountDividendReadPlatformService.retriveDividendDetailsForPostDividents(); List<Map<String, Object>> dividendDetails = this.shareAccountDividendReadPlatformService.retriveDividendDetailsForPostDividents();
StringBuilder errorMsg = new StringBuilder();
for (Map<String, Object> dividendMap : dividendDetails) { for (Map<String, Object> dividendMap : dividendDetails) {
Long id = null ; Long id = null ;
Long savingsId = null ; Long savingsId = null ;
if(dividendMap.get("id") instanceof BigInteger) { //Drizzle is returning BigInteger if (dividendMap.get("id") instanceof BigInteger) {
// Drizzle is returningBigInteger
id = ((BigInteger) dividendMap.get("id")).longValue(); id = ((BigInteger) dividendMap.get("id")).longValue();
savingsId = ((BigInteger) dividendMap.get("savingsAccountId")).longValue(); savingsId = ((BigInteger) dividendMap.get("savingsAccountId")).longValue();
} else { // MySQL connector is returning Long } else { // MySQL connector is returning Long
@@ -400,22 +392,19 @@ public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService
} catch (final PlatformApiDataValidationException e) { } catch (final PlatformApiDataValidationException e) {
final List<ApiParameterError> errors = e.getErrors(); final List<ApiParameterError> errors = e.getErrors();
for (final ApiParameterError error : errors) { for (final ApiParameterError error : errors) {
logger.error("Post Dividends to savings failed for Divident detail Id:" + id + " and savings Id: " + savingsId logger.error("Post Dividends to savings failed due to ApiParameterError for Divident detail Id: {} and savings Id: {} with message: ", id, savingsId, error.getDeveloperMessage(), e);
+ " with message " + error.getDeveloperMessage()); ++numberOfErrors;
errorMsg.append("Post Dividends to savings failed for Divident detail Id:").append(id).append(" and savings Id:")
.append(savingsId).append(" with message ").append(error.getDeveloperMessage());
} }
} catch (final Exception e) { } catch (final Exception e) {
logger.error("Post Dividends to savings failed for Divident detail Id:" + id + " and savings Id: " + savingsId logger.error("Post Dividends to savings failed for Divident detail Id: {} and savings Id: {}", id, savingsId, e);
+ " with message " + e.getLocalizedMessage()); ++numberOfErrors;
errorMsg.append("Post Dividends to savings failed for Divident detail Id:").append(id).append(" and savings Id:")
.append(savingsId).append(" with message ").append(e.getLocalizedMessage());
} }
} }
if (errorMsg.length() > 0) { throw new JobExecutionException(errorMsg.toString()); } if (numberOfErrors > 0) { throw new JobExecutionException(numberOfErrors); }
} }
@Override
@CronTarget(jobName = JobName.UPDATE_TRAIL_BALANCE_DETAILS) @CronTarget(jobName = JobName.UPDATE_TRAIL_BALANCE_DETAILS)
public void updateTrialBalanceDetails() throws JobExecutionException { public void updateTrialBalanceDetails() throws JobExecutionException {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSourceServiceFactory.determineDataSourceService().retrieveDataSource()); final JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSourceServiceFactory.determineDataSourceService().retrieveDataSource());
@@ -429,8 +418,9 @@ public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService
for(Date tbGap : tbGaps) { for(Date tbGap : tbGaps) {
LocalDate convDate = new DateTime(tbGap).toLocalDate(); LocalDate convDate = new DateTime(tbGap).toLocalDate();
int days = Days.daysBetween(convDate, DateUtils.getLocalDateOfTenant()).getDays(); int days = Days.daysBetween(convDate, DateUtils.getLocalDateOfTenant()).getDays();
if(days < 1) if(days < 1) {
continue; continue;
}
final String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(tbGap); final String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(tbGap);
final StringBuilder sqlBuilder = new StringBuilder(600); final StringBuilder sqlBuilder = new StringBuilder(600);
sqlBuilder.append("Insert Into m_trial_balance(office_id, account_id, Amount, entry_date, created_date,closing_balance) ") sqlBuilder.append("Insert Into m_trial_balance(office_id, account_id, Amount, entry_date, created_date,closing_balance) ")
@@ -439,9 +429,7 @@ public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService
.append("from acc_gl_journal_entry je WHERE je.transaction_date = ? ") .append("from acc_gl_journal_entry je WHERE je.transaction_date = ? ")
.append("group by je.account_id, je.office_id, je.transaction_date, Date(je.entry_date)"); .append("group by je.account_id, je.office_id, je.transaction_date, Date(je.entry_date)");
final int result = jdbcTemplate.update(sqlBuilder.toString(), new Object[] { final int result = jdbcTemplate.update(sqlBuilder.toString(), formattedDate);
formattedDate
});
logger.info(ThreadLocalContextUtil.getTenant().getName() + ": Results affected by update: " + result); logger.info(ThreadLocalContextUtil.getTenant().getName() + ": Results affected by update: " + result);
} }
@@ -459,8 +447,9 @@ public class ScheduledJobRunnerServiceImpl implements ScheduledJobRunnerService
List<BigDecimal> closingBalanceData = jdbcTemplate.queryForList(closingBalanceQuery, new Object[] {officeId, accountId}, BigDecimal.class); List<BigDecimal> closingBalanceData = jdbcTemplate.queryForList(closingBalanceQuery, new Object[] {officeId, accountId}, BigDecimal.class);
List<TrialBalance> tbRows = this.trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, accountId); List<TrialBalance> tbRows = this.trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, accountId);
BigDecimal closingBalance = null; BigDecimal closingBalance = null;
if(!CollectionUtils.isEmpty(closingBalanceData)) if(!CollectionUtils.isEmpty(closingBalanceData)) {
closingBalance = closingBalanceData.get(0); closingBalance = closingBalanceData.get(0);
}
if(CollectionUtils.isEmpty(closingBalanceData)) { if(CollectionUtils.isEmpty(closingBalanceData)) {
closingBalance = BigDecimal.ZERO; closingBalance = BigDecimal.ZERO;
for(TrialBalance row : tbRows) { for(TrialBalance row : tbRows) {