@Override public void reloadValues() throws BaleenException { reset(); for (Entry<String, String> countryEntry : country.getCountryNames().entrySet()) { addTerm( countryEntry.getValue(), caseSensitive ? countryEntry.getKey() : countryEntry.getKey().toLowerCase()); } }
@Override public Map<String, Object> getAdditionalData(String key) { String cca3 = getId(caseSensitive ? key : key.toLowerCase()); if (cca3 == null) { return Collections.emptyMap(); } Map<String, Object> data = new HashMap<>(); data.put("geoJson", country.getGeoJson(cca3)); return data; } }
@Override protected boolean doInitialize(ResourceSpecifier specifier, Map<String, Object> additionalParams) throws ResourceInitializationException { ObjectMapper mapper = new ObjectMapper(); synchronized (this) { UimaMonitor monitor = getMonitor(); if (demonyms == null) { monitor.info("Shared country resource initialising"); JsonNode countriesJson = loadCountriesJson(mapper, monitor); countryNames = ImmutableMap.copyOf(loadCountryNames(countriesJson, monitor)); demonyms = ImmutableMap.copyOf(loadDemonyms(countriesJson, monitor)); geoJson = ImmutableMap.copyOf(loadCountriesGeoJson(mapper, monitor)); } else { monitor.info("Shared country resource already initialised"); } } return true; }
private static Map<String, String> loadCountryNames(JsonNode rootNode, UimaMonitor uimaMonitor) { Iterator<JsonNode> iter = rootNode.elements(); Map<String, String> countryNames = new HashMap<>(); while (iter.hasNext()) { JsonNode node = iter.next(); String cca3 = getProperty(node, "cca3").toUpperCase(); if (node == null || cca3.isEmpty()) { uimaMonitor.warn("Empty country code found - entry will be skipped"); continue; } for (String name : getNames(node.path("name"))) { countryNames.put(name, cca3); } } uimaMonitor.info("{} country names read", countryNames.size()); return countryNames; }
ExternalResourceFactory.createExternalResourceDescription( "country", SharedCountryResource.class); SharedCountryResource scr = new SharedCountryResource(); scr.initialize(erd.getResourceSpecifier(), Collections.emptyMap()); Set<String> countryCodes = new HashSet<>(scr.getCountryNames().values()); l.setGeoJson(scr.getGeoJson(cca3)); l.addToIndexes(); scr.destroy();
@BeforeClass public static void beforeClass() throws ResourceInitializationException { scr = new SharedCountryResource(); scr.initialize(new CustomResourceSpecifier_impl(), Collections.emptyMap()); }
@After public void afterTest() throws Exception { scr.destroy(); scr = null; }
private static Map<String, String> loadDemonyms(JsonNode rootNode, UimaMonitor uimaMonitor) { Iterator<JsonNode> iter = rootNode.elements(); demonyms = new HashMap<>(); while (iter.hasNext()) { JsonNode node = iter.next(); String demonym = getProperty(node, "demonym").toLowerCase(); String cca3 = getProperty(node, "cca3").toUpperCase(); if (demonym.isEmpty() || cca3.isEmpty()) { uimaMonitor.warn("Empty demonym or country code found - entry will be skipped"); continue; } demonyms.put(demonym, cca3); } uimaMonitor.info("{} nationalities read", demonyms.size()); return demonyms; }
@Test public void testGetDemonyms() throws Exception { Map<String, String> demonyms = scr.getDemonyms(); assertTrue(demonyms.containsKey("mahoran")); assertEquals("MYT", demonyms.get("mahoran")); assertEquals(231, demonyms.size()); }
@Before public void beforeTest() throws Exception { ExternalResourceDescription erd = ExternalResourceFactory.createExternalResourceDescription( "country", SharedCountryResource.class); scr = new SharedCountryResource(); scr.initialize(erd.getResourceSpecifier(), Collections.emptyMap()); }
@AfterClass public static void afterClass() { scr.destroy(); }
private static Map<String, String> loadCountryNames(JsonNode rootNode, UimaMonitor uimaMonitor) { Iterator<JsonNode> iter = rootNode.elements(); Map<String, String> countryNames = new HashMap<>(); while (iter.hasNext()) { JsonNode node = iter.next(); String cca3 = getProperty(node, "cca3").toUpperCase(); if (node == null || cca3.isEmpty()) { uimaMonitor.warn("Empty country code found - entry will be skipped"); continue; } for (String name : getNames(node.path("name"))) { countryNames.put(name, cca3); } } uimaMonitor.info("{} country names read", countryNames.size()); return countryNames; }
private static Map<String, String> loadDemonyms(JsonNode rootNode, UimaMonitor uimaMonitor) { Iterator<JsonNode> iter = rootNode.elements(); demonyms = new HashMap<>(); while (iter.hasNext()) { JsonNode node = iter.next(); String demonym = getProperty(node, "demonym").toLowerCase(); String cca3 = getProperty(node, "cca3").toUpperCase(); if (demonym.isEmpty() || cca3.isEmpty()) { uimaMonitor.warn("Empty demonym or country code found - entry will be skipped"); continue; } demonyms.put(demonym, cca3); } uimaMonitor.info("{} nationalities read", demonyms.size()); return demonyms; }
@Override protected boolean doInitialize(ResourceSpecifier specifier, Map<String, Object> additionalParams) throws ResourceInitializationException { ObjectMapper mapper = new ObjectMapper(); synchronized (this) { UimaMonitor monitor = getMonitor(); if (demonyms == null) { monitor.info("Shared country resource initialising"); JsonNode countriesJson = loadCountriesJson(mapper, monitor); countryNames = ImmutableMap.copyOf(loadCountryNames(countriesJson, monitor)); demonyms = ImmutableMap.copyOf(loadDemonyms(countriesJson, monitor)); geoJson = ImmutableMap.copyOf(loadCountriesGeoJson(mapper, monitor)); } else { monitor.info("Shared country resource already initialised"); } } return true; }
@Override public void reloadValues() throws BaleenException { reset(); for (Entry<String, String> countryEntry : country.getCountryNames().entrySet()) { addTerm( countryEntry.getValue(), caseSensitive ? countryEntry.getKey() : countryEntry.getKey().toLowerCase()); } }
@Override public Map<String, Object> getAdditionalData(String key) { String cca3 = getId(caseSensitive ? key : key.toLowerCase()); if (cca3 == null) { return Collections.emptyMap(); } Map<String, Object> data = new HashMap<>(); data.put("geoJson", country.getGeoJson(cca3)); return data; } }
String cca3 = getProperty(node.path(PROPERTIES), "ISO_A3").toUpperCase(); String geojson = getProperty(node, "geometry"); uimaMonitor.warn( "Generic country code -99 found - entry {} will not have GeoJSON information", getProperty(node.path(PROPERTIES), "ADMIN")); } else { geoJson.put(cca3, geojson);
@Test public void testGetNames() throws Exception { Map<String, String> names = scr.getCountryNames(); assertTrue(names.containsKey("Aruba")); assertEquals("ABW", names.get("Aruba")); int lbnCount = 0; for (String s : names.values()) { if ("LBN".equals(s)) { lbnCount++; } } assertEquals(6, lbnCount); } }
@Test public void testGetGeoJson() throws Exception { assertNotNull(scr.getGeoJson("BRA")); assertNotNull(scr.getGeoJson("bra")); assertNull(scr.getGeoJson("FOO")); }
String cca3 = getProperty(node.path(PROPERTIES), "ISO_A3").toUpperCase(); String geojson = getProperty(node, "geometry"); uimaMonitor.warn( "Generic country code -99 found - entry {} will not have GeoJSON information", getProperty(node.path(PROPERTIES), "ADMIN")); } else { geoJson.put(cca3, geojson);