use of de.symeda.sormas.api.utils.DataHelper.Pair in project SORMAS-Project by hzi-braunschweig.
the class CaseFacadeEjb method getCaseMeasurePerDistrict.
@Override
public List<Pair<DistrictDto, BigDecimal>> getCaseMeasurePerDistrict(Date fromDate, Date toDate, Disease disease, CaseMeasure caseMeasure) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
Root<Case> caseRoot = cq.from(Case.class);
Root<District> districtRoot = cq.from(District.class);
Predicate filter = service.createDefaultFilter(cb, caseRoot);
if (fromDate != null || toDate != null) {
filter = service.createCaseRelevanceFilter(cb, caseRoot, fromDate, toDate);
}
if (disease != null) {
Predicate diseaseFilter = cb.equal(caseRoot.get(Case.DISEASE), disease);
filter = filter != null ? cb.and(filter, diseaseFilter) : diseaseFilter;
}
Predicate districtFilter = cb.or(cb.equal(caseRoot.get(Case.DISTRICT), districtRoot), cb.and(cb.isNull(caseRoot.get(Case.DISTRICT)), cb.equal(caseRoot.get(Case.RESPONSIBLE_DISTRICT), districtRoot)));
filter = filter != null ? cb.and(filter, districtFilter) : districtFilter;
cq.where(filter);
cq.groupBy(districtRoot);
cq.multiselect(districtRoot, cb.count(caseRoot));
if (caseMeasure == CaseMeasure.CASE_COUNT) {
cq.orderBy(cb.asc(cb.count(caseRoot)));
}
List<Object[]> results = em.createQuery(cq).getResultList();
if (caseMeasure == CaseMeasure.CASE_COUNT) {
List<Pair<DistrictDto, BigDecimal>> resultList = results.stream().map(e -> new Pair<DistrictDto, BigDecimal>(districtFacade.toDto((District) e[0]), new BigDecimal((Long) e[1]))).collect(Collectors.toList());
return resultList;
} else {
List<Pair<DistrictDto, BigDecimal>> resultList = results.stream().map(e -> {
District district = (District) e[0];
Integer population = populationDataFacade.getProjectedDistrictPopulation(district.getUuid());
Long caseCount = (Long) e[1];
if (population == null || population <= 0) {
// No or negative population - these entries will be cut off in the UI
return new Pair<DistrictDto, BigDecimal>(districtFacade.toDto(district), new BigDecimal(0));
} else {
return new Pair<DistrictDto, BigDecimal>(districtFacade.toDto(district), InfrastructureHelper.getCaseIncidence(caseCount.intValue(), population, InfrastructureHelper.CASE_INCIDENCE_DIVISOR));
}
}).sorted(new Comparator<Pair<DistrictDto, BigDecimal>>() {
@Override
public int compare(Pair<DistrictDto, BigDecimal> o1, Pair<DistrictDto, BigDecimal> o2) {
return o1.getElement1().compareTo(o2.getElement1());
}
}).collect(Collectors.toList());
return resultList;
}
}
use of de.symeda.sormas.api.utils.DataHelper.Pair in project SORMAS-Project by hzi-braunschweig.
the class DashboardMapComponent method showRegionsShapes.
private void showRegionsShapes(CaseMeasure caseMeasure, Date fromDate, Date toDate, Disease disease) {
clearRegionShapes();
map.setTileLayerOpacity(0.5f);
List<RegionReferenceDto> regions = FacadeProvider.getRegionFacade().getAllActiveByServerCountry();
List<LeafletPolygon> regionPolygons = new ArrayList<LeafletPolygon>();
// draw outlines of all regions
for (RegionReferenceDto region : regions) {
GeoLatLon[][] regionShape = FacadeProvider.getGeoShapeProvider().getRegionShape(region);
if (regionShape == null) {
continue;
}
for (GeoLatLon[] regionShapePart : regionShape) {
LeafletPolygon polygon = new LeafletPolygon();
polygon.setCaption(region.getCaption());
// fillOpacity is used, so we can still hover the region
polygon.setOptions("{\"weight\": 1, \"color\": '#444', \"fillOpacity\": 0.02}");
polygon.setLatLons(regionShapePart);
regionPolygons.add(polygon);
polygonRegions.add(region);
}
}
map.addPolygonGroup(REGIONS_GROUP_ID, regionPolygons);
List<Pair<DistrictDto, BigDecimal>> measurePerDistrict = FacadeProvider.getCaseFacade().getCaseMeasurePerDistrict(fromDate, toDate, disease, caseMeasure);
if (caseMeasure == CaseMeasure.CASE_COUNT) {
districtValuesLowerQuartile = measurePerDistrict.size() > 0 ? measurePerDistrict.get((int) (measurePerDistrict.size() * 0.25)).getElement1() : null;
districtValuesMedian = measurePerDistrict.size() > 0 ? measurePerDistrict.get((int) (measurePerDistrict.size() * 0.5)).getElement1() : null;
districtValuesUpperQuartile = measurePerDistrict.size() > 0 ? measurePerDistrict.get((int) (measurePerDistrict.size() * 0.75)).getElement1() : null;
} else {
// For case incidence, districts without or with a population <= 0 should not be
// used for the calculation of the quartiles because they will falsify the
// result
List<Pair<DistrictDto, BigDecimal>> measurePerDistrictWithoutMissingPopulations = new ArrayList<>();
measurePerDistrictWithoutMissingPopulations.addAll(measurePerDistrict);
measurePerDistrictWithoutMissingPopulations.removeIf(d -> d.getElement1() == null || d.getElement1().intValue() <= 0);
districtValuesLowerQuartile = measurePerDistrictWithoutMissingPopulations.size() > 0 ? measurePerDistrictWithoutMissingPopulations.get((int) (measurePerDistrictWithoutMissingPopulations.size() * 0.25)).getElement1() : null;
districtValuesMedian = measurePerDistrictWithoutMissingPopulations.size() > 0 ? measurePerDistrictWithoutMissingPopulations.get((int) (measurePerDistrictWithoutMissingPopulations.size() * 0.5)).getElement1() : null;
districtValuesUpperQuartile = measurePerDistrictWithoutMissingPopulations.size() > 0 ? measurePerDistrictWithoutMissingPopulations.get((int) (measurePerDistrictWithoutMissingPopulations.size() * 0.75)).getElement1() : null;
}
List<LeafletPolygon> districtPolygons = new ArrayList<LeafletPolygon>();
// Draw relevant district fills
for (Pair<DistrictDto, BigDecimal> districtMeasure : measurePerDistrict) {
DistrictDto district = districtMeasure.getElement0();
DistrictReferenceDto districtRef = district.toReference();
BigDecimal districtValue = districtMeasure.getElement1();
GeoLatLon[][] districtShape = FacadeProvider.getGeoShapeProvider().getDistrictShape(districtRef);
if (districtShape == null) {
continue;
}
String fillColor;
if (districtValue.compareTo(BigDecimal.ZERO) == 0) {
fillColor = "#000";
} else if (districtValue.compareTo(districtValuesLowerQuartile) < 0) {
fillColor = "#FEDD6C";
} else if (districtValue.compareTo(districtValuesMedian) < 0) {
fillColor = "#FDBF44";
} else if (districtValue.compareTo(districtValuesUpperQuartile) < 0) {
fillColor = "#F47B20";
} else {
fillColor = "#ED1B24";
}
if (caseMeasure == CaseMeasure.CASE_INCIDENCE) {
if (districtValue == null || districtValue.intValue() <= 0) {
// grey when region has no population data
emptyPopulationDistrictPresent = true;
fillColor = "#999";
}
}
for (GeoLatLon[] districtShapePart : districtShape) {
LeafletPolygon polygon = new LeafletPolygon();
polygon.setCaption(district.getName() + "<br>" + districtValue);
polygon.setOptions("{\"stroke\": false, \"color\": '" + fillColor + "', \"fillOpacity\": 0.8}");
polygon.setLatLons(districtShapePart);
districtPolygons.add(polygon);
polygonDistricts.add(districtRef);
}
}
map.addPolygonGroup(DISTRICTS_GROUP_ID, districtPolygons);
}
Aggregations