Skip to content

Commit

Permalink
Add new config property for country codes to exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtrew committed Oct 4, 2024
1 parent e4b2402 commit 566ec97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/main/java/uk/ac/cam/cl/dtg/segue/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> customCountryCodes, List<String> priorityCountryCodes) {
allCountryCodesAndNames = getAllCountryCodesAndNames(customCountryCodes);
public CountryLookupManager(Map<String, String> customCountryCodes, List<String> priorityCountryCodes,
List<String> removedCountryCodes) {
allCountryCodesAndNames = getAllCountryCodesAndNames(customCountryCodes, removedCountryCodes);
priorityCountryCodesAndNames = getPriorityCountryCodesAndNames(allCountryCodesAndNames, priorityCountryCodes);
}

Expand Down Expand Up @@ -73,10 +74,12 @@ public Map<String, String> getPriorityCountryCodesAndNames() {
return priorityCountryCodesAndNames;
}

private static Map<String, String> getAllCountryCodesAndNames(Map<String, String> customCountries) {
private static Map<String, String> getAllCountryCodesAndNames(Map<String, String> customCountries,
List<String> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String> customCountryCodesMap = new HashMap<>();
List<String> priorityCountryCodesList = new ArrayList<>();
List<String> removedCountryCodesList = new ArrayList<>();

if (null != customCountryCodes) {
for (String country : customCountryCodes.split(",")) {
Expand All @@ -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;
Expand Down

0 comments on commit 566ec97

Please sign in to comment.