From 07052f983ed02501f61a8e93c003836589234a1e Mon Sep 17 00:00:00 2001 From: Vishwas Babu A J Date: Tue, 2 Dec 2014 17:33:19 -0800 Subject: [PATCH] cath database errors related to savings withdrawals --- ...SavingsAccountTransactionsApiResource.java | 40 ++++++++++++------- .../savings/domain/SavingsAccount.java | 8 ++-- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/api/SavingsAccountTransactionsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/api/SavingsAccountTransactionsApiResource.java index d88b6988f..f2cfae597 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/api/SavingsAccountTransactionsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/api/SavingsAccountTransactionsApiResource.java @@ -26,6 +26,7 @@ import org.mifosplatform.infrastructure.codes.data.CodeValueData; import org.mifosplatform.infrastructure.codes.service.CodeValueReadPlatformService; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException; import org.mifosplatform.infrastructure.core.exception.UnrecognizedQueryParamException; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; @@ -37,6 +38,8 @@ import org.mifosplatform.portfolio.savings.data.SavingsAccountTransactionData; import org.mifosplatform.portfolio.savings.service.SavingsAccountReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; +import org.springframework.dao.CannotAcquireLockException; +import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.stereotype.Component; @Path("/savingsaccounts/{savingsId}/transactions") @@ -119,24 +122,31 @@ public class SavingsAccountTransactionsApiResource { @Produces({ MediaType.APPLICATION_JSON }) public String transaction(@PathParam("savingsId") final Long savingsId, @QueryParam("command") final String commandParam, final String apiRequestBodyAsJson) { + try { + final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(apiRequestBodyAsJson); - final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(apiRequestBodyAsJson); + CommandProcessingResult result = null; + if (is(commandParam, "deposit")) { + final CommandWrapper commandRequest = builder.savingsAccountDeposit(savingsId).build(); + result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + } else if (is(commandParam, "withdrawal")) { + final CommandWrapper commandRequest = builder.savingsAccountWithdrawal(savingsId).build(); + result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + } - CommandProcessingResult result = null; - if (is(commandParam, "deposit")) { - final CommandWrapper commandRequest = builder.savingsAccountDeposit(savingsId).build(); - result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); - } else if (is(commandParam, "withdrawal")) { - final CommandWrapper commandRequest = builder.savingsAccountWithdrawal(savingsId).build(); - result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + if (result == null) { + // + throw new UnrecognizedQueryParamException("command", commandParam, new Object[] { "deposit", "withdrawal" }); + } + + return this.toApiJsonSerializer.serialize(result); + } catch (ObjectOptimisticLockingFailureException lockingFailureException) { + throw new PlatformDataIntegrityException("error.msg.savings.concurrent.operations", + "Concurrent Transactions being made on this savings account: " + lockingFailureException.getMessage()); + } catch (CannotAcquireLockException cannotAcquireLockException) { + throw new PlatformDataIntegrityException("error.msg.savings.concurrent.operations.unable.to.acquire.lock", + "Unable to acquir lock for this transaction: " + cannotAcquireLockException.getMessage()); } - - if (result == null) { - // - throw new UnrecognizedQueryParamException("command", commandParam, new Object[] { "deposit", "withdrawal" }); - } - - return this.toApiJsonSerializer.serialize(result); } @POST diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/domain/SavingsAccount.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/domain/SavingsAccount.java index 25b4105d7..c82a1aa66 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/domain/SavingsAccount.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/domain/SavingsAccount.java @@ -1149,16 +1149,16 @@ public class SavingsAccount extends AbstractPersistable { accountingBridgeData.put("accrualBasedAccountingEnabled", isAccrualBasedAccountingEnabledOnSavingsProduct()); accountingBridgeData.put("isAccountTransfer", isAccountTransfer); - final List> newLoanTransactions = new ArrayList<>(); + final List> newSavingsTransactions = new ArrayList<>(); for (final SavingsAccountTransaction transaction : this.transactions) { if (transaction.isReversed() && !existingReversedTransactionIds.contains(transaction.getId())) { - newLoanTransactions.add(transaction.toMapData(currencyData)); + newSavingsTransactions.add(transaction.toMapData(currencyData)); } else if (!existingTransactionIds.contains(transaction.getId())) { - newLoanTransactions.add(transaction.toMapData(currencyData)); + newSavingsTransactions.add(transaction.toMapData(currencyData)); } } - accountingBridgeData.put("newSavingsTransactions", newLoanTransactions); + accountingBridgeData.put("newSavingsTransactions", newSavingsTransactions); return accountingBridgeData; }