Commit e97cbc83 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

Region and Territory data first commit

parent 885d7f46
......@@ -62,5 +62,4 @@ public class FirstRunListener extends RunAsAdminListener {
instituteUpdater.updateFaoInstitutes();
}
}
}
package org.genesys2.server.listener.sample;
import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.service.GeoRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class GeoRegionListener extends RunAsAdminListener {
@Autowired
GeoRegionService geoRegionService;
@Override
protected void init() throws Exception {
if (geoRegionService.findAll().isEmpty()) {
_logger.info("No geoRegion data found. Loading");
geoRegionService.updateGeoRegionData();
}
}
}
......@@ -23,14 +23,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.*;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.servlet.controller.rest.serialization.CountrySerializer;
......@@ -62,6 +55,10 @@ public class Country extends BusinessModel {
private String codeNum;
private Long refnameId;
@ManyToOne()
@JoinTable(name = "georegioncountry", joinColumns = @JoinColumn(name = "coutryId"), inverseJoinColumns = @JoinColumn(name = "georegionId"))
private GeoRegion region;
/**
* Localized names
*/
......@@ -197,4 +194,12 @@ public class Country extends BusinessModel {
}
});
}
public GeoRegion getRegion() {
return region;
}
public void setRegion(GeoRegion region) {
this.region = region;
}
}
package org.genesys2.server.model.impl;
import org.genesys2.server.model.BusinessModel;
import javax.persistence.*;
import java.util.List;
@Entity
public class GeoRegion extends BusinessModel {
private static final long serialVersionUID = -1L;
@Column
private String isoCode;
@Column
private String name;
@Column
private Long parentId;
@Transient
private GeoRegion parentGeoRegion;
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "georegioncountry", joinColumns = @JoinColumn(name = "georegionId"), inverseJoinColumns = @JoinColumn(name = "coutryId"))
private List<Country> members;
public String getIsoCode() {
return isoCode;
}
public void setIsoCode(String isoCode) {
this.isoCode = isoCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public GeoRegion getParentGeoRegion() {
return parentGeoRegion;
}
public void setParentGeoRegion(GeoRegion parentGeoRegion) {
this.parentGeoRegion = parentGeoRegion;
}
public List<Country> getMembers() {
return members;
}
public void setMembers(List<Country> members) {
this.members = members;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
}
\ No newline at end of file
package org.genesys2.server.model.impl;
public class GeoRegionCountry{
}
......@@ -64,6 +64,6 @@ public interface CountryRepository extends JpaRepository<Country, Long> {
@Query("select distinct c from Country c where lower(c.code3) like lower(?1) or lower(c.code2) like lower(?1) or lower(c.name) like lower(?1) or lower(c.nameL) like lower(?1)")
List<Country> autocomplete(String ac, Pageable pageable);
// @Query("select distinct c from Country c where c.region in ( ?1 )")
// List<Country> findByRegions(List<Region> regions);
@Query("select distinct c from Country c where c.region.id = ?1")
List<Country> findByRegions(Long id );
}
package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.impl.GeoRegion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface GeoRegionRepository extends JpaRepository<GeoRegion, Long>{
@Query("select distinct gr from GeoRegion gr where gr.isoCode = ?1")
GeoRegion findeByIsoCode(int isoCode);
}
package org.genesys2.server.service;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.GeoRegion;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
public interface GeoRegionService {
GeoRegion find(int regionIsoCode);
void save(GeoRegion geoRegion);
void delete(GeoRegion geoRegion);
List<Country> listCountries(long regionIsoCode);
void removeCountry(int regionIsoCode, Country country);
void addCountry(int regionIsoCode, Country country);
List<GeoRegion> findAll();
void updateGeoRegionData() throws IOException, ParserConfigurationException, SAXException;
}
package org.genesys2.server.service.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.server.persistence.domain.CountryRepository;
import org.genesys2.server.persistence.domain.GeoRegionRepository;
import org.genesys2.server.service.GeoRegionService;
import org.genesys2.server.service.worker.GeoRegionDataCLDR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
@Service
public class GeoRegionServiceImpl implements GeoRegionService {
public static final Log LOG = LogFactory.getLog(GeoRegionServiceImpl.class);
@Autowired
CountryRepository countryRepository;
@Autowired
GeoRegionRepository geoRegionRepository;
@Autowired
GeoRegionDataCLDR geoRegionDataCLDR;
@Override
public GeoRegion find(int regionIsoCode) {
return geoRegionRepository.findeByIsoCode(regionIsoCode);
}
@Override
public void save(GeoRegion geoRegion) {
geoRegionRepository.save(geoRegion);
}
@Override
public void delete(GeoRegion geoRegion) {
geoRegionRepository.delete(geoRegion);
}
@Override
public List<Country> listCountries(long regionIsoCode) {
return countryRepository.findByRegions(regionIsoCode);
}
@Override
public void removeCountry(int regionIsoCode, Country country) {
countryRepository.delete(country);
}
@Override
public void addCountry(int regionIsoCode, Country country) {
country.setCodeNum(String.valueOf(regionIsoCode));
countryRepository.save(country);
}
@Override
public List<GeoRegion> findAll() {
return geoRegionRepository.findAll();
}
@Override
@Transactional(readOnly = false)
public void updateGeoRegionData() throws IOException, ParserConfigurationException, SAXException {
//update current geoRegions
updateGeoRegions();
}
private void updateGeoRegions() throws IOException, ParserConfigurationException, SAXException {
if (geoRegionDataCLDR == null) {
LOG.warn("unicode.org geoRegions source not available");
return;
}
List<GeoRegion> geoRegionList = geoRegionDataCLDR.getGeoRigionDataCLDR();
if(!geoRegionList.isEmpty()){
geoRegionRepository.save(geoRegionList);
}
}
}
package org.genesys2.server.service.worker;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.util.ParserForCLDR;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@Component
public class GeoRegionDataCLDR {
private static final String CLDR_URL = "http://unicode.org/Public/cldr/26.0.1/core.zip";
public static final Log LOG = LogFactory.getLog(GeoRegionDataCLDR.class);
public List<GeoRegion> getGeoRigionDataCLDR() throws IOException, ParserConfigurationException, SAXException {
ZipFile zipFile = getCoreZip();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
ParserForCLDR parserForCLDR = new ParserForCLDR();
final Set<String> supportedLocales = new HashSet<>();
supportedLocales.add("en");
supportedLocales.add("ar");
supportedLocales.add("de");
supportedLocales.add("fa");
supportedLocales.add("fr");
supportedLocales.add("pt");
supportedLocales.add("ru");
supportedLocales.add("zh");
supportedLocales.add("es");
ZipEntry zipEntry;
List<GeoRegion> geoRegionList = new ArrayList<>();
InputStream inputStream;
for (String language : supportedLocales) {
String fileName = "common/main/" + language + ".xml";
zipEntry = zipFile.getEntry(fileName);
inputStream = zipFile.getInputStream(zipEntry);
parser.parse(inputStream, parserForCLDR);
List<GeoRegion> geoRegionsWithAssigndParents = assignParents(parserForCLDR.getRegionList());
geoRegionList.addAll(geoRegionsWithAssigndParents);
inputStream.close();
}
return geoRegionList;
}
private List<GeoRegion> assignParents(Map<String, GeoRegion> geoRegions) {
String world = "001";
String continents = "002,019,142,150,009";
String forAfrica = "015,014,017,018,011";
String forAsia = "143,030,034,035,145";
String forEurope = "151,154,039,155";
String forOceania = "053,054,057,061";
String forAmerica = "419,003,021";
String forLatinAmerica = "029,013,005";
for(String isoCode : continents.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(world));
}
for(String isoCode : forAfrica.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[0]));
}
for(String isoCode : forAmerica.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[1]));
}
for(String isoCode : forAsia.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[2]));
}
for(String isoCode : forEurope.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[3]));
}
for(String isoCode : forOceania.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[4]));
}
for(String isoCode : forLatinAmerica.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(forAmerica.split(",")[0]));
}
return new ArrayList<>(geoRegions.values());
}
private ZipFile getCoreZip() throws IOException {
URL url = new URL(CLDR_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
File tempFileForXml = File.createTempFile("core", ".zip");
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(tempFileForXml);
copyZip(in, out, 1024);
ZipFile zipFile = new ZipFile(tempFileForXml);
out.close();
return zipFile;
}
private void copyZip(InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}
}
package org.genesys2.util;
import org.genesys2.server.model.impl.GeoRegion;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ParserForCLDR extends DefaultHandler{
private GeoRegion geoRegion;
private Map<String, GeoRegion> regionMap;
//needs for avoid duplicate geoRegions in regionList
private List<String> isoCodeList;
private String xmlTag = "";
private Attributes attributes;
public Map<String, GeoRegion> getRegionList() {
return regionMap;
}
@Override
public void startDocument() throws SAXException {
regionMap = new HashMap<>();
isoCodeList = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
geoRegion = new GeoRegion();
this.xmlTag = qName;
this.attributes = attributes;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String attribute = attributes.getValue("type");
if(this.xmlTag.equalsIgnoreCase("territory") && attribute.matches("[0-9]+") && !isoCodeList.contains(attribute)) {
geoRegion.setIsoCode(attribute);
geoRegion.setName(new String(ch, start, length));
isoCodeList.add(attribute);
regionMap.put(geoRegion.getIsoCode(), geoRegion);
}
}
}
Supports Markdown
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