diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/DataValidatorBuilder.java b/mifosng-provider/src/main/java/org/mifosng/platform/DataValidatorBuilder.java index 24b722f1f..03e58b859 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/DataValidatorBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/DataValidatorBuilder.java @@ -175,7 +175,7 @@ public class DataValidatorBuilder { return this; } - public DataValidatorBuilder greaterThanZero() { + public DataValidatorBuilder integerGreaterThanZero() { if (value == null && ignoreNullValue) { return this; } 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 index 7417e65c8..5dcf6f4f5 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java @@ -3,6 +3,7 @@ package org.mifosng.platform.api; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.ws.rs.Consumes; @@ -18,13 +19,15 @@ 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.commands.DepositAccountCommand; +import org.mifosng.platform.api.data.CurrencyData; +import org.mifosng.platform.api.data.DepositAccountData; 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.mifosng.platform.currency.service.CurrencyReadPlatformService; +import org.mifosng.platform.savingproduct.service.DepositAccountReadPlatformService; +import org.mifosng.platform.savingproduct.service.DepositAccountWritePlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -35,10 +38,13 @@ import org.springframework.stereotype.Component; public class DepositAccountsApiResource { @Autowired - private SavingProductReadPlatformService savingProductReadPlatformService; + private DepositAccountReadPlatformService depositAccountReadPlatformService; @Autowired - private SavingProductWritePlatformService savingProductWritePlatformService; + private CurrencyReadPlatformService currencyReadPlatformService; + + @Autowired + private DepositAccountWritePlatformService depositAccountWritePlatformService; @Autowired private ApiDataConversionService apiDataConversionService; @@ -48,31 +54,36 @@ public class DepositAccountsApiResource { @Produces({MediaType.APPLICATION_JSON}) public Response createDepositAccount(final String jsonRequestBody){ - final SavingProductCommand command=this.apiDataConversionService.convertJsonToSavingProductCommand(null, jsonRequestBody); + final DepositAccountCommand command = this.apiDataConversionService.convertJsonToDepositAccountCommand(null, jsonRequestBody); - EntityIdentifier entityIdentifier=this.savingProductWritePlatformService.createSavingProduct(command); + EntityIdentifier entityIdentifier = this.depositAccountWritePlatformService.createDepositAccount(command); return Response.ok().entity(entityIdentifier).build(); } @PUT - @Path("{productId}") + @Path("{accountId}") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public Response updateSavingProduct(@PathParam("productId") final Long productId, final String jsonRequestBody){ + public Response updateSavingProduct(@PathParam("accountId") final Long accountId, final String jsonRequestBody){ + + final DepositAccountCommand command = this.apiDataConversionService.convertJsonToDepositAccountCommand(accountId, jsonRequestBody); + + EntityIdentifier entityIdentifier = this.depositAccountWritePlatformService.updateDepositAccount(command); - 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) { + public String retrieveAllDepositAccounts(@Context final UriInfo uriInfo) { - Set typicalResponseParameters = new HashSet(Arrays.asList("id", "name", "description", "createdOn", "lastModifedOn", - "interestRate","currencyCode","digitsAfterDecimal")); + Set typicalResponseParameters = new HashSet( + Arrays.asList("createdOn", "lastModifedOn", + "id", "clientId", "clientName", "productId", "productName", + "currency", "deposit", "interestRate") + ); Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); if (responseParameters.isEmpty()) { @@ -80,63 +91,74 @@ public class DepositAccountsApiResource { } boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - Collection products = this.savingProductReadPlatformService.retrieveAllSavingProducts(); + Collection accounts = this.depositAccountReadPlatformService.retrieveAllDepositAccounts(); - return this.apiDataConversionService.convertSavingProductDataToJson(prettyPrint, responseParameters, products.toArray(new SavingProductData[products.size()])); + return this.apiDataConversionService.convertDepositAccountDataToJson(prettyPrint, responseParameters, accounts.toArray(new DepositAccountData[accounts.size()])); } @GET - @Path("{productId}") + @Path("{accountId}") @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")); + public String retrieveDepositAccount(@PathParam("accountId") final Long accountId, @Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet( + Arrays.asList("createdOn", "lastModifedOn", + "id", "clientId", "clientName", "productId", "productName", + "currency", "deposit", "interestRate") + ); Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); if (responseParameters.isEmpty()) { responseParameters.addAll(typicalResponseParameters); } boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + DepositAccountData account = this.depositAccountReadPlatformService.retrieveDepositAccount(accountId); + boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); if (template) { responseParameters.addAll(Arrays.asList("currencyOptions")); + List allowedCurrencies = this.currencyReadPlatformService.retrieveAllowedCurrencies(); + account = new DepositAccountData(account, allowedCurrencies); } - SavingProductData savingProduct = this.savingProductReadPlatformService.retrieveSavingProduct(productId); - - return this.apiDataConversionService.convertSavingProductDataToJson(prettyPrint, responseParameters, savingProduct); + return this.apiDataConversionService.convertDepositAccountDataToJson(prettyPrint, responseParameters, account); } @GET @Path("template") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public String retrieveNewSavingProductDetails(@Context final UriInfo uriInfo) { + public String retrieveNewDepositAccountDetails(@Context final UriInfo uriInfo) { Set typicalResponseParameters = new HashSet( - Arrays.asList("id", "name", "description", "createdOn", "lastModifedOn","interestRate","currencyCode","digitsAfterDecimal", "currencyOptions")); + Arrays.asList("createdOn", "lastModifedOn", + "id", "clientId", "clientName", "productId", "productName", + "currency", "deposit", "interestRate", "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); + DepositAccountData account = this.depositAccountReadPlatformService.retrieveNewDepositAccountDetails(); + List allowedCurrencies = this.currencyReadPlatformService.retrieveAllowedCurrencies(); + account = new DepositAccountData(account, allowedCurrencies); + + return this.apiDataConversionService.convertDepositAccountDataToJson(prettyPrint, responseParameters, account); } @DELETE - @Path("{productId}") + @Path("{accountId}") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public Response deleteProduct(@PathParam("productId") final Long productId) { + public Response deleteProduct(@PathParam("accountId") final Long accountId) { - this.savingProductWritePlatformService.deleteSavingProduct(productId); + this.depositAccountWritePlatformService.deleteDepositAccount(accountId); - return Response.ok(new EntityIdentifier(productId)).build(); + return Response.ok(new EntityIdentifier(accountId)).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 index 03da71053..5de7aa136 100644 --- 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 @@ -9,26 +9,39 @@ import java.util.Set; public class DepositAccountCommand { private final Long id; + private final Long clientId; + private final Long productId; private final String externalId; private final String currencyCode; private final Integer digitsAfterDecimal; - private final BigDecimal maturityActualInterestRate; - + private final BigDecimal depositAmount; + private final BigDecimal interestRate; + private final Integer termInMonths; + private final Set modifiedParameters; public DepositAccountCommand(final Set modifiedParameters, final Long id, + final Long clientId, + final Long productId, final String externalId, final String currencyCode, final Integer digitsAfterDecimal, - final BigDecimal maturityActualInterestRate) { + final BigDecimal depositAmount, + final BigDecimal interestRate, + final Integer termInMonths) { this.id = id; + this.clientId = clientId; + this.productId = productId; this.externalId = externalId; + this.currencyCode = currencyCode; this.digitsAfterDecimal = digitsAfterDecimal; - this.maturityActualInterestRate = maturityActualInterestRate; - + this.depositAmount = depositAmount; + this.interestRate = interestRate; + this.termInMonths = termInMonths; + this.modifiedParameters = modifiedParameters; } @@ -36,6 +49,14 @@ public class DepositAccountCommand { return id; } + public Long getClientId() { + return clientId; + } + + public Long getProductId() { + return productId; + } + public String getExternalId() { return externalId; } @@ -48,8 +69,16 @@ public class DepositAccountCommand { return digitsAfterDecimal; } - public BigDecimal getMaturityActualInterestRate() { - return maturityActualInterestRate; + public BigDecimal getDepositAmount() { + return depositAmount; + } + + public BigDecimal getInterestRate() { + return interestRate; + } + + public Integer getTermInMonths() { + return termInMonths; } public boolean isCurrencyCodeChanged() { @@ -63,4 +92,4 @@ public class DepositAccountCommand { public boolean isMaturityActualInterestRateChanged() { return this.modifiedParameters.contains("interestRate"); } -} +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java new file mode 100644 index 000000000..151e4a193 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java @@ -0,0 +1,125 @@ +package org.mifosng.platform.api.data; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import org.joda.time.DateTime; + +/** + * Immutable data object for deposit accounts. + */ +public class DepositAccountData { + + private final Long id; + + private final Long clientId; + private final String clientName; + + private final Long productId; + private final String productName; + + private final CurrencyData currency; + private final BigDecimal deposit; + private final BigDecimal interestRate; + + private final DateTime createdOn; + private final DateTime lastModifedOn; + + private final List currencyOptions; + + public DepositAccountData() { + this.createdOn = null; + this.lastModifedOn = null; + this.id = null; + this.clientId = null; + this.clientName = null; + this.productId = null; + this.productName = null; + this.currency = null; + this.deposit = null; + this.interestRate = null; + this.currencyOptions = new ArrayList(); + } + + public DepositAccountData(final DepositAccountData account, final List currencies) { + this.createdOn = account.getCreatedOn(); + this.lastModifedOn = account.getLastModifedOn(); + this.id = account.getId(); + this.clientId = account.getClientId(); + this.clientName = account.getClientName(); + this.productId = account.getProductId(); + this.productName = account.getProductName(); + this.currency = account.getCurrency(); + this.deposit = account.getDeposit(); + this.interestRate = account.getInterestRate(); + this.currencyOptions = currencies; + } + + public DepositAccountData( + final DateTime createdOn, + final DateTime lastModifedOn, + final Long id, + final Long clientId, + final String clientName, + final Long productId, + final String productName, + final CurrencyData currency, + final BigDecimal deposit, BigDecimal interestRate) { + this.createdOn=createdOn; + this.lastModifedOn=lastModifedOn; + this.id=id; + this.clientId = clientId; + this.clientName = clientName; + this.productId = productId; + this.productName = productName; + this.currency = currency; + this.deposit = deposit; + this.interestRate=interestRate; + this.currencyOptions = new ArrayList(); + } + + public Long getId() { + return id; + } + + public Long getClientId() { + return clientId; + } + + public String getClientName() { + return clientName; + } + + public Long getProductId() { + return productId; + } + + public String getProductName() { + return productName; + } + + public CurrencyData getCurrency() { + return currency; + } + + public BigDecimal getDeposit() { + return deposit; + } + + public BigDecimal getInterestRate() { + return interestRate; + } + + public DateTime getCreatedOn() { + return createdOn; + } + + public DateTime getLastModifedOn() { + return lastModifedOn; + } + + public List getCurrencyOptions() { + return currencyOptions; + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java index de97253ed..a68c997bc 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java @@ -6,6 +6,7 @@ import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.DepositProductCommand; +import org.mifosng.platform.api.commands.DepositAccountCommand; import org.mifosng.platform.api.commands.FundCommand; import org.mifosng.platform.api.commands.GroupCommand; import org.mifosng.platform.api.commands.LoanProductCommand; @@ -25,6 +26,7 @@ import org.mifosng.platform.api.data.ClientData; import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData; import org.mifosng.platform.api.data.ConfigurationData; import org.mifosng.platform.api.data.DepositProductData; +import org.mifosng.platform.api.data.DepositAccountData; import org.mifosng.platform.api.data.FundData; import org.mifosng.platform.api.data.GenericResultsetData; import org.mifosng.platform.api.data.GroupData; @@ -67,6 +69,8 @@ public interface ApiDataConversionService { String convertDepositProductDataToJson(boolean prettyPrint, Set responseParameters, DepositProductData... products); + String convertDepositAccountDataToJson(boolean prettyPrint, Set responseParameters, DepositAccountData... accounts); + String convertNewLoanDataToJson(boolean prettyPrint, Set responseParameters, NewLoanData newLoanData); String convertLoanAccountDataToJson(boolean prettyPrint, Set responseParameters, LoanAccountData loanAccount); @@ -116,4 +120,6 @@ public interface ApiDataConversionService { OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(String jsonRequestBody); NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String jsonRequestBody); + + DepositAccountCommand convertJsonToDepositAccountCommand(Long resourceIdentifier, String jsonRequestBody); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index 459ad3cff..19e5b5c4d 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -22,6 +22,7 @@ import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.DepositProductCommand; +import org.mifosng.platform.api.commands.DepositAccountCommand; import org.mifosng.platform.api.commands.FundCommand; import org.mifosng.platform.api.commands.GroupCommand; import org.mifosng.platform.api.commands.LoanProductCommand; @@ -42,6 +43,7 @@ import org.mifosng.platform.api.data.ClientData; import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData; import org.mifosng.platform.api.data.ConfigurationData; import org.mifosng.platform.api.data.DepositProductData; +import org.mifosng.platform.api.data.DepositAccountData; import org.mifosng.platform.api.data.FundData; import org.mifosng.platform.api.data.GenericResultsetData; import org.mifosng.platform.api.data.GroupData; @@ -1479,10 +1481,42 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { return new SavingProductCommand(modifiedParameters, resourceIdentifier,name, description,currencyCode,digitsAfterDecimalValue,interestRate,minimumBalance,maximumBalance); } + + @Override + public DepositAccountCommand convertJsonToDepositAccountCommand(final Long resourceIdentifier, final String json) { + + if (StringUtils.isBlank(json)) { + throw new InvalidJsonException(); + } + + Type typeOfMap = new TypeToken>() {}.getType(); + Map requestMap = gsonConverter.fromJson(json, typeOfMap); + + Set supportedParams = new HashSet( + Arrays.asList("clientId", "productId", "externalId", "currencyCode", "digitsAfterDecimal", "depositAmount", "interestRate", "termInMonths", "locale") + ); + checkForUnsupportedParameters(requestMap, supportedParams); + Set modifiedParameters = new HashSet(); + + Long clientId = extractLongParameter("clientId", requestMap, modifiedParameters); + Long productId = extractLongParameter("productId", requestMap, modifiedParameters); + String externalId = extractStringParameter("externalId", requestMap,modifiedParameters); + String currencyCode=extractStringParameter("currencyCode", requestMap,modifiedParameters); + Integer digitsAfterDecimalValue = extractIntegerParameter("digitsAfterDecimal", requestMap, modifiedParameters); + BigDecimal depositAmount=extractBigDecimalParameter("depositAmount", requestMap, modifiedParameters); + BigDecimal interestRate = extractBigDecimalParameter("interestRate", requestMap, modifiedParameters); + Integer termInMonths = extractIntegerParameter("termInMonths", requestMap, modifiedParameters); + + // isRenewable, isPreClosureAllowed, expectedDepositDate, pojectMaturityDate, projectedInterestAccrued + + return new DepositAccountCommand(modifiedParameters, resourceIdentifier, clientId, productId, externalId, currencyCode, digitsAfterDecimalValue, depositAmount, interestRate, termInMonths); + } @Override - public String convertSavingProductDataToJson(boolean prettyPrint, - Set responseParameters, SavingProductData... products) { + public String convertSavingProductDataToJson( + final boolean prettyPrint, + final Set responseParameters, + final SavingProductData... products) { Set supportedParameters = new HashSet( Arrays.asList("id", "name", "description","createdOn", "lastModifedOn","interestRate","currencyCode","digitsAfterDecimal", "currencyOptions", "minimumBalance","maximumBalance")); @@ -1519,7 +1553,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } @Override - public DepositProductCommand convertJsonToDepositProductCommand(Long resourceIdentifier, String json) { + public DepositProductCommand convertJsonToDepositProductCommand(final Long resourceIdentifier, final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); @@ -1556,17 +1590,27 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { Set responseParameters, DepositProductData... products) { Set supportedParameters = new HashSet( - Arrays.asList("id", "name", "description","createdOn", "lastModifedOn","currencyCode","digitsAfterDecimal", "currencyOptions", "minimumBalance","maximumBalance")); + Arrays.asList("id", "name", "description","createdOn", "lastModifedOn","currencyCode","digitsAfterDecimal", "currencyOptions", + "minimumBalance","maximumBalance")); + final Set parameterNamesToSkip = new HashSet(); + if (!responseParameters.isEmpty()) { - if (!supportedParameters.containsAll(responseParameters)) { - throw new UnsupportedParameterException(new ArrayList(responseParameters)); + + // strip out all know support params from expected response to see if unsupported parameters requested for response. + Set differentParametersSet = new HashSet(responseParameters); + differentParametersSet.removeAll(supportedParameters); + + if (!differentParametersSet.isEmpty()) { + throw new UnsupportedParameterException(new ArrayList(differentParametersSet)); } parameterNamesToSkip.addAll(supportedParameters); parameterNamesToSkip.removeAll(responseParameters); } + ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); + GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); @@ -1581,6 +1625,53 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } else { json = gsonDeserializer.toJson(products); } + return json; + } + + @Override + public String convertDepositAccountDataToJson( + final boolean prettyPrint, + final Set responseParameters, + final DepositAccountData... accounts) { + + Set supportedParameters = new HashSet( + Arrays.asList("createdOn", "lastModifedOn", + "id", "clientId", "clientName", "productId", "productName", + "currency", "deposit", "interestRate", "currencyOptions") + ); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + + // strip out all know support params from expected response to see if unsupported parameters requested for response. + Set differentParametersSet = new HashSet(responseParameters); + differentParametersSet.removeAll(supportedParameters); + + if (!differentParametersSet.isEmpty()) { + throw new UnsupportedParameterException(new ArrayList(differentParametersSet)); + } + + parameterNamesToSkip.addAll(supportedParameters); + parameterNamesToSkip.removeAll(responseParameters); + } + + ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); + + GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); + builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); + builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); + if (prettyPrint) { + builder.setPrettyPrinting(); + } + Gson gsonDeserializer = builder.create(); + + String json = ""; + if (accounts != null && accounts.length == 1) { + json = gsonDeserializer.toJson(accounts[0]); + } else { + json = gsonDeserializer.toJson(accounts); + } return json; } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientCommandValidator.java index 69c2b61a3..611c34150 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientCommandValidator.java @@ -34,7 +34,7 @@ public class ClientCommandValidator { baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100); if (command.isOfficeChanged()) { - baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().integerGreaterThanZero(); } baseDataValidator.reset().anyOfNotNull(command.getFirstname(), command.getLastname(), command.getClientOrBusinessName(), @@ -60,7 +60,7 @@ public class ClientCommandValidator { baseDataValidator.reset().parameter("joiningDate").value(command.getJoiningDate()).notBlank(); baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).notExceedingLengthOf(100); - baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().integerGreaterThanZero(); if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountNotFoundException.java b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountNotFoundException.java new file mode 100644 index 000000000..5fa92c897 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountNotFoundException.java @@ -0,0 +1,9 @@ +package org.mifosng.platform.exceptions; + +public class DepositAccountNotFoundException extends AbstractPlatformResourceNotFoundException { + + public DepositAccountNotFoundException(final Long id) { + super("error.msg.deposit.account.id.invalid", "Deposit account with identifier " + id + " does not exist", id); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/AdjustLoanTransactionCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/AdjustLoanTransactionCommandValidator.java index 75f210b4e..fb95085ff 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/AdjustLoanTransactionCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/AdjustLoanTransactionCommandValidator.java @@ -22,8 +22,8 @@ public class AdjustLoanTransactionCommandValidator { DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan.transaction"); - baseDataValidator.reset().parameter("loanId").value(command.getLoanId()).notNull().greaterThanZero(); - baseDataValidator.reset().parameter("transactionId").value(command.getTransactionId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("loanId").value(command.getLoanId()).notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter("transactionId").value(command.getTransactionId()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("transactionDate").value(command.getTransactionDate()).notNull(); baseDataValidator.reset().parameter("transactionAmount").value(command.getTransactionAmount()).notNull().positiveAmount(); baseDataValidator.reset().parameter("note").value(command.getNote()).notExceedingLengthOf(1000); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/CalculateLoanScheduleCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/CalculateLoanScheduleCommandValidator.java index c973ab8cd..045f2e6f7 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/CalculateLoanScheduleCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/CalculateLoanScheduleCommandValidator.java @@ -24,11 +24,11 @@ public class CalculateLoanScheduleCommandValidator { baseDataValidator.reset().parameter("principal").value(command.getPrincipal()).notNull().positiveAmount(); - baseDataValidator.reset().parameter("loanTermFrequency").value(command.getLoanTermFrequency()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("loanTermFrequency").value(command.getLoanTermFrequency()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("loanTermFrequencyType").value(command.getLoanTermFrequencyType()).notNull().inMinMaxRange(0, 4); - baseDataValidator.reset().parameter("numberOfRepayments").value(command.getNumberOfRepayments()).notNull().greaterThanZero(); - baseDataValidator.reset().parameter("repaymentEvery").value(command.getRepaymentEvery()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("numberOfRepayments").value(command.getNumberOfRepayments()).notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter("repaymentEvery").value(command.getRepaymentEvery()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("repaymentFrequencyType").value(command.getRepaymentFrequencyType()).notNull().inMinMaxRange(0, 3); // FIXME - this constraint doesnt really need to be here. should be possible to express loan term as say 12 months whilst also saying diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanAssembler.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanAssembler.java index c103d4e17..42e347894 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanAssembler.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanAssembler.java @@ -66,7 +66,7 @@ public class LoanAssembler { } Client client = this.clientRepository.findOne(command.getClientId()); - if (client == null) { + if (client == null || client.isDeleted()) { throw new ClientNotFoundException(command.getClientId()); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanStateTransitionCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanStateTransitionCommandValidator.java index c123fae83..ad479bcd4 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanStateTransitionCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanStateTransitionCommandValidator.java @@ -21,7 +21,7 @@ public class LoanStateTransitionCommandValidator { DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan.transition"); - baseDataValidator.reset().parameter("loanId").value(command.getLoanId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("loanId").value(command.getLoanId()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("eventDate").value(command.getEventDate()).notNull(); baseDataValidator.reset().parameter("note").value(command.getNote()).notExceedingLengthOf(1000); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanTransactionCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanTransactionCommandValidator.java index c3e9a7057..df76781d0 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanTransactionCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanTransactionCommandValidator.java @@ -22,7 +22,7 @@ public class LoanTransactionCommandValidator { DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan.transaction"); - baseDataValidator.reset().parameter("loanId").value(command.getLoanId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("loanId").value(command.getLoanId()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("transactionDate").value(command.getTransactionDate()).notNull(); baseDataValidator.reset().parameter("transactionAmount").value(command.getTransactionAmount()).notNull().positiveAmount(); baseDataValidator.reset().parameter("note").value(command.getNote()).notExceedingLengthOf(1000); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loanproduct/service/LoanProductCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/loanproduct/service/LoanProductCommandValidator.java index 889dbc47c..10bb1d329 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loanproduct/service/LoanProductCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loanproduct/service/LoanProductCommandValidator.java @@ -33,8 +33,8 @@ public class LoanProductCommandValidator { baseDataValidator.reset().parameter("repaymentFrequencyType").value(command.getRepaymentFrequencyType()).ignoreIfNull().notNull().inMinMaxRange(0, 3); - baseDataValidator.reset().parameter("repaymentEvery").value(command.getRepaymentEvery()).ignoreIfNull().notNull().greaterThanZero(); - baseDataValidator.reset().parameter("numberOfRepayments").value(command.getNumberOfRepayments()).ignoreIfNull().notNull().greaterThanZero(); + baseDataValidator.reset().parameter("repaymentEvery").value(command.getRepaymentEvery()).ignoreIfNull().notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter("numberOfRepayments").value(command.getNumberOfRepayments()).ignoreIfNull().notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("interestRatePerPeriod").value(command.getInterestRatePerPeriod()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("interestRateFrequencyType").value(command.getInterestRateFrequencyType()).ignoreIfNull().notNull().inMinMaxRange(0, 3); @@ -67,8 +67,8 @@ public class LoanProductCommandValidator { baseDataValidator.reset().parameter("inArrearsTolerance").value(command.getInArrearsTolerance()).notNull().zeroOrPositiveAmount(); baseDataValidator.reset().parameter("repaymentFrequencyType").value(command.getRepaymentFrequencyType()).notNull().inMinMaxRange(0, 3); - baseDataValidator.reset().parameter("repaymentEvery").value(command.getRepaymentEvery()).notNull().greaterThanZero(); - baseDataValidator.reset().parameter("numberOfRepayments").value(command.getNumberOfRepayments()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("repaymentEvery").value(command.getRepaymentEvery()).notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter("numberOfRepayments").value(command.getNumberOfRepayments()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("interestRatePerPeriod").value(command.getInterestRatePerPeriod()).notNull(); baseDataValidator.reset().parameter("interestRateFrequencyType").value(command.getInterestRateFrequencyType()).notNull().inMinMaxRange(0, 3); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/BranchMoneyTransferCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/BranchMoneyTransferCommandValidator.java index 90fdb8a46..cb7229c46 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/BranchMoneyTransferCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/BranchMoneyTransferCommandValidator.java @@ -22,8 +22,8 @@ public class BranchMoneyTransferCommandValidator { DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("office.money.transfer"); - baseDataValidator.reset().parameter("fromOfficeId").value(command.getFromOfficeId()).ignoreIfNull().greaterThanZero(); - baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).ignoreIfNull().greaterThanZero(); + baseDataValidator.reset().parameter("fromOfficeId").value(command.getFromOfficeId()).ignoreIfNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).ignoreIfNull().integerGreaterThanZero(); if (command.getFromOfficeId() == null && command.getToOfficeId() == null) { baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).notNull(); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/OfficeCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/OfficeCommandValidator.java index 20796021f..839a74635 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/OfficeCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/organisation/service/OfficeCommandValidator.java @@ -25,7 +25,7 @@ public class OfficeCommandValidator { baseDataValidator.reset().parameter("name").value(command.getName()).notBlank(); baseDataValidator.reset().parameter("openingDate").value(command.getOpeningDate()).notBlank(); baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).notExceedingLengthOf(100); - baseDataValidator.reset().parameter("parentId").value(command.getParentId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("parentId").value(command.getParentId()).notNull().integerGreaterThanZero(); if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); @@ -41,7 +41,7 @@ public class OfficeCommandValidator { baseDataValidator.reset().parameter("name").value(command.getName()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("openingDate").value(command.getOpeningDate()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100); - baseDataValidator.reset().parameter("parentId").value(command.getParentId()).ignoreIfNull().notNull().greaterThanZero(); + baseDataValidator.reset().parameter("parentId").value(command.getParentId()).ignoreIfNull().notNull().integerGreaterThanZero(); baseDataValidator.reset().anyOfNotNull(command.getName(), command.getOpeningDate(), command.getExternalId(), command.getParentId()); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java new file mode 100644 index 000000000..20069917f --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java @@ -0,0 +1,84 @@ +package org.mifosng.platform.saving.domain; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + +import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.client.domain.Client; +import org.mifosng.platform.currency.domain.MonetaryCurrency; +import org.mifosng.platform.currency.domain.Money; +import org.mifosng.platform.infrastructure.AbstractAuditableCustom; +import org.mifosng.platform.user.domain.AppUser; + +@Entity +@Table(name = "portfolio_deposit_account", uniqueConstraints = @UniqueConstraint(name="deposit_acc_external_id", columnNames = { "external_id" })) +public class DepositAccount extends AbstractAuditableCustom { + + @ManyToOne + @JoinColumn(name = "client_id", nullable = false) + private Client client; + +// @ManyToOne +// @JoinColumn(name = "product_id") +// private final DepositAccountProduct depositAccountProduct; + + @Column(name = "external_id") + private String externalId; + + @Embedded + private MonetaryCurrency currency; + + @Column(name = "deposit_amount", scale = 6, precision = 19, nullable = false) + private BigDecimal depositAmount; + + @Column(name = "maturity_interest_rate", scale = 6, precision = 19, nullable = false) + private BigDecimal interestRate; + + @Column(name = "tenure_months", nullable = false) + private Integer tenureInMonths; + + @Column(name = "pre_closure_interest_rate", scale = 6, precision = 19, nullable = false) + private BigDecimal preClosureInterestRate; + + @Column(name = "can_renew", nullable = false) + private boolean renewalAllowed; + + @Column(name = "can_pre_close", nullable = false) + private boolean preClosureAllowed; + + @Column(name = "is_deleted", nullable=false) + private boolean deleted = false; + + protected DepositAccount() { + // + } + + public DepositAccount(final Client client, final String externalId, final Money deposit, final BigDecimal interestRate, final Integer termInMonths) { + this.client = client; + this.externalId = externalId; + this.currency = deposit.getCurrency(); + this.depositAmount = deposit.getAmount(); + this.interestRate = interestRate; + this.tenureInMonths = termInMonths; + this.preClosureInterestRate = BigDecimal.ZERO; + } + + public boolean isDeleted() { + return deleted; + } + + public void delete() { + this.deleted = true; + } + + public void update(final DepositAccountCommand command) { + + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountRepository.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountRepository.java new file mode 100644 index 000000000..ad8856b09 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountRepository.java @@ -0,0 +1,10 @@ +package org.mifosng.platform.saving.domain; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface DepositAccountRepository extends + JpaRepository, + JpaSpecificationExecutor { + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java new file mode 100644 index 000000000..fc84e0cfb --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java @@ -0,0 +1,38 @@ +package org.mifosng.platform.savingproduct.service; + +import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.client.domain.Client; +import org.mifosng.platform.client.domain.ClientRepository; +import org.mifosng.platform.currency.domain.MonetaryCurrency; +import org.mifosng.platform.currency.domain.Money; +import org.mifosng.platform.exceptions.ClientNotFoundException; +import org.mifosng.platform.saving.domain.DepositAccount; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * An assembler for turning {@link DepositAccountCommand} into {@link DepositAccount}'s. + */ +@Service +public class DepositAccountAssembler { + + private final ClientRepository clientRepository; + + @Autowired + public DepositAccountAssembler(final ClientRepository clientRepository) { + this.clientRepository = clientRepository; + } + + public DepositAccount assembleFrom(final DepositAccountCommand command) { + + Client client = this.clientRepository.findOne(command.getClientId()); + if (client == null || client.isDeleted()) { + throw new ClientNotFoundException(command.getClientId()); + } + + MonetaryCurrency currency = new MonetaryCurrency(command.getCurrencyCode(), command.getDigitsAfterDecimal()); + Money deposit = Money.of(currency, command.getDepositAmount()); + + return new DepositAccount(client, command.getExternalId(), deposit, command.getInterestRate(), command.getTermInMonths()); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java new file mode 100644 index 000000000..8ac9dc8bd --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java @@ -0,0 +1,66 @@ +package org.mifosng.platform.savingproduct.service; + +import java.util.ArrayList; +import java.util.List; + +import org.mifosng.platform.DataValidatorBuilder; +import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.data.ApiParameterError; +import org.mifosng.platform.exceptions.PlatformApiDataValidationException; + +public class DepositAccountCommandValidator { + + private final DepositAccountCommand command; + + public DepositAccountCommandValidator(final DepositAccountCommand command) { + this.command=command; + } + + public void validateForUpdate() { + + List dataValidationErrors = new ArrayList(); + + DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("deposit.account"); + + baseDataValidator.reset().parameter("id").value(command.getId()).notNull(); + baseDataValidator.reset().parameter("clientId").value(command.getClientId()).ignoreIfNull().notNull(); + baseDataValidator.reset().parameter("productId").value(command.getProductId()).ignoreIfNull().notNull(); + baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100); + + baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).ignoreIfNull().notBlank(); + baseDataValidator.reset().parameter("digitsAfterDecimal").value(command.getDigitsAfterDecimal()).ignoreIfNull().notNull().inMinMaxRange(0, 6); + baseDataValidator.reset().parameter("depositAmount").value(command.getDepositAmount()).ignoreIfNull().notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter("interestRate").value(command.getInterestRate()).ignoreIfNull().notNull().zeroOrPositiveAmount(); + baseDataValidator.reset().parameter("termInMonths").value(command.getTermInMonths()).ignoreIfNull().notNull().integerGreaterThanZero(); + + baseDataValidator.reset().anyOfNotNull( + command.getClientId(), command.getProductId(), command.getExternalId(), + command.getCurrencyCode(),command.getDigitsAfterDecimal(), + command.getDepositAmount(), command.getInterestRate(),command.getTermInMonths()); + + if (!dataValidationErrors.isEmpty()) { + throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); + } + } + + public void validateForCreate() { + + List dataValidationErrors = new ArrayList(); + + DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("deposit.account"); + + baseDataValidator.reset().parameter("clientId").value(command.getClientId()).notNull(); + baseDataValidator.reset().parameter("productId").value(command.getProductId()).notNull(); + baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100); + + baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).notBlank(); + baseDataValidator.reset().parameter("digitsAfterDecimal").value(command.getDigitsAfterDecimal()).notNull().inMinMaxRange(0, 6); + baseDataValidator.reset().parameter("depositAmount").value(command.getDepositAmount()).notNull().zeroOrPositiveAmount(); + baseDataValidator.reset().parameter("interestRate").value(command.getInterestRate()).notNull().zeroOrPositiveAmount(); + baseDataValidator.reset().parameter("termInMonths").value(command.getTermInMonths()).notNull().integerGreaterThanZero(); + + if (!dataValidationErrors.isEmpty()) { + throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); + } + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountReadPlatformService.java new file mode 100644 index 000000000..79147ce94 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountReadPlatformService.java @@ -0,0 +1,14 @@ +package org.mifosng.platform.savingproduct.service; + +import java.util.Collection; + +import org.mifosng.platform.api.data.DepositAccountData; + +public interface DepositAccountReadPlatformService { + + Collection retrieveAllDepositAccounts(); + + DepositAccountData retrieveDepositAccount(Long accountId); + + DepositAccountData retrieveNewDepositAccountDetails(); +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountReadPlatformServiceImpl.java new file mode 100644 index 000000000..34ed85da2 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountReadPlatformServiceImpl.java @@ -0,0 +1,109 @@ +package org.mifosng.platform.savingproduct.service; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collection; + +import org.joda.time.DateTime; +import org.mifosng.platform.api.data.CurrencyData; +import org.mifosng.platform.api.data.DepositAccountData; +import org.mifosng.platform.exceptions.LoanProductNotFoundException; +import org.mifosng.platform.infrastructure.JdbcSupport; +import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource; +import org.mifosng.platform.security.PlatformSecurityContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Service; + +@Service +public class DepositAccountReadPlatformServiceImpl implements DepositAccountReadPlatformService { + + private final PlatformSecurityContext context; + private final JdbcTemplate jdbcTemplate; + + @Autowired + public DepositAccountReadPlatformServiceImpl(final PlatformSecurityContext context,final TenantAwareRoutingDataSource dataSource) { + this.context=context; + jdbcTemplate=new JdbcTemplate(dataSource); + } + + @Override + public Collection retrieveAllDepositAccounts() { + + this.context.authenticatedUser(); + + DepositAccountMapper mapper = new DepositAccountMapper(); + + String sql = "select " + mapper.schema() + " where da.is_deleted=0"; + + return this.jdbcTemplate.query(sql,mapper, new Object[]{}); + + } + + @Override + public DepositAccountData retrieveDepositAccount(final Long accountId) { + try{ + DepositAccountMapper mapper = new DepositAccountMapper(); + + String sql = "select " + mapper.schema() + + " where da.id = ? and da.is_deleted=0"; + + DepositAccountData productData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { accountId }); + + return productData; + } catch (EmptyResultDataAccessException e) { + throw new LoanProductNotFoundException(accountId); + } + } + + @Override + public DepositAccountData retrieveNewDepositAccountDetails() { + + DepositAccountData productData = new DepositAccountData(); + + return productData; + } + + private static final class DepositAccountMapper implements RowMapper { + + public String schema() { + return "da.id as id, da.client_id as clientId, da.product_id as productId," + + " da.currency_code as currencyCode, da.currency_digits as currencyDigits, " + + " da.deposit_amount as depositAmount, " + + " da.maturity_interest_rate as interestRate, " + + " da.created_date as createdon, da.lastmodified_date as modifiedon, " + + " c.firstname as firstname, c.lastname as lastname, " + + " curr.name as currencyName, curr.internationalized_name_code as currencyNameCode, curr.display_symbol as currencyDisplaySymbol" + + " from portfolio_deposit_account da join ref_currency curr on curr.code = da.currency_code join portfolio_client c on c.id = da.client_id"; + } + + @Override + public DepositAccountData mapRow(ResultSet rs, int rowNum) + throws SQLException { + + Long id = rs.getLong("id"); + Long clientId = rs.getLong("clientId"); + String clientName = rs.getString("firstname") + " " + rs.getString("lastname"); + Long productId = rs.getLong("productId"); + String productName = ""; + + String currencyCode = rs.getString("currencyCode"); + String currencyName = rs.getString("currencyName"); + String currencyNameCode = rs.getString("currencyNameCode"); + String currencyDisplaySymbol = rs.getString("currencyDisplaySymbol"); + Integer currencyDigits = JdbcSupport.getInteger(rs,"currencyDigits"); + CurrencyData currencyData = new CurrencyData(currencyCode,currencyName, currencyDigits, currencyDisplaySymbol,currencyNameCode); + + BigDecimal depositAmount = rs.getBigDecimal("depositAmount"); + BigDecimal interestRate = rs.getBigDecimal("interestRate"); + + DateTime createdOn = JdbcSupport.getDateTime(rs, "createdon"); + DateTime lastModifedOn = JdbcSupport.getDateTime(rs, "modifiedon"); + + return new DepositAccountData(createdOn, lastModifedOn, id, clientId, clientName, productId, productName, currencyData, depositAmount, interestRate); + } + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountWritePlatformService.java new file mode 100644 index 000000000..417ebeee3 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountWritePlatformService.java @@ -0,0 +1,17 @@ +package org.mifosng.platform.savingproduct.service; + +import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.data.EntityIdentifier; +import org.springframework.security.access.prepost.PreAuthorize; + +public interface DepositAccountWritePlatformService { + + @PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')") + EntityIdentifier createDepositAccount(DepositAccountCommand command); + + @PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')") + EntityIdentifier updateDepositAccount(DepositAccountCommand command); + + @PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')") + EntityIdentifier deleteDepositAccount(Long productId); +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java new file mode 100644 index 000000000..3c0c98853 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java @@ -0,0 +1,83 @@ +package org.mifosng.platform.savingproduct.service; + +import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.data.EntityIdentifier; +import org.mifosng.platform.exceptions.DepositAccountNotFoundException; +import org.mifosng.platform.exceptions.ProductNotFoundException; +import org.mifosng.platform.saving.domain.DepositAccount; +import org.mifosng.platform.saving.domain.DepositAccountRepository; +import org.mifosng.platform.security.PlatformSecurityContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements DepositAccountWritePlatformService { + + private final PlatformSecurityContext context; + private final DepositAccountRepository depositAccountRepository; + private final DepositAccountAssembler depositAccountAssembler; + + @Autowired + public DepositAccountWritePlatformServiceJpaRepositoryImpl( + final PlatformSecurityContext context, + final DepositAccountRepository depositAccountRepository, + final DepositAccountAssembler depositAccountAssembler) { + + this.context=context; + this.depositAccountRepository = depositAccountRepository; + this.depositAccountAssembler = depositAccountAssembler; + } + + @Transactional + @Override + public EntityIdentifier createDepositAccount(final DepositAccountCommand command) { + + this.context.authenticatedUser(); + + DepositAccountCommandValidator validator = new DepositAccountCommandValidator(command); + validator.validateForCreate(); + + final DepositAccount account = this.depositAccountAssembler.assembleFrom(command); + this.depositAccountRepository.save(account); + + return new EntityIdentifier(account.getId()); + } + + @Transactional + @Override + public EntityIdentifier updateDepositAccount(final DepositAccountCommand command) { + + this.context.authenticatedUser(); + + DepositAccountCommandValidator validator = new DepositAccountCommandValidator(command); + validator.validateForUpdate(); + + final DepositAccount account = this.depositAccountAssembler.assembleFrom(command); + if (account == null || account.isDeleted()) { + throw new DepositAccountNotFoundException(command.getId()); + } + + account.update(command); + this.depositAccountRepository.save(account); + + return new EntityIdentifier(Long.valueOf(1)); + } + + @Transactional + @Override + public EntityIdentifier deleteDepositAccount(final Long accountId) { + + this.context.authenticatedUser(); + + DepositAccount account = this.depositAccountRepository.findOne(accountId); + if (account==null || account.isDeleted()) { + throw new ProductNotFoundException(accountId); + } + + account.delete(); + this.depositAccountRepository.save(account); + + return new EntityIdentifier(accountId); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/UserCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/UserCommandValidator.java index 6519844e4..f1fea1174 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/UserCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/UserCommandValidator.java @@ -26,7 +26,7 @@ public class UserCommandValidator { baseDataValidator.reset().parameter("firstname").value(command.getFirstname()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("lastname").value(command.getLastname()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("email").value(command.getEmail()).ignoreIfNull().notBlank(); - baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).ignoreIfNull().notNull().greaterThanZero(); + baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).ignoreIfNull().notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("roles").value(command.getRoles()).ignoreIfNull().arrayNotEmpty(); baseDataValidator.reset().parameter("password").value(command.getPassword()).ignoreIfNull().notBlank(); @@ -51,7 +51,7 @@ public class UserCommandValidator { baseDataValidator.reset().parameter("firstname").value(command.getFirstname()).notBlank(); baseDataValidator.reset().parameter("lastname").value(command.getLastname()).notBlank(); baseDataValidator.reset().parameter("email").value(command.getEmail()).notBlank(); - baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().greaterThanZero(); + baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().integerGreaterThanZero(); baseDataValidator.reset().parameter("roles").value(command.getRoles()).arrayNotEmpty(); if (!dataValidationErrors.isEmpty()) {