Commit 06a1f5e3 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added "replacedBy" to Country for 1-1 replacements

parent 75eab597
......@@ -22,7 +22,9 @@ import java.util.Locale;
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;
......@@ -46,7 +48,7 @@ public class Country extends BusinessModel {
@Column(nullable = false, unique = true, length = 3)
@Field(name = "title")
private String code3;
@Column(unique = false, length = 2)
private String code2;
private boolean current;
......@@ -71,6 +73,9 @@ public class Country extends BusinessModel {
@Transient
private JsonNode nameJ;
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "replacedBy")
private Country replacedBy;
public Country() {
}
......@@ -171,4 +176,12 @@ public class Country extends BusinessModel {
public void setWikiLink(String wikiLink) {
this.wikiLink = wikiLink;
}
public Country getReplacedBy() {
return replacedBy;
}
public void setReplacedBy(Country replacedBy) {
this.replacedBy = replacedBy;
}
}
......@@ -38,6 +38,8 @@ public interface CountryRepository extends JpaRepository<Country, Long> {
@Query("from Country c where c.current=false")
List<Country> findInactive();
List<Country> findByCurrent(boolean current);
Page<Country> findByCurrent(boolean current, Pageable pageable);
@Modifying
......
......@@ -45,6 +45,14 @@ public interface GeoService {
List<Country> listAll(Locale locale);
/**
* Lists only "current" entries
*
* @param locale
* @return
*/
List<Country> listActive(Locale locale);
Country getCountryByRefnameId(long refnameId);
List<Long> listCountryRefnameIds();
......@@ -53,4 +61,13 @@ public interface GeoService {
Country updateCountryWiki(String code3, String wiki);
/**
* Get active country, following "replacedBy" where possible.
*
* @param ISO3
* code
* @return
*/
Country getCurrentCountry(String code3);
}
......@@ -19,8 +19,10 @@ package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
......@@ -66,6 +68,18 @@ public class GeoServiceImpl implements GeoService {
return all;
}
@Override
public List<Country> listActive(final Locale locale) {
List<Country> all = countryRepository.findByCurrent(true);
Collections.sort(all, new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
return o1.getName(locale).compareTo(o2.getName(locale));
}
});
return all;
}
@Override
public Country getCountry(String countryString) {
Country country = null;
......@@ -77,6 +91,36 @@ public class GeoServiceImpl implements GeoService {
return country != null ? country : countryRepository.findByName(countryString);
}
/**
* Get current country based on ISO3 code. Follow replacedBy where possible.
*
* @param code3
* @return
*/
@Override
public Country getCurrentCountry(String code3) {
if (code3 == null) {
return null;
}
Country country = getCountry(code3);
if (country != null) {
// Loop detection
Set<Long> seenCountryId = new HashSet<Long>();
while (!seenCountryId.contains(country.getId()) && country.getReplacedBy() != null) {
LOG.info("Country " + country.getCode3() + " replaced by " + country.getReplacedBy());
// Update reference
country = country.getReplacedBy();
// Put countryId to seen list
seenCountryId.add(country.getId());
}
}
return country;
}
@Override
public Country getCountryByRefnameId(long refnameId) {
return countryRepository.findByRefnameId(refnameId);
......@@ -90,7 +134,7 @@ public class GeoServiceImpl implements GeoService {
// update from Davros, it has info on inactive country codes
updateDavrosCountries();
LOG.info("Country data up to date");
}
......
......@@ -58,7 +58,7 @@ public class InstituteUpdater {
@Autowired
private GeoService geoService;
@Autowired
private TaskExecutor taskExecutor;
......@@ -237,15 +237,12 @@ public class InstituteUpdater {
Double alt = parseDoubleIgnore0(altitude, 1);
faoInstitute.setAltitude(alt);
// Update institute country if null or when not matching the code
// FIXME Some countries have "changed" (e.g. YUG)
if (faoInstitute.getCountry() == null || ! faoInstitute.getCountry().getCode3().equals(instCode.substring(0, 3))) {
faoInstitute.setCountry(geoService.getCountry(instCode.substring(0, 3)));
}
// Update institute country if null or when not matching the
// code
faoInstitute.setCountry(geoService.getCurrentCountry(faoInstitute.getCode().substring(0, 3)));
return true;
}
});
}
}
......@@ -57,7 +57,7 @@ public class CountryController extends BaseController {
@RequestMapping
public String view(ModelMap model) {
model.addAttribute("countries", geoService.listAll(getLocale()));
model.addAttribute("countries", geoService.listActive(getLocale()));
return "/country/index";
}
......
......@@ -106,7 +106,8 @@ country.stat.countByOrigin={0} accessions registered in Genesys come from this c
country.statistics=Country statistics
country.accessions.from=Accessions collected in {0}
country.more-information=More information:
country.replaced-by=Country code is replaced by: {0}
select-country=Select country
faoInstitutes.page.list.title=WIEWS Institutes
faoInstitutes.page.profile.title=WIEWS {0}
......
......@@ -15,6 +15,10 @@
<c:if test="${not country.current}">
<div class="alert alert-info">
<spring:message code="country.page.not-current" />
<c:if test="${country.replacedBy != null}">
<spring:message code="country.replaced-by" arguments="${country.replacedBy.code3}" />
<a href="${country.replacedBy.code3.toLowerCase()}"><c:out value="${country.replacedBy.getName(pageContext.response.locale)}" /></a>
</c:if>
</div>
</c:if>
......
......@@ -24,22 +24,48 @@
<a href="<c:url value="/org/${organization.slug}/data" />"><spring:message code="view.accessions" /></a>
</div>
<c:set value="" var="countryName" />
<ul class="funny-list">
<c:forEach items="${members}" var="faoInstitute" varStatus="status">
<c:if test="${countryName ne faoInstitute.country.getName(pageContext.response.locale)}">
<c:set var="countryName" value="${faoInstitute.country.getName(pageContext.response.locale)}" />
<li class="hoofdleter"><c:out value="${countryName}" />
<%-- <small><a href="#letter-top"><spring:message code="jump-to-top" /></a></small> --%>
</li>
</c:if>
<li class="clearfix ${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show pull-left" href="<c:url value="/wiews/${faoInstitute.code.toLowerCase()}" />"><b><c:out value="${faoInstitute.code}" /></b> <c:out value="${faoInstitute.fullName}" /></a>
<div class="pull-right">
<spring:message code="faoInstitute.accessionCount" arguments="${faoInstitute.accessionCount}" />
</div></li>
</c:forEach>
</ul>
<div class="form-horizontal">
<div class="form-group">
<label class="col-lg-2 control-label"> <spring:message code="select-country" /></label>
<div class="col-lg-3">
<select id="countryNavigation" class="form-control">
<c:forEach items="${members}" var="faoInstitute" varStatus="status">
<c:if test="${countryName ne faoInstitute.country.getName(pageContext.response.locale)}">
<c:set var="countryName" value="${faoInstitute.country.getName(pageContext.response.locale)}" />
<option value="${faoInstitute.country.code3}">
<c:out value="${faoInstitute.country.getName(pageContext.response.locale)}" />
</option>
</c:if>
</c:forEach>
</select>
</div>
</div>
<c:remove var="countryName" />
<c:set value="" var="countryName" />
<ul class="funny-list">
<c:forEach items="${members}" var="faoInstitute" varStatus="status">
<c:if test="${countryName ne faoInstitute.country.getName(pageContext.response.locale)}">
<c:set var="countryName" value="${faoInstitute.country.getName(pageContext.response.locale)}" />
<li class="hoofdleter" id="nav-${faoInstitute.country.code3}"><c:out value="${countryName}" /> <small><a href="#"><spring:message code="jump-to-top" /></a></small></li>
</c:if>
<li class="clearfix ${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show pull-left" href="<c:url value="/wiews/${faoInstitute.code.toLowerCase()}" />"><b><c:out value="${faoInstitute.code}" /></b> <c:out value="${faoInstitute.fullName}" /></a>
<div class="pull-right">
<spring:message code="faoInstitute.accessionCount" arguments="${faoInstitute.accessionCount}" />
</div></li>
</c:forEach>
</ul>
<script type="text/javascript">
jQuery(document).ready(function() {
$("#countryNavigation").on("change", function() {
window.location.hash="nav-" + this.value;
});
});
</script>
</body>
</html>
\ 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