diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/command/CurrencyCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/command/CurrencyCommand.java deleted file mode 100644 index a59d750c5..000000000 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/command/CurrencyCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.mifosplatform.organisation.monetary.command; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.mifosplatform.infrastructure.core.data.ApiParameterError; -import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder; -import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; - -/** - * Immutable command for updating allowed currencies. - */ -public class CurrencyCommand { - - private String[] currencies; - - public CurrencyCommand(final String[] currencies) { - final List listOfCurrencyCodes = new ArrayList(Arrays.asList(currencies)); - Collections.sort(listOfCurrencyCodes); - this.currencies = listOfCurrencyCodes.toArray(new String[listOfCurrencyCodes.size()]); - } - - public String[] getCurrencies() { - return currencies; - } - - public void validateForUpdate() { - List dataValidationErrors = new ArrayList(); - DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("currencies"); - - baseDataValidator.reset().parameter("currencies").value(this.currencies).arrayNotEmpty(); - - 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/organisation/monetary/serialization/CurrencyCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/serialization/CurrencyCommandFromApiJsonDeserializer.java index 224a4b6bb..396f89c58 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/serialization/CurrencyCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/serialization/CurrencyCommandFromApiJsonDeserializer.java @@ -1,29 +1,27 @@ package org.mifosplatform.organisation.monetary.serialization; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; +import org.mifosplatform.infrastructure.core.data.ApiParameterError; +import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder; import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; -import org.mifosplatform.infrastructure.core.serialization.AbstractFromApiJsonDeserializer; -import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer; +import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; -import org.mifosplatform.organisation.monetary.command.CurrencyCommand; 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 CurrencyCommand} - * 's. - */ @Component -public final class CurrencyCommandFromApiJsonDeserializer extends AbstractFromApiJsonDeserializer { +public final class CurrencyCommandFromApiJsonDeserializer { /** * The parameters supported for this command. @@ -37,16 +35,25 @@ public final class CurrencyCommandFromApiJsonDeserializer extends AbstractFromAp this.fromApiJsonHelper = fromApiJsonHelper; } - @Override - public CurrencyCommand commandFromApiJson(final String json) { + public void validateForUpdate(final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } final Type typeOfMap = new TypeToken>() {}.getType(); fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + final List dataValidationErrors = new ArrayList(); + final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("currencies"); + final JsonElement element = fromApiJsonHelper.parse(json); final String[] currencies = fromApiJsonHelper.extractArrayNamed("currencies", element); - return new CurrencyCommand(currencies); + baseDataValidator.reset().parameter("currencies").value(currencies).arrayNotEmpty(); + + throwExceptionIfValidationWarningsExist(dataValidationErrors); + } + + private void throwExceptionIfValidationWarningsExist(final List 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/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java index 37e981dd5..f7d8ddf91 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java @@ -11,7 +11,6 @@ import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; -import org.mifosplatform.organisation.monetary.command.CurrencyCommand; import org.mifosplatform.organisation.monetary.domain.ApplicationCurrency; import org.mifosplatform.organisation.monetary.domain.ApplicationCurrencyRepository; import org.mifosplatform.organisation.monetary.exception.CurrencyNotFoundException; @@ -47,13 +46,14 @@ public class CurrencyWritePlatformServiceJpaRepositoryImpl implements CurrencyWr context.authenticatedUser(); - final CurrencyCommand currencyCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json()); - currencyCommand.validateForUpdate(); + this.fromApiJsonDeserializer.validateForUpdate(command.json()); + + final String[] currencies = command.arrayValueOfParameterNamed("currencies"); final Map changes = new LinkedHashMap(); final List allowedCurrencyCodes = new ArrayList(); final Set allowedCurrencies = new HashSet(); - for (final String currencyCode : currencyCommand.getCurrencies()) { + for (final String currencyCode : currencies) { final ApplicationCurrency currency = this.applicationCurrencyRepository.findOneByCode(currencyCode); if (currency == null) { throw new CurrencyNotFoundException(currencyCode); }