Commit c5c69fdf authored by Matija Obreza's avatar Matija Obreza
Browse files

Make Region data available in country controller

parent 98969d32
package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.GeoRegion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
......@@ -8,4 +9,7 @@ public interface GeoRegionRepository extends JpaRepository<GeoRegion, Long>{
@Query("select distinct gr from GeoRegion gr where gr.isoCode = ?1")
GeoRegion findeByIsoCode(String isoCode);
@Query("select c.region from Country c where c = ?1")
GeoRegion findByCountry(Country country);
}
......@@ -16,13 +16,10 @@ public interface GeoRegionService {
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;
GeoRegion getRegion(Country country);
}
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.Country;
......@@ -13,103 +18,87 @@ 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
@Transactional(readOnly = true)
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(String 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
if (geoRegionDataCLDR == null) {
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);
}
}
}
}
} 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);
}
}
}
}
}
}
public static final Log LOG = LogFactory.getLog(GeoRegionServiceImpl.class);
@Autowired
CountryRepository countryRepository;
@Autowired
GeoRegionRepository geoRegionRepository;
@Autowired
GeoRegionDataCLDR geoRegionDataCLDR;
@Override
public GeoRegion find(String 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 GeoRegion getRegion(Country country) {
return geoRegionRepository.findByCountry(country);
}
@Override
public List<GeoRegion> findAll() {
return geoRegionRepository.findAll();
}
@Override
@Transactional(readOnly = false)
public void updateGeoRegionData() throws IOException, ParserConfigurationException, SAXException {
// update current geoRegions
if (geoRegionDataCLDR == null) {
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);
}
}
}
}
} 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);
}
}
}
}
}
}
}
......@@ -27,6 +27,7 @@ import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoRegionService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.impl.FilterHandler;
......@@ -57,6 +58,9 @@ public class CountryController extends BaseController {
@Autowired
private ContentService contentService;
@Autowired
private GeoRegionService geoRegionService;
@RequestMapping
public String view(ModelMap model) {
model.addAttribute("countries", geoService.listActive(getLocale()));
......@@ -76,6 +80,7 @@ public class CountryController extends BaseController {
throw new ResourceNotFoundException();
}
model.addAttribute("country", country);
model.addAttribute("region", geoRegionService.getRegion(country));
model.addAttribute("itpgrfa", geoService.getITPGRFAStatus(country));
model.addAttribute("blurp", contentService.getArticle(country, "blurp", getLocale()));
......
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