use of de.symeda.sormas.api.infrastructure.district.DistrictDto 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.infrastructure.district.DistrictDto in project SORMAS-Project by hzi-braunschweig.
the class CaseFacadeEjbTest method testGetCaseMeasurePerDistrict.
@Test
public void testGetCaseMeasurePerDistrict() {
List<DataHelper.Pair<DistrictDto, BigDecimal>> caseMeasurePerDistrict = getCaseFacade().getCaseMeasurePerDistrict(null, null, Disease.EVD, CaseMeasure.CASE_COUNT);
assertTrue(caseMeasurePerDistrict.isEmpty());
RDCF rdcf1 = creator.createRDCF("Region", "District1", "Community", "Facility");
RDCF rdcf2 = creator.createRDCF("Region", "District2", "Community", "Facility");
UserDto user = creator.createUser(rdcf1, UserRole.NATIONAL_USER);
CaseDataDto caze = creator.createCase(user.toReference(), creator.createPerson("Person", "One").toReference(), rdcf1);
caseMeasurePerDistrict = getCaseFacade().getCaseMeasurePerDistrict(null, null, Disease.EVD, CaseMeasure.CASE_COUNT);
assertEquals(1, caseMeasurePerDistrict.size());
DataHelper.Pair<DistrictDto, BigDecimal> districtCaseCount = caseMeasurePerDistrict.get(0);
assertEquals(rdcf1.district.getUuid(), districtCaseCount.getElement0().getUuid());
assertEquals(1, districtCaseCount.getElement1().intValue());
creator.createCase(user.toReference(), creator.createPerson("Person", "Two").toReference(), rdcf1);
caseMeasurePerDistrict = getCaseFacade().getCaseMeasurePerDistrict(null, null, Disease.EVD, CaseMeasure.CASE_COUNT);
assertEquals(1, caseMeasurePerDistrict.size());
districtCaseCount = caseMeasurePerDistrict.get(0);
assertEquals(rdcf1.district.getUuid(), districtCaseCount.getElement0().getUuid());
assertEquals(2, districtCaseCount.getElement1().intValue());
caze.setDistrict(rdcf2.district);
getCaseFacade().save(caze);
creator.createCase(user.toReference(), creator.createPerson("Person", "Three").toReference(), rdcf2);
caseMeasurePerDistrict = getCaseFacade().getCaseMeasurePerDistrict(null, null, Disease.EVD, CaseMeasure.CASE_COUNT);
assertEquals(2, caseMeasurePerDistrict.size());
districtCaseCount = caseMeasurePerDistrict.get(0);
assertEquals(rdcf1.district.getUuid(), districtCaseCount.getElement0().getUuid());
assertEquals(1, districtCaseCount.getElement1().intValue());
districtCaseCount = caseMeasurePerDistrict.get(1);
assertEquals(rdcf2.district.getUuid(), districtCaseCount.getElement0().getUuid());
assertEquals(2, districtCaseCount.getElement1().intValue());
}
use of de.symeda.sormas.api.infrastructure.district.DistrictDto in project SORMAS-Project by hzi-braunschweig.
the class PopulationDataFacadeEjbTest method testGetProjectedDistrictPopulation.
@Test
public void testGetProjectedDistrictPopulation() {
RDCFEntities rdcf = creator.createRDCFEntities("Region", "District", "Community", "Facility");
DistrictDto district = getDistrictFacade().getByUuid(rdcf.district.getUuid());
district.setGrowthRate(2.7f);
getDistrictFacade().save(district);
creator.createPopulationData(new RegionReferenceDto(rdcf.region.getUuid(), null, null), new DistrictReferenceDto(rdcf.district.getUuid(), null, null), 450000, DateHelper.subtractYears(new Date(), 3));
assertEquals(new Integer(487440), getPopulationDataFacade().getProjectedDistrictPopulation(rdcf.district.getUuid()));
}
use of de.symeda.sormas.api.infrastructure.district.DistrictDto in project SORMAS-Project by hzi-braunschweig.
the class DistrictFacadeEjbTest method testGetAllAfter.
@Test
public void testGetAllAfter() throws InterruptedException {
Region region = creator.createRegion("region");
creator.createDistrict("district1", region);
getDistrictService().doFlush();
Date date = new Date();
List<DistrictDto> results = getDistrictFacade().getAllAfter(date);
// List should be empty
assertEquals(0, results.size());
// delay to ignore known rounding issues in change date filter
Thread.sleep(1);
String districtName = "district2";
creator.createDistrict(districtName, region);
results = getDistrictFacade().getAllAfter(date);
// List should have one entry
assertEquals(1, results.size());
assertEquals(districtName, results.get(0).getName());
assertEquals(region.getUuid(), results.get(0).getRegion().getUuid());
}
use of de.symeda.sormas.api.infrastructure.district.DistrictDto in project SORMAS-Project by hzi-braunschweig.
the class TestDataCreator method createRDP.
public RDP createRDP() {
RegionDto region = createRegion("Region");
DistrictDto district = createDistrict("District", region.toReference());
PointOfEntryDto pointOfEntry = createPointOfEntry("POE", region.toReference(), district.toReference());
return new RDP(region, district, pointOfEntry);
}
Aggregations