public Country map(final int index, final ResultSet resultRow, final StatementContext ctx) throws SQLException {
final String countryIso3Code = resultRow.getString("iso3Code");
return Country.builder().name(resultRow.getString("name"))
.iso2Code(resultRow.getString("iso2Code"))
.iso3Code(resultRow.getString("iso3Code"))
.regions(new ArrayList<>())
.build();
}
SELECT
cn.name AS name,
c.iso3_code AS iso3Code,
c.iso2_code AS iso2Code,
r.region_code AS region
FROM website_locale wl
INNER JOIN country_name cn
ON cn.language_code = wl.language_code
INNER JOIN country c
ON cn.country_iso3_code = c.iso3_code
LEFT OUTER JOIN region r
ON r.country_iso3_code = c.iso3_code
WHERE wl.website_locale = "us";
@Test
public void getCountriesForUSEnglishLocale() throws Exception {
final WebsiteLocale websiteLocale = WebsiteLocale.US;
final List<Country> expectedCountries = ImmutableList.of(
new Country("Canada", "CA", "CAN", ImmutableList.of("AB", "BC")),
new Country("United Kingdom", "GB", "GBR", ImmutableList.of()),
new Country("United States", "US", "USA", ImmutableList.of("PR", "RI", "WA")));
final List<Country> countries = countryRepository.getCountriesForWebsiteLocale(websiteLocale);
assertEquals(countries.size(), expectedCountries.size());
assertTrue(countries.containsAll(expectedCountries));
}
--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
@Override
public List<Country> getCountriesForWebsiteLocale(final WebsiteLocale websiteLocale) {
checkNotNull(websiteLocale, "websiteLocale required");
try (Handle queryHandler = connection.open()) {
List<Country> countryResultList = queryHandler.createQuery(CountryQueries.GET_COUNTRIES_FOR_WEBSITE_LOCALE)
.bind("website_locale", websiteLocale.getId())
.map(new CountryMapper())
.list();
return getStringCountryMap(countryResultList);
}
}
/**
* Builds a unique list of Countries and merges properties
* @param countryResultList unique list of countries
* @return list
*/
List<Country> getStringCountryMap(final List<Country> countryResultList) {
final Map<String, Country> countryMap = new LinkedHashMap<>();
//build unique lists of country records, folding joined regions table
for (Country country : countryResultList) {
if (countryMap.containsKey(country.getIso3Code())) {
final Country oldCountryRecord = countryMap.get(country.getIso3Code());
final List<String> mergedRegions = new ArrayList<>(oldCountryRecord.getRegions());
mergedRegions.addAll(country.getRegions());
countryMap.replace(country.getIso3Code(), Country
.builder()
.name(country.getName())
.iso2Code(country.getIso2Code())
.iso3Code(country.getIso3Code())
.regions(mergedRegions)
.build());
} else {
countryMap.put(country.getIso3Code(), country);
}
} return countryMap.values().stream().collect(Collectors.toList());
}
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
@Override
public List<Country> getCountriesForWebsiteLocale(final WebsiteLocale websiteLocale) {
checkNotNull(websiteLocale, "websiteLocale required");
try (Handle queryHandler = connection.open()) {
LinkedHashMap<String, Country.Builder> countryBuilderList = queryHandler.createQuery(CountryQueries.GET_COUNTRIES_FOR_WEBSITE_LOCALE)
.bind("website_locale", websiteLocale.getId())
//fold allows us to merge rows of regions which has a cardinality of 1 -> n with one country record
.fold(new LinkedHashMap<String, Country.Builder>(), (map, resultRow, ctx) -> {
final String countryIso3 = resultRow.getString("iso3Code");
if (map.get(countryIso3) == null) {
final Country.Builder countryBuilder = Country.builder().name(resultRow.getString("name"))
.iso2Code(resultRow.getString("iso2Code"))
.iso3Code(resultRow.getString("iso3Code"));
if (!StringUtils.isBlank(resultRow.getString("region"))) {
countryBuilder.regions(ImmutableList.of(resultRow.getString("region")));
}
map.put(countryIso3, countryBuilder);
} else {
map.put(countryIso3, map.get(countryIso3).addRegion(resultRow.getString("region")));
}
return map;
});
return countryBuilderList.values().stream().map(Country.Builder::build).collect(toList());
}
}
--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.