Commit c0b364eb authored by Alexander Dolzhenko's avatar Alexander Dolzhenko Committed by Matija Obreza

Geographical Regions

parent 1698424d
......@@ -160,7 +160,7 @@ module.exports = function(grunt) {
'bower_components/leaflet/dist/leaflet.js', 'bower_components/leaflet-locationfilter/src/locationfilter.js', 'bower_components/bootstrap-sass/assets/javascripts/bootstrap.js',
'bower_components/jquery-ui/jquery-ui.js', 'bower_components/jquery-ui/ui/autocomplete.js',
//
'bower_components/dyn-css/lib/dyncss.js' ],
'bower_components/dyn-css/lib/dyncss.js', 'bower_components/jstree/dist/jstree.min.js'],
dest : '<%= app.dist %>/js/libraries.js',
},
app : {
......
......@@ -17,6 +17,7 @@
"webfont-notosans": "~0.1.0",
"fontawesome": "~4.4.0",
"highcharts": "~4.1.8",
"highmaps-beta": "highmaps#~1.0.1"
"highmaps-beta": "highmaps#~1.0.1",
"jstree": "~3.2.1"
}
}
......@@ -22,4 +22,6 @@ public interface GeoRegionService {
GeoRegion getRegion(Country country);
List<GeoRegion> getChildren(String regionIsoCode);
}
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.xml.parsers.ParserConfigurationException;
......@@ -35,7 +37,9 @@ public class GeoRegionServiceImpl implements GeoRegionService {
@Override
public GeoRegion find(String regionIsoCode) {
return geoRegionRepository.findeByIsoCode(regionIsoCode);
GeoRegion geoRegion = geoRegionRepository.findeByIsoCode(regionIsoCode);
geoRegion.getCountries().size();
return geoRegion;
}
@Override
......@@ -50,7 +54,20 @@ public class GeoRegionServiceImpl implements GeoRegionService {
@Override
public GeoRegion getRegion(Country country) {
return geoRegionRepository.findByCountry(country);
GeoRegion geoRegion = geoRegionRepository.findByCountry(country);
geoRegion.getCountries().size();
return geoRegion;
}
@Override
public List<GeoRegion> getChildren(String regionIsoCode) {
List<GeoRegion> all = findAll();
GeoRegion region = find(regionIsoCode);
List<GeoRegion> geoRegions = new ArrayList<>();
geoRegions.addAll(all.stream().filter(reg -> isChild(reg, region.getIsoCode())).collect(Collectors.toList()));
return geoRegions;
}
@Override
......@@ -101,4 +118,10 @@ 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));
}
}
package org.genesys2.server.servlet.controller;
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.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")
public class GeoRegionController extends BaseController {
@Autowired
GeoRegionService geoRegionService;
@Autowired
CountryRepository countryRepository;
@RequestMapping
public String viewAll() {
return "/region/index";
}
@RequestMapping("/{isoCode}")
public String viewRegion(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 list page");
return "redirect:/geo/regions/list/" + code;
}
List<Country> countryList = geoRegion.getCountries();
Country.sort(countryList, getLocale());
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";
}
}
package org.genesys2.server.servlet.controller.rest;
import java.util.ArrayList;
import java.util.List;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.server.persistence.domain.CountryRepository;
import org.genesys2.server.service.GeoRegionService;
import org.genesys2.server.servlet.controller.rest.model.GeoJsData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = {"/api/v0/geo/regions", "/json/v0/geo/regions"})
public class GeoController extends RestController {
@Autowired
GeoRegionService geoRegionService;
@Autowired
protected MessageSource messageSource;
@Autowired
CountryRepository countryRepository;
@RequestMapping(value = "", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public
@ResponseBody
Object getAllGeoRegions() {
List<GeoRegion> regions = geoRegionService.findAll();
List<GeoJsData> geoData = new ArrayList<>();
for (GeoRegion region : regions) {
GeoJsData jsData = new GeoJsData();
jsData.setId(region.getIsoCode());
jsData.setText(region.getName(LocaleContextHolder.getLocale()));
if (region.getParentRegion() != null) {
jsData.setParent(region.getParentRegion().getIsoCode());
} else {
jsData.setParent("#");
}
GeoRegion geoRegion = geoRegionService.find(region.getIsoCode());
if (geoRegion.getCountries().size() > 0) {
jsData.setHref("/geo/regions/" + region.getIsoCode());
}
geoData.add(jsData);
}
return geoData;
}
@RequestMapping(value = "/{isoCode}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public
@ResponseBody
Object getGeoRegions(@PathVariable(value = "isoCode") String code) {
GeoRegion geoRegion = geoRegionService.find(code);
List<GeoRegion> regions = geoRegionService.getChildren(geoRegion.getIsoCode());
List<GeoJsData> geoData = new ArrayList<>();
GeoJsData currentRegion = new GeoJsData();
currentRegion.setId(geoRegion.getIsoCode());
currentRegion.setText(geoRegion.getName(LocaleContextHolder.getLocale()));
currentRegion.setParent("#");
geoData.add(currentRegion);
for (GeoRegion region : regions) {
GeoJsData jsData = new GeoJsData();
jsData.setId(region.getIsoCode());
jsData.setText(region.getName(LocaleContextHolder.getLocale()));
if (region.getParentRegion() != null) {
jsData.setParent(region.getParentRegion().getIsoCode());
} else {
jsData.setParent("#");
}
GeoRegion childRegion = geoRegionService.find(region.getIsoCode());
if (childRegion.getCountries().size() > 0) {
jsData.setHref("/geo/regions/" + region.getIsoCode());
}
geoData.add(jsData);
}
return geoData;
}
}
\ No newline at end of file
package org.genesys2.server.servlet.controller.rest.model;
public class GeoJsData {
private String id;
private String parent;
private String text;
private String href;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
}
......@@ -725,3 +725,8 @@ userlist.description=List description
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.show.world = Show all world regions
......@@ -709,3 +709,8 @@ chart.attribution-text=www.genesys-pgr.org
chart.collections.series=Количество образцов в генофондах
label.load-more-data=Загрузить еще данные...
region.page.list.title = Список регионов
#region.page.list.intro =
region.page.show.parent = Показать родительский регион
region.page.show.world = Показать все регионы
<!DOCTYPE html>
<%@include file="/WEB-INF/jsp/init.jsp" %>
<html>
<head>
<title><spring:message code="region.page.list.title"/></title>
</head>
<body>
<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>
<content tag="javascript">
<%--<script src="<c:url value="/html/js/jstree.min.js" />"></script>--%>
<script type="text/javascript">
$(document).ready(function () {
$("#regionTree").jstree({
'core': {
'data': {
"url": '${url}' ,
"dataType": "json"
}
}
}).bind('select_node.jstree', function (e, data) {
if (!!data.node.original.href) {
document.location.href = '/' + '${pageContext.response.locale}' + data.node.original.href;
}
return data.instance.toggle_node(data.node);
}).bind("loaded.jstree", function (event, data) {
$(this).jstree("open_all");
})
});
</script>
</content>
</body>
</html>
<!DOCTYPE html>
<%@include file="/WEB-INF/jsp/init.jsp" %>
<html>
<head>
<title><spring:message code="country.page.list.title"/></title>
</head>
<body>
<cms:informative-h1 title="country.page.list.title" fancy="true" info="country.page.list.intro"/>
<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>
<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>
</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