From 566ec9799a7085639032ab82fa0d5362385355e5 Mon Sep 17 00:00:00 2001 From: Matthew Trew Date: Fri, 4 Oct 2024 16:54:34 +0100 Subject: [PATCH] Add new config property for country codes to exclude --- .../java/uk/ac/cam/cl/dtg/segue/api/Constants.java | 1 + .../dtg/segue/api/managers/CountryLookupManager.java | 9 ++++++--- .../configuration/SegueGuiceConfigurationModule.java | 11 +++++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/uk/ac/cam/cl/dtg/segue/api/Constants.java b/src/main/java/uk/ac/cam/cl/dtg/segue/api/Constants.java index a3ca1b6b22..cce4faf0f0 100644 --- a/src/main/java/uk/ac/cam/cl/dtg/segue/api/Constants.java +++ b/src/main/java/uk/ac/cam/cl/dtg/segue/api/Constants.java @@ -599,6 +599,7 @@ public enum SegueUserPreferences { public static final String CUSTOM_COUNTRY_CODES = "CUSTOM_COUNTRY_CODES"; public static final String PRIORITY_COUNTRY_CODES = "PRIORITY_COUNTRY_CODES"; + public static final String REMOVED_COUNTRY_CODES = "REMOVED_COUNTRY_CODES"; public static final String ALLOW_SELF_TEACHER_ACCOUNT_UPGRADES = "ALLOW_SELF_TEACHER_ACCOUNT_UPGRADES"; diff --git a/src/main/java/uk/ac/cam/cl/dtg/segue/api/managers/CountryLookupManager.java b/src/main/java/uk/ac/cam/cl/dtg/segue/api/managers/CountryLookupManager.java index 2abd3a0c7f..9f4b4d1683 100644 --- a/src/main/java/uk/ac/cam/cl/dtg/segue/api/managers/CountryLookupManager.java +++ b/src/main/java/uk/ac/cam/cl/dtg/segue/api/managers/CountryLookupManager.java @@ -38,8 +38,9 @@ public class CountryLookupManager { * * @param customCountryCodes A map of custom country codes and display names to be included, which can be empty. */ - public CountryLookupManager(Map customCountryCodes, List priorityCountryCodes) { - allCountryCodesAndNames = getAllCountryCodesAndNames(customCountryCodes); + public CountryLookupManager(Map customCountryCodes, List priorityCountryCodes, + List removedCountryCodes) { + allCountryCodesAndNames = getAllCountryCodesAndNames(customCountryCodes, removedCountryCodes); priorityCountryCodesAndNames = getPriorityCountryCodesAndNames(allCountryCodesAndNames, priorityCountryCodes); } @@ -73,10 +74,12 @@ public Map getPriorityCountryCodesAndNames() { return priorityCountryCodesAndNames; } - private static Map getAllCountryCodesAndNames(Map customCountries) { + private static Map getAllCountryCodesAndNames(Map customCountries, + List removedCountryCodes) { // Merge custom and ISO country maps. return Stream.of(getISOCountryCodesAndNames(), customCountries) .flatMap(map -> map.entrySet().stream()) + .filter(e -> !removedCountryCodes.contains(e.getKey())) .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); } diff --git a/src/main/java/uk/ac/cam/cl/dtg/segue/configuration/SegueGuiceConfigurationModule.java b/src/main/java/uk/ac/cam/cl/dtg/segue/configuration/SegueGuiceConfigurationModule.java index 954f77feb5..8998b9299b 100644 --- a/src/main/java/uk/ac/cam/cl/dtg/segue/configuration/SegueGuiceConfigurationModule.java +++ b/src/main/java/uk/ac/cam/cl/dtg/segue/configuration/SegueGuiceConfigurationModule.java @@ -294,6 +294,7 @@ private void configureProperties() { // Additional countries this.bindConstantToNullableProperty(Constants.CUSTOM_COUNTRY_CODES, globalProperties); this.bindConstantToNullableProperty(Constants.PRIORITY_COUNTRY_CODES, globalProperties); + this.bindConstantToNullableProperty(Constants.REMOVED_COUNTRY_CODES, globalProperties); this.bind(String.class).toProvider(() -> { // Any binding to String without a matching @Named annotation will always get the empty string @@ -930,11 +931,13 @@ private IMisuseMonitor getMisuseMonitor(final EmailManager emailManager, final A @Singleton private CountryLookupManager getCountryLookupManager( @Nullable @Named(Constants.CUSTOM_COUNTRY_CODES) final String customCountryCodes, - @Nullable @Named(Constants.PRIORITY_COUNTRY_CODES) final String priorityCountryCodes + @Nullable @Named(Constants.PRIORITY_COUNTRY_CODES) final String priorityCountryCodes, + @Nullable @Named(Constants.REMOVED_COUNTRY_CODES) final String removedCountryCodes ) { if (null == countryLookupManager) { Map customCountryCodesMap = new HashMap<>(); List priorityCountryCodesList = new ArrayList<>(); + List removedCountryCodesList = new ArrayList<>(); if (null != customCountryCodes) { for (String country : customCountryCodes.split(",")) { @@ -947,7 +950,11 @@ private CountryLookupManager getCountryLookupManager( priorityCountryCodesList = List.of(priorityCountryCodes.split(",")); } - countryLookupManager = new CountryLookupManager(customCountryCodesMap, priorityCountryCodesList); + if (null != removedCountryCodes) { + removedCountryCodesList = List.of(removedCountryCodes.split(",")); + } + + countryLookupManager = new CountryLookupManager(customCountryCodesMap, priorityCountryCodesList, removedCountryCodesList); log.info("Creating singleton of CountryLookupManager"); } return countryLookupManager;