diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/data/FieldConfigurationData.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/data/FieldConfigurationData.java index 1700bc9eb..da5d4fe17 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/data/FieldConfigurationData.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/data/FieldConfigurationData.java @@ -66,7 +66,7 @@ public final class FieldConfigurationData { return this.field; } - public boolean isisEnabled() { + public boolean isEnabled() { return this.isEnabled; } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/serialization/AddressCommandFromApiJsonDeserializer.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/serialization/AddressCommandFromApiJsonDeserializer.java index a97ec133f..9b8d37eeb 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/serialization/AddressCommandFromApiJsonDeserializer.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/address/serialization/AddressCommandFromApiJsonDeserializer.java @@ -21,13 +21,11 @@ package org.apache.fineract.portfolio.address.serialization; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; -import java.math.BigDecimal; import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.fineract.infrastructure.core.data.ApiParameterError; import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder; @@ -71,170 +69,39 @@ public class AddressCommandFromApiJsonDeserializer { final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("Address"); final JsonElement element = this.fromApiJsonHelper.parse(json); - Set supportedParameters = new HashSet<>(); - final List configurationData = new ArrayList<>(this.readservice.retrieveFieldConfigurationList("ADDRESS")); - final List enabledFieldList = new ArrayList<>(); - - final Map madatoryFieldsMap = new HashMap(); - final Map enabledFieldsMap = new HashMap(); - final Map regexFieldsMap = new HashMap(); - // validate the json fields from the configuration data fields + final List configData = configurationData.stream().filter(FieldConfigurationData::isEnabled) + .collect(Collectors.toList()); - for (final FieldConfigurationData data : configurationData) { - madatoryFieldsMap.put(data.getField(), data.isIs_mandatory()); - enabledFieldsMap.put(data.getField(), data.isisEnabled()); - regexFieldsMap.put(data.getField(), data.getValidation_regex()); - if (data.isisEnabled()) { - enabledFieldList.add(data.getField()); - } - } - if (fromNewClient) { + final Set supportedParameters = configData.stream() + .map(FieldConfigurationData::getField) + .collect(Collectors.toSet()); - enabledFieldList.add("addressTypeId"); - enabledFieldList.add("locale"); - enabledFieldList.add("dateFormat"); - supportedParameters = new HashSet<>(enabledFieldList); - - madatoryFieldsMap.put("addressTypeId", true); - - } - if (!fromNewClient) { - enabledFieldList.add("locale"); - enabledFieldList.add("dateFormat"); - enabledFieldList.add("addressId"); - madatoryFieldsMap.put("addressId", true); - supportedParameters = new HashSet<>(enabledFieldList); - } + supportedParameters.add("locale"); + supportedParameters.add("dateFormat"); + supportedParameters.add(fromNewClient ? "addressTypeId" : "addressId"); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); - final String addressLine1 = this.fromApiJsonHelper.extractStringNamed("addressLine1", element); - if (enabledFieldsMap.get("addressLine1")) { - if (madatoryFieldsMap.get("addressLine1") && fromNewClient) { - baseDataValidator.reset().parameter("addressLine1").value(addressLine1).notBlank(); - } - if (!regexFieldsMap.get("addressLine1").isEmpty()) { - baseDataValidator.reset().parameter("addressLine1").value(addressLine1) - .matchesRegularExpression(regexFieldsMap.get("addressLine1")); - } + configData.forEach(fieldConfiguration -> { + final String field = + fieldConfiguration.getField().equals("addressType") ? "addressTypeId" : fieldConfiguration.getField(); + final String fieldValue = this.fromApiJsonHelper.extractStringNamed(field, element); - } - final String addressLine2 = this.fromApiJsonHelper.extractStringNamed("addressLine2", element); - if (enabledFieldsMap.get("addressLine2")) { - if (madatoryFieldsMap.get("addressLine2") && fromNewClient) { - baseDataValidator.reset().parameter("addressLine2").value(addressLine2).notBlank(); - } - if (!regexFieldsMap.get("addressLine2").isEmpty()) { - baseDataValidator.reset().parameter("addressLine2").value(addressLine2) - .matchesRegularExpression(regexFieldsMap.get("addressLine2")); - } - } - final String addressLine3 = this.fromApiJsonHelper.extractStringNamed("addressLine3", element); - if (enabledFieldsMap.get("addressLine3")) { - if (madatoryFieldsMap.get("addressLine3") && fromNewClient) { - baseDataValidator.reset().parameter("addressLine3").value(addressLine3).notBlank(); - } - if (!regexFieldsMap.get("addressLine3").isEmpty()) { - baseDataValidator.reset().parameter("addressLine3").value(addressLine3) - .matchesRegularExpression(regexFieldsMap.get("addressLine3")); - } - } - final String townVillage = this.fromApiJsonHelper.extractStringNamed("townVillage", element); - if (enabledFieldsMap.get("townVillage")) { - if (madatoryFieldsMap.get("townVillage") && fromNewClient) { - baseDataValidator.reset().parameter("townVillage").value(townVillage).notBlank(); - } - if (!regexFieldsMap.get("townVillage").isEmpty()) { - baseDataValidator.reset().parameter("townVillage").value(townVillage) - .matchesRegularExpression(regexFieldsMap.get("townVillage")); - } - } - final String city = this.fromApiJsonHelper.extractStringNamed("city", element); - - if (enabledFieldsMap.get("city")) { - if (madatoryFieldsMap.get("city") && fromNewClient) { - baseDataValidator.reset().parameter("city").value(city).notBlank(); - } - if (!regexFieldsMap.get("city").isEmpty()) { - baseDataValidator.reset().parameter("city").value(city).matchesRegularExpression(regexFieldsMap.get("city")); - } - } - final String countyDistrict = this.fromApiJsonHelper.extractStringNamed("countyDistrict", element); - if (enabledFieldsMap.get("countyDistrict")) { - if (madatoryFieldsMap.get("countyDistrict") && fromNewClient) { - baseDataValidator.reset().parameter("countyDistrict").value(countyDistrict).notBlank(); - } - if (!regexFieldsMap.get("countyDistrict").isEmpty()) { - baseDataValidator.reset().parameter("countyDistrict").value(countyDistrict) - .matchesRegularExpression(regexFieldsMap.get("countyDistrict")); - } - } - - if (this.fromApiJsonHelper.extractLongNamed("stateProvinceId", element) != null) { - - final long stateProvinceId = this.fromApiJsonHelper.extractLongNamed("stateProvinceId", element); - if (enabledFieldsMap.get("stateProvinceId")) { - if (madatoryFieldsMap.get("stateProvinceId") && fromNewClient) { - baseDataValidator.reset().parameter("stateProvinceId").value(stateProvinceId).notBlank(); - } - if (!regexFieldsMap.get("stateProvinceId").isEmpty()) { - baseDataValidator.reset().parameter("stateProvinceId").value(stateProvinceId) - .matchesRegularExpression(regexFieldsMap.get("stateProvinceId")); + if (fieldConfiguration.getField().equals("addressType") && fromNewClient) { + baseDataValidator.reset().parameter(field).value(fieldValue).notBlank(); + } else { + if (fieldConfiguration.isIs_mandatory() && fromNewClient) { + baseDataValidator.reset().parameter(field).value(fieldValue).notBlank(); } } - } - if (this.fromApiJsonHelper.extractLongNamed("countryId", element) != null) { - final long countryId = this.fromApiJsonHelper.extractLongNamed("countryId", element); - if (enabledFieldsMap.get("countryId")) { - if (madatoryFieldsMap.get("countryId") && fromNewClient) { - baseDataValidator.reset().parameter("countryId").value(countryId).notBlank(); - } - if (!regexFieldsMap.get("countryId").isEmpty()) { - baseDataValidator.reset().parameter("countryId").value(countryId) - .matchesRegularExpression(regexFieldsMap.get("countryId")); - } + if (!fieldConfiguration.getValidation_regex().isEmpty()) { + baseDataValidator.reset().parameter(field).value(fieldValue) + .matchesRegularExpression(fieldConfiguration.getValidation_regex()); } - } - - final String postalCode = this.fromApiJsonHelper.extractStringNamed("postalCode", element); - if (enabledFieldsMap.get("postalCode")) { - if (madatoryFieldsMap.get("postalCode") && fromNewClient) { - baseDataValidator.reset().parameter("postalCode").value(postalCode).notBlank(); - } - if (!regexFieldsMap.get("postalCode").isEmpty()) { - baseDataValidator.reset().parameter("postalCode").value(postalCode) - .matchesRegularExpression(regexFieldsMap.get("postalCode")); - } - } - - if (this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("latitude", element) != null) { - final BigDecimal latitude = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("latitude", element); - if (enabledFieldsMap.get("latitude")) { - if (madatoryFieldsMap.get("latitude") && fromNewClient) { - baseDataValidator.reset().parameter("latitude").value(latitude).notBlank(); - } - if (!regexFieldsMap.get("latitude").isEmpty()) { - baseDataValidator.reset().parameter("latitude").value(latitude) - .matchesRegularExpression(regexFieldsMap.get("latitude")); - } - } - } - - if (this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("longitude", element) != null) { - final BigDecimal longitude = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("longitude", element); - if (enabledFieldsMap.get("longitude")) { - if (madatoryFieldsMap.get("longitude") && fromNewClient) { - baseDataValidator.reset().parameter("longitude").value(longitude).notBlank(); - } - if (!regexFieldsMap.get("longitude").isEmpty()) { - baseDataValidator.reset().parameter("longitude").value(longitude) - .matchesRegularExpression(regexFieldsMap.get("longitude")); - } - } - } + }); throwExceptionIfValidationWarningsExist(dataValidationErrors);