tidy up validation for currency configuration.
This commit is contained in:
-38
@@ -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<String> listOfCurrencyCodes = new ArrayList<String>(Arrays.asList(currencies));
|
||||
Collections.sort(listOfCurrencyCodes);
|
||||
this.currencies = listOfCurrencyCodes.toArray(new String[listOfCurrencyCodes.size()]);
|
||||
}
|
||||
|
||||
public String[] getCurrencies() {
|
||||
return currencies;
|
||||
}
|
||||
|
||||
public void validateForUpdate() {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
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); }
|
||||
}
|
||||
}
|
||||
+18
-11
@@ -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<CurrencyCommand> {
|
||||
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<Map<String, Object>>() {}.getType();
|
||||
fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters);
|
||||
|
||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
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<ApiParameterError> dataValidationErrors) {
|
||||
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
|
||||
"Validation errors exist.", dataValidationErrors); }
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -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<String, Object> changes = new LinkedHashMap<String, Object>();
|
||||
final List<String> allowedCurrencyCodes = new ArrayList<String>();
|
||||
final Set<OrganisationCurrency> allowedCurrencies = new HashSet<OrganisationCurrency>();
|
||||
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); }
|
||||
|
||||
Reference in New Issue
Block a user