diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ChargeDefinitionCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ChargeDefinitionCommandHandler.java index b61253597..f43a2d29c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ChargeDefinitionCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ChargeDefinitionCommandHandler.java @@ -6,9 +6,9 @@ import java.util.List; import org.joda.time.LocalDate; import org.mifosplatform.commands.domain.CommandSource; import org.mifosplatform.commands.service.ChangeDetectionService; -import org.mifosplatform.infrastructure.core.api.PortfolioCommandDeserializerService; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; +import org.mifosplatform.portfolio.charge.serialization.ChargeCommandFromCommandJsonDeserializer; import org.mifosplatform.portfolio.charge.service.ChargeWritePlatformService; import org.mifosplatform.portfolio.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException; import org.mifosplatform.useradministration.domain.AppUser; @@ -20,15 +20,16 @@ public class ChargeDefinitionCommandHandler implements CommandSourceHandler { private final PlatformSecurityContext context; private final ChangeDetectionService changeDetectionService; - private final PortfolioCommandDeserializerService commandDeserializerService; + private final ChargeCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer; private final ChargeWritePlatformService writePlatformService; @Autowired public ChargeDefinitionCommandHandler(final PlatformSecurityContext context, final ChangeDetectionService changeDetectionService, - final PortfolioCommandDeserializerService commandDeserializerService, final ChargeWritePlatformService writePlatformService) { + final ChargeCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer, + final ChargeWritePlatformService writePlatformService) { this.context = context; this.changeDetectionService = changeDetectionService; - this.commandDeserializerService = commandDeserializerService; + this.commandFromCommandJsonDeserializer = commandFromCommandJsonDeserializer; this.writePlatformService = writePlatformService; } @@ -41,8 +42,8 @@ public class ChargeDefinitionCommandHandler implements CommandSourceHandler { CommandSource commandSourceResult = commandSource.copy(); final Long resourceId = commandSource.resourceId(); - final ChargeDefinitionCommand command = this.commandDeserializerService.deserializeChargeDefinitionCommand(resourceId, - commandSourceResult.json(), false); + final ChargeDefinitionCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceId, + commandSourceResult.json()); if (commandSource.isCreate()) { try { @@ -58,9 +59,8 @@ public class ChargeDefinitionCommandHandler implements CommandSourceHandler { commandSource.resourceId(), commandSource.json()); commandSourceResult.updateJsonTo(jsonOfChangesOnly); - final ChargeDefinitionCommand changesOnly = this.commandDeserializerService.deserializeChargeDefinitionCommand(resourceId, - jsonOfChangesOnly, false); - + final ChargeDefinitionCommand changesOnly = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceId, + jsonOfChangesOnly); this.writePlatformService.updateCharge(changesOnly); commandSourceResult.markAsChecked(maker, asToday); @@ -85,7 +85,7 @@ public class ChargeDefinitionCommandHandler implements CommandSourceHandler { final AppUser checker = context.authenticatedUser(); Long resourceId = commandSourceResult.resourceId(); - final ChargeDefinitionCommand command = this.commandDeserializerService.deserializeChargeDefinitionCommand(resourceId, + final ChargeDefinitionCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceId, commandSourceResult.json(), true); if (commandSourceResult.isCreate()) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientCommandHandler.java index 61f34118a..d4479784a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientCommandHandler.java @@ -6,10 +6,10 @@ import java.util.List; import org.joda.time.LocalDate; import org.mifosplatform.commands.domain.CommandSource; import org.mifosplatform.commands.service.ChangeDetectionService; -import org.mifosplatform.infrastructure.core.api.PortfolioCommandDeserializerService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.client.command.ClientCommand; +import org.mifosplatform.portfolio.client.serialization.ClientCommandFromCommandJsonDeserializer; import org.mifosplatform.portfolio.client.service.ClientWritePlatformService; import org.mifosplatform.portfolio.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException; import org.mifosplatform.useradministration.domain.AppUser; @@ -21,16 +21,16 @@ public class ClientCommandHandler implements CommandSourceHandler { private final PlatformSecurityContext context; private final ChangeDetectionService changeDetectionService; + private final ClientCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer; private final ClientWritePlatformService clientWritePlatformService; - private final PortfolioCommandDeserializerService commandDeserializerService; @Autowired public ClientCommandHandler(final PlatformSecurityContext context, final ChangeDetectionService changeDetectionService, - final PortfolioCommandDeserializerService commandDeserializerService, + final ClientCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer, final ClientWritePlatformService clientWritePlatformService) { this.context = context; this.changeDetectionService = changeDetectionService; - this.commandDeserializerService = commandDeserializerService; + this.commandFromCommandJsonDeserializer = commandFromCommandJsonDeserializer; this.clientWritePlatformService = clientWritePlatformService; } @@ -40,8 +40,8 @@ public class ClientCommandHandler implements CommandSourceHandler { final AppUser maker = context.authenticatedUser(); final LocalDate asToday = new LocalDate(); - final ClientCommand command = this.commandDeserializerService.deserializeClientCommand(commandSource.resourceId(), - commandSource.json(), false); + final ClientCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(commandSource.resourceId(), + commandSource.json()); CommandSource commandSourceResult = commandSource.copy(); Long newResourceId = null; @@ -60,9 +60,8 @@ public class ClientCommandHandler implements CommandSourceHandler { commandSource.resourceId(), commandSource.json()); commandSourceResult.updateJsonTo(jsonOfChangesOnly); - final ClientCommand changesOnly = this.commandDeserializerService.deserializeClientCommand(commandSource.resourceId(), - jsonOfChangesOnly, false); - + final ClientCommand changesOnly = this.commandFromCommandJsonDeserializer.commandFromCommandJson( + commandSource.resourceId(), jsonOfChangesOnly); EntityIdentifier result = this.clientWritePlatformService.updateClientDetails(changesOnly); newResourceId = result.getEntityId(); @@ -88,8 +87,7 @@ public class ClientCommandHandler implements CommandSourceHandler { final AppUser checker = context.authenticatedUser(); - Long resourceId = null; - final ClientCommand command = this.commandDeserializerService.deserializeClientCommand(commandSourceResult.resourceId(), + final ClientCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(commandSourceResult.resourceId(), commandSourceResult.json(), true); if (commandSourceResult.isCreate()) { @@ -97,7 +95,7 @@ public class ClientCommandHandler implements CommandSourceHandler { "CREATE_CLIENT_CHECKER"); context.authenticatedUser().validateHasPermissionTo("CREATE_CLIENT_CHECKER", allowedPermissions); - resourceId = this.clientWritePlatformService.createClient(command); + Long resourceId = this.clientWritePlatformService.createClient(command); commandSourceResult.updateResourceId(resourceId); commandSourceResult.markAsChecked(checker, new LocalDate()); } else if (commandSourceResult.isUpdate()) { @@ -105,16 +103,14 @@ public class ClientCommandHandler implements CommandSourceHandler { "UPDATE_CLIENT_CHECKER"); context.authenticatedUser().validateHasPermissionTo("UPDATE_CLIENT_CHECKER", allowedPermissions); - EntityIdentifier result = this.clientWritePlatformService.updateClientDetails(command); - resourceId = result.getEntityId(); + this.clientWritePlatformService.updateClientDetails(command); commandSourceResult.markAsChecked(checker, new LocalDate()); } else if (commandSourceResult.isDelete()) { final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "PORTFOLIO_MANAGEMENT_SUPER_USER", "DELETE_CLIENT_CHECKER"); context.authenticatedUser().validateHasPermissionTo("DELETE_CLIENT_CHECKER", allowedPermissions); - EntityIdentifier result = this.clientWritePlatformService.deleteClient(command); - resourceId = result.getEntityId(); + this.clientWritePlatformService.deleteClient(command); commandSourceResult.markAsChecked(checker, new LocalDate()); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientIdentifierCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientIdentifierCommandHandler.java index ca080fae2..2a1427b0e 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientIdentifierCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/ClientIdentifierCommandHandler.java @@ -6,10 +6,9 @@ import java.util.List; import org.joda.time.LocalDate; import org.mifosplatform.commands.domain.CommandSource; import org.mifosplatform.commands.service.ChangeDetectionService; -import org.mifosplatform.infrastructure.core.api.PortfolioCommandDeserializerService; -import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; +import org.mifosplatform.portfolio.client.serialization.ClientIdentifierCommandFromCommandJsonDeserializer; import org.mifosplatform.portfolio.client.service.ClientWritePlatformService; import org.mifosplatform.portfolio.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException; import org.mifosplatform.useradministration.domain.AppUser; @@ -22,15 +21,15 @@ public class ClientIdentifierCommandHandler implements CommandSourceHandler { private final PlatformSecurityContext context; private final ChangeDetectionService changeDetectionService; private final ClientWritePlatformService clientWritePlatformService; - private final PortfolioCommandDeserializerService commandDeserializerService; + private final ClientIdentifierCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer; @Autowired public ClientIdentifierCommandHandler(final PlatformSecurityContext context, final ChangeDetectionService changeDetectionService, - final PortfolioCommandDeserializerService commandDeserializerService, + final ClientIdentifierCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer, final ClientWritePlatformService clientWritePlatformService) { this.context = context; this.changeDetectionService = changeDetectionService; - this.commandDeserializerService = commandDeserializerService; + this.commandFromCommandJsonDeserializer = commandFromCommandJsonDeserializer; this.clientWritePlatformService = clientWritePlatformService; } @@ -40,15 +39,13 @@ public class ClientIdentifierCommandHandler implements CommandSourceHandler { final AppUser maker = context.authenticatedUser(); final LocalDate asToday = new LocalDate(); - final ClientIdentifierCommand command = this.commandDeserializerService.deserializeClientIdentifierCommand( - commandSource.resourceId(), null, commandSource.json(), false); - + final ClientIdentifierCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(commandSource.resourceId(), + commandSource.json()); CommandSource commandSourceResult = commandSource.copy(); - Long newResourceId = null; if (commandSource.isCreate()) { try { - newResourceId = this.clientWritePlatformService.addClientIdentifier(command); + Long newResourceId = this.clientWritePlatformService.addClientIdentifier(command); commandSourceResult.markAsChecked(maker, asToday); commandSourceResult.updateResourceId(newResourceId); } catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) { @@ -60,20 +57,17 @@ public class ClientIdentifierCommandHandler implements CommandSourceHandler { commandSource.resourceId(), commandSource.json()); commandSourceResult.updateJsonTo(jsonOfChangesOnly); - final ClientIdentifierCommand changesOnly = this.commandDeserializerService.deserializeClientIdentifierCommand( - commandSource.resourceId(), null, jsonOfChangesOnly, false); - - EntityIdentifier result = this.clientWritePlatformService.updateClientIdentifier(changesOnly); - newResourceId = result.getEntityId(); + final ClientIdentifierCommand changesOnly = this.commandFromCommandJsonDeserializer.commandFromCommandJson( + commandSource.resourceId(), jsonOfChangesOnly); + this.clientWritePlatformService.updateClientIdentifier(changesOnly); commandSourceResult.markAsChecked(maker, asToday); } catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) { // swallow this rollback transaction by design } } else if (commandSource.isDelete()) { try { - EntityIdentifier result = this.clientWritePlatformService.deleteClientIdentifier(command); - newResourceId = result.getEntityId(); + this.clientWritePlatformService.deleteClientIdentifier(command); commandSourceResult.markAsChecked(maker, asToday); } catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) { // swallow this rollback transaction by design @@ -88,16 +82,14 @@ public class ClientIdentifierCommandHandler implements CommandSourceHandler { final AppUser checker = context.authenticatedUser(); - Long resourceId = null; - final ClientIdentifierCommand command = this.commandDeserializerService.deserializeClientIdentifierCommand( - commandSourceResult.resourceId(), null, commandSourceResult.json(), true); - + final ClientIdentifierCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson( + commandSourceResult.resourceId(), commandSourceResult.json(), true); if (commandSourceResult.isCreate()) { final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "PORTFOLIO_MANAGEMENT_SUPER_USER", "CREATE_CLIENTIDENTIFIER_CHECKER"); context.authenticatedUser().validateHasPermissionTo("CREATE_CLIENTIDENTIFIER_CHECKER", allowedPermissions); - resourceId = this.clientWritePlatformService.addClientIdentifier(command); + Long resourceId = this.clientWritePlatformService.addClientIdentifier(command); commandSourceResult.updateResourceId(resourceId); commandSourceResult.markAsChecked(checker, new LocalDate()); } else if (commandSourceResult.isUpdate()) { @@ -105,16 +97,14 @@ public class ClientIdentifierCommandHandler implements CommandSourceHandler { "UPDATE_CLIENTIDENTIFIER_CHECKER"); context.authenticatedUser().validateHasPermissionTo("UPDATE_CLIENTIDENTIFIER_CHECKER", allowedPermissions); - EntityIdentifier result = this.clientWritePlatformService.updateClientIdentifier(command); - resourceId = result.getEntityId(); + this.clientWritePlatformService.updateClientIdentifier(command); commandSourceResult.markAsChecked(checker, new LocalDate()); } else if (commandSourceResult.isDelete()) { final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "PORTFOLIO_MANAGEMENT_SUPER_USER", "DELETE_CLIENTIDENTIFIER_CHECKER"); context.authenticatedUser().validateHasPermissionTo("DELETE_CLIENTIDENTIFIER_CHECKER", allowedPermissions); - EntityIdentifier result = this.clientWritePlatformService.deleteClientIdentifier(command); - resourceId = result.getEntityId(); + this.clientWritePlatformService.deleteClientIdentifier(command); commandSourceResult.markAsChecked(checker, new LocalDate()); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/LoanProductCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/LoanProductCommandHandler.java index 0d273f28a..067d05228 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/LoanProductCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/handler/LoanProductCommandHandler.java @@ -7,11 +7,11 @@ import org.joda.time.LocalDate; import org.mifosplatform.commands.domain.CommandSource; import org.mifosplatform.commands.exception.UnsupportedCommandException; import org.mifosplatform.commands.service.ChangeDetectionService; -import org.mifosplatform.infrastructure.core.api.PortfolioCommandDeserializerService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException; import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; +import org.mifosplatform.portfolio.loanproduct.serialization.LoanProductCommandFromCommandJsonDeserializer; import org.mifosplatform.portfolio.loanproduct.service.LoanProductWritePlatformService; import org.mifosplatform.useradministration.domain.AppUser; import org.springframework.beans.factory.annotation.Autowired; @@ -22,15 +22,16 @@ public class LoanProductCommandHandler implements CommandSourceHandler { private final PlatformSecurityContext context; private final ChangeDetectionService changeDetectionService; - private final PortfolioCommandDeserializerService commandDeserializerService; + private final LoanProductCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer; private final LoanProductWritePlatformService writePlatformService; @Autowired public LoanProductCommandHandler(final PlatformSecurityContext context, final ChangeDetectionService changeDetectionService, - final PortfolioCommandDeserializerService commandDeserializerService, final LoanProductWritePlatformService writePlatformService) { + final LoanProductCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer, + final LoanProductWritePlatformService writePlatformService) { this.context = context; this.changeDetectionService = changeDetectionService; - this.commandDeserializerService = commandDeserializerService; + this.commandFromCommandJsonDeserializer = commandFromCommandJsonDeserializer; this.writePlatformService = writePlatformService; } @@ -41,9 +42,7 @@ public class LoanProductCommandHandler implements CommandSourceHandler { final LocalDate asToday = new LocalDate(); final Long resourceId = commandSource.resourceId(); - final LoanProductCommand command = this.commandDeserializerService.deserializeLoanProductCommand(resourceId, commandSource.json(), - false); - + final LoanProductCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceId, commandSource.json()); CommandSource commandSourceResult = commandSource.copy(); if (commandSource.isCreate()) { @@ -60,9 +59,8 @@ public class LoanProductCommandHandler implements CommandSourceHandler { commandSource.resourceId(), commandSource.json()); commandSourceResult.updateJsonTo(jsonOfChangesOnly); - final LoanProductCommand changesOnly = this.commandDeserializerService.deserializeLoanProductCommand(resourceId, - jsonOfChangesOnly, false); - + final LoanProductCommand changesOnly = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceId, + jsonOfChangesOnly); this.writePlatformService.updateLoanProduct(changesOnly); commandSourceResult.markAsChecked(maker, asToday); @@ -80,7 +78,7 @@ public class LoanProductCommandHandler implements CommandSourceHandler { final AppUser checker = context.authenticatedUser(); Long resourceId = commandSourceResult.resourceId(); - final LoanProductCommand command = this.commandDeserializerService.deserializeLoanProductCommand(resourceId, + final LoanProductCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceId, commandSourceResult.json(), true); if (commandSourceResult.isCreate()) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java index 041bf6e9b..1268d047a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java @@ -1,9 +1,7 @@ package org.mifosplatform.infrastructure.core.api; import org.mifosplatform.organisation.staff.command.BulkTransferLoanOfficerCommand; -import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; import org.mifosplatform.portfolio.client.command.ClientCommand; -import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; import org.mifosplatform.portfolio.client.command.NoteCommand; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.group.command.GroupCommand; @@ -13,7 +11,6 @@ import org.mifosplatform.portfolio.loanaccount.command.LoanChargeCommand; import org.mifosplatform.portfolio.loanaccount.command.LoanStateTransitionCommand; import org.mifosplatform.portfolio.loanaccount.command.LoanTransactionCommand; import org.mifosplatform.portfolio.loanaccount.gaurantor.command.GuarantorCommand; -import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; import org.mifosplatform.portfolio.savingsaccount.command.SavingAccountCommand; import org.mifosplatform.portfolio.savingsaccountproduct.command.SavingProductCommand; import org.mifosplatform.portfolio.savingsdepositaccount.command.DepositAccountCommand; @@ -25,16 +22,10 @@ import org.mifosplatform.portfolio.savingsdepositproduct.command.DepositProductC public interface PortfolioApiDataConversionService { - ChargeDefinitionCommand convertApiRequestJsonToChargeDefinitionCommand(Long resourceIdentifier, String json); - - LoanProductCommand convertApiRequestJsonToLoanProductCommand(Long resourceIdentifier, String json); - SavingProductCommand convertJsonToSavingProductCommand(Long resourceIdentifier, String json); DepositProductCommand convertJsonToDepositProductCommand(Long resourceIdentifier, String json); - ClientCommand convertApiRequestJsonToClientCommand(Long resourceIdentifier, String json); - ClientData convertInternalJsonFormatToClientDataChange(Long clientId, String json); ClientCommand detectChanges(Long resourceId, String baseJson, String workingJson); @@ -67,8 +58,6 @@ public interface PortfolioApiDataConversionService { DepositAccountWithdrawInterestCommand convertJsonToDepositAccountWithdrawInterestCommand(Long resourceIdentifier, String json); - ClientIdentifierCommand convertApiRequestJsonToClientIdentifierCommand(Long resourceIdentifier, Long clientId, String json); - SavingAccountCommand convertJsonToSavingAccountCommand(Long resourceIdentifier, String json); GuarantorCommand convertJsonToGuarantorCommand(Long resourceIdentifier, Long loanId, String json); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java index 1a546b42f..6e514e55e 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java @@ -23,11 +23,10 @@ import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidation import org.mifosplatform.infrastructure.core.exception.UnsupportedParameterException; import org.mifosplatform.infrastructure.core.serialization.JsonParserHelper; import org.mifosplatform.organisation.staff.command.BulkTransferLoanOfficerCommand; -import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; import org.mifosplatform.portfolio.client.command.ClientCommand; -import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; import org.mifosplatform.portfolio.client.command.NoteCommand; import org.mifosplatform.portfolio.client.data.ClientData; +import org.mifosplatform.portfolio.client.serialization.ClientCommandFromCommandJsonDeserializer; import org.mifosplatform.portfolio.group.command.GroupCommand; import org.mifosplatform.portfolio.loanaccount.command.AdjustLoanTransactionCommand; import org.mifosplatform.portfolio.loanaccount.command.LoanApplicationCommand; @@ -35,7 +34,6 @@ import org.mifosplatform.portfolio.loanaccount.command.LoanChargeCommand; import org.mifosplatform.portfolio.loanaccount.command.LoanStateTransitionCommand; import org.mifosplatform.portfolio.loanaccount.command.LoanTransactionCommand; import org.mifosplatform.portfolio.loanaccount.gaurantor.command.GuarantorCommand; -import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; import org.mifosplatform.portfolio.savingsaccount.command.SavingAccountCommand; import org.mifosplatform.portfolio.savingsaccountproduct.command.SavingProductCommand; import org.mifosplatform.portfolio.savingsdepositaccount.command.DepositAccountCommand; @@ -58,62 +56,17 @@ import com.google.gson.reflect.TypeToken; @Service public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataConversionService { - /** - * Used in places where the support json of the api request is not any - * different to that of the json of a serialized command object. - */ - private final PortfolioCommandDeserializerService portfolioCommandDeserializerService; - /** * Google-gson class for converting to and from json. */ private final Gson gsonConverter; - /** - * Google-gson class for parsing json. - */ - // private final JsonParser parser; + private final ClientCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer; @Autowired - public PortfolioApiDataConversionServiceImpl(final PortfolioCommandDeserializerService portfolioCommandDeserializerService) { - this.portfolioCommandDeserializerService = portfolioCommandDeserializerService; + public PortfolioApiDataConversionServiceImpl(final ClientCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer) { + this.commandFromCommandJsonDeserializer = commandFromCommandJsonDeserializer; this.gsonConverter = new Gson(); - // this.parser = new JsonParser(); - } - - @Override - public ChargeDefinitionCommand convertApiRequestJsonToChargeDefinitionCommand(final Long resourceIdentifier, final String json) { - if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } - - final Type typeOfMap = new TypeToken>() {}.getType(); - final Map requestMap = gsonConverter.fromJson(json, typeOfMap); - - final Set supportedParams = new HashSet(Arrays.asList("name", "amount", "locale", "currencyCode", - "currencyOptions", "chargeAppliesTo", "chargeTimeType", "chargeCalculationType", "chargeCalculationTypeOptions", "penalty", - "active")); - - checkForUnsupportedParameters(requestMap, supportedParams); - - final JsonParserHelper helper = new JsonParserHelper(); - JsonParser parser = new JsonParser(); - final JsonElement element = parser.parse(json); - - final Set parametersPassedInRequest = new HashSet(); - - final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); - final String currencyCode = helper.extractStringNamed("currencyCode", element, parametersPassedInRequest); - final BigDecimal amount = helper.extractBigDecimalWithLocaleNamed("amount", element.getAsJsonObject(), parametersPassedInRequest); - - final Integer chargeTimeType = helper.extractIntegerWithLocaleNamed("chargeTimeType", element, parametersPassedInRequest); - final Integer chargeAppliesTo = helper.extractIntegerWithLocaleNamed("chargeAppliesTo", element, parametersPassedInRequest); - final Integer chargeCalculationType = helper.extractIntegerWithLocaleNamed("chargeCalculationType", element, - parametersPassedInRequest); - - final Boolean penalty = helper.extractBooleanNamed("penalty", element, parametersPassedInRequest); - final Boolean active = helper.extractBooleanNamed("active", element, parametersPassedInRequest); - - return new ChargeDefinitionCommand(parametersPassedInRequest, false, resourceIdentifier, name, amount, currencyCode, - chargeTimeType, chargeAppliesTo, chargeCalculationType, penalty, active); } @Override @@ -293,38 +246,10 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo return differenceExists; } - @Override - public ClientCommand convertApiRequestJsonToClientCommand(final Long resourceIdentifier, final String json) { - if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } - - final Type typeOfMap = new TypeToken>() {}.getType(); - final Map requestMap = gsonConverter.fromJson(json, typeOfMap); - - final Set supportedParams = new HashSet(Arrays.asList("id", "externalId", "firstname", "lastname", - "clientOrBusinessName", "officeId", "joiningDate", "locale", "dateFormat")); - - checkForUnsupportedParameters(requestMap, supportedParams); - - final JsonParser parser = new JsonParser(); - final JsonElement element = parser.parse(json); - final JsonParserHelper helper = new JsonParserHelper(); - final Set parametersPassedInRequest = new HashSet(); - - final Long officeId = helper.extractLongNamed("officeId", element, parametersPassedInRequest); - final String externalId = helper.extractStringNamed("externalId", element, parametersPassedInRequest); - final String firstname = helper.extractStringNamed("firstname", element, parametersPassedInRequest); - final String lastname = helper.extractStringNamed("lastname", element, parametersPassedInRequest); - final String clientOrBusinessName = helper.extractStringNamed("clientOrBusinessName", element, parametersPassedInRequest); - final LocalDate joiningDate = helper.extractLocalDateNamed("joiningDate", element, parametersPassedInRequest); - - return new ClientCommand(parametersPassedInRequest, resourceIdentifier, externalId, firstname, lastname, clientOrBusinessName, - officeId, joiningDate, false); - } - @Override public ClientData convertInternalJsonFormatToClientDataChange(final Long resourceIdentifier, final String json) { - final ClientCommand command = this.portfolioCommandDeserializerService.deserializeClientCommand(resourceIdentifier, json, false); + final ClientCommand command = this.commandFromCommandJsonDeserializer.commandFromCommandJson(resourceIdentifier, json); return ClientData.dataChangeInstance(resourceIdentifier, command.getOfficeId(), command.getExternalId(), command.getFirstname(), command.getLastname(), command.getClientOrBusinessName(), command.getJoiningDate()); @@ -368,78 +293,6 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo return new GroupCommand(modifiedParameters, resourceIdentifier, externalId, name, officeId, clientMembers); } - @Override - public LoanProductCommand convertApiRequestJsonToLoanProductCommand(final Long resourceIdentifier, final String json) { - - if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } - - final Type typeOfMap = new TypeToken>() {}.getType(); - final Map requestMap = gsonConverter.fromJson(json, typeOfMap); - - final Set supportedParams = new HashSet(Arrays.asList("name", "description", "fundId", - "transactionProcessingStrategyId", "currencyCode", "digitsAfterDecimal", "principal", "inArrearsTolerance", - "interestRatePerPeriod", "repaymentEvery", "numberOfRepayments", "repaymentFrequencyType", "interestRateFrequencyType", - "amortizationType", "interestType", "interestCalculationPeriodType", "charges", "locale")); - - checkForUnsupportedParameters(requestMap, supportedParams); - - final JsonParser parser = new JsonParser(); - final JsonElement element = parser.parse(json); - final JsonParserHelper helper = new JsonParserHelper(); - - final Set modifiedParameters = new HashSet(); - final String name = helper.extractStringNamed("name", element, modifiedParameters); - final String description = helper.extractStringNamed("description", element, modifiedParameters); - final Long fundId = helper.extractLongNamed("fundId", element, modifiedParameters); - - final Long transactionProcessingStrategyId = helper - .extractLongNamed("transactionProcessingStrategyId", element, modifiedParameters); - - final String currencyCode = helper.extractStringNamed("currencyCode", element, modifiedParameters); - final Integer digitsAfterDecimal = helper.extractIntegerWithLocaleNamed("digitsAfterDecimal", element, modifiedParameters); - - final BigDecimal principal = helper.extractBigDecimalWithLocaleNamed("principal", element, modifiedParameters); - final BigDecimal inArrearsTolerance = helper.extractBigDecimalWithLocaleNamed("inArrearsTolerance", element, modifiedParameters); - final BigDecimal interestRatePerPeriod = helper.extractBigDecimalWithLocaleNamed("interestRatePerPeriod", element, - modifiedParameters); - final Integer repaymentEvery = helper.extractIntegerWithLocaleNamed("repaymentEvery", element, modifiedParameters); - final Integer numberOfRepayments = helper.extractIntegerWithLocaleNamed("numberOfRepayments", element, modifiedParameters); - final Integer repaymentFrequencyType = helper.extractIntegerWithLocaleNamed("repaymentFrequencyType", element, modifiedParameters); - - // FIXME - A - LoanProduct - KW - should loan term fields be part of - // product also as are basic part of 'loan terms' - // final Integer loanTermFrequency = - // helper.extractIntegerNamed("loanTermFrequency", element, - // modifiedParameters); - // final Integer loanTermFrequencyType = - // helper.extractIntegerNamed("loanTermFrequencyType", element, - // modifiedParameters); - final Integer interestRateFrequencyType = helper.extractIntegerWithLocaleNamed("interestRateFrequencyType", element, - modifiedParameters); - final Integer amortizationType = helper.extractIntegerWithLocaleNamed("amortizationType", element, modifiedParameters); - final Integer interestType = helper.extractIntegerWithLocaleNamed("interestType", element, modifiedParameters); - final Integer interestCalculationPeriodType = helper.extractIntegerWithLocaleNamed("interestCalculationPeriodType", element, - modifiedParameters); - - String[] charges = null; - if (element.isJsonObject()) { - JsonObject object = element.getAsJsonObject(); - if (object.has("charges")) { - modifiedParameters.add("charges"); - JsonArray array = object.get("charges").getAsJsonArray(); - charges = new String[array.size()]; - for (int i = 0; i < array.size(); i++) { - charges[i] = array.get(i).getAsString(); - } - } - } - - return new LoanProductCommand(modifiedParameters, false, resourceIdentifier, name, description, fundId, - transactionProcessingStrategyId, currencyCode, digitsAfterDecimal, principal, inArrearsTolerance, numberOfRepayments, - repaymentEvery, interestRatePerPeriod, repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, - interestCalculationPeriodType, charges); - } - @Override public LoanApplicationCommand convertApiRequestJsonToLoanApplicationCommand(final Long resourceIdentifier, final String json) { @@ -1337,21 +1190,6 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo return new DepositAccountWithdrawInterestCommand(resourceIdentifier, amount, note); } - @Override - public ClientIdentifierCommand convertApiRequestJsonToClientIdentifierCommand(final Long resourceIdentifier, final Long clientId, - 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", "documentTypeId", "documentKey", "description")); - - checkForUnsupportedParameters(requestMap, supportedParams); - - return this.portfolioCommandDeserializerService.deserializeClientIdentifierCommand(resourceIdentifier, clientId, json, false); - } - @Override public SavingAccountCommand convertJsonToSavingAccountCommand(Long resourceIdentifier, String json) { @@ -1365,7 +1203,7 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo "digitsAfterDecimal", "savingsDepositAmountPerPeriod", "recurringInterestRate", "savingInterestRate", "tenure", "commencementDate", "locale", "dateFormat", "isLockinPeriodAllowed", "lockinPeriod", "lockinPeriodType", "savingProductType", "tenureType", "frequency", "interestType", "interestCalculationMethod", "minimumBalanceForWithdrawal", - "isPartialDepositAllowed","payEvery")); + "isPartialDepositAllowed", "payEvery")); checkForUnsupportedParameters(requestMap, supportedParams); Set modifiedParameters = new HashSet(); @@ -1396,7 +1234,7 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo return new SavingAccountCommand(modifiedParameters, resourceIdentifier, clientId, productId, externalId, currencyCode, digitsAfterDecimalValue, savingsDepositAmount, recurringInterestRate, savingInterestRate, tenure, commencementDate, savingProductType, tenureType, frequency, interestType, minimumBalanceForWithdrawal, interestCalculationMethod, - isLockinPeriodAllowed, isPartialDepositAllowed, lockinPeriod, lockinPeriodType,payEvery); + isLockinPeriodAllowed, isPartialDepositAllowed, lockinPeriod, lockinPeriodType, payEvery); } @Override diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioCommandDeerializerServiceGoogleGson.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioCommandDeerializerServiceGoogleGson.java deleted file mode 100644 index 2ed5bc8fa..000000000 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioCommandDeerializerServiceGoogleGson.java +++ /dev/null @@ -1,165 +0,0 @@ -package org.mifosplatform.infrastructure.core.api; - -import java.math.BigDecimal; -import java.util.HashSet; -import java.util.Locale; -import java.util.Set; - -import org.apache.commons.lang.StringUtils; -import org.joda.time.LocalDate; -import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; -import org.mifosplatform.infrastructure.core.serialization.JsonParserHelper; -import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; -import org.mifosplatform.portfolio.client.command.ClientCommand; -import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; -import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; -import org.springframework.stereotype.Service; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -/** - * Implementation of {@link PortfolioCommandDeserializerService} that - * de-serializes JSON of commands into Java object representation using - * google-gson. - */ -@Service -public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCommandDeserializerService { - - private final JsonParser parser; - - public PortfolioCommandDeerializerServiceGoogleGson() { - parser = new JsonParser(); - } - - @Override - public ChargeDefinitionCommand deserializeChargeDefinitionCommand(final Long chargeDefinitionId, final String commandAsJson, - final boolean makerCheckerApproval) { - - if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } - - final JsonParserHelper helper = new JsonParserHelper(); - final JsonElement element = parser.parse(commandAsJson); - - final Set parametersPassedInRequest = new HashSet(); - - final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); - final String currencyCode = helper.extractStringNamed("currencyCode", element, parametersPassedInRequest); - final BigDecimal amount = helper.extractBigDecimalNamed("amount", element.getAsJsonObject(), Locale.US, parametersPassedInRequest); - - final Integer chargeTimeType = helper.extractIntegerNamed("chargeTimeType", element, Locale.US, parametersPassedInRequest); - final Integer chargeAppliesTo = helper.extractIntegerNamed("chargeAppliesTo", element, Locale.US, parametersPassedInRequest); - final Integer chargeCalculationType = helper.extractIntegerNamed("chargeCalculationType", element, Locale.US, - parametersPassedInRequest); - - final Boolean penalty = helper.extractBooleanNamed("penalty", element, parametersPassedInRequest); - final Boolean active = helper.extractBooleanNamed("active", element, parametersPassedInRequest); - - return new ChargeDefinitionCommand(parametersPassedInRequest, makerCheckerApproval, chargeDefinitionId, name, amount, currencyCode, - chargeTimeType, chargeAppliesTo, chargeCalculationType, penalty, active); - } - - @Override - public ClientCommand deserializeClientCommand(final Long clientId, final String commandAsJson, final boolean makerCheckerApproval) { - - if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } - - final JsonParserHelper helper = new JsonParserHelper(); - final JsonElement element = parser.parse(commandAsJson); - - final Set parametersPassedInRequest = new HashSet(); - - final Long officeId = helper.extractLongNamed("officeId", element, parametersPassedInRequest); - final String externalId = helper.extractStringNamed("externalId", element, parametersPassedInRequest); - final String firstname = helper.extractStringNamed("firstname", element, parametersPassedInRequest); - final String lastname = helper.extractStringNamed("lastname", element, parametersPassedInRequest); - final String clientOrBusinessName = helper.extractStringNamed("clientOrBusinessName", element, parametersPassedInRequest); - final LocalDate joiningDate = helper.extractLocalDateAsArrayNamed("joiningDate", element, parametersPassedInRequest); - - return new ClientCommand(parametersPassedInRequest, clientId, externalId, firstname, lastname, clientOrBusinessName, officeId, - joiningDate, makerCheckerApproval); - } - - @Override - public LoanProductCommand deserializeLoanProductCommand(final Long loanProductId, final String commandAsJson, - final boolean makerCheckerApproval) { - - if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } - - final JsonParserHelper helper = new JsonParserHelper(); - final JsonElement element = parser.parse(commandAsJson); - - final Set parametersPassedInRequest = new HashSet(); - - final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); - final String description = helper.extractStringNamed("description", element, parametersPassedInRequest); - final Long fundId = helper.extractLongNamed("fundId", element, parametersPassedInRequest); - - final Long transactionProcessingStrategyId = helper.extractLongNamed("transactionProcessingStrategyId", element, - parametersPassedInRequest); - - final String currencyCode = helper.extractStringNamed("currencyCode", element, parametersPassedInRequest); - final Integer digitsAfterDecimal = helper.extractIntegerNamed("digitsAfterDecimal", element, Locale.US, parametersPassedInRequest); - - final BigDecimal principal = helper.extractBigDecimalNamed("principal", element.getAsJsonObject(), Locale.US, - parametersPassedInRequest); - final BigDecimal inArrearsTolerance = helper.extractBigDecimalNamed("inArrearsTolerance", element.getAsJsonObject(), Locale.US, - parametersPassedInRequest); - final BigDecimal interestRatePerPeriod = helper.extractBigDecimalNamed("interestRatePerPeriod", element.getAsJsonObject(), - Locale.US, parametersPassedInRequest); - final Integer repaymentEvery = helper.extractIntegerNamed("repaymentEvery", element, Locale.US, parametersPassedInRequest); - final Integer numberOfRepayments = helper.extractIntegerNamed("numberOfRepayments", element, Locale.US, parametersPassedInRequest); - final Integer repaymentFrequencyType = helper.extractIntegerNamed("repaymentFrequencyType", element, Locale.US, - parametersPassedInRequest); - final Integer interestRateFrequencyType = helper.extractIntegerNamed("interestRateFrequencyType", element, Locale.US, - parametersPassedInRequest); - final Integer amortizationType = helper.extractIntegerNamed("amortizationType", element, Locale.US, parametersPassedInRequest); - final Integer interestType = helper.extractIntegerNamed("interestType", element, Locale.US, parametersPassedInRequest); - final Integer interestCalculationPeriodType = helper.extractIntegerNamed("interestCalculationPeriodType", element, Locale.US, - parametersPassedInRequest); - - String[] charges = null; - if (element.isJsonObject()) { - JsonObject object = element.getAsJsonObject(); - if (object.has("charges")) { - parametersPassedInRequest.add("charges"); - JsonArray array = object.get("charges").getAsJsonArray(); - charges = new String[array.size()]; - for (int i = 0; i < array.size(); i++) { - charges[i] = array.get(i).getAsString(); - } - } - } - - return new LoanProductCommand(parametersPassedInRequest, makerCheckerApproval, loanProductId, name, description, fundId, - transactionProcessingStrategyId, currencyCode, digitsAfterDecimal, principal, inArrearsTolerance, numberOfRepayments, - repaymentEvery, interestRatePerPeriod, repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, - interestCalculationPeriodType, charges); - } - - @Override - public ClientIdentifierCommand deserializeClientIdentifierCommand(final Long clientIdentifierId, final Long clientId, - final String commandAsJson, final boolean makerCheckerApproval) { - - if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } - - final JsonParserHelper helper = new JsonParserHelper(); - final JsonElement element = parser.parse(commandAsJson); - - final Set parametersPassedInRequest = new HashSet(); - - Long actualClientId = clientId; - final Long clientIdInternal = helper.extractLongNamed("clientId", element, parametersPassedInRequest); - if (clientIdInternal != null) { - actualClientId = clientIdInternal; - } - final Long documentTypeId = helper.extractLongNamed("documentTypeId", element, parametersPassedInRequest); - final String documentKey = helper.extractStringNamed("documentKey", element, parametersPassedInRequest); - final String documentDescription = helper.extractStringNamed("documentDescription", element, parametersPassedInRequest); - - return new ClientIdentifierCommand(parametersPassedInRequest, makerCheckerApproval, clientIdentifierId, actualClientId, - documentTypeId, documentKey, documentDescription); - } -} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioCommandDeserializerService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioCommandDeserializerService.java deleted file mode 100644 index 028593fc9..000000000 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioCommandDeserializerService.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.mifosplatform.infrastructure.core.api; - -import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; -import org.mifosplatform.portfolio.client.command.ClientCommand; -import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; -import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; - -/** - * Service for de-serializing JSON for a command into the platforms internal - * Java object representation of the command. - * - *

- * Known implementations: - *

- * - * @see PortfolioCommandDeerializerServiceGoogleGson - */ -public interface PortfolioCommandDeserializerService { - - ChargeDefinitionCommand deserializeChargeDefinitionCommand(Long chargeDefinitionId, String commandAsJson, boolean makerCheckerApproval); - - ClientCommand deserializeClientCommand(Long clientId, String commandAsJson, boolean makerCheckerApproval); - - LoanProductCommand deserializeLoanProductCommand(Long loanProductId, String commandAsJson, boolean makerCheckerApproval); - - ClientIdentifierCommand deserializeClientIdentifierCommand(Long clientIdentifierId, Long clientId, String commandAsJson, - boolean makerCheckerApproval); -} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/FromJsonHelper.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/FromJsonHelper.java index 680e264b8..e875df3fe 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/FromJsonHelper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/FromJsonHelper.java @@ -93,4 +93,14 @@ public class FromJsonHelper { final Set parametersPassedInRequest) { return helperDelegator.extractBigDecimalNamed(parameterName, element.getAsJsonObject(), Locale.US, parametersPassedInRequest); } + + public Integer extractIntegerWithLocaleNamed(final String parameterName, final JsonElement element, + final Set parametersPassedInRequest) { + return helperDelegator.extractIntegerWithLocaleNamed(parameterName, element.getAsJsonObject(), parametersPassedInRequest); + } + + public Integer extractIntegerNamed(final String parameterName, final JsonElement element, + final Set parametersPassedInRequest) { + return helperDelegator.extractIntegerNamed(parameterName, element.getAsJsonObject(), Locale.US, parametersPassedInRequest); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/api/ChargesApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/api/ChargesApiResource.java index b51f13233..5b84add6f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/api/ChargesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/api/ChargesApiResource.java @@ -20,14 +20,12 @@ import javax.ws.rs.core.UriInfo; import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; -import org.mifosplatform.infrastructure.core.api.PortfolioApiDataConversionService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; -import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; -import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; import org.mifosplatform.portfolio.charge.data.ChargeData; +import org.mifosplatform.portfolio.charge.serialization.ChargeDefinitionCommandFromApiJsonDeserializer; import org.mifosplatform.portfolio.charge.service.ChargeReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; @@ -38,38 +36,32 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class ChargesApiResource { - private final Set CHARGES_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "name", "amount", "currency", - "penalty", "active", "chargeAppliesTo", "chargeTimeType", "chargeCalculationType", "chargeCalculationTypeOptions", + private final Set CHARGES_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "name", "amount", "currency", "penalty", + "active", "chargeAppliesTo", "chargeTimeType", "chargeCalculationType", "chargeCalculationTypeOptions", "chargeAppliesToOptions", "chargeTimeTypeOptions", "currencyOptions")); - + private final String resourceNameForPermissions = "CHARGE"; - + private final PlatformSecurityContext context; private final ChargeReadPlatformService readPlatformService; private final DefaultToApiJsonSerializer toApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; - - private final PortfolioApiDataConversionService apiDataConversionService; - private final CommandSerializer commandSerializerService; + private final ChargeDefinitionCommandFromApiJsonDeserializer fromApiJsonDeserializer; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; - + @Autowired - public ChargesApiResource(final PlatformSecurityContext context, - final ChargeReadPlatformService readPlatformService, - final DefaultToApiJsonSerializer toApiJsonSerializer, - final ApiRequestParameterHelper apiRequestParameterHelper, - final PortfolioApiDataConversionService apiDataConversionService, - final CommandSerializer commandSerializerService, + public ChargesApiResource(final PlatformSecurityContext context, final ChargeReadPlatformService readPlatformService, + final ChargeDefinitionCommandFromApiJsonDeserializer fromApiJsonDeserializer, + final DefaultToApiJsonSerializer toApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { this.context = context; this.readPlatformService = readPlatformService; + this.fromApiJsonDeserializer = fromApiJsonDeserializer; this.toApiJsonSerializer = toApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; - this.apiDataConversionService = apiDataConversionService; - this.commandSerializerService = commandSerializerService; this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; } - + @GET @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @@ -92,7 +84,7 @@ public class ChargesApiResource { context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions); final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - + ChargeData charge = this.readPlatformService.retrieveCharge(chargeId); if (settings.isTemplate()) { ChargeData templateData = this.readPlatformService.retrieveNewChargeDetails(); @@ -120,14 +112,14 @@ public class ChargesApiResource { @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public String createCharge(final String apiRequestBodyAsJson) { - + final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "ORGANISATION_ADMINISTRATION_SUPER_USER", "CREATE_CHARGE"); context.authenticatedUser().validateHasPermissionTo("CREATE_CHARGE", allowedPermissions); - final ChargeDefinitionCommand command = this.apiDataConversionService.convertApiRequestJsonToChargeDefinitionCommand(null, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); - - final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("CREATE", "charges", null, commandSerializedAsJson); + final String commandSerializedAsJson = this.fromApiJsonDeserializer.serializedCommandJsonFromApiJson(apiRequestBodyAsJson); + + final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("CREATE", "charges", null, + commandSerializedAsJson); return this.toApiJsonSerializer.serialize(result); } @@ -137,14 +129,15 @@ public class ChargesApiResource { @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public String updateCharge(@PathParam("chargeId") final Long chargeId, final String apiRequestBodyAsJson) { - + final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "ORGANISATION_ADMINISTRATION_SUPER_USER", "UPDATE_CHARGE"); context.authenticatedUser().validateHasPermissionTo("UPDATE_CHARGE", allowedPermissions); - - final ChargeDefinitionCommand command = this.apiDataConversionService.convertApiRequestJsonToChargeDefinitionCommand(chargeId, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); - - final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("UPDATE", "charges", chargeId, commandSerializedAsJson); + + final String commandSerializedAsJson = this.fromApiJsonDeserializer + .serializedCommandJsonFromApiJson(chargeId, apiRequestBodyAsJson); + + final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("UPDATE", "charges", chargeId, + commandSerializedAsJson); return this.toApiJsonSerializer.serialize(result); } @@ -157,7 +150,7 @@ public class ChargesApiResource { final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "ORGANISATION_ADMINISTRATION_SUPER_USER", "DELETE_CHARGE"); context.authenticatedUser().validateHasPermissionTo("DELETE_CHARGE", allowedPermissions); - + final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("DELETE", "charges", chargeId, "{}"); return this.toApiJsonSerializer.serialize(result); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/command/ChargeDefinitionCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/command/ChargeDefinitionCommand.java index bf99152f4..43d292dca 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/command/ChargeDefinitionCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/command/ChargeDefinitionCommand.java @@ -17,30 +17,19 @@ public class ChargeDefinitionCommand { private final String name; private final BigDecimal amount; private final String currencyCode; - private final Integer chargeTimeType; private final Integer chargeAppliesTo; private final Integer chargeCalculationType; + private final Boolean penalty; + private final Boolean active; - private final boolean penalty; - private final boolean active; - - private final transient Set modifiedParameters; + private final transient Set parametersPassedInRequest; private final transient boolean makerCheckerApproval; private final transient Long id; - - public ChargeDefinitionCommand( - final Set modifiedParameters, - final boolean makerCheckerApproval, - final Long id, - final String name, - final BigDecimal amount, - final String currencyCode, - final Integer chargeTimeType, - final Integer chargeAppliesTo, - final Integer chargeCalculationType, - final boolean penalty, - final boolean active) { + + public ChargeDefinitionCommand(final Set parametersPassedInRequest, final boolean makerCheckerApproval, final Long id, + final String name, final BigDecimal amount, final String currencyCode, final Integer chargeTimeType, + final Integer chargeAppliesTo, final Integer chargeCalculationType, final Boolean penalty, final Boolean active) { this.makerCheckerApproval = makerCheckerApproval; this.id = id; this.name = name; @@ -49,7 +38,7 @@ public class ChargeDefinitionCommand { this.chargeTimeType = chargeTimeType; this.chargeAppliesTo = chargeAppliesTo; this.chargeCalculationType = chargeCalculationType; - this.modifiedParameters = modifiedParameters; + this.parametersPassedInRequest = parametersPassedInRequest; this.penalty = penalty; this.active = active; } @@ -67,7 +56,7 @@ public class ChargeDefinitionCommand { } public Set getModifiedParameters() { - return modifiedParameters; + return parametersPassedInRequest; } public String getCurrencyCode() { @@ -85,51 +74,51 @@ public class ChargeDefinitionCommand { public Integer getChargeTimeType() { return chargeTimeType; } - - public boolean isPenalty() { - return penalty; - } - - public boolean isFee() { - return !penalty; - } - public boolean isActive() { + public boolean isPenalty() { + return penalty; + } + + public boolean isFee() { + return !penalty; + } + + public boolean isActive() { return active; } - public boolean isNameChanged(){ - return this.modifiedParameters.contains("name"); + public boolean isNameChanged() { + return this.parametersPassedInRequest.contains("name"); } - public boolean isAmountChanged(){ - return this.modifiedParameters.contains("amount"); + public boolean isAmountChanged() { + return this.parametersPassedInRequest.contains("amount"); } - public boolean isCurrencyCodeChanged(){ - return this.modifiedParameters.contains("currencyCode"); + public boolean isCurrencyCodeChanged() { + return this.parametersPassedInRequest.contains("currencyCode"); } - public boolean isChargeTimeTypeChanged(){ - return this.modifiedParameters.contains("chargeTimeType"); + public boolean isChargeTimeTypeChanged() { + return this.parametersPassedInRequest.contains("chargeTimeType"); } - public boolean isChargeAppliesToChanged(){ - return this.modifiedParameters.contains("chargeAppliesTo"); + public boolean isChargeAppliesToChanged() { + return this.parametersPassedInRequest.contains("chargeAppliesTo"); } - public boolean isChargeCalculationTypeChanged(){ - return this.modifiedParameters.contains("chargeCalculationType"); + public boolean isChargeCalculationTypeChanged() { + return this.parametersPassedInRequest.contains("chargeCalculationType"); } - public boolean isPenaltyChanged(){ - return this.modifiedParameters.contains("penalty"); + public boolean isPenaltyChanged() { + return this.parametersPassedInRequest.contains("penalty"); } - - public boolean isActiveChanged(){ - return this.modifiedParameters.contains("active"); + + public boolean isActiveChanged() { + return this.parametersPassedInRequest.contains("active"); } - + public boolean isApprovedByChecker() { return this.makerCheckerApproval; } @@ -145,14 +134,13 @@ public class ChargeDefinitionCommand { baseDataValidator.reset().parameter("chargeAppliesTo").value(this.chargeAppliesTo).notNull().inMinMaxRange(1, 1); baseDataValidator.reset().parameter("chargeTimeType").value(this.chargeTimeType).notNull().inMinMaxRange(1, 2); baseDataValidator.reset().parameter("chargeCalculationType").value(this.chargeCalculationType).notNull().inMinMaxRange(1, 4); - + if (this.penalty && this.chargeTimeType.equals(Integer.valueOf(1))) { - // FIXME - KW - cannot have penalty charge at time of disbursement + // FIXME - KW - cannot have penalty charge at time of disbursement } - if (!dataValidationErrors.isEmpty()) { - throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); - } + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", + "Validation errors exist.", dataValidationErrors); } } public void validateForUpdate() { @@ -166,10 +154,10 @@ public class ChargeDefinitionCommand { baseDataValidator.reset().parameter("currencyCode").value(this.currencyCode).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("chargeAppliesTo").value(this.chargeAppliesTo).ignoreIfNull().notNull().inMinMaxRange(1, 1); baseDataValidator.reset().parameter("chargeTimeType").value(this.chargeTimeType).ignoreIfNull().notNull().inMinMaxRange(1, 2); - baseDataValidator.reset().parameter("chargeCalculationType").value(this.chargeCalculationType).ignoreIfNull().notNull().inMinMaxRange(1, 4); + baseDataValidator.reset().parameter("chargeCalculationType").value(this.chargeCalculationType).ignoreIfNull().notNull() + .inMinMaxRange(1, 4); - if (!dataValidationErrors.isEmpty()) { - throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); - } + 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/mifosplatform/portfolio/charge/serialization/ChargeCommandFromCommandJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/serialization/ChargeCommandFromCommandJsonDeserializer.java new file mode 100644 index 000000000..59a61a484 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/serialization/ChargeCommandFromCommandJsonDeserializer.java @@ -0,0 +1,59 @@ +package org.mifosplatform.portfolio.charge.serialization; + +import java.math.BigDecimal; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; +import org.mifosplatform.infrastructure.core.serialization.FromCommandJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; + +@Component +public final class ChargeCommandFromCommandJsonDeserializer implements FromCommandJsonDeserializer { + + private final FromJsonHelper fromJsonHelper; + + @Autowired + public ChargeCommandFromCommandJsonDeserializer(final FromJsonHelper fromApiJsonHelper) { + this.fromJsonHelper = fromApiJsonHelper; + } + + @Override + public ChargeDefinitionCommand commandFromCommandJson(final String commandAsJson) { + return commandFromCommandJson(null, commandAsJson); + } + + @Override + public ChargeDefinitionCommand commandFromCommandJson(final Long chargeDefinitionId, final String commandAsJson) { + return commandFromCommandJson(chargeDefinitionId, commandAsJson, false); + } + + @Override + public ChargeDefinitionCommand commandFromCommandJson(final Long chargeDefinitionId, final String commandAsJson, + final boolean makerCheckerApproval) { + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromJsonHelper.parse(commandAsJson); + + final String name = fromJsonHelper.extractStringNamed("name", element, parametersPassedInRequest); + final String currencyCode = fromJsonHelper.extractStringNamed("currencyCode", element, parametersPassedInRequest); + final BigDecimal amount = fromJsonHelper.extractBigDecimalNamed("amount", element.getAsJsonObject(), parametersPassedInRequest); + final Integer chargeTimeType = fromJsonHelper.extractIntegerNamed("chargeTimeType", element, parametersPassedInRequest); + final Integer chargeAppliesTo = fromJsonHelper.extractIntegerNamed("chargeAppliesTo", element, parametersPassedInRequest); + final Integer chargeCalculationType = fromJsonHelper.extractIntegerNamed("chargeCalculationType", element, + parametersPassedInRequest); + final Boolean penalty = fromJsonHelper.extractBooleanNamed("penalty", element, parametersPassedInRequest); + final Boolean active = fromJsonHelper.extractBooleanNamed("active", element, parametersPassedInRequest); + + return new ChargeDefinitionCommand(parametersPassedInRequest, makerCheckerApproval, chargeDefinitionId, name, amount, currencyCode, + chargeTimeType, chargeAppliesTo, chargeCalculationType, penalty, active); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/serialization/ChargeDefinitionCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/serialization/ChargeDefinitionCommandFromApiJsonDeserializer.java new file mode 100644 index 000000000..797bf2824 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/serialization/ChargeDefinitionCommandFromApiJsonDeserializer.java @@ -0,0 +1,90 @@ +package org.mifosplatform.portfolio.charge.serialization; + +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; +import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand; +import org.mifosplatform.portfolio.fund.command.FundCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; + +/** + * Implementation of {@link FromApiJsonDeserializer} for {@link FundCommand}'s. + */ +@Component +public final class ChargeDefinitionCommandFromApiJsonDeserializer implements FromApiJsonDeserializer { + + /** + * The parameters supported for this command. + */ + private final Set supportedParameters = new HashSet(Arrays.asList("name", "amount", "locale", "currencyCode", + "currencyOptions", "chargeAppliesTo", "chargeTimeType", "chargeCalculationType", "chargeCalculationTypeOptions", "penalty", + "active")); + + private final FromJsonHelper fromApiJsonHelper; + private final CommandSerializer commandSerializerService; + + @Autowired + public ChargeDefinitionCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper, + final CommandSerializer commandSerializerService) { + this.fromApiJsonHelper = fromApiJsonHelper; + this.commandSerializerService = commandSerializerService; + } + + @Override + public ChargeDefinitionCommand commandFromApiJson(final String json) { + return commandFromApiJson(null, json); + } + + @Override + public ChargeDefinitionCommand commandFromApiJson(final Long chargeId, final String json) { + + final Type typeOfMap = new TypeToken>() {}.getType(); + + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromApiJsonHelper.parse(json); + + final String name = fromApiJsonHelper.extractStringNamed("name", element, parametersPassedInRequest); + final String currencyCode = fromApiJsonHelper.extractStringNamed("currencyCode", element, parametersPassedInRequest); + final BigDecimal amount = fromApiJsonHelper.extractBigDecimalWithLocaleNamed("amount", element.getAsJsonObject(), + parametersPassedInRequest); + + final Integer chargeTimeType = fromApiJsonHelper + .extractIntegerWithLocaleNamed("chargeTimeType", element, parametersPassedInRequest); + final Integer chargeAppliesTo = fromApiJsonHelper.extractIntegerWithLocaleNamed("chargeAppliesTo", element, + parametersPassedInRequest); + final Integer chargeCalculationType = fromApiJsonHelper.extractIntegerWithLocaleNamed("chargeCalculationType", element, + parametersPassedInRequest); + + final Boolean penalty = fromApiJsonHelper.extractBooleanNamed("penalty", element, parametersPassedInRequest); + final Boolean active = fromApiJsonHelper.extractBooleanNamed("active", element, parametersPassedInRequest); + + return new ChargeDefinitionCommand(parametersPassedInRequest, false, chargeId, name, amount, currencyCode, chargeTimeType, + chargeAppliesTo, chargeCalculationType, penalty, active); + } + + @Override + public String serializedCommandJsonFromApiJson(final String json) { + final ChargeDefinitionCommand command = commandFromApiJson(json); + return this.commandSerializerService.serializeCommandToJson(command); + } + + @Override + public String serializedCommandJsonFromApiJson(final Long roleId, final String json) { + final ChargeDefinitionCommand command = commandFromApiJson(roleId, json); + return this.commandSerializerService.serializeCommandToJson(command); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientIdentifiersApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientIdentifiersApiResource.java index 6181c5329..f4090e81e 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientIdentifiersApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientIdentifiersApiResource.java @@ -22,16 +22,14 @@ import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformSer import org.mifosplatform.infrastructure.codes.data.CodeValueData; import org.mifosplatform.infrastructure.codes.service.CodeValueReadPlatformService; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; -import org.mifosplatform.infrastructure.core.api.PortfolioApiDataConversionService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; -import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; -import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.client.data.ClientIdentifierData; import org.mifosplatform.portfolio.client.exception.DuplicateClientIdentifierException; +import org.mifosplatform.portfolio.client.serialization.ClientIdentifierCommandFromApiJsonDeserializer; import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; @@ -50,26 +48,24 @@ public class ClientIdentifiersApiResource { private final PlatformSecurityContext context; private final ClientReadPlatformService clientReadPlatformService; private final CodeValueReadPlatformService codeValueReadPlatformService; + private final ClientIdentifierCommandFromApiJsonDeserializer commandFromApiJsonDeserializer; private final DefaultToApiJsonSerializer toApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; - private final PortfolioApiDataConversionService apiDataConversionService; - private final CommandSerializer commandSerializerService; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; @Autowired public ClientIdentifiersApiResource(final PlatformSecurityContext context, final ClientReadPlatformService readPlatformService, final CodeValueReadPlatformService codeValueReadPlatformService, + final ClientIdentifierCommandFromApiJsonDeserializer commandFromApiJsonDeserializer, final DefaultToApiJsonSerializer toApiJsonSerializer, - final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioApiDataConversionService apiDataConversionService, - final CommandSerializer commandSerializerService, + final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { this.context = context; this.clientReadPlatformService = readPlatformService; this.codeValueReadPlatformService = codeValueReadPlatformService; + this.commandFromApiJsonDeserializer = commandFromApiJsonDeserializer; this.toApiJsonSerializer = toApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; - this.apiDataConversionService = apiDataConversionService; - this.commandSerializerService = commandSerializerService; this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; } @@ -111,9 +107,8 @@ public class ClientIdentifiersApiResource { "CREATE_CLIENTIDENTIFIER"); context.authenticatedUser().validateHasPermissionTo("CREATE_CLIENTIDENTIFIER", allowedPermissions); - final ClientIdentifierCommand command = this.apiDataConversionService.convertApiRequestJsonToClientIdentifierCommand(null, - clientId, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); + final String commandSerializedAsJson = this.commandFromApiJsonDeserializer.serializedCommandJsonFromApiJsonWithSubResource( + apiRequestBodyAsJson, clientId); final String resourceUrl = "clients/" + clientId + "/identifiers"; final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("CREATE", resourceUrl, null, @@ -165,9 +160,8 @@ public class ClientIdentifiersApiResource { "UPDATE_CLIENTIDENTIFIER"); context.authenticatedUser().validateHasPermissionTo("UPDATE_CLIENTIDENTIFIER", allowedPermissions); - final ClientIdentifierCommand command = this.apiDataConversionService.convertApiRequestJsonToClientIdentifierCommand( - clientIdentifierId, clientId, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); + final String commandSerializedAsJson = this.commandFromApiJsonDeserializer.serializedCommandJsonFromApiJsonWithSubResource( + clientIdentifierId, apiRequestBodyAsJson, clientId); final String resourceUrl = "clients/" + clientId + "/identifiers"; final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("UPDATE", resourceUrl, diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java index 616c746c9..471673bde 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java @@ -34,18 +34,17 @@ import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerServi import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.core.domain.Base64EncodedImage; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; -import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; import org.mifosplatform.infrastructure.core.service.FileUtils; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.office.data.OfficeLookup; import org.mifosplatform.organisation.office.service.OfficeReadPlatformService; -import org.mifosplatform.portfolio.client.command.ClientCommand; import org.mifosplatform.portfolio.client.command.NoteCommand; import org.mifosplatform.portfolio.client.data.ClientAccountSummaryCollectionData; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.client.data.NoteData; import org.mifosplatform.portfolio.client.exception.ImageNotFoundException; +import org.mifosplatform.portfolio.client.serialization.ClientCommandFromApiJsonDeserializer; import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; import org.mifosplatform.portfolio.client.service.ClientWritePlatformService; import org.slf4j.Logger; @@ -73,10 +72,10 @@ public class ClientsApiResource { private final ClientReadPlatformService clientReadPlatformService; private final ClientWritePlatformService clientWritePlatformService; private final OfficeReadPlatformService officeReadPlatformService; + private final ClientCommandFromApiJsonDeserializer fromApiJsonDeserializer; private final DefaultToApiJsonSerializer toApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; private final PortfolioApiDataConversionService apiDataConversionService; - private final CommandSerializer commandSerializerService; private final PortfolioApiJsonSerializerService apiJsonSerializerService; private final PortfolioCommandsReadPlatformService commandSourceReadPlatformService; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; @@ -84,8 +83,9 @@ public class ClientsApiResource { @Autowired public ClientsApiResource(final PlatformSecurityContext context, final ClientReadPlatformService readPlatformService, final ClientWritePlatformService clientWritePlatformService, final OfficeReadPlatformService officeReadPlatformService, + final ClientCommandFromApiJsonDeserializer fromApiJsonDeserializer, final DefaultToApiJsonSerializer toApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, - final PortfolioApiDataConversionService apiDataConversionService, final CommandSerializer commandSerializerService, + final PortfolioApiDataConversionService apiDataConversionService, final PortfolioApiJsonSerializerService apiJsonSerializerService, final PortfolioCommandsReadPlatformService commandsReadPlatformService, final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { @@ -93,10 +93,10 @@ public class ClientsApiResource { this.clientReadPlatformService = readPlatformService; this.clientWritePlatformService = clientWritePlatformService; this.officeReadPlatformService = officeReadPlatformService; + this.fromApiJsonDeserializer = fromApiJsonDeserializer; this.toApiJsonSerializer = toApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; this.apiDataConversionService = apiDataConversionService; - this.commandSerializerService = commandSerializerService; this.apiJsonSerializerService = apiJsonSerializerService; this.commandSourceReadPlatformService = commandsReadPlatformService; this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; @@ -243,8 +243,7 @@ public class ClientsApiResource { final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "PORTFOLIO_MANAGEMENT_SUPER_USER", "CREATE_CLIENT"); context.authenticatedUser().validateHasPermissionTo("CREATE_CLIENT", allowedPermissions); - final ClientCommand command = this.apiDataConversionService.convertApiRequestJsonToClientCommand(null, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); + final String commandSerializedAsJson = this.fromApiJsonDeserializer.serializedCommandJsonFromApiJson(apiRequestBodyAsJson); final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("CREATE", "clients", null, commandSerializedAsJson); @@ -261,9 +260,8 @@ public class ClientsApiResource { final List allowedPermissions = Arrays.asList("ALL_FUNCTIONS", "PORTFOLIO_MANAGEMENT_SUPER_USER", "UPDATE_CLIENT"); context.authenticatedUser().validateHasPermissionTo("UPDATE_CLIENT", allowedPermissions); - final ClientCommand command = this.apiDataConversionService.convertApiRequestJsonToClientCommand(clientId, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); - + final String commandSerializedAsJson = this.fromApiJsonDeserializer.serializedCommandJsonFromApiJson(clientId, apiRequestBodyAsJson); + final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("UPDATE", "clients", clientId, commandSerializedAsJson); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/command/ClientIdentifierCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/command/ClientIdentifierCommand.java index 93485c7fb..bb83dd159 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/command/ClientIdentifierCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/command/ClientIdentifierCommand.java @@ -18,6 +18,10 @@ public class ClientIdentifierCommand { private final transient boolean makerCheckerApproval; private final transient Long id; + public static ClientIdentifierCommand clientSubResource(final ClientIdentifierCommand command, final Long providedClientId) { + return new ClientIdentifierCommand(command.modifiedParameters, command.makerCheckerApproval, command.id, providedClientId, command.documentTypeId, command.documentKey, command.description); + } + public ClientIdentifierCommand(final Set modifiedParameters, final boolean makerCheckerApproval, final Long id, final Long clientId, final Long documentTypeId, final String documentKey, final String description) { this.modifiedParameters = modifiedParameters; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientCommandFromApiJsonDeserializer.java new file mode 100644 index 000000000..d3a975a38 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientCommandFromApiJsonDeserializer.java @@ -0,0 +1,80 @@ +package org.mifosplatform.portfolio.client.serialization; + +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.joda.time.LocalDate; +import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; +import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.client.command.ClientCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; + +/** + * Implementation of {@link FromApiJsonDeserializer} for {@link ClientCommand} + * 's. + */ +@Component +public final class ClientCommandFromApiJsonDeserializer implements FromApiJsonDeserializer { + + /** + * The parameters supported for this command. + */ + private final Set supportedParameters = new HashSet(Arrays.asList("id", "externalId", "firstname", "lastname", + "clientOrBusinessName", "officeId", "joiningDate", "locale", "dateFormat")); + + private final FromJsonHelper fromApiJsonHelper; + private final CommandSerializer commandSerializerService; + + @Autowired + public ClientCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper, final CommandSerializer commandSerializerService) { + this.fromApiJsonHelper = fromApiJsonHelper; + this.commandSerializerService = commandSerializerService; + } + + @Override + public ClientCommand commandFromApiJson(final String json) { + return commandFromApiJson(null, json); + } + + @Override + public ClientCommand commandFromApiJson(final Long clientId, final String json) { + + final Type typeOfMap = new TypeToken>() {}.getType(); + + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromApiJsonHelper.parse(json); + final Long officeId = fromApiJsonHelper.extractLongNamed("officeId", element, parametersPassedInRequest); + final String externalId = fromApiJsonHelper.extractStringNamed("externalId", element, parametersPassedInRequest); + final String firstname = fromApiJsonHelper.extractStringNamed("firstname", element, parametersPassedInRequest); + final String lastname = fromApiJsonHelper.extractStringNamed("lastname", element, parametersPassedInRequest); + final String clientOrBusinessName = fromApiJsonHelper + .extractStringNamed("clientOrBusinessName", element, parametersPassedInRequest); + final LocalDate joiningDate = fromApiJsonHelper.extractLocalDateNamed("joiningDate", element, parametersPassedInRequest); + + return new ClientCommand(parametersPassedInRequest, clientId, externalId, firstname, lastname, clientOrBusinessName, officeId, + joiningDate, false); + } + + @Override + public String serializedCommandJsonFromApiJson(final String json) { + final ClientCommand command = commandFromApiJson(json); + return this.commandSerializerService.serializeCommandToJson(command); + } + + @Override + public String serializedCommandJsonFromApiJson(final Long fundId, final String json) { + final ClientCommand command = commandFromApiJson(fundId, json); + return this.commandSerializerService.serializeCommandToJson(command); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientCommandFromCommandJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientCommandFromCommandJsonDeserializer.java new file mode 100644 index 000000000..2b0fa1f36 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientCommandFromCommandJsonDeserializer.java @@ -0,0 +1,54 @@ +package org.mifosplatform.portfolio.client.serialization; + +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.joda.time.LocalDate; +import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; +import org.mifosplatform.infrastructure.core.serialization.FromCommandJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.client.command.ClientCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; + +@Component +public final class ClientCommandFromCommandJsonDeserializer implements FromCommandJsonDeserializer { + + private final FromJsonHelper fromJsonHelper; + + @Autowired + public ClientCommandFromCommandJsonDeserializer(final FromJsonHelper fromApiJsonHelper) { + this.fromJsonHelper = fromApiJsonHelper; + } + + @Override + public ClientCommand commandFromCommandJson(final String commandAsJson) { + return commandFromCommandJson(null, commandAsJson); + } + + @Override + public ClientCommand commandFromCommandJson(final Long clientId, final String commandAsJson) { + return commandFromCommandJson(clientId, commandAsJson, false); + } + + @Override + public ClientCommand commandFromCommandJson(final Long clientId, final String commandAsJson, final boolean makerCheckerApproval) { + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromJsonHelper.parse(commandAsJson); + final Long officeId = fromJsonHelper.extractLongNamed("officeId", element, parametersPassedInRequest); + final String externalId = fromJsonHelper.extractStringNamed("externalId", element, parametersPassedInRequest); + final String firstname = fromJsonHelper.extractStringNamed("firstname", element, parametersPassedInRequest); + final String lastname = fromJsonHelper.extractStringNamed("lastname", element, parametersPassedInRequest); + final String clientOrBusinessName = fromJsonHelper.extractStringNamed("clientOrBusinessName", element, parametersPassedInRequest); + final LocalDate joiningDate = fromJsonHelper.extractLocalDateAsArrayNamed("joiningDate", element, parametersPassedInRequest); + + return new ClientCommand(parametersPassedInRequest, clientId, externalId, firstname, lastname, clientOrBusinessName, officeId, + joiningDate, makerCheckerApproval); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientIdentifierCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientIdentifierCommandFromApiJsonDeserializer.java new file mode 100644 index 000000000..d9343c1aa --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientIdentifierCommandFromApiJsonDeserializer.java @@ -0,0 +1,82 @@ +package org.mifosplatform.portfolio.client.serialization; + +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; +import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.client.command.ClientCommand; +import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.reflect.TypeToken; + +/** + * Implementation of {@link FromApiJsonDeserializer} for {@link ClientCommand} + * 's. + */ +@Component +public final class ClientIdentifierCommandFromApiJsonDeserializer implements FromApiJsonDeserializer { + + /** + * The parameters supported for this command. + */ + private final Set supportedParameters = new HashSet(Arrays.asList("clientId", "documentTypeId", "documentKey", + "description")); + + private final FromJsonHelper fromApiJsonHelper; + private final CommandSerializer commandSerializerService; + + private final ClientIdentifierCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer; + + @Autowired + public ClientIdentifierCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper, + final ClientIdentifierCommandFromCommandJsonDeserializer commandFromCommandJsonDeserializer, + final CommandSerializer commandSerializerService) { + this.fromApiJsonHelper = fromApiJsonHelper; + this.commandFromCommandJsonDeserializer = commandFromCommandJsonDeserializer; + this.commandSerializerService = commandSerializerService; + } + + @Override + public ClientIdentifierCommand commandFromApiJson(final String json) { + return commandFromApiJson(null, json); + } + + @Override + public ClientIdentifierCommand commandFromApiJson(final Long clientId, final String json) { + + final Type typeOfMap = new TypeToken>() {}.getType(); + + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + + return this.commandFromCommandJsonDeserializer.commandFromCommandJson(clientId, json); + } + + @Override + public String serializedCommandJsonFromApiJson(final String json) { + final ClientIdentifierCommand command = commandFromApiJson(json); + return this.commandSerializerService.serializeCommandToJson(command); + } + + @Override + public String serializedCommandJsonFromApiJson(final Long clientIdentifierId, final String json) { + final ClientIdentifierCommand command = commandFromApiJson(clientIdentifierId, json); + return this.commandSerializerService.serializeCommandToJson(command); + } + + public String serializedCommandJsonFromApiJsonWithSubResource(final String json, final Long clientId) { + return serializedCommandJsonFromApiJsonWithSubResource(null, json, clientId); + } + + public String serializedCommandJsonFromApiJsonWithSubResource(final Long clientIdentifierId, final String json, final Long clientId) { + final ClientIdentifierCommand command = commandFromApiJson(clientIdentifierId, json); + final ClientIdentifierCommand commandWithSubResource = ClientIdentifierCommand.clientSubResource(command, clientId); + return this.commandSerializerService.serializeCommandToJson(commandWithSubResource); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientIdentifierCommandFromCommandJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientIdentifierCommandFromCommandJsonDeserializer.java new file mode 100644 index 000000000..828d4dec6 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/serialization/ClientIdentifierCommandFromCommandJsonDeserializer.java @@ -0,0 +1,56 @@ +package org.mifosplatform.portfolio.client.serialization; + +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; +import org.mifosplatform.infrastructure.core.serialization.FromCommandJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.client.command.ClientIdentifierCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; + +@Component +public final class ClientIdentifierCommandFromCommandJsonDeserializer implements FromCommandJsonDeserializer { + + private final FromJsonHelper fromJsonHelper; + + @Autowired + public ClientIdentifierCommandFromCommandJsonDeserializer(final FromJsonHelper fromApiJsonHelper) { + this.fromJsonHelper = fromApiJsonHelper; + } + + @Override + public ClientIdentifierCommand commandFromCommandJson(final String commandAsJson) { + return commandFromCommandJson(null, commandAsJson); + } + + @Override + public ClientIdentifierCommand commandFromCommandJson(final Long clientIdentifierId, final String commandAsJson) { + return commandFromCommandJson(clientIdentifierId, commandAsJson, false); + } + + @Override + public ClientIdentifierCommand commandFromCommandJson(final Long clientIdentifierId, final String commandAsJson, + final boolean makerCheckerApproval) { + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromJsonHelper.parse(commandAsJson); + Long actualClientId = null; // FIXME - kw -clientId + final Long clientIdInternal = fromJsonHelper.extractLongNamed("clientId", element, parametersPassedInRequest); + if (clientIdInternal != null) { + actualClientId = clientIdInternal; + } + final Long documentTypeId = fromJsonHelper.extractLongNamed("documentTypeId", element, parametersPassedInRequest); + final String documentKey = fromJsonHelper.extractStringNamed("documentKey", element, parametersPassedInRequest); + final String documentDescription = fromJsonHelper.extractStringNamed("documentDescription", element, parametersPassedInRequest); + + return new ClientIdentifierCommand(parametersPassedInRequest, makerCheckerApproval, clientIdentifierId, actualClientId, + documentTypeId, documentKey, documentDescription); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/fund/serialization/FundCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/fund/serialization/FundCommandFromApiJsonDeserializer.java index 9e654aa00..9a92a4bc9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/fund/serialization/FundCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/fund/serialization/FundCommandFromApiJsonDeserializer.java @@ -44,13 +44,13 @@ public final class FundCommandFromApiJsonDeserializer implements FromApiJsonDese } @Override - public FundCommand commandFromApiJson(final Long roleId, final String json) { + public FundCommand commandFromApiJson(final Long fundId, final String json) { final Type typeOfMap = new TypeToken>() {}.getType(); fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); - return this.fromCommandJsonDeserializer.commandFromCommandJson(roleId, json); + return this.fromCommandJsonDeserializer.commandFromCommandJson(fundId, json); } @Override @@ -60,8 +60,8 @@ public final class FundCommandFromApiJsonDeserializer implements FromApiJsonDese } @Override - public String serializedCommandJsonFromApiJson(final Long roleId, final String json) { - final FundCommand command = commandFromApiJson(roleId, json); + public String serializedCommandJsonFromApiJson(final Long fundId, final String json) { + final FundCommand command = commandFromApiJson(fundId, json); return this.commandSerializerService.serializeCommandToJson(command); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java index faebd8bfe..192a6dd70 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java @@ -19,11 +19,9 @@ import javax.ws.rs.core.UriInfo; import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; -import org.mifosplatform.infrastructure.core.api.PortfolioApiDataConversionService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; -import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.monetary.data.CurrencyData; @@ -32,9 +30,9 @@ import org.mifosplatform.portfolio.charge.data.ChargeData; import org.mifosplatform.portfolio.charge.service.ChargeReadPlatformService; import org.mifosplatform.portfolio.fund.data.FundData; import org.mifosplatform.portfolio.fund.service.FundReadPlatformService; -import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; import org.mifosplatform.portfolio.loanproduct.data.LoanProductData; import org.mifosplatform.portfolio.loanproduct.data.TransactionProcessingStrategyData; +import org.mifosplatform.portfolio.loanproduct.serialization.LoanProductCommandFromApiJsonDeserializer; import org.mifosplatform.portfolio.loanproduct.service.LoanDropdownReadPlatformService; import org.mifosplatform.portfolio.loanproduct.service.LoanProductReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; @@ -61,20 +59,19 @@ public class LoanProductsApiResource { private final ChargeReadPlatformService chargeReadPlatformService; private final CurrencyReadPlatformService currencyReadPlatformService; private final FundReadPlatformService fundReadPlatformService; + private final LoanProductCommandFromApiJsonDeserializer fromApiJsonDeserializer; private final DefaultToApiJsonSerializer toApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; private final LoanDropdownReadPlatformService dropdownReadPlatformService; - private final PortfolioApiDataConversionService apiDataConversionService; - private final CommandSerializer commandSerializerService; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; @Autowired public LoanProductsApiResource(final PlatformSecurityContext context, final LoanProductReadPlatformService readPlatformService, final ChargeReadPlatformService chargeReadPlatformService, final CurrencyReadPlatformService currencyReadPlatformService, final FundReadPlatformService fundReadPlatformService, final LoanDropdownReadPlatformService dropdownReadPlatformService, + final LoanProductCommandFromApiJsonDeserializer fromApiJsonDeserializer, final DefaultToApiJsonSerializer toApiJsonSerializer, - final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioApiDataConversionService apiDataConversionService, - final CommandSerializer commandSerializerService, + final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { this.context = context; this.loanProductReadPlatformService = readPlatformService; @@ -82,10 +79,9 @@ public class LoanProductsApiResource { this.currencyReadPlatformService = currencyReadPlatformService; this.fundReadPlatformService = fundReadPlatformService; this.dropdownReadPlatformService = dropdownReadPlatformService; + this.fromApiJsonDeserializer = fromApiJsonDeserializer; this.toApiJsonSerializer = toApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; - this.apiDataConversionService = apiDataConversionService; - this.commandSerializerService = commandSerializerService; this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; } @@ -98,9 +94,7 @@ public class LoanProductsApiResource { "CREATE_LOANPRODUCT"); context.authenticatedUser().validateHasPermissionTo("CREATE_LOANPRODUCT", allowedPermissions); - final LoanProductCommand command = this.apiDataConversionService.convertApiRequestJsonToLoanProductCommand(null, - apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); + final String commandSerializedAsJson = this.fromApiJsonDeserializer.serializedCommandJsonFromApiJson(apiRequestBodyAsJson); final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("CREATE", "loanproducts", null, commandSerializedAsJson); @@ -164,11 +158,10 @@ public class LoanProductsApiResource { "UPDATE_LOANPRODUCT"); context.authenticatedUser().validateHasPermissionTo("UPDATE_LOANPRODUCT", allowedPermissions); - final LoanProductCommand command = this.apiDataConversionService.convertApiRequestJsonToLoanProductCommand(productId, + final String commandSerializedAsJson = this.fromApiJsonDeserializer.serializedCommandJsonFromApiJson(productId, apiRequestBodyAsJson); - final String commandSerializedAsJson = this.commandSerializerService.serializeCommandToJson(command); - final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("UPDATE", "loanproducts", null, + final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("UPDATE", "loanproducts", productId, commandSerializedAsJson); return this.toApiJsonSerializer.serialize(result); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/command/LoanProductCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/command/LoanProductCommand.java index 1392f37f6..e665f2860 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/command/LoanProductCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/command/LoanProductCommand.java @@ -8,43 +8,35 @@ import java.util.Set; */ public class LoanProductCommand { - private final String name; private final String description; - private final Long fundId; private final Long transactionProcessingStrategyId; - private final String currencyCode; private final Integer digitsAfterDecimal; private final BigDecimal principal; private final BigDecimal inArrearsTolerance; - private final Integer numberOfRepayments; private final Integer repaymentEvery; - private final BigDecimal interestRatePerPeriod; - private final Integer repaymentFrequencyType; private final Integer interestRateFrequencyType; private final Integer amortizationType; private final Integer interestType; private final Integer interestCalculationPeriodType; - private final String[] charges; - private final transient Set modifiedParameters; + private final transient Set parametersPassedInRequest; private final transient boolean makerCheckerApproval; private final transient Long id; - public LoanProductCommand(final Set modifiedParameters, - final boolean makerCheckerApproval, final Long id, final String name, final String description, - final Long fundId, final Long transactionProcessingStrategyId, final String currencyCode, final Integer digitsAfterDecimal, - final BigDecimal principal, final BigDecimal inArrearsTolerance, final Integer numberOfRepayments, - final Integer repaymentEvery, final BigDecimal interestRatePerPeriod, final Integer repaymentFrequencyType, - final Integer interestRateFrequencyType, final Integer amortizationType, final Integer interestType, - final Integer interestCalculationPeriodType, final String[] charges) { - this.modifiedParameters = modifiedParameters; + public LoanProductCommand(final Set parametersPassedInRequest, final boolean makerCheckerApproval, final Long id, + final String name, final String description, final Long fundId, final Long transactionProcessingStrategyId, + final String currencyCode, final Integer digitsAfterDecimal, final BigDecimal principal, final BigDecimal inArrearsTolerance, + final Integer numberOfRepayments, final Integer repaymentEvery, final BigDecimal interestRatePerPeriod, + final Integer repaymentFrequencyType, final Integer interestRateFrequencyType, final Integer amortizationType, + final Integer interestType, final Integer interestCalculationPeriodType, final String[] charges) { + this.parametersPassedInRequest = parametersPassedInRequest; this.makerCheckerApproval = makerCheckerApproval; this.id = id; this.name = name; @@ -143,73 +135,73 @@ public class LoanProductCommand { } public boolean isNameChanged() { - return this.modifiedParameters.contains("name"); + return this.parametersPassedInRequest.contains("name"); } public boolean isDescriptionChanged() { - return this.modifiedParameters.contains("description"); + return this.parametersPassedInRequest.contains("description"); } public boolean isFundChanged() { - return this.modifiedParameters.contains("fundId"); + return this.parametersPassedInRequest.contains("fundId"); } public boolean isTransactionProcessingStrategyChanged() { - return this.modifiedParameters.contains("transactionProcessingStrategyId"); + return this.parametersPassedInRequest.contains("transactionProcessingStrategyId"); } public boolean isCurrencyCodeChanged() { - return this.modifiedParameters.contains("currencyCode"); + return this.parametersPassedInRequest.contains("currencyCode"); } public boolean isDigitsAfterDecimalChanged() { - return this.modifiedParameters.contains("digitsAfterDecimal"); + return this.parametersPassedInRequest.contains("digitsAfterDecimal"); } public boolean isPrincipalChanged() { - return this.modifiedParameters.contains("principal"); + return this.parametersPassedInRequest.contains("principal"); } public boolean isInArrearsToleranceChanged() { - return this.modifiedParameters.contains("inArrearsTolerance"); + return this.parametersPassedInRequest.contains("inArrearsTolerance"); } public boolean isNumberOfRepaymentsChanged() { - return this.modifiedParameters.contains("numberOfRepayments"); + return this.parametersPassedInRequest.contains("numberOfRepayments"); } public boolean isRepaymentEveryChanged() { - return this.modifiedParameters.contains("repaymentEvery"); + return this.parametersPassedInRequest.contains("repaymentEvery"); } public boolean isRepaymentFrequencyTypeChanged() { - return this.modifiedParameters.contains("repaymentFrequencyType"); + return this.parametersPassedInRequest.contains("repaymentFrequencyType"); } public boolean isAmortizationTypeChanged() { - return this.modifiedParameters.contains("amortizationType"); + return this.parametersPassedInRequest.contains("amortizationType"); } public boolean isInterestTypeChanged() { - return this.modifiedParameters.contains("interestType"); + return this.parametersPassedInRequest.contains("interestType"); } public boolean isInterestRatePerPeriodChanged() { - return this.modifiedParameters.contains("interestRatePerPeriod"); + return this.parametersPassedInRequest.contains("interestRatePerPeriod"); } public boolean isInterestRateFrequencyTypeChanged() { - return this.modifiedParameters.contains("interestRateFrequencyType"); + return this.parametersPassedInRequest.contains("interestRateFrequencyType"); } public boolean isInterestCalculationPeriodTypeChanged() { - return this.modifiedParameters.contains("interestCalculationPeriodType"); + return this.parametersPassedInRequest.contains("interestCalculationPeriodType"); } public boolean isChargesChanged() { - return this.modifiedParameters.contains("charges"); + return this.parametersPassedInRequest.contains("charges"); } - + public boolean isApprovedByChecker() { return this.makerCheckerApproval; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/serialization/LoanProductCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/serialization/LoanProductCommandFromApiJsonDeserializer.java new file mode 100644 index 000000000..64a7df3e8 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/serialization/LoanProductCommandFromApiJsonDeserializer.java @@ -0,0 +1,108 @@ +package org.mifosplatform.portfolio.loanproduct.serialization; + +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.mifosplatform.infrastructure.core.serialization.CommandSerializer; +import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; + +/** + * Implementation of {@link FromApiJsonDeserializer} for + * {@link LoanProductCommand}'s. + */ +@Component +public final class LoanProductCommandFromApiJsonDeserializer implements FromApiJsonDeserializer { + + /** + * The parameters supported for this command. + */ + private final Set supportedParameters = new HashSet(Arrays.asList("name", "description", "fundId", + "transactionProcessingStrategyId", "currencyCode", "digitsAfterDecimal", "principal", "inArrearsTolerance", + "interestRatePerPeriod", "repaymentEvery", "numberOfRepayments", "repaymentFrequencyType", "interestRateFrequencyType", + "amortizationType", "interestType", "interestCalculationPeriodType", "charges", "locale")); + + private final FromJsonHelper fromApiJsonHelper; + private final CommandSerializer commandSerializerService; + + @Autowired + public LoanProductCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper, + final CommandSerializer commandSerializerService) { + this.fromApiJsonHelper = fromApiJsonHelper; + this.commandSerializerService = commandSerializerService; + } + + @Override + public LoanProductCommand commandFromApiJson(final String json) { + return commandFromApiJson(null, json); + } + + @Override + public LoanProductCommand commandFromApiJson(final Long loanProductId, final String json) { + + final Type typeOfMap = new TypeToken>() {}.getType(); + + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromApiJsonHelper.parse(json); + final String name = fromApiJsonHelper.extractStringNamed("name", element, parametersPassedInRequest); + final String description = fromApiJsonHelper.extractStringNamed("description", element, parametersPassedInRequest); + final Long fundId = fromApiJsonHelper.extractLongNamed("fundId", element, parametersPassedInRequest); + + final Long transactionProcessingStrategyId = fromApiJsonHelper.extractLongNamed("transactionProcessingStrategyId", element, + parametersPassedInRequest); + + final String currencyCode = fromApiJsonHelper.extractStringNamed("currencyCode", element, parametersPassedInRequest); + final Integer digitsAfterDecimal = fromApiJsonHelper.extractIntegerWithLocaleNamed("digitsAfterDecimal", element, + parametersPassedInRequest); + + final BigDecimal principal = fromApiJsonHelper.extractBigDecimalWithLocaleNamed("principal", element, parametersPassedInRequest); + final BigDecimal inArrearsTolerance = fromApiJsonHelper.extractBigDecimalWithLocaleNamed("inArrearsTolerance", element, + parametersPassedInRequest); + final BigDecimal interestRatePerPeriod = fromApiJsonHelper.extractBigDecimalWithLocaleNamed("interestRatePerPeriod", element, + parametersPassedInRequest); + final Integer repaymentEvery = fromApiJsonHelper + .extractIntegerWithLocaleNamed("repaymentEvery", element, parametersPassedInRequest); + final Integer numberOfRepayments = fromApiJsonHelper.extractIntegerWithLocaleNamed("numberOfRepayments", element, + parametersPassedInRequest); + final Integer repaymentFrequencyType = fromApiJsonHelper.extractIntegerWithLocaleNamed("repaymentFrequencyType", element, + parametersPassedInRequest); + final Integer interestRateFrequencyType = fromApiJsonHelper.extractIntegerWithLocaleNamed("interestRateFrequencyType", element, + parametersPassedInRequest); + final Integer amortizationType = fromApiJsonHelper.extractIntegerWithLocaleNamed("amortizationType", element, + parametersPassedInRequest); + final Integer interestType = fromApiJsonHelper.extractIntegerWithLocaleNamed("interestType", element, parametersPassedInRequest); + final Integer interestCalculationPeriodType = fromApiJsonHelper.extractIntegerWithLocaleNamed("interestCalculationPeriodType", + element, parametersPassedInRequest); + final String[] charges = fromApiJsonHelper.extractArrayNamed("charges", element, parametersPassedInRequest); + + return new LoanProductCommand(parametersPassedInRequest, false, loanProductId, name, description, fundId, + transactionProcessingStrategyId, currencyCode, digitsAfterDecimal, principal, inArrearsTolerance, numberOfRepayments, + repaymentEvery, interestRatePerPeriod, repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, + interestCalculationPeriodType, charges); + } + + @Override + public String serializedCommandJsonFromApiJson(final String json) { + final LoanProductCommand command = commandFromApiJson(json); + return this.commandSerializerService.serializeCommandToJson(command); + } + + @Override + public String serializedCommandJsonFromApiJson(final Long loanProductId, final String json) { + final LoanProductCommand command = commandFromApiJson(loanProductId, json); + return this.commandSerializerService.serializeCommandToJson(command); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/serialization/LoanProductCommandFromCommandJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/serialization/LoanProductCommandFromCommandJsonDeserializer.java new file mode 100644 index 000000000..86eb3b8b2 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/serialization/LoanProductCommandFromCommandJsonDeserializer.java @@ -0,0 +1,77 @@ +package org.mifosplatform.portfolio.loanproduct.serialization; + +import java.math.BigDecimal; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; +import org.mifosplatform.infrastructure.core.serialization.FromCommandJsonDeserializer; +import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.portfolio.loanproduct.command.LoanProductCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.JsonElement; + +@Component +public final class LoanProductCommandFromCommandJsonDeserializer implements FromCommandJsonDeserializer { + + private final FromJsonHelper fromJsonHelper; + + @Autowired + public LoanProductCommandFromCommandJsonDeserializer(final FromJsonHelper fromApiJsonHelper) { + this.fromJsonHelper = fromApiJsonHelper; + } + + @Override + public LoanProductCommand commandFromCommandJson(final String commandAsJson) { + return commandFromCommandJson(null, commandAsJson); + } + + @Override + public LoanProductCommand commandFromCommandJson(final Long loanProductId, final String commandAsJson) { + return commandFromCommandJson(loanProductId, commandAsJson, false); + } + + @Override + public LoanProductCommand commandFromCommandJson(final Long loanProductId, final String commandAsJson, + final boolean makerCheckerApproval) { + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } + + final Set parametersPassedInRequest = new HashSet(); + + final JsonElement element = fromJsonHelper.parse(commandAsJson); + + final String name = fromJsonHelper.extractStringNamed("name", element, parametersPassedInRequest); + final String description = fromJsonHelper.extractStringNamed("description", element, parametersPassedInRequest); + final Long fundId = fromJsonHelper.extractLongNamed("fundId", element, parametersPassedInRequest); + final Long transactionProcessingStrategyId = fromJsonHelper.extractLongNamed("transactionProcessingStrategyId", element, + parametersPassedInRequest); + final String currencyCode = fromJsonHelper.extractStringNamed("currencyCode", element, parametersPassedInRequest); + final Integer digitsAfterDecimal = fromJsonHelper.extractIntegerNamed("digitsAfterDecimal", element, parametersPassedInRequest); + + final BigDecimal principal = fromJsonHelper.extractBigDecimalNamed("principal", element.getAsJsonObject(), + parametersPassedInRequest); + final BigDecimal inArrearsTolerance = fromJsonHelper.extractBigDecimalNamed("inArrearsTolerance", element.getAsJsonObject(), + parametersPassedInRequest); + final BigDecimal interestRatePerPeriod = fromJsonHelper.extractBigDecimalNamed("interestRatePerPeriod", element.getAsJsonObject(), + parametersPassedInRequest); + final Integer repaymentEvery = fromJsonHelper.extractIntegerNamed("repaymentEvery", element, parametersPassedInRequest); + final Integer numberOfRepayments = fromJsonHelper.extractIntegerNamed("numberOfRepayments", element, parametersPassedInRequest); + final Integer repaymentFrequencyType = fromJsonHelper.extractIntegerNamed("repaymentFrequencyType", element, + parametersPassedInRequest); + final Integer interestRateFrequencyType = fromJsonHelper.extractIntegerNamed("interestRateFrequencyType", element, + parametersPassedInRequest); + final Integer amortizationType = fromJsonHelper.extractIntegerNamed("amortizationType", element, parametersPassedInRequest); + final Integer interestType = fromJsonHelper.extractIntegerNamed("interestType", element, parametersPassedInRequest); + final Integer interestCalculationPeriodType = fromJsonHelper.extractIntegerNamed("interestCalculationPeriodType", element, + parametersPassedInRequest); + final String[] charges = fromJsonHelper.extractArrayNamed("charges", element, parametersPassedInRequest); + + return new LoanProductCommand(parametersPassedInRequest, makerCheckerApproval, loanProductId, name, description, fundId, + transactionProcessingStrategyId, currencyCode, digitsAfterDecimal, principal, inArrearsTolerance, numberOfRepayments, + repaymentEvery, interestRatePerPeriod, repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, + interestCalculationPeriodType, charges); + } +} \ No newline at end of file