FINERACT-1483: Optional value should accessed and throw exception if null

This commit is contained in:
percyashu
2022-06-16 09:20:18 +02:00
committed by Arnold Gálovics
parent 68fcddf7a7
commit 645994f870
18 changed files with 56 additions and 60 deletions
@@ -777,11 +777,11 @@ public class AccountingProcessorHelper {
}
public LoanTransaction getLoanTransactionById(final long loanTransactionId) {
return this.loanTransactionRepository.findById(loanTransactionId).get();
return this.loanTransactionRepository.findById(loanTransactionId).orElseThrow();
}
public SavingsAccountTransaction getSavingsTransactionById(final long savingsTransactionId) {
return this.savingsAccountTransactionRepository.findById(savingsTransactionId).get();
return this.savingsAccountTransactionRepository.findById(savingsTransactionId).orElseThrow();
}
private void createCreditJournalEntryOrReversalForLoan(final Office office, final String currencyCode, final int accountMappingTypeId,
@@ -833,7 +833,7 @@ public class AccountingProcessorHelper {
String modifiedTransactionId = transactionId;
if (StringUtils.isNumeric(transactionId)) {
long id = Long.parseLong(transactionId);
savingsAccountTransaction = this.savingsAccountTransactionRepository.findById(id).get();
savingsAccountTransaction = this.savingsAccountTransactionRepository.findById(id).orElseThrow();
modifiedTransactionId = SAVINGS_TRANSACTION_IDENTIFIER + transactionId;
}
final JournalEntry journalEntry = JournalEntry.createNew(office, paymentDetail, account, currencyCode, modifiedTransactionId,
@@ -854,7 +854,7 @@ public class AccountingProcessorHelper {
String modifiedTransactionId = transactionId;
if (StringUtils.isNumeric(transactionId)) {
long id = Long.parseLong(transactionId);
loanTransaction = this.loanTransactionRepository.findById(id).get();
loanTransaction = this.loanTransactionRepository.findById(id).orElseThrow();
modifiedTransactionId = LOAN_TRANSACTION_IDENTIFIER + transactionId;
}
final JournalEntry journalEntry = JournalEntry.createNew(office, paymentDetail, account, currencyCode, modifiedTransactionId,
@@ -904,7 +904,7 @@ public class AccountingProcessorHelper {
String modifiedTransactionId = transactionId;
if (StringUtils.isNumeric(transactionId)) {
long id = Long.parseLong(transactionId);
loanTransaction = this.loanTransactionRepository.findById(id).get();
loanTransaction = this.loanTransactionRepository.findById(id).orElseThrow();
modifiedTransactionId = LOAN_TRANSACTION_IDENTIFIER + transactionId;
}
final JournalEntry journalEntry = JournalEntry.createNew(office, paymentDetail, account, currencyCode, modifiedTransactionId,
@@ -924,7 +924,7 @@ public class AccountingProcessorHelper {
String modifiedTransactionId = transactionId;
if (StringUtils.isNumeric(transactionId)) {
long id = Long.parseLong(transactionId);
savingsAccountTransaction = this.savingsAccountTransactionRepository.findById(id).get();
savingsAccountTransaction = this.savingsAccountTransactionRepository.findById(id).orElseThrow();
modifiedTransactionId = SAVINGS_TRANSACTION_IDENTIFIER + transactionId;
}
final JournalEntry journalEntry = JournalEntry.createNew(office, paymentDetail, account, currencyCode, modifiedTransactionId,
@@ -210,11 +210,11 @@ public class ProvisioningEntriesWritePlatformServiceJpaRepositoryImpl implements
.retrieveLoanProductsProvisioningData(date);
Map<Integer, LoanProductProvisioningEntry> provisioningEntries = new HashMap<>();
for (LoanProductProvisioningEntryData data : entries) {
LoanProduct loanProduct = this.loanProductRepository.findById(data.getProductId()).get();
LoanProduct loanProduct = this.loanProductRepository.findById(data.getProductId()).orElseThrow();
Office office = this.officeRepositoryWrapper.findOneWithNotFoundDetection(data.getOfficeId());
ProvisioningCategory provisioningCategory = provisioningCategoryRepository.findById(data.getCategoryId()).get();
GLAccount liabilityAccount = glAccountRepository.findById(data.getLiablityAccount()).get();
GLAccount expenseAccount = glAccountRepository.findById(data.getExpenseAccount()).get();
ProvisioningCategory provisioningCategory = provisioningCategoryRepository.findById(data.getCategoryId()).orElse(null);
GLAccount liabilityAccount = glAccountRepository.findById(data.getLiablityAccount()).orElseThrow();
GLAccount expenseAccount = glAccountRepository.findById(data.getExpenseAccount()).orElseThrow();
MonetaryCurrency currency = loanProduct.getPrincipalAmount().getCurrency();
Money money = Money.of(currency, data.getBalance());
Money amountToReserve = money.percentageOf(data.getPercentage(), MoneyHelper.getRoundingMode());
@@ -117,15 +117,16 @@ public class ConfigurationDomainServiceJpa implements ConfigurationDomainService
@Override
public boolean isEhcacheEnabled() {
return this.cacheTypeRepository.findById(1L).get().isEhcacheEnabled();
return this.cacheTypeRepository.findById(1L).map(PlatformCache::isEhcacheEnabled).orElseThrow();
}
@Transactional
@Override
public void updateCache(final CacheType cacheType) {
final PlatformCache cache = this.cacheTypeRepository.findById(1L).get();
cache.update(cacheType);
this.cacheTypeRepository.save(cache);
this.cacheTypeRepository.findById(1L).ifPresent(cache -> {
cache.update(cacheType);
this.cacheTypeRepository.save(cache);
});
}
@Override
@@ -89,15 +89,9 @@ public class CreditReportWritePlatformServiceImpl implements CreditReportWritePl
try {
Long creditBureauID = command.longValueOfParameterNamed("creditBureauID");
Optional<String> creditBureauName = getCreditBureau(creditBureauID);
String creditBureauName = getCreditBureauName(creditBureauID);
if (creditBureauName.isEmpty()) {
baseDataValidator.reset().failWithCode("creditBureau.has.not.been.Integrated");
throw new PlatformApiDataValidationException("creditBureau.has.not.been.Integrated", "creditBureau.has.not.been.Integrated",
dataValidationErrors);
}
if (Objects.equals(creditBureauName.get(), CreditBureauConfigurations.THITSAWORKS.toString())) {
if (Objects.equals(creditBureauName, CreditBureauConfigurations.THITSAWORKS.toString())) {
CreditBureauReportData reportobj = this.thitsaWorksCreditBureauIntegrationWritePlatformService
.getCreditReportFromThitsaWorks(command);
@@ -128,10 +122,10 @@ public class CreditReportWritePlatformServiceImpl implements CreditReportWritePl
@Transactional
public String addCreditReport(Long bureauId, File creditReport, FormDataContentDisposition fileDetail) {
Optional<String> creditBureauName = getCreditBureau(bureauId);
String creditBureauName = getCreditBureauName(bureauId);
String responseMessage = null;
if (Objects.equals(creditBureauName.get(), CreditBureauConfigurations.THITSAWORKS.toString())) {
if (Objects.equals(creditBureauName, CreditBureauConfigurations.THITSAWORKS.toString())) {
responseMessage = this.thitsaWorksCreditBureauIntegrationWritePlatformService.addCreditReport(bureauId, creditReport,
fileDetail);
} else {
@@ -146,20 +140,19 @@ public class CreditReportWritePlatformServiceImpl implements CreditReportWritePl
}
private Optional<String> getCreditBureau(Long creditBureauID) {
private String getCreditBureauName(Long creditBureauID) {
if (creditBureauID != null) {
Optional<CreditBureau> creditBureau = this.creditBureauRepository.findById(creditBureauID);
if (creditBureau.isEmpty()) {
return Optional.empty();
if (!creditBureau.isEmpty()) {
return creditBureau.get().getName();
}
return Optional.of(creditBureau.get().getName());
}
return Optional.empty();
baseDataValidator.reset().failWithCode("creditBureau.has.not.been.Integrated");
throw new PlatformApiDataValidationException("creditBureau.has.not.been.Integrated", "creditBureau.has.not.been.Integrated",
dataValidationErrors);
}
// saving the fetched creditreport in database
@@ -170,10 +163,10 @@ public class CreditReportWritePlatformServiceImpl implements CreditReportWritePl
try {
this.context.authenticatedUser();
Optional<String> creditBureauName = getCreditBureau(creditBureauId);
String creditBureauName = getCreditBureauName(creditBureauId);
CreditReport creditReport = null;
if (Objects.equals(creditBureauName.get(), CreditBureauConfigurations.THITSAWORKS.toString())) {
if (Objects.equals(creditBureauName, CreditBureauConfigurations.THITSAWORKS.toString())) {
// checks whether creditReport for same nationalId was saved before. if yes, then deletes it & replaces
// with new one.
@@ -208,10 +201,10 @@ public class CreditReportWritePlatformServiceImpl implements CreditReportWritePl
this.context.authenticatedUser();
Optional<String> creditBureauName = getCreditBureau(creditBureauId);
String creditBureauName = getCreditBureauName(creditBureauId);
CreditReport creditReport = null;
if (Objects.equals(creditBureauName.get(), CreditBureauConfigurations.THITSAWORKS.toString())) {
if (Objects.equals(creditBureauName, CreditBureauConfigurations.THITSAWORKS.toString())) {
String nationalId = command.stringValueOfParameterNamed("nationalId");
@@ -52,7 +52,7 @@ public class ImageResizer {
@Override
public InputStream openStream() throws IOException {
return optResizedIS.get();
return optResizedIS.get(); // NOSONAR
}
}, fileData.name(), fileData.contentType());
return resizedImage;
@@ -52,7 +52,7 @@ public class NotificationEventListener {
if (notificationData.getOfficeId() != null) {
List<Long> tempUserIds = new ArrayList<>(userIds);
for (Long userId : tempUserIds) {
AppUser appUser = appUserRepository.findById(userId).get();
AppUser appUser = appUserRepository.findById(userId).orElseThrow();
if (!Objects.equals(appUser.getOffice().getId(), notificationData.getOfficeId())) {
userIds.remove(userId);
}
@@ -83,7 +83,7 @@ public class NotificationWritePlatformServiceImpl implements NotificationWritePl
private List<Long> insertIntoNotificationMapper(Collection<Long> userIds, Long generatedNotificationId) {
List<Long> mappedIds = new ArrayList<>();
for (Long userId : userIds) {
AppUser appUser = this.appUserRepository.findById(userId).get();
AppUser appUser = this.appUserRepository.findById(userId).orElseThrow();
NotificationMapper notificationMapper = new NotificationMapper(
this.notificationGeneratorReadRepositoryWrapper.findById(generatedNotificationId), appUser, false,
DateUtils.getDateOfTenant());
@@ -440,7 +440,7 @@ public class AccountTransfersWritePlatformServiceImpl implements AccountTransfer
// if the savings account is GSIM, update its parent as well
if (toSavingsAccount.getGsim() != null) {
GroupSavingsIndividualMonitoring gsim = gsimRepository.findById(toSavingsAccount.getGsim().getId()).get();
GroupSavingsIndividualMonitoring gsim = gsimRepository.findById(toSavingsAccount.getGsim().getId()).orElseThrow();
BigDecimal currentBalance = gsim.getParentDeposit();
BigDecimal newBalance = currentBalance.add(accountTransferDTO.getTransactionAmount());
gsim.setParentDeposit(newBalance);
@@ -183,7 +183,7 @@ public class StandingInstructionWritePlatformServiceImpl implements StandingInst
@Override
public CommandProcessingResult delete(final Long id) {
AccountTransferStandingInstruction standingInstructionsForUpdate = this.standingInstructionRepository.findById(id).get();
AccountTransferStandingInstruction standingInstructionsForUpdate = this.standingInstructionRepository.findById(id).orElseThrow();
// update the "deleted" and "name" properties of the standing
// instruction
standingInstructionsForUpdate.delete();
@@ -76,7 +76,7 @@ public class LoanScheduleHistoryWritePlatformServiceImpl implements LoanSchedule
Date createdOnDate = null;
if (repaymentScheduleInstallment.getCreatedDate().isPresent()) {
createdOnDate = Date.from(repaymentScheduleInstallment.getCreatedDate().get());
createdOnDate = Date.from(repaymentScheduleInstallment.getCreatedDate().get()); // NOSONAR
}
final AppUser createdByUser = repaymentScheduleInstallment.getCreatedBy().orElse(null);
@@ -85,7 +85,7 @@ public class LoanScheduleHistoryWritePlatformServiceImpl implements LoanSchedule
Date lastModifiedOnDate = null;
if (repaymentScheduleInstallment.getLastModifiedDate().isPresent()) {
lastModifiedOnDate = Date.from(repaymentScheduleInstallment.getLastModifiedDate().get());
lastModifiedOnDate = Date.from(repaymentScheduleInstallment.getLastModifiedDate().get()); // NOSONAR
}
LoanRepaymentScheduleHistory loanRepaymentScheduleHistory = LoanRepaymentScheduleHistory.instance(loan, loanRescheduleRequest,
@@ -1338,7 +1338,7 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
public CommandProcessingResult approveGLIMLoanAppication(final Long loanId, final JsonCommand command) {
final Long parentLoanId = loanId;
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).get();
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).orElseThrow();
JsonArray approvalFormData = command.arrayOfParameterNamed("approvalFormData");
JsonObject jsonObject = null;
@@ -1498,7 +1498,7 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
// GroupLoanIndividualMonitoringAccount
// glimAccount=glimRepository.findOne(loanId);
final Long parentLoanId = loanId;
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).get();
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).orElseThrow();
List<Loan> childLoans = this.loanRepository.findByGlimId(loanId);
CommandProcessingResult result = null;
@@ -1577,7 +1577,7 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
// GroupLoanIndividualMonitoringAccount
// glimAccount=glimRepository.findOne(loanId);
final Long parentLoanId = glimId;
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).get();
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).orElseThrow();
List<Loan> childLoans = this.loanRepository.findByGlimId(glimId);
CommandProcessingResult result = null;
@@ -341,7 +341,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
@Override
public CommandProcessingResult disburseGLIMLoan(final Long loanId, final JsonCommand command) {
final Long parentLoanId = loanId;
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).get();
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).orElseThrow();
List<Loan> childLoans = this.loanRepository.findByGlimId(loanId);
CommandProcessingResult result = null;
int count = 0;
@@ -848,7 +848,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
// GroupLoanIndividualMonitoringAccount
// glimAccount=glimRepository.findOne(loanId);
final Long parentLoanId = loanId;
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).get();
GroupLoanIndividualMonitoringAccount parentLoan = glimRepository.findById(parentLoanId).orElseThrow();
List<Loan> childLoans = this.loanRepository.findByGlimId(loanId);
CommandProcessingResult result = null;
int count = 0;
@@ -938,7 +938,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
final Long parentLoanId = loanId;
glimRepository.findById(parentLoanId).get();
glimRepository.findById(parentLoanId).orElseThrow();
JsonArray repayments = command.arrayOfParameterNamed("formDataArray");
JsonCommand childCommand = null;
@@ -164,7 +164,7 @@ public class MeetingWritePlatformServiceJpaRepositoryImpl implements MeetingWrit
/*
* If group is within a center then center entityType should be passed for retrieving CalendarInstance.
*/
final Group group = this.groupRepository.findById(entityId).get();
final Group group = this.groupRepository.findById(entityId).orElseThrow();
if (group.isCenter()) {
entityType = CalendarEntityType.CENTERS;
} else if (group.isChildGroup()) {
@@ -432,7 +432,7 @@ public class SavingsAccountAssembler {
}
accountType = AccountType.JLG;
}
final SavingsProduct product = this.savingProductRepository.findById(productId).get();
final SavingsProduct product = this.savingProductRepository.findById(productId).orElseThrow();
final Set<SavingsAccountCharge> charges = this.savingsAccountChargeAssembler.fromSavingsProduct(product);
final SavingsAccount account = SavingsAccount.createNewApplicationForSubmittal(client, group, product, null, null, null,
accountType, appliedonDate, appliedBy, product.nominalAnnualInterestRate(), product.interestCompoundingPeriodType(),
@@ -225,7 +225,7 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
public CommandProcessingResult gsimActivate(final Long gsimId, final JsonCommand command) {
Long parentSavingId = gsimId;
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).get();
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).orElseThrow();
List<SavingsAccount> childSavings = this.savingAccountRepositoryWrapper.findByGsimId(gsimId);
CommandProcessingResult result = null;
@@ -376,14 +376,15 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
LOG.debug("Deposit account has been created: {} ", deposit);
GroupSavingsIndividualMonitoring gsim = gsimRepository.findById(account.getGsim().getId()).get();
GroupSavingsIndividualMonitoring gsim = gsimRepository.findById(account.getGsim().getId()).orElseThrow();
LOG.info("parent deposit : {} ", gsim.getParentDeposit());
LOG.info("child account : {} ", savingsId);
BigDecimal currentBalance = gsim.getParentDeposit();
BigDecimal newBalance = currentBalance.add(transactionAmount);
gsim.setParentDeposit(newBalance);
gsimRepository.save(gsim);
LOG.info("balance after making deposit : {} ", gsimRepository.findById(account.getGsim().getId()).get().getParentDeposit());
LOG.info("balance after making deposit : {} ",
gsimRepository.findById(account.getGsim().getId()).orElseThrow().getParentDeposit());
}
@@ -448,7 +449,7 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
transactionAmount, paymentDetail, transactionBooleanValues, backdatedTxnsAllowedTill);
if (isGsim && (withdrawal.getId() != null)) {
GroupSavingsIndividualMonitoring gsim = gsimRepository.findById(account.getGsim().getId()).get();
GroupSavingsIndividualMonitoring gsim = gsimRepository.findById(account.getGsim().getId()).orElseThrow();
BigDecimal currentBalance = gsim.getParentDeposit().subtract(transactionAmount);
gsim.setParentDeposit(currentBalance);
gsimRepository.save(gsim);
@@ -939,7 +940,7 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
public CommandProcessingResult bulkGSIMClose(final Long gsimId, final JsonCommand command) {
final Long parentSavingId = gsimId;
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).get();
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).orElseThrow();
List<SavingsAccount> childSavings = this.savingAccountRepositoryWrapper.findByGsimId(gsimId);
CommandProcessingResult result = null;
@@ -488,7 +488,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
// GroupLoanIndividualMonitoringAccount
// glimAccount=glimRepository.findOne(loanId);
Long parentSavingId = gsimId;
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).get();
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).orElseThrow();
List<SavingsAccount> childSavings = this.savingAccountRepository.findByGsimId(gsimId);
CommandProcessingResult result = null;
int count = 0;
@@ -552,7 +552,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
@Override
public CommandProcessingResult undoGSIMApplicationApproval(final Long gsimId, final JsonCommand command) {
final Long parentSavingId = gsimId;
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).get();
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).orElseThrow();
List<SavingsAccount> childSavings = this.savingAccountRepository.findByGsimId(gsimId);
CommandProcessingResult result = null;
@@ -612,7 +612,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
public CommandProcessingResult rejectGSIMApplication(final Long gsimId, final JsonCommand command) {
final Long parentSavingId = gsimId;
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).get();
GroupSavingsIndividualMonitoring parentSavings = gsimRepository.findById(parentSavingId).orElseThrow();
List<SavingsAccount> childSavings = this.savingAccountRepository.findByGsimId(gsimId);
CommandProcessingResult result = null;
@@ -49,7 +49,8 @@ public class ShareAccountSchedularServiceImpl implements ShareAccountSchedularSe
@Transactional
public void postDividend(final Long dividendDetailId, final Long savingsId) {
ShareAccountDividendDetails shareAccountDividendDetails = this.shareAccountDividendRepository.findById(dividendDetailId).get();
ShareAccountDividendDetails shareAccountDividendDetails = this.shareAccountDividendRepository.findById(dividendDetailId)
.orElseThrow();
final SavingsAccount savingsAccount = this.savingsAccountAssembler.assembleFrom(savingsId, false);
SavingsAccountTransaction savingsAccountTransaction = this.savingsAccountDomainService.handleDividendPayout(savingsAccount,
DateUtils.getLocalDateOfTenant(), shareAccountDividendDetails.getAmount(), false);
@@ -457,7 +457,7 @@ public class TransferWritePlatformServiceJpaRepositoryImpl implements TransferWr
} else if (!destinationGroup.isActive()) {
throw new GroupNotActiveException(destinationGroup.getId());
}
transferClientBetweenGroups(client.getGroups().stream().findFirst().get(), client, destinationGroup, true, staff);
transferClientBetweenGroups(client.getGroups().iterator().next(), client, destinationGroup, true, staff);
} else if (client.getGroups().size() == 0 && destinationGroup != null) {
client.getGroups().add(destinationGroup);
client.updateStaff(destinationGroup.getStaff());