remove command concept for office transactions to distinguish between external and intra office transactions. allow the provision of a from or to office id to do this solely.

This commit is contained in:
Keith Woodlock
2012-07-16 17:02:43 +01:00
parent 393ba7f829
commit cbbeb50f98
6 changed files with 27 additions and 90 deletions
@@ -1,7 +0,0 @@
/* remove column org_id */
ALTER TABLE `portfolio_client` DROP FOREIGN KEY `FKCE00CAB354DF2770`;
ALTER TABLE `portfolio_client`
DROP COLUMN `org_id` ,
DROP INDEX `org_id` ,
ADD INDEX `external_id` (`external_id` ASC) ,
DROP INDEX `FKCE00CAB354DF2770`;
@@ -8,6 +8,18 @@ DROP INDEX `externalid_org`,
ADD UNIQUE INDEX `externalid_org` (`external_id` ASC), ADD UNIQUE INDEX `externalid_org` (`external_id` ASC),
DROP INDEX `FK2291C47754DF2770`; DROP INDEX `FK2291C47754DF2770`;
ALTER TABLE `portfolio_client` DROP FOREIGN KEY `FKCE00CAB354DF2770`;
ALTER TABLE `portfolio_client`
DROP COLUMN `org_id` ,
DROP INDEX `org_id` ,
ADD UNIQUE INDEX `external_id` (`external_id` ASC) ,
DROP INDEX `FKCE00CAB354DF2770`;
ALTER TABLE `portfolio_client`
DROP INDEX `external_id`,
ADD UNIQUE INDEX `external_id` (`external_id` ASC);
ALTER TABLE `org_fund` DROP FOREIGN KEY `FK4E56DB4054DF2770` ; ALTER TABLE `org_fund` DROP FOREIGN KEY `FK4E56DB4054DF2770` ;
ALTER TABLE `org_fund` DROP COLUMN `org_id` , DROP INDEX `fund_name_org` , ADD UNIQUE INDEX `fund_name_org` (`name` ASC) , DROP INDEX `fund_externalid_org` , ADD UNIQUE INDEX `fund_externalid_org` (`external_id` ASC) , DROP INDEX `FK4E56DB4054DF2770` ; ALTER TABLE `org_fund` DROP COLUMN `org_id` , DROP INDEX `fund_name_org` , ADD UNIQUE INDEX `fund_name_org` (`name` ASC) , DROP INDEX `fund_externalid_org` , ADD UNIQUE INDEX `fund_externalid_org` (`external_id` ASC) , DROP INDEX `FK4E56DB4054DF2770` ;
@@ -8,20 +8,17 @@ import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService; import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
import org.mifosng.platform.exceptions.UnrecognizedQueryParamException;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService; import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.mifosng.platform.organisation.service.OfficeWritePlatformService; import org.mifosng.platform.organisation.service.OfficeWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -63,11 +60,10 @@ public class OfficeTransactionsApiResource {
uriInfo.getQueryParameters()); uriInfo.getQueryParameters());
} }
@POST @POST
@Consumes({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public Response transferMoneyFrom(@QueryParam("command") final String commandParam, final BranchMoneyTransferCommand command) { public Response transferMoneyFrom(final BranchMoneyTransferCommand command) {
LocalDate transactionLocalDate = apiDataConversionService.convertFrom(command.getTransactionDate(), "transactionDate", command.getDateFormat()); LocalDate transactionLocalDate = apiDataConversionService.convertFrom(command.getTransactionDate(), "transactionDate", command.getDateFormat());
command.setTransactionLocalDate(transactionLocalDate); command.setTransactionLocalDate(transactionLocalDate);
@@ -77,25 +73,7 @@ public class OfficeTransactionsApiResource {
BigDecimal transactionAmountValue = apiDataConversionService.convertFrom(command.getTransactionAmount(), "transactionAmount", clientLocale); BigDecimal transactionAmountValue = apiDataConversionService.convertFrom(command.getTransactionAmount(), "transactionAmount", clientLocale);
command.setTransactionAmountValue(transactionAmountValue); command.setTransactionAmountValue(transactionAmountValue);
Response response = null; Long id = this.writePlatformService.externalBranchMoneyTransfer(command);
return Response.ok().entity(new EntityIdentifier(id)).build();
if (is(commandParam, "intra-transfer")) {
Long id = this.writePlatformService.interBranchMoneyTransfer(command);
response = Response.ok().entity(new EntityIdentifier(id)).build();
} else if (is(commandParam, "external-transfer")) {
Long id = this.writePlatformService.externalBranchMoneyTransfer(command);
response = Response.ok().entity(new EntityIdentifier(id)).build();
}
if (response == null) {
throw new UnrecognizedQueryParamException("command", commandParam);
}
return response;
}
private boolean is(final String commandParam, final String commandValue) {
return StringUtils.isNotBlank(commandParam)
&& commandParam.trim().equalsIgnoreCase(commandValue);
} }
} }
@@ -16,30 +16,15 @@ public class BranchMoneyTransferCommandValidator {
this.command = command; this.command = command;
} }
public void validateInterBranchTransfer() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("office.money.transfer");
baseDataValidator.reset().parameter("fromOfficeId").value(command.getFromOfficeId()).notNull();
baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).notNull();
baseDataValidator.reset().parameter("transactionDate").value(command.getTransactionLocalDate()).notNull();
baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).notBlank();
baseDataValidator.reset().parameter("transactionAmount").value(command.getTransactionAmountValue()).notNull();
baseDataValidator.reset().parameter("description").value(command.getDescription()).notExceedingLengthOf(100);
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
}
}
public void validateExternalBranchTransfer() { public void validateExternalBranchTransfer() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>(); List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("office.money.transfer"); DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("office.money.transfer");
baseDataValidator.reset().parameter("fromOfficeId").value(command.getFromOfficeId()).greaterThanZero();
baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).greaterThanZero();
if (command.getFromOfficeId() == null) { if (command.getFromOfficeId() == null) {
baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).notNull(); baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).notNull();
} }
@@ -12,9 +12,6 @@ public interface OfficeWritePlatformService {
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')") @PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
Long updateOffice(final OfficeCommand command); Long updateOffice(final OfficeCommand command);
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
Long interBranchMoneyTransfer(BranchMoneyTransferCommand command);
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')") @PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
Long externalBranchMoneyTransfer(BranchMoneyTransferCommand command); Long externalBranchMoneyTransfer(BranchMoneyTransferCommand command);
} }
@@ -100,41 +100,6 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP
} }
} }
@Transactional
@Override
public Long interBranchMoneyTransfer(final BranchMoneyTransferCommand command) {
context.authenticatedUser();
BranchMoneyTransferCommandValidator validator = new BranchMoneyTransferCommandValidator(command);
validator.validateInterBranchTransfer();
Office fromOffice = this.officeRepository.findOne(command.getFromOfficeId());
if (fromOffice == null) {
throw new OfficeNotFoundException(command.getFromOfficeId());
}
Office toOffice = this.officeRepository.findOne(command.getToOfficeId());
if (toOffice == null) {
throw new OfficeNotFoundException(command.getToOfficeId());
}
final String currencyCode = command.getCurrencyCode();
ApplicationCurrency appCurrency = this.applicationCurrencyRepository.findOneByCode(currencyCode);
if (appCurrency == null) {
throw new CurrencyNotFoundException(currencyCode);
}
MonetaryCurrency currency = new MonetaryCurrency(appCurrency.getCode(), appCurrency.getDecimalPlaces());
Money amount = Money.of(currency, command.getTransactionAmountValue());
OfficeTransaction entity = OfficeTransaction.create(fromOffice, toOffice, command.getTransactionLocalDate(), amount);
this.officeMonetaryTransferRepository.save(entity);
return entity.getId();
}
@Transactional @Transactional
@Override @Override
public Long externalBranchMoneyTransfer(BranchMoneyTransferCommand command) { public Long externalBranchMoneyTransfer(BranchMoneyTransferCommand command) {
@@ -144,8 +109,15 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP
BranchMoneyTransferCommandValidator validator = new BranchMoneyTransferCommandValidator(command); BranchMoneyTransferCommandValidator validator = new BranchMoneyTransferCommandValidator(command);
validator.validateExternalBranchTransfer(); validator.validateExternalBranchTransfer();
Office fromOffice = this.officeRepository.findOne(command.getFromOfficeId()); Office fromOffice = null;
Office toOffice = this.officeRepository.findOne(command.getToOfficeId()); if (command.getFromOfficeId() != null) {
fromOffice = this.officeRepository.findOne(command.getFromOfficeId());
}
Office toOffice = null;
if (command.getToOfficeId() != null) {
toOffice = this.officeRepository.findOne(command.getToOfficeId());
}
if (fromOffice == null && toOffice == null) { if (fromOffice == null && toOffice == null) {
throw new OfficeNotFoundException(command.getToOfficeId()); throw new OfficeNotFoundException(command.getToOfficeId());
} }