From 85297aaa65c35fc7db58e7063ed619e7da6f4f3b Mon Sep 17 00:00:00 2001 From: Pramod Nuthakki Date: Wed, 26 Feb 2014 20:09:03 +0530 Subject: [PATCH] MIFOSX-929 - added check for multiple permission for client --- .../service/CommandProcessingService.java | 3 ++ .../SynchronousCommandProcessingService.java | 12 +++++-- .../core/data/CommandProcessingResult.java | 18 +++++++++-- .../data/CommandProcessingResultBuilder.java | 8 ++++- ...WritePlatformServiceJpaRepositoryImpl.java | 32 +++++++++++++------ ...pplicationProcessWritePlatformService.java | 2 +- ...WritePlatformServiceJpaRepositoryImpl.java | 18 +++++++++-- 7 files changed, 73 insertions(+), 20 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandProcessingService.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandProcessingService.java index 4dea4de20..ef9cdffc7 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandProcessingService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandProcessingService.java @@ -9,11 +9,14 @@ import org.mifosplatform.commands.domain.CommandSource; import org.mifosplatform.commands.domain.CommandWrapper; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.useradministration.domain.AppUser; public interface CommandProcessingService { CommandProcessingResult processAndLogCommand(CommandWrapper wrapper, JsonCommand command, boolean isApprovedByChecker); CommandProcessingResult logCommand(CommandSource commandSourceResult); + + boolean validateCommand(final CommandWrapper commandWrapper,final AppUser user); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java index 4d4c20649..d0d310726 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java @@ -53,8 +53,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer public CommandProcessingResult processAndLogCommand(final CommandWrapper wrapper, final JsonCommand command, final boolean isApprovedByChecker) { - final boolean rollbackTransaction = this.configurationDomainService.isMakerCheckerEnabledForTask(wrapper.taskPermissionName()) - && !isApprovedByChecker; + final boolean rollbackTransaction = this.configurationDomainService.isMakerCheckerEnabledForTask(wrapper.taskPermissionName()); final NewCommandSourceHandler handler = findCommandHandler(wrapper); final CommandProcessingResult result = handler.processCommand(command); @@ -86,7 +85,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer this.commandSourceRepository.save(commandSourceResult); } - if (rollbackTransaction) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(commandSourceResult); } + if ((rollbackTransaction || result.isRollbackTransaction()) && !isApprovedByChecker) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(commandSourceResult); } return result; } @@ -627,4 +626,11 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer return handler; } + + @Override + public boolean validateCommand(final CommandWrapper commandWrapper, final AppUser user) { + boolean rollbackTransaction = this.configurationDomainService.isMakerCheckerEnabledForTask(commandWrapper.taskPermissionName()); + user.validateHasPermissionTo(commandWrapper.getTaskPermissionName()); + return rollbackTransaction; + } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResult.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResult.java index 0370c58df..460f395b2 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResult.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResult.java @@ -26,12 +26,13 @@ public class CommandProcessingResult { @SuppressWarnings("unused") private final String resourceIdentifier; private final Long productId; + private Boolean rollbackTransaction; public static CommandProcessingResult fromDetails(final Long commandId, final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String resourceIdentifier, final Long entityId, final String transactionId, - final Map changes, final Long productId) { + final Map changes, final Long productId,final Boolean rollbackTransaction) { return new CommandProcessingResult(commandId, officeId, groupId, clientId, loanId, savingsId, resourceIdentifier, entityId, - transactionId, changes, productId); + transactionId, changes, productId,rollbackTransaction); } public static CommandProcessingResult commandOnlyResult(final Long commandId) { @@ -85,7 +86,7 @@ public class CommandProcessingResult { private CommandProcessingResult(final Long commandId, final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String resourceIdentifier, final Long resourceId, final String transactionId, - final Map changesOnly, final Long productId) { + final Map changesOnly, final Long productId, Boolean rollbackTransaction) { this.commandId = commandId; this.officeId = officeId; this.groupId = groupId; @@ -97,6 +98,7 @@ public class CommandProcessingResult { this.changes = changesOnly; this.transactionId = transactionId; this.productId = productId; + this.rollbackTransaction = rollbackTransaction; } private CommandProcessingResult(final Long resourceId, final Long officeId, final Long commandId, final Map changesOnly) { @@ -169,4 +171,14 @@ public class CommandProcessingResult { public Long getProductId() { return this.productId; } + + + public boolean isRollbackTransaction() { + return this.rollbackTransaction!=null && this.rollbackTransaction; + } + + + public void setRollbackTransaction(boolean rollbackTransaction) { + this.rollbackTransaction = rollbackTransaction; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResultBuilder.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResultBuilder.java index 6fc9bfda4..aa7c6a7f5 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResultBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/CommandProcessingResultBuilder.java @@ -24,10 +24,11 @@ public class CommandProcessingResultBuilder { private String transactionId; private Map changes; private Long productId; + private boolean rollbackTransaction = false; public CommandProcessingResult build() { return CommandProcessingResult.fromDetails(this.commandId, this.officeId, this.groupId, this.clientId, this.loanId, this.savingsId, - this.resourceIdentifier, this.entityId, this.transactionId, this.changes, this.productId); + this.resourceIdentifier, this.entityId, this.transactionId, this.changes, this.productId,this.rollbackTransaction); } public CommandProcessingResultBuilder withCommandId(final Long withCommandId) { @@ -84,5 +85,10 @@ public class CommandProcessingResultBuilder { this.productId = productId; return this; } + + public CommandProcessingResultBuilder setRollbackTransaction(final boolean rollbackTransaction) { + this.rollbackTransaction = this.rollbackTransaction || rollbackTransaction; + return this; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java index d3a1b3306..7e171237f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java @@ -13,6 +13,9 @@ import java.util.Map; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; +import org.mifosplatform.commands.domain.CommandWrapper; +import org.mifosplatform.commands.service.CommandProcessingService; +import org.mifosplatform.commands.service.CommandWrapperBuilder; import org.mifosplatform.infrastructure.codes.domain.CodeValue; import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper; import org.mifosplatform.infrastructure.core.api.JsonCommand; @@ -77,6 +80,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP private final SavingsAccountRepository savingsRepository; private final SavingsProductRepository savingsProductRepository; private final SavingsApplicationProcessWritePlatformService savingsApplicationProcessWritePlatformService; + private final CommandProcessingService commandProcessingService; @Autowired public ClientWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, @@ -85,7 +89,8 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP final GroupRepository groupRepository, final StaffRepositoryWrapper staffRepository, final CodeValueRepositoryWrapper codeValueRepository, final LoanRepository loanRepository, final SavingsAccountRepository savingsRepository,final SavingsProductRepository savingsProductRepository, - final SavingsApplicationProcessWritePlatformService savingsApplicationProcessWritePlatformService) { + final SavingsApplicationProcessWritePlatformService savingsApplicationProcessWritePlatformService, + final CommandProcessingService commandProcessingService) { this.context = context; this.clientRepository = clientRepository; this.officeRepository = officeRepository; @@ -99,6 +104,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP this.savingsRepository = savingsRepository; this.savingsProductRepository = savingsProductRepository; this.savingsApplicationProcessWritePlatformService = savingsApplicationProcessWritePlatformService; + this.commandProcessingService = commandProcessingService; } @Transactional @@ -185,6 +191,11 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP } final Client newClient = Client.createNew(currentUser,clientOffice, clientParentGroup, staff, savingsProduct, command); + boolean rollbackTransaction = false; + if(newClient.isActive()){ + final CommandWrapper commandWrapper = new CommandWrapperBuilder().activateClient(null).build(); + rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, currentUser); + } this.clientRepository.save(newClient); @@ -197,7 +208,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP final Locale locale = command.extractLocale(); final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale); - Long savingsId = openOverdraftSavingsAccount(newClient, fmt); + CommandProcessingResult result = openOverdraftSavingsAccount(newClient, fmt); return new CommandProcessingResultBuilder() // .withCommandId(command.commandId()) // @@ -205,7 +216,9 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP .withClientId(newClient.getId()) // .withGroupId(groupId) // .withEntityId(newClient.getId()) // - .withSavingsId(savingsId)// + .withSavingsId(result.getSavingsId())// + .setRollbackTransaction(rollbackTransaction)// + .setRollbackTransaction(result.isRollbackTransaction())// .build(); } catch (final DataIntegrityViolationException dve) { handleDataIntegrityIssues(command, dve); @@ -284,7 +297,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP final AppUser currentUser = this.context.authenticatedUser(); client.activate(currentUser, fmt, activationDate); - Long savingsId = openOverdraftSavingsAccount(client, fmt); + CommandProcessingResult result = openOverdraftSavingsAccount(client, fmt); this.clientRepository.saveAndFlush(client); return new CommandProcessingResultBuilder() // @@ -292,7 +305,8 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP .withOfficeId(client.officeId()) // .withClientId(clientId) // .withEntityId(clientId) // - .withSavingsId(savingsId)// + .withSavingsId(result.getSavingsId())// + .setRollbackTransaction(result.isRollbackTransaction())// .build(); } catch (final DataIntegrityViolationException dve) { handleDataIntegrityIssues(command, dve); @@ -300,17 +314,17 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP } } - private Long openOverdraftSavingsAccount(final Client client, final DateTimeFormatter fmt) { - Long accountId = null; + private CommandProcessingResult openOverdraftSavingsAccount(final Client client, final DateTimeFormatter fmt) { + CommandProcessingResult commandProcessingResult = CommandProcessingResult.empty(); if(client.isActive() && client.SavingsProduct() != null){ if(!client.SavingsProduct().isAllowOverdraft()){ String defaultUserMessage = "Client's saving account must be a overdraft account"; throw new InvalidClientSavingProductException("saving.product", "must.be.ovrdraft.account", defaultUserMessage, client.SavingsProduct().getId(),client.getId()); } SavingsAccountDataDTO savingsAccountDataDTO = new SavingsAccountDataDTO(client, null, client.SavingsProduct(), client.getActivationLocalDate(), client.activatedBy(), fmt); - accountId = this.savingsApplicationProcessWritePlatformService.createActiveApplication(savingsAccountDataDTO); + commandProcessingResult = this.savingsApplicationProcessWritePlatformService.createActiveApplication(savingsAccountDataDTO); } - return accountId; + return commandProcessingResult; } private void logAsErrorUnexpectedDataIntegrityException(final DataIntegrityViolationException dve) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformService.java index 2f1b4d7d4..bf76b59f7 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformService.java @@ -25,5 +25,5 @@ public interface SavingsApplicationProcessWritePlatformService { CommandProcessingResult applicantWithdrawsFromApplication(Long savingsId, JsonCommand command); - Long createActiveApplication(SavingsAccountDataDTO savingsAccountDataDTO); + CommandProcessingResult createActiveApplication(SavingsAccountDataDTO savingsAccountDataDTO); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl.java index 5735aeb43..df5be6f56 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl.java @@ -16,6 +16,9 @@ import java.util.Set; import org.apache.commons.lang.StringUtils; import org.joda.time.format.DateTimeFormatter; +import org.mifosplatform.commands.domain.CommandWrapper; +import org.mifosplatform.commands.service.CommandProcessingService; +import org.mifosplatform.commands.service.CommandWrapperBuilder; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.ApiParameterError; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; @@ -75,6 +78,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl private final StaffRepositoryWrapper staffRepository; private final SavingsAccountApplicationTransitionApiJsonValidator savingsAccountApplicationTransitionApiJsonValidator; private final SavingsAccountChargeAssembler savingsAccountChargeAssembler; + private final CommandProcessingService commandProcessingService; @Autowired public SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, @@ -84,7 +88,8 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl final GroupRepository groupRepository, final SavingsProductRepository savingsProductRepository, final NoteRepository noteRepository, final StaffRepositoryWrapper staffRepository, final SavingsAccountApplicationTransitionApiJsonValidator savingsAccountApplicationTransitionApiJsonValidator, - final SavingsAccountChargeAssembler savingsAccountChargeAssembler) { + final SavingsAccountChargeAssembler savingsAccountChargeAssembler, + final CommandProcessingService commandProcessingService) { this.context = context; this.savingAccountRepository = savingAccountRepository; this.savingAccountAssembler = savingAccountAssembler; @@ -97,6 +102,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl this.staffRepository = staffRepository; this.savingsAccountApplicationTransitionApiJsonValidator = savingsAccountApplicationTransitionApiJsonValidator; this.savingsAccountChargeAssembler = savingsAccountChargeAssembler; + this.commandProcessingService = commandProcessingService; } /* @@ -433,7 +439,10 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl } @Override - public Long createActiveApplication(final SavingsAccountDataDTO savingsAccountDataDTO) { + public CommandProcessingResult createActiveApplication(final SavingsAccountDataDTO savingsAccountDataDTO) { + + final CommandWrapper commandWrapper = new CommandWrapperBuilder().savingsAccountActivation(null).build(); + boolean rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, savingsAccountDataDTO.getAppliedBy()); final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsAccountDataDTO.getClient(),savingsAccountDataDTO.getGroup(), savingsAccountDataDTO.getSavingsProduct(),savingsAccountDataDTO.getApplicationDate(),savingsAccountDataDTO.getAppliedBy()); @@ -446,6 +455,9 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl account.updateAccountNo(accountNoGenerator.generate()); this.savingAccountRepository.save(account); } - return account.getId(); + return new CommandProcessingResultBuilder() // + .withSavingsId(account.getId()) // + .setRollbackTransaction(rollbackTransaction)// + .build(); } } \ No newline at end of file