Commit 0834c4e8 authored by Matija Obreza's avatar Matija Obreza
Browse files

Relink GeoRegion’s parent on update

parent 1115a13a
......@@ -3,18 +3,19 @@ package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.xml.parsers.ParserConfigurationException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -24,7 +25,7 @@ import org.xml.sax.SAXException;
@Transactional(readOnly = true)
public class GeoRegionServiceImpl implements GeoRegionService {
public static final Log LOG = LogFactory.getLog(GeoRegionServiceImpl.class);
private static final Logger LOG = LoggerFactory.getLogger(GeoRegionServiceImpl.class);
@Autowired
CountryRepository countryRepository;
......@@ -90,36 +91,34 @@ public class GeoRegionServiceImpl implements GeoRegionService {
LOG.warn("unicode.org geoRegions source not available");
return;
}
List<GeoRegion> geoRegionList = geoRegionDataCLDR.getGeoRegionDataCLDR();
if (null == geoRegionRepository.findAll() || geoRegionRepository.findAll().isEmpty()) {
geoRegionRepository.save(geoRegionList);
for (GeoRegion geoRegion : geoRegionList) {
if (null != geoRegion.getCountries()) {
for (Country country : geoRegion.getCountries()) {
if (null != country) {
country.setRegion(geoRegion);
}
}
}
List<GeoRegion> existingRegions = geoRegionRepository.findAll();
for (final GeoRegion src : geoRegionDataCLDR.getGeoRegionDataCLDR()) {
GeoRegion geoRegionForUpdate = null;
Optional<GeoRegion> match = existingRegions.stream().filter(r -> r.getIsoCode().equals(src.getIsoCode())).findFirst();
if (match.isPresent()) {
geoRegionForUpdate = match.get();
} else {
geoRegionForUpdate = new GeoRegion();
geoRegionForUpdate.setIsoCode(src.getIsoCode());
}
} else if (null != geoRegionRepository.findAll() || !geoRegionRepository.findAll().isEmpty()) {
List<GeoRegion> list = geoRegionRepository.findAll();
for (int i = 0; i < geoRegionList.size(); i++) {
GeoRegion geoRegionForUpdate = list.get(i);
geoRegionForUpdate.setName(geoRegionList.get(i).getName());
geoRegionForUpdate.setIsoCode(geoRegionList.get(i).getIsoCode());
geoRegionForUpdate.setNameL(geoRegionList.get(i).getNameL());
geoRegionForUpdate.setCountries(geoRegionList.get(i).getCountries());
geoRegionRepository.save(geoRegionForUpdate);
if (null != geoRegionForUpdate.getCountries()) {
for (Country country : geoRegionForUpdate.getCountries()) {
if (null != country) {
country.setRegion(geoRegionForUpdate);
// countryRepository.save(country);
}
geoRegionForUpdate.setName(src.getName());
geoRegionForUpdate.setNameL(src.getNameL());
geoRegionForUpdate.setCountries(src.getCountries());
if (src.getParentRegion() == null) {
geoRegionForUpdate.setParentRegion(null);
} else {
geoRegionForUpdate.setParentRegion(geoRegionRepository.findByIsoCode(src.getParentRegion().getIsoCode()));
}
geoRegionRepository.save(geoRegionForUpdate);
if (null != geoRegionForUpdate.getCountries()) {
for (Country country : geoRegionForUpdate.getCountries()) {
if (null != country) {
country.setRegion(geoRegionForUpdate);
countryRepository.save(country);
}
}
}
......@@ -128,8 +127,7 @@ public class GeoRegionServiceImpl implements GeoRegionService {
private boolean isChild(GeoRegion region, String parentIsoCode) {
return region.getParentRegion() != null
&& (region.getParentRegion().getIsoCode().equals(parentIsoCode) || isChild(region.getParentRegion(), parentIsoCode));
return region.getParentRegion() != null && (region.getParentRegion().getIsoCode().equals(parentIsoCode) || isChild(region.getParentRegion(), parentIsoCode));
}
}
package org.genesys2.server.service.worker;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.annotation.Resource;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
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.service.GeoRegionService;
import org.genesys2.server.service.GeoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -15,20 +34,6 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.annotation.Resource;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 {
......@@ -39,9 +44,6 @@ public class GeoRegionDataCLDR {
@Autowired
private GeoService geoService;
@Autowired
private GeoRegionService geoRegionService;
@Resource
private Set<String> supportedLocales;
......@@ -225,8 +227,6 @@ public class GeoRegionDataCLDR {
private Attributes attributes;
private int count = 0;
public Map<String, GeoRegion> getRegionMap() {
return regionMap;
}
......@@ -241,7 +241,6 @@ public class GeoRegionDataCLDR {
@Override
public void endDocument() throws SAXException {
super.endDocument();
count = 0;
}
@Override
......
......@@ -810,6 +810,7 @@ userlist.shared=Allow others to access the list
region.page.list.title = FAO Geographical regions
region.page.list.intro = FAO Geographical regions lists below allow you to access the data about accessions collected or maintained in the region.
region.page.profile.title={0}
region.page.show.parent = Show parent region {0}
region.page.show.world = Show all world regions
region.countries-in-region=List of countries in {0}
......
......@@ -7,11 +7,10 @@
<title><c:out value="${region.getName(pageContext.response.locale)}" /></title>
</head>
<body>
<cms:informative-h1 title="region.page.profile.title" titleArgument="${region.getName(pageContext.response.locale)}" />
<div class="informative-h1 row">
<div class="col-md-12 col-sm-12">
<h1>
<c:out value="${region.getName(pageContext.response.locale)}" />
</h1>
<c:out value="${blurp.summary}" escapeXml="false" />
</div>
</div>
......
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