diff --git a/mifosng-db/patches/05-add-delete-office-transaction-permission.sql b/mifosng-db/patches/05-add-delete-office-transaction-permission.sql new file mode 100644 index 000000000..c6c1ca137 --- /dev/null +++ b/mifosng-db/patches/05-add-delete-office-transaction-permission.sql @@ -0,0 +1 @@ +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('organisation', 'DELETE_OFFICETRANSACTION', 'OFFICETRANSACTION', 'DELETE', 1); \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java index d68aea744..6f4d52d83 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java @@ -126,6 +126,14 @@ public class CommandWrapperBuilder { this.href = "/officetransactions/template"; return this; } + + public CommandWrapperBuilder deleteOfficeTransaction(final Long transactionId) { + this.actionName = "DELETE"; + this.entityName = "OFFICETRANSACTION"; + this.entityId = transactionId; + this.href = "/officetransactions/" + transactionId; + return this; + } public CommandWrapperBuilder createStaff() { this.actionName = "CREATE"; 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 487d48c3a..756a96bba 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 @@ -45,10 +45,12 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer @Transactional @Override - public CommandProcessingResult processAndLogCommand(final CommandWrapper wrapper, final JsonCommand command, final boolean isApprovedByChecker) { + 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()) && !isApprovedByChecker; - final NewCommandSourceHandler handler = findCommandHandler(wrapper); final CommandProcessingResult result = handler.processCommand(command); @@ -95,7 +97,8 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer commandSourceRepository.save(commandSourceResult); } - return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(commandSourceResult.resourceId()).build(); + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(commandSourceResult.resourceId()) + .build(); } private NewCommandSourceHandler findCommandHandler(final CommandWrapper wrapper) { @@ -103,8 +106,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer if (wrapper.isConfigurationResource()) { handler = applicationContext.getBean("updateGlobalConfigurationCommandHandler", NewCommandSourceHandler.class); - } - else if (wrapper.isDatatableResource()) { + } else if (wrapper.isDatatableResource()) { if (wrapper.isCreate()) { handler = applicationContext.getBean("createDatatableEntryCommandHandler", NewCommandSourceHandler.class); } else if (wrapper.isUpdateMultiple()) { @@ -209,6 +211,8 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer } else if (wrapper.isOfficeTransactionResource()) { if (wrapper.isCreate()) { handler = applicationContext.getBean("createOfficeTransactionCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isDelete()) { + handler = applicationContext.getBean("deleteOfficeTransactionCommandHandler", NewCommandSourceHandler.class); } else { throw new UnsupportedCommandException(wrapper.commandName()); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/api/OfficeTransactionsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/api/OfficeTransactionsApiResource.java index a6e406619..1f6ccc8b1 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/api/OfficeTransactionsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/api/OfficeTransactionsApiResource.java @@ -6,9 +6,11 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @@ -95,4 +97,17 @@ public class OfficeTransactionsApiResource { return this.toApiJsonSerializer.serialize(result); } + + @DELETE + @Path("{transactionId}") + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) + public String delete(@PathParam("transactionId") final Long transactionId) { + + final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteOfficeTransaction(transactionId).build(); + + final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + + return this.toApiJsonSerializer.serialize(result); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeNotFoundException.java index e3a4ae74e..a8a00cb59 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeNotFoundException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeNotFoundException.java @@ -7,7 +7,7 @@ import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceN */ public class OfficeNotFoundException extends AbstractPlatformResourceNotFoundException { - public OfficeNotFoundException(final Long id) { - super("error.msg.office.id.invalid", "Office with identifier " + id + " does not exist", id); - } + public OfficeNotFoundException(final Long id) { + super("error.msg.office.id.invalid", "Office with identifier " + id + " does not exist", id); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeTransactionNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeTransactionNotFoundException.java new file mode 100644 index 000000000..4d0a741b6 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/exception/OfficeTransactionNotFoundException.java @@ -0,0 +1,13 @@ +package org.mifosplatform.organisation.office.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException; + +/** + * A {@link RuntimeException} thrown when office transaction resources are not found. + */ +public class OfficeTransactionNotFoundException extends AbstractPlatformResourceNotFoundException { + + public OfficeTransactionNotFoundException(final Long id) { + super("error.msg.officetransaction.id.invalid", "Office transaction with identifier " + id + " does not exist", id); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/handler/DeleteOfficeTransactionCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/handler/DeleteOfficeTransactionCommandHandler.java new file mode 100644 index 000000000..8310f5f9f --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/handler/DeleteOfficeTransactionCommandHandler.java @@ -0,0 +1,27 @@ +package org.mifosplatform.organisation.office.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.organisation.office.service.OfficeWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class DeleteOfficeTransactionCommandHandler implements NewCommandSourceHandler { + + private final OfficeWritePlatformService writePlatformService; + + @Autowired + public DeleteOfficeTransactionCommandHandler(final OfficeWritePlatformService writePlatformService) { + this.writePlatformService = writePlatformService; + } + + @Transactional + @Override + public CommandProcessingResult processCommand(final JsonCommand command) { + + return this.writePlatformService.deleteOfficeTransaction(command.entityId(), command); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformService.java index 30b670b20..df3f458f9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformService.java @@ -10,4 +10,6 @@ public interface OfficeWritePlatformService { CommandProcessingResult updateOffice(Long officeId, JsonCommand command); CommandProcessingResult externalBranchMoneyTransfer(JsonCommand command); + + CommandProcessingResult deleteOfficeTransaction(Long id, JsonCommand command); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformServiceJpaRepositoryImpl.java index 560709783..8de243c8d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/service/OfficeWritePlatformServiceJpaRepositoryImpl.java @@ -38,7 +38,7 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP private final OfficeCommandFromApiJsonDeserializer fromApiJsonDeserializer; private final BranchMoneyTransferCommandFromApiJsonDeserializer moneyTransferCommandFromApiJsonDeserializer; private final OfficeRepository officeRepository; - private final OfficeTransactionRepository officeMonetaryTransferRepository; + private final OfficeTransactionRepository officeTransactionRepository; private final ApplicationCurrencyRepository applicationCurrencyRepository; @Autowired @@ -51,7 +51,7 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP this.fromApiJsonDeserializer = fromApiJsonDeserializer; this.moneyTransferCommandFromApiJsonDeserializer = moneyTransferCommandFromApiJsonDeserializer; this.officeRepository = officeRepository; - this.officeMonetaryTransferRepository = officeMonetaryTransferRepository; + this.officeTransactionRepository = officeMonetaryTransferRepository; this.applicationCurrencyRepository = applicationCurrencyRepository; } @@ -155,12 +155,26 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP final OfficeTransaction entity = OfficeTransaction.fromJson(fromOffice, toOffice, amount, command); - this.officeMonetaryTransferRepository.save(entity); + this.officeTransactionRepository.save(entity); return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(entity.getId()).withOfficeId(officeId) .build(); } + @Transactional + @Override + public CommandProcessingResult deleteOfficeTransaction(final Long transactionId, JsonCommand command) { + + context.authenticatedUser(); + + this.officeTransactionRepository.delete(transactionId); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(transactionId) // + .build(); + } + /* * Guaranteed to throw an exception no matter what the data integrity issue * is.