Commit 3c99d2c4 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Custom ISO-3166 codes

Signed-off-by: Matija Obreza's avatarMatija Obreza <matija.obreza@croptrust.org>
parent 2f8dbd58
......@@ -53,6 +53,8 @@ import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.worker.CountryInfo;
import org.genesys2.server.service.worker.DavrosCountrySource;
import org.genesys2.server.service.worker.CustomISO3166Source;
import org.genesys2.server.service.worker.CustomISO3166Source.Custom3166Entry;
import org.genesys2.server.service.worker.GeoNamesCountrySource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -92,6 +94,9 @@ public class GeoServiceImpl implements GeoService {
@Autowired(required = false)
private DavrosCountrySource davrosCountrySource;
@Autowired(required = false)
private CustomISO3166Source customISO3166Source;
@Autowired
private GenesysFilterService filterService;
......@@ -253,12 +258,50 @@ public class GeoServiceImpl implements GeoService {
// update from Davros, it has info on inactive country codes
updateDavrosCountries();
// update custom ISO3316-3 codes
updateCustomCountryCodes();
LOG.info("Country data up to date");
}
private void updateCustomCountryCodes() throws IOException {
if (customISO3166Source == null) {
LOG.warn("Custom ISO3166 reader not available");
return;
}
final List<Custom3166Entry> countries = customISO3166Source.fetchCountryData();
if (LOG.isDebugEnabled()) {
LOG.debug("Got {} custom ISO3166 codes from source.", countries.size());
}
// check against repository
for (final Custom3166Entry countryInfo : countries) {
final Country country = countryRepository.findByCode3(countryInfo.getCode3());
if (country == null) {
LOG.info("ISO3166 code {} is not registered: {}", countryInfo.getCode3(), countryInfo);
final Country newCountry = new Country();
newCountry.setCode3(countryInfo.getCode3());
newCountry.setName(countryInfo.getName());
newCountry.setWikiLink(countryInfo.getUrl());
countryRepository.save(newCountry);
LOG.info("Added ISO3166 code {}", newCountry);
} else {
LOG.debug("Updating existing custom ISO3166 code {}", country.getCode3());
country.setName(countryInfo.getName());
country.setWikiLink(countryInfo.getUrl());
countryRepository.save(country);
}
}
}
private void updateDavrosCountries() throws IOException {
if (davrosCountrySource == null) {
LOG.warn("davros.org contry source not available");
LOG.warn("davros.org country source not available");
return;
}
......@@ -325,7 +368,7 @@ public class GeoServiceImpl implements GeoService {
private void updateGeoNamesCountries() throws IOException {
if (geoNamesCountrySource == null) {
LOG.warn("geonames.org contry source not available");
LOG.warn("geonames.org country source not available");
return;
}
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service.worker;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.opencsv.CSVReader;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author Maxym Borodenko
*/
@Component
public class CustomISO3166Source {
private static final String CUSTOM_CSV_RESOURCE = "/iso3166/custom.csv";
public static final String[] CUSTOM_COUNTRIES_HEADERS = { "NAME", "CODE3", "URL" };
public static final int COL_NAME = 0;
public static final int COL_CODE3 = 1;
public static final int COL_URL = 2;
public static final Logger LOG = LoggerFactory.getLogger(CustomISO3166Source.class);
public List<Custom3166Entry> fetchCountryData() throws IOException {
try (CSVReader reader = new CSVReader(new FileReader(getClass().getResource(CUSTOM_CSV_RESOURCE).getPath()), ',')) {
// Ensure headers match known format
final String[] headers = reader.readNext();
LOG.warn("{} headers: {}", CUSTOM_CSV_RESOURCE, ArrayUtils.toString(headers, "<null>"));
if (CUSTOM_COUNTRIES_HEADERS.length != headers.length || !Arrays.equals(CUSTOM_COUNTRIES_HEADERS, headers)) {
throw new ExtraCountryUpdateException(CUSTOM_CSV_RESOURCE + " headers mismatch: " + ArrayUtils.toString(headers, "<null>"));
}
final List<Custom3166Entry> dataToImport = new ArrayList<>();
String[] line;
while ((line = reader.readNext()) != null) {
for (int i = 0; i < line.length; i++) {
if (line[i].equals("null") || StringUtils.isBlank(line[i])) {
line[i] = null;
}
}
dataToImport.add(new Custom3166Entry(line[COL_NAME], line[COL_CODE3], line[COL_URL]));
}
LOG.info("Done reading {}", CUSTOM_CSV_RESOURCE);
return dataToImport;
} catch (ExtraCountryUpdateException e) {
throw new IOException(e);
}
}
/**
* The Class Custom3166Entry.
*/
public static class Custom3166Entry {
private final String name;
private final String code3;
private final String url;
/**
* Instantiates a new country info.
*
* @param code3 the code 3
* @param name the name
* @param url the url
*/
public Custom3166Entry(final String name, final String code3, final String url) {
this.name = name;
this.code3 = code3;
this.url = url;
}
/**
* Gets the code 3.
*
* @return the code 3
*/
public String getCode3() {
return code3;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets the URL.
*
* @return the url
*/
public String getUrl() {
return url;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("name=").append(name).append(" code3=").append(code3).append(" URL=").append(url);
return sb.toString();
}
}
public static class ExtraCountryUpdateException extends Exception {
private static final long serialVersionUID = -747223163381736058L;
public ExtraCountryUpdateException(final String message) {
super(message);
}
}
}
NAME,CODE3,URL
AfricaRice,XAB,http://www.africarice.org/
Bioversity,XAC,https://www.bioversityinternational.org/
CIAT,XAD,https://ciat.cgiar.org/
CIMMYT,XAE,https://www.cimmyt.org/
CIP,XAF,https://cipotato.org/
ICARDA,XAG,http://www.icarda.org/
ICRAF,XAH,http://www.worldagroforestry.org/
ICRISAT,XAI,https://www.icrisat.org/
IITA,XAJ,http://www.iita.org/
ILRI,XAK,https://www.ilri.org/
IRRI,XAM,http://irri.org/
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment