Search in sources :

Example 1 with DiseaseConfigurationFacade

use of de.symeda.sormas.api.disease.DiseaseConfigurationFacade in project SORMAS-Project by hzi-braunschweig.

the class CaseStatisticsFacadeEjb method queryCaseCount.

@SuppressWarnings("unchecked")
@Override
public List<StatisticsCaseCountDto> queryCaseCount(StatisticsCaseCriteria caseCriteria, StatisticsCaseAttribute rowGrouping, StatisticsCaseSubAttribute rowSubGrouping, StatisticsCaseAttribute columnGrouping, StatisticsCaseSubAttribute columnSubGrouping, boolean includePopulation, boolean includeZeroValues, Integer populationReferenceYear) {
    // case counts
    Pair<String, List<Object>> caseCountQueryAndParams = buildCaseCountQuery(caseCriteria, rowGrouping, rowSubGrouping, columnGrouping, columnSubGrouping);
    Query caseCountQuery = em.createNativeQuery(caseCountQueryAndParams.getKey().toString());
    for (int i = 0; i < caseCountQueryAndParams.getValue().size(); i++) {
        caseCountQuery.setParameter(i + 1, caseCountQueryAndParams.getValue().get(i));
    }
    Function<Integer, RegionReferenceDto> regionProvider = id -> regionFacade.getRegionReferenceById(id);
    Function<Integer, DistrictReferenceDto> districtProvider = id -> districtFacade.getDistrictReferenceById(id);
    Function<Integer, CommunityReferenceDto> communityProvider = id -> communityFacade.getCommunityReferenceById(id);
    Function<Integer, FacilityReferenceDto> healthFacilityProvider = id -> facilityFacade.getFacilityReferenceById(id);
    List<StatisticsCaseCountDto> caseCountResults = ((Stream<Object[]>) caseCountQuery.getResultStream()).map(result -> {
        Object rowKey = "".equals(result[1]) ? null : result[1];
        Object columnKey = "".equals(result[2]) ? null : result[2];
        return new StatisticsCaseCountDto(result[0] != null ? ((Number) result[0]).intValue() : null, null, StatisticsHelper.buildGroupingKey(rowKey, rowGrouping, rowSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider), StatisticsHelper.buildGroupingKey(columnKey, columnGrouping, columnSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider));
    }).collect(Collectors.toList());
    if (includeZeroValues) {
        List<StatisticsGroupingKey> allRowKeys;
        if (rowGrouping != null) {
            allRowKeys = (List<StatisticsGroupingKey>) caseCriteria.getFilterValuesForGrouping(rowGrouping, rowSubGrouping);
            if (allRowKeys == null) {
                allRowKeys = StatisticsHelper.getAttributeGroupingKeys(rowGrouping, rowSubGrouping, diseaseConfigurationFacade, caseFacade, regionFacade, districtFacade);
            }
        } else {
            allRowKeys = Arrays.asList((StatisticsGroupingKey) null);
        }
        List<StatisticsGroupingKey> allColumnKeys;
        if (columnGrouping != null) {
            allColumnKeys = (List<StatisticsGroupingKey>) caseCriteria.getFilterValuesForGrouping(columnGrouping, columnSubGrouping);
            if (allColumnKeys == null) {
                allColumnKeys = StatisticsHelper.getAttributeGroupingKeys(columnGrouping, columnSubGrouping, diseaseConfigurationFacade, caseFacade, regionFacade, districtFacade);
            }
        } else {
            allColumnKeys = Arrays.asList((StatisticsGroupingKey) null);
        }
        for (StatisticsGroupingKey rowKey : allRowKeys) {
            for (StatisticsGroupingKey columnKey : allColumnKeys) {
                StatisticsCaseCountDto zeroDto = new StatisticsCaseCountDto(0, null, rowKey, columnKey);
                if (!caseCountResults.contains(zeroDto)) {
                    caseCountResults.add(zeroDto);
                }
            }
        }
    }
    // population
    if (includePopulation) {
        Pair<String, List<Object>> populationQueryAndParams = buildPopulationQuery(caseCriteria, rowGrouping, rowSubGrouping, columnGrouping, columnSubGrouping, populationReferenceYear);
        Query populationQuery = em.createNativeQuery(populationQueryAndParams.getKey().toString());
        for (int i = 0; i < populationQueryAndParams.getValue().size(); i++) {
            populationQuery.setParameter(i + 1, populationQueryAndParams.getValue().get(i));
        }
        List<StatisticsCaseCountDto> populationResults = ((Stream<Object[]>) populationQuery.getResultStream()).map(result -> {
            Object rowKey = "".equals(result[1]) ? null : result[1];
            Object columnKey = "".equals(result[2]) ? null : result[2];
            return new StatisticsCaseCountDto(null, result[0] != null ? ((Number) result[0]).intValue() : null, StatisticsHelper.buildGroupingKey(rowKey, rowGrouping, rowSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider), StatisticsHelper.buildGroupingKey(columnKey, columnGrouping, columnSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider));
        }).collect(Collectors.toList());
        boolean rowIsPopulation = rowGrouping != null && rowGrouping.isPopulationData();
        boolean columnIsPopulation = columnGrouping != null && columnGrouping.isPopulationData();
        if (!populationResults.isEmpty()) {
            assert ((populationResults.get(0).getRowKey() != null) == rowIsPopulation);
            assert ((populationResults.get(0).getColumnKey() != null) == columnIsPopulation);
        }
        // add the population data to the case counts
        // when a key is not a population data key, we use null instead
        StatisticsCaseCountDto searchDto = new StatisticsCaseCountDto(null, null, null, null);
        for (StatisticsCaseCountDto caseCountResult : caseCountResults) {
            if (rowIsPopulation) {
                searchDto.setRowKey(caseCountResult.getRowKey());
            }
            if (columnIsPopulation) {
                searchDto.setColumnKey(caseCountResult.getColumnKey());
            }
            int index = populationResults.indexOf(searchDto);
            if (index >= 0) {
                caseCountResult.setPopulation(populationResults.get(index).getPopulation());
            }
        }
    }
    return caseCountResults;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) IntegerRange(de.symeda.sormas.api.IntegerRange) PopulationData(de.symeda.sormas.backend.infrastructure.PopulationData) DistrictService(de.symeda.sormas.backend.infrastructure.district.DistrictService) StringUtils(org.apache.commons.lang3.StringUtils) AgeGroup(de.symeda.sormas.api.AgeGroup) CommunityFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.community.CommunityFacadeEjb.CommunityFacadeEjbLocal) StatisticsCaseAttribute(de.symeda.sormas.api.statistics.StatisticsCaseAttribute) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) Pair(org.apache.commons.lang3.tuple.Pair) Stateless(javax.ejb.Stateless) StatisticsGroupingKey(de.symeda.sormas.api.statistics.StatisticsGroupingKey) FacilityService(de.symeda.sormas.backend.infrastructure.facility.FacilityService) Person(de.symeda.sormas.backend.person.Person) CommunityService(de.symeda.sormas.backend.infrastructure.community.CommunityService) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) DistrictFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.district.DistrictFacadeEjb.DistrictFacadeEjbLocal) Collectors(java.util.stream.Collectors) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) User(de.symeda.sormas.backend.user.User) LocalDate(java.time.LocalDate) Location(de.symeda.sormas.backend.location.Location) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) IntStream(java.util.stream.IntStream) Community(de.symeda.sormas.backend.infrastructure.community.Community) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CollectionUtils(org.apache.commons.collections.CollectionUtils) FacilityFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.facility.FacilityFacadeEjb.FacilityFacadeEjbLocal) LocalBean(javax.ejb.LocalBean) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms) DiseaseConfigurationFacadeEjb(de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb) StatisticsCaseCriteria(de.symeda.sormas.api.statistics.StatisticsCaseCriteria) EJB(javax.ejb.EJB) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) CaseStatisticsFacade(de.symeda.sormas.api.caze.CaseStatisticsFacade) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) UserDto(de.symeda.sormas.api.user.UserDto) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) DiseaseConfigurationFacade(de.symeda.sormas.api.disease.DiseaseConfigurationFacade) RegionService(de.symeda.sormas.backend.infrastructure.region.RegionService) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) StatisticsCaseCountDto(de.symeda.sormas.api.statistics.StatisticsCaseCountDto) StatisticsCaseSubAttribute(de.symeda.sormas.api.statistics.StatisticsCaseSubAttribute) RegionFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.region.RegionFacadeEjb.RegionFacadeEjbLocal) StatisticsHelper(de.symeda.sormas.api.statistics.StatisticsHelper) Query(javax.persistence.Query) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) List(java.util.List) ArrayList(java.util.ArrayList) StatisticsGroupingKey(de.symeda.sormas.api.statistics.StatisticsGroupingKey) StatisticsCaseCountDto(de.symeda.sormas.api.statistics.StatisticsCaseCountDto)

Aggregations

AgeGroup (de.symeda.sormas.api.AgeGroup)1 IntegerRange (de.symeda.sormas.api.IntegerRange)1 CaseStatisticsFacade (de.symeda.sormas.api.caze.CaseStatisticsFacade)1 DiseaseConfigurationFacade (de.symeda.sormas.api.disease.DiseaseConfigurationFacade)1 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)1 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)1 FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)1 RegionReferenceDto (de.symeda.sormas.api.infrastructure.region.RegionReferenceDto)1 StatisticsCaseAttribute (de.symeda.sormas.api.statistics.StatisticsCaseAttribute)1 StatisticsCaseCountDto (de.symeda.sormas.api.statistics.StatisticsCaseCountDto)1 StatisticsCaseCriteria (de.symeda.sormas.api.statistics.StatisticsCaseCriteria)1 StatisticsCaseSubAttribute (de.symeda.sormas.api.statistics.StatisticsCaseSubAttribute)1 StatisticsGroupingKey (de.symeda.sormas.api.statistics.StatisticsGroupingKey)1 StatisticsHelper (de.symeda.sormas.api.statistics.StatisticsHelper)1 UserDto (de.symeda.sormas.api.user.UserDto)1 DiseaseConfigurationFacadeEjb (de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb)1 PopulationData (de.symeda.sormas.backend.infrastructure.PopulationData)1 Community (de.symeda.sormas.backend.infrastructure.community.Community)1 CommunityFacadeEjbLocal (de.symeda.sormas.backend.infrastructure.community.CommunityFacadeEjb.CommunityFacadeEjbLocal)1 CommunityService (de.symeda.sormas.backend.infrastructure.community.CommunityService)1