Commit 04ede25e authored by Matija Obreza's avatar Matija Obreza

FAO Regions cleanup

parent c0b364eb
......@@ -14,7 +14,7 @@
- Use DS to hold results of calculation, similar to worldclim DS
- Re-calculation needs to happen after any update to Accession or AccessionRelated (same as ES update)
- PDCI dataset is downloadable as Excel (for selected accessions)
- PDCI dataset is downloadable as Excel: this works on institute level, but not for selected accessions
- WorldClim dataset is downloadable as Excel (for selected accessions)
# TODO Accession Lists
......
package org.genesys2.server.model.impl;
import java.io.IOException;
import java.util.Collections;
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.FetchType;
......@@ -22,6 +25,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "georegion")
@Cacheable
public class GeoRegion extends VersionedModel {
private static final long serialVersionUID = -1L;
......@@ -114,4 +118,13 @@ public class GeoRegion extends VersionedModel {
return this.nameJ != null && this.nameJ.has(locale.getLanguage()) ? this.nameJ.get(locale.getLanguage()).textValue() : this.name;
}
public static void sort(List<GeoRegion> all, Locale locale) {
Collections.sort(all, new Comparator<GeoRegion>() {
@Override
public int compare(GeoRegion o1, GeoRegion o2) {
return o1.getName(locale).compareTo(o2.getName(locale));
}
});
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ 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(String isoCode);
GeoRegion findByIsoCode(String isoCode);
@Query("select c.region from Country c where c = ?1")
GeoRegion findByCountry(Country country);
......
......@@ -37,17 +37,19 @@ public class GeoRegionServiceImpl implements GeoRegionService {
@Override
public GeoRegion find(String regionIsoCode) {
GeoRegion geoRegion = geoRegionRepository.findeByIsoCode(regionIsoCode);
GeoRegion geoRegion = geoRegionRepository.findByIsoCode(regionIsoCode);
geoRegion.getCountries().size();
return geoRegion;
}
@Override
@Transactional(readOnly = false)
public void save(GeoRegion geoRegion) {
geoRegionRepository.save(geoRegion);
}
@Override
@Transactional(readOnly = false)
public void delete(GeoRegion geoRegion) {
geoRegionRepository.delete(geoRegion);
}
......@@ -121,7 +123,8 @@ 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.servlet.controller;
import java.util.List;
import java.util.stream.Collectors;
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.service.GeoRegionService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/geo/regions")
......@@ -37,40 +39,23 @@ public class GeoRegionController extends BaseController {
if (geoRegion == null) {
_logger.error("Can't find region for region code " + code);
return "redirect:/geo/regions";
throw new ResourceNotFoundException("Can't find region for region code " + code);
}
if (geoRegion.getCountries().size()==0){
_logger.info("Redirect for geoRegion " + geoRegion.getName() + " to the list page");
return "redirect:/geo/regions/list/" + code;
}
List<Country> countryList = geoRegion.getCountries();
List<Country> countryList = geoRegion.getCountries().stream().filter(country -> isCurrent(country)).collect(Collectors.toList());
Country.sort(countryList, getLocale());
model.addAttribute("region", geoRegion);
List<GeoRegion> subRegions = geoRegionService.getChildren(geoRegion.getIsoCode());
GeoRegion.sort(subRegions, getLocale());
model.addAttribute("subRegions", subRegions);
model.addAttribute("countries", countryList);
model.addAttribute("parent", geoRegion.getParentRegion().getIsoCode());
return "/region/region";
}
@RequestMapping("/list/{isoCode}")
public String viewRegionList(ModelMap model, @PathVariable(value = "isoCode") String code) {
GeoRegion geoRegion = geoRegionService.find(code);
if (geoRegion == null) {
_logger.error("Can't find region for region code " + code);
return "redirect:/geo/regions";
}
if (geoRegion.getCountries().size() > 0) {
_logger.info("Redirect for geoRegion " + geoRegion.getName() + " to the region page");
return "redirect:/geo/regions";
}
model.addAttribute("isoCode", code);
return "/region/index";
}
private boolean isCurrent(Country country) {
return country.isCurrent();
}
}
......@@ -726,7 +726,10 @@ userlist.disconnect=Disconnect list
userlist.update-list=Save updated list
userlist.make-new-list=Create new list
region.page.list.title = Regions
#region.page.list.intro =
region.page.show.parent = Show parent region
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.show.parent = Show parent region {0}
region.page.show.world = Show all world regions
region.countries-in-region=List of countries in {0}
region.regions-in-region=FAO Regions in {0}
region.show-all-regions=List all FAO regions
......@@ -10,17 +10,8 @@
<cms:informative-h1 title="region.page.list.title" fancy="true" info="region.page.list.intro"/>
<c:set var="url" value="/${pageContext.response.locale}/json/v0/geo/regions${not empty isoCode? '/'.concat(isoCode) :''}"/>
<h4>
<c:if test="${not empty isoCode}">
<a href="<c:url value="/geo/regions"/>"><span class="glyphicon glyphicon-eye-open"></span><span><spring:message code="region.page.show.world" /></span></a>
</c:if>
</h4>
<div class="main-col-header clearfix">
<div class="nav-header pull-left">
<div id="regionTree"></div>
</div>
</div>
<div id="regionTree"></div>
<content tag="javascript">
<%--<script src="<c:url value="/html/js/jstree.min.js" />"></script>--%>
......
......@@ -4,20 +4,44 @@
<html>
<head>
<title><spring:message code="country.page.list.title"/></title>
<title><c:out value="${region.getName(pageContext.response.locale)}" /></title>
</head>
<body>
<cms:informative-h1 title="country.page.list.title" fancy="true" info="country.page.list.intro"/>
<h1>
<c:out value="${region.getName(pageContext.response.locale)}" />
</h1>
<h4>
<a href="<c:url value="/geo/regions/${parent}"/>"><span class="glyphicon glyphicon-eye-open"></span><span><spring:message code="region.page.show.parent" /></span></a>
</h4>
<div class="results main-col-header">
<h4>
<a class="btn btn-default" href="<c:url value="/geo/regions" />"><spring:message code="region.show-all-regions" /></a>
<c:if test="${region.parentRegion ne null}">
<a class="btn btn-default" href="<c:url value="/geo/regions/${region.parentRegion.isoCode}"/>">
<span class="glyphicon glyphicon-eye-open"></span>
<span>
<spring:message code="region.page.show.parent" arguments="${region.parentRegion.getName(pageContext.response.locale)}" />
</span>
</a>
</c:if>
</h4>
</div>
<ul class="funny-list">
<c:forEach items="${countries}" var="country" varStatus="status">
<li class="${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show ${not country.current ? 'disabled' : ''}" href="<c:url value="/geo/${country.code3}" />"><c:out value="${country.getName(pageContext.response.locale)}" /></a></li>
</c:forEach>
</ul>
<c:if test="${subRegions ne null and subRegions.size() gt 0}">
<h3><spring:message code="region.regions-in-region" arguments="${region.getName(pageContext.response.locale)}" /></h3>
<ul class="funny-list">
<c:forEach items="${subRegions}" var="subRegion" varStatus="status">
<li class="${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show" href="<c:url value="/geo/regions/${subRegion.isoCode}" />"><c:out value="${subRegion.getName(pageContext.response.locale)}" /></a></li>
</c:forEach>
</ul>
</c:if>
<c:if test="${countries ne null and countries.size() gt 0}">
<h3><spring:message code="region.countries-in-region" arguments="${region.getName(pageContext.response.locale)}" /></h3>
<ul class="funny-list">
<c:forEach items="${countries}" var="country" varStatus="status">
<li class="${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show ${not country.current ? 'disabled' : ''}" href="<c:url value="/geo/${country.code3}" />"><c:out value="${country.getName(pageContext.response.locale)}" /></a></li>
</c:forEach>
</ul>
</c:if>
</body>
</html>
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