From ddf33c475a267c2077ef555046f8a5e4716acace Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Wed, 8 Aug 2012 14:42:27 +0100 Subject: [PATCH] start of work for deposit accounts. --- .../patch-02-deposit-account-table.sql | 28 ++++ .../api/DepositAccountsApiResource.java | 142 ++++++++++++++++++ .../api/commands/DepositAccountCommand.java | 66 ++++++++ 3 files changed, 236 insertions(+) create mode 100644 mifosng-db/patches/patch-02-deposit-account-table.sql create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java diff --git a/mifosng-db/patches/patch-02-deposit-account-table.sql b/mifosng-db/patches/patch-02-deposit-account-table.sql new file mode 100644 index 000000000..53959a6b6 --- /dev/null +++ b/mifosng-db/patches/patch-02-deposit-account-table.sql @@ -0,0 +1,28 @@ +CREATE TABLE `portfolio_deposit_account` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `external_id` varchar(100) DEFAULT NULL, + `client_id` bigint(20) NOT NULL, + `product_id` bigint(20) NOT NULL, + `currency_code` varchar(3) NOT NULL, + `currency_digits` smallint(5) NOT NULL, + `minimum_balance` decimal(19,6) DEFAULT NULL, + `maximum_balance` decimal(19,6) DEFAULT NULL, + `tenure_months` int(11) NOT NULL, + `maturity_default_interest_rate` decimal(19,6) NOT NULL, + `maturity_min_interest_rate` decimal(19,6) NOT NULL, + `maturity_max_interest_rate` decimal(19,6) NOT NULL, + `can_renew` tinyint(1) NOT NULL DEFAULT '0', + `can_pre_close` tinyint(1) NOT NULL DEFAULT '0', + `pre_closure_interest_rate` decimal(19,6) NOT NULL, + `is_deleted` tinyint(1) NOT NULL DEFAULT '0', + `createdby_id` bigint(20) DEFAULT NULL, + `created_date` datetime DEFAULT NULL, + `lastmodified_date` datetime DEFAULT NULL, + `lastmodifiedby_id` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `deposit_acc_external_id` (`external_id`), + KEY `FKKW0000000000001` (`client_id`), + KEY `FKKW0000000000002` (`product_id`), + CONSTRAINT `FKKW0000000000001` FOREIGN KEY (`client_id`) REFERENCES `portfolio_client` (`id`), + CONSTRAINT `FKKW0000000000002` FOREIGN KEY (`product_id`) REFERENCES `portfolio_product_deposit` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java new file mode 100644 index 000000000..7417e65c8 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java @@ -0,0 +1,142 @@ +package org.mifosng.platform.api; + +import java.util.Arrays; +import java.util.Collection; +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.PUT; +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; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.mifosng.platform.api.commands.SavingProductCommand; +import org.mifosng.platform.api.data.EntityIdentifier; +import org.mifosng.platform.api.data.SavingProductData; +import org.mifosng.platform.api.infrastructure.ApiDataConversionService; +import org.mifosng.platform.api.infrastructure.ApiParameterHelper; +import org.mifosng.platform.savingproduct.service.SavingProductReadPlatformService; +import org.mifosng.platform.savingproduct.service.SavingProductWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Path("/depositaccounts") +@Component +@Scope("singleton") +public class DepositAccountsApiResource { + + @Autowired + private SavingProductReadPlatformService savingProductReadPlatformService; + + @Autowired + private SavingProductWritePlatformService savingProductWritePlatformService; + + @Autowired + private ApiDataConversionService apiDataConversionService; + + @POST + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response createDepositAccount(final String jsonRequestBody){ + + final SavingProductCommand command=this.apiDataConversionService.convertJsonToSavingProductCommand(null, jsonRequestBody); + + EntityIdentifier entityIdentifier=this.savingProductWritePlatformService.createSavingProduct(command); + + return Response.ok().entity(entityIdentifier).build(); + } + + @PUT + @Path("{productId}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response updateSavingProduct(@PathParam("productId") final Long productId, final String jsonRequestBody){ + + SavingProductCommand command=this.apiDataConversionService.convertJsonToSavingProductCommand(productId, jsonRequestBody); + EntityIdentifier entityIdentifier=this.savingProductWritePlatformService.updateSavingProduct(command); + return Response.ok().entity(entityIdentifier).build(); + } + + @GET + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public String retrieveAllSavingProducts(@Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "name", "description", "createdOn", "lastModifedOn", + "interestRate","currencyCode","digitsAfterDecimal")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + Collection products = this.savingProductReadPlatformService.retrieveAllSavingProducts(); + + return this.apiDataConversionService.convertSavingProductDataToJson(prettyPrint, responseParameters, products.toArray(new SavingProductData[products.size()])); + } + + @GET + @Path("{productId}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public String retrieveSavingProductDetails(@PathParam("productId") final Long productId, @Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "name", "description", "createdOn", "lastModifedOn", + "interestRate","currencyCode","digitsAfterDecimal")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); + if (template) { + responseParameters.addAll(Arrays.asList("currencyOptions")); + } + + SavingProductData savingProduct = this.savingProductReadPlatformService.retrieveSavingProduct(productId); + + return this.apiDataConversionService.convertSavingProductDataToJson(prettyPrint, responseParameters, savingProduct); + } + + @GET + @Path("template") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public String retrieveNewSavingProductDetails(@Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet( + Arrays.asList("id", "name", "description", "createdOn", "lastModifedOn","interestRate","currencyCode","digitsAfterDecimal", "currencyOptions")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + SavingProductData savingProduct = this.savingProductReadPlatformService.retrieveNewSavingProductDetails(); + + return this.apiDataConversionService.convertSavingProductDataToJson(prettyPrint, responseParameters, savingProduct); + } + + @DELETE + @Path("{productId}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response deleteProduct(@PathParam("productId") final Long productId) { + + this.savingProductWritePlatformService.deleteSavingProduct(productId); + + return Response.ok(new EntityIdentifier(productId)).build(); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java new file mode 100644 index 000000000..03da71053 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java @@ -0,0 +1,66 @@ +package org.mifosng.platform.api.commands; + +import java.math.BigDecimal; +import java.util.Set; + +/** + * Immutable command used when create/renewing deposit accounts + */ +public class DepositAccountCommand { + + private final Long id; + private final String externalId; + + private final String currencyCode; + private final Integer digitsAfterDecimal; + private final BigDecimal maturityActualInterestRate; + + private final Set modifiedParameters; + + public DepositAccountCommand(final Set modifiedParameters, + final Long id, + final String externalId, + final String currencyCode, + final Integer digitsAfterDecimal, + final BigDecimal maturityActualInterestRate) { + this.id = id; + this.externalId = externalId; + this.currencyCode = currencyCode; + this.digitsAfterDecimal = digitsAfterDecimal; + this.maturityActualInterestRate = maturityActualInterestRate; + + this.modifiedParameters = modifiedParameters; + } + + public Long getId() { + return id; + } + + public String getExternalId() { + return externalId; + } + + public String getCurrencyCode() { + return currencyCode; + } + + public Integer getDigitsAfterDecimal() { + return digitsAfterDecimal; + } + + public BigDecimal getMaturityActualInterestRate() { + return maturityActualInterestRate; + } + + public boolean isCurrencyCodeChanged() { + return this.modifiedParameters.contains("currencyCode"); + } + + public boolean isDigitsAfterDecimalChanged() { + return this.modifiedParameters.contains("digitsAfterDecimal"); + } + + public boolean isMaturityActualInterestRateChanged() { + return this.modifiedParameters.contains("interestRate"); + } +}