Search in sources :

Example 1 with DashboardContactDto

use of de.symeda.sormas.api.dashboard.DashboardContactDto in project SORMAS-Project by hzi-braunschweig.

the class ContactService method getContactsForDashboard.

public List<DashboardContactDto> getContactsForDashboard(Region region, District district, Disease disease, Date from, Date to, User user) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<DashboardContactDto> cq = cb.createQuery(DashboardContactDto.class);
    Root<Contact> contact = cq.from(getElementClass());
    Join<Contact, Case> caze = contact.join(Contact.CAZE, JoinType.LEFT);
    Predicate filter = createDefaultFilter(cb, contact);
    filter = CriteriaBuilderHelper.and(cb, filter, createUserFilter(cb, cq, contact));
    Predicate dateFilter = buildDateFilter(cb, contact, from, to);
    if (filter != null) {
        filter = cb.and(filter, dateFilter);
    } else {
        filter = dateFilter;
    }
    filter = CriteriaBuilderHelper.and(cb, filter, getRegionDistrictDiseasePredicate(region, district, disease, cb, contact, caze));
    if (filter != null) {
        cq.where(filter);
        cq.multiselect(contact.get(AbstractDomainObject.ID), contact.get(Contact.REPORT_DATE_TIME), contact.get(Contact.CONTACT_STATUS), contact.get(Contact.CONTACT_CLASSIFICATION), contact.get(Contact.FOLLOW_UP_STATUS), contact.get(Contact.FOLLOW_UP_UNTIL), contact.get(Contact.DISEASE), contact.get(Contact.QUARANTINE_FROM), contact.get(Contact.QUARANTINE_TO));
        List<DashboardContactDto> dashboardContacts = em.createQuery(cq).getResultList();
        if (!dashboardContacts.isEmpty()) {
            List<Long> dashboardContactIds = dashboardContacts.stream().map(DashboardContactDto::getId).collect(Collectors.toList());
            CriteriaQuery<DashboardVisit> visitsCq = cb.createQuery(DashboardVisit.class);
            Root<Contact> visitsCqRoot = visitsCq.from(getElementClass());
            Join<Contact, Visit> visitsJoin = visitsCqRoot.join(Contact.VISITS, JoinType.LEFT);
            Join<Visit, Symptoms> visitSymptomsJoin = visitsJoin.join(Visit.SYMPTOMS, JoinType.LEFT);
            visitsCq.where(CriteriaBuilderHelper.and(cb, contact.get(AbstractDomainObject.ID).in(dashboardContactIds), cb.isNotEmpty(visitsCqRoot.get(Contact.VISITS))));
            visitsCq.multiselect(visitsCqRoot.get(AbstractDomainObject.ID), visitSymptomsJoin.get(Symptoms.SYMPTOMATIC), visitsJoin.get(Visit.VISIT_STATUS), visitsJoin.get(Visit.VISIT_DATE_TIME));
            List<DashboardVisit> contactVisits = em.createQuery(visitsCq).getResultList();
            // Add visit information to the DashboardContactDtos
            for (DashboardContactDto dashboardContact : dashboardContacts) {
                List<DashboardVisit> visits = contactVisits.stream().filter(v -> v.getContactId() == dashboardContact.getId()).collect(Collectors.toList());
                DashboardVisit lastVisit = visits.stream().max((v1, v2) -> v1.getVisitDateTime().compareTo(v2.getVisitDateTime())).orElse(null);
                if (lastVisit != null) {
                    dashboardContact.setLastVisitDateTime(lastVisit.getVisitDateTime());
                    dashboardContact.setLastVisitStatus(lastVisit.getVisitStatus());
                    dashboardContact.setSymptomatic(lastVisit.isSymptomatic());
                    List<VisitStatus> visitStatuses = visits.stream().map(DashboardVisit::getVisitStatus).collect(Collectors.toList());
                    Map<VisitStatus, Integer> frequency = new EnumMap<>(VisitStatus.class);
                    for (VisitStatus status : VisitStatus.values()) {
                        int freq = Collections.frequency(visitStatuses, status);
                        frequency.put(status, freq);
                    }
                    dashboardContact.setVisitStatusMap(frequency);
                }
            }
            return dashboardContacts;
        }
    }
    return Collections.emptyList();
}
Also used : FollowUpStatus(de.symeda.sormas.api.contact.FollowUpStatus) Arrays(java.util.Arrays) VisitStatus(de.symeda.sormas.api.visit.VisitStatus) ExternalDataUpdateException(de.symeda.sormas.api.externaldata.ExternalDataUpdateException) StringUtils(org.apache.commons.lang3.StringUtils) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) FeatureConfigurationFacadeEjb(de.symeda.sormas.backend.feature.FeatureConfigurationFacadeEjb) Visit(de.symeda.sormas.backend.visit.Visit) EpiData(de.symeda.sormas.backend.epidata.EpiData) From(javax.persistence.criteria.From) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) EnumMap(java.util.EnumMap) Transactional(javax.transaction.Transactional) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) ExternalDataDto(de.symeda.sormas.api.externaldata.ExternalDataDto) JurisdictionHelper(de.symeda.sormas.backend.util.JurisdictionHelper) DeletableAdo(de.symeda.sormas.backend.common.DeletableAdo) SampleService(de.symeda.sormas.backend.sample.SampleService) Sample(de.symeda.sormas.backend.sample.Sample) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Community(de.symeda.sormas.backend.infrastructure.community.Community) CaseUserFilterCriteria(de.symeda.sormas.backend.caze.CaseUserFilterCriteria) MapContactDto(de.symeda.sormas.api.contact.MapContactDto) Selection(javax.persistence.criteria.Selection) Case(de.symeda.sormas.backend.caze.Case) LocalBean(javax.ejb.LocalBean) ContactListEntryDtoResultTransformer(de.symeda.sormas.backend.contact.transformers.ContactListEntryDtoResultTransformer) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ChangeDateFilterBuilder(de.symeda.sormas.backend.common.ChangeDateFilterBuilder) TaskCriteria(de.symeda.sormas.api.task.TaskCriteria) DataHelper(de.symeda.sormas.api.utils.DataHelper) Task(de.symeda.sormas.backend.task.Task) ContactLogic(de.symeda.sormas.api.contact.ContactLogic) Disease(de.symeda.sormas.api.Disease) FollowUpLogic(de.symeda.sormas.api.followup.FollowUpLogic) HealthConditions(de.symeda.sormas.backend.clinicalcourse.HealthConditions) Subquery(javax.persistence.criteria.Subquery) FeatureTypeProperty(de.symeda.sormas.api.feature.FeatureTypeProperty) FeatureType(de.symeda.sormas.api.feature.FeatureType) VisitFacadeEjb(de.symeda.sormas.backend.visit.VisitFacadeEjb) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) Join(javax.persistence.criteria.Join) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Date(java.util.Date) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) ChangeDateBuilder(de.symeda.sormas.backend.common.ChangeDateBuilder) AbstractCoreAdoService(de.symeda.sormas.backend.common.AbstractCoreAdoService) ContactProximity(de.symeda.sormas.api.contact.ContactProximity) ExposureService(de.symeda.sormas.backend.exposure.ExposureService) UserRole(de.symeda.sormas.api.user.UserRole) VaccinationStatus(de.symeda.sormas.api.caze.VaccinationStatus) Stateless(javax.ejb.Stateless) CaseService(de.symeda.sormas.backend.caze.CaseService) DiseaseConfigurationFacadeEjbLocal(de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb.DiseaseConfigurationFacadeEjbLocal) Person(de.symeda.sormas.backend.person.Person) Timestamp(java.sql.Timestamp) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Collectors(java.util.stream.Collectors) ContactClassification(de.symeda.sormas.api.contact.ContactClassification) List(java.util.List) CoreAdo(de.symeda.sormas.backend.common.CoreAdo) ContactDto(de.symeda.sormas.api.contact.ContactDto) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Location(de.symeda.sormas.backend.location.Location) SormasToSormasShareInfoService(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfoService) TaskService(de.symeda.sormas.backend.task.TaskService) DateHelper(de.symeda.sormas.api.utils.DateHelper) EntityRelevanceStatus(de.symeda.sormas.api.EntityRelevanceStatus) ExternalDataUtil(de.symeda.sormas.backend.util.ExternalDataUtil) HashSet(java.util.HashSet) CriteriaUpdate(javax.persistence.criteria.CriteriaUpdate) CollectionUtils(org.apache.commons.collections.CollectionUtils) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) EpiDataService(de.symeda.sormas.backend.epidata.EpiDataService) ContactListEntryDto(de.symeda.sormas.api.contact.ContactListEntryDto) ContactStatus(de.symeda.sormas.api.contact.ContactStatus) Event(de.symeda.sormas.backend.event.Event) SormasToSormasShareInfo(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfo) ExternalJournalService(de.symeda.sormas.backend.externaljournal.ExternalJournalService) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) Comparator(java.util.Comparator) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections) Visit(de.symeda.sormas.backend.visit.Visit) Case(de.symeda.sormas.backend.caze.Case) Predicate(javax.persistence.criteria.Predicate) EnumMap(java.util.EnumMap) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) VisitStatus(de.symeda.sormas.api.visit.VisitStatus) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms)

Example 2 with DashboardContactDto

use of de.symeda.sormas.api.dashboard.DashboardContactDto in project SORMAS-Project by hzi-braunschweig.

the class DashboardFacadeEjb method getDashboardContactStatistic.

@RolesAllowed(UserRight._DASHBOARD_CONTACT_VIEW)
public DashboardContactStatisticDto getDashboardContactStatistic(DashboardCriteria dashboardCriteria) {
    List<DashboardContactDto> dashboardContacts = contactFacade.getContactsForDashboard(dashboardCriteria.getRegion(), dashboardCriteria.getDistrict(), dashboardCriteria.getDisease(), dashboardCriteria.getDateFrom(), dashboardCriteria.getDateTo());
    List<DashboardContactDto> previousDashboardContacts = contactFacade.getContactsForDashboard(dashboardCriteria.getRegion(), dashboardCriteria.getDistrict(), dashboardCriteria.getDisease(), dashboardCriteria.getPreviousDateFrom(), dashboardCriteria.getPreviousDateTo());
    int contactsCount = dashboardContacts.size();
    int newContactsCount = (int) dashboardContacts.stream().filter(c -> c.getReportDate().after(dashboardCriteria.getDateFrom()) || c.getReportDate().equals(dashboardCriteria.getDateFrom())).count();
    int newContactsPercentage = calculatePercentage(newContactsCount, contactsCount);
    int symptomaticContactsCount = (int) dashboardContacts.stream().filter(c -> Boolean.TRUE.equals(c.getSymptomatic())).count();
    int symptomaticContactsPercentage = calculatePercentage(symptomaticContactsCount, contactsCount);
    int contactClassificationUnconfirmedCount = (int) dashboardContacts.stream().filter(c -> c.getContactClassification() == ContactClassification.UNCONFIRMED).count();
    int contactClassificationUnconfirmedPercentage = calculatePercentage(contactClassificationUnconfirmedCount, contactsCount);
    int contactClassificationConfirmedCount = (int) dashboardContacts.stream().filter(c -> c.getContactClassification() == ContactClassification.CONFIRMED).count();
    int contactClassificationConfirmedPercentage = calculatePercentage(contactClassificationConfirmedCount, contactsCount);
    int contactClassificationNotAContactCount = (int) dashboardContacts.stream().filter(c -> c.getContactClassification() == ContactClassification.NO_CONTACT).count();
    int contactClassificationNotAContactPercentage = calculatePercentage(contactClassificationNotAContactCount, contactsCount);
    Map<Disease, Map<String, Integer>> diseaseMap = new TreeMap<>();
    for (Disease disease : diseaseConfigurationFacade.getAllDiseasesWithFollowUp()) {
        Map<String, Integer> countValues = new HashMap<>();
        countValues.put(PREVIOUS_CONTACTS, (int) previousDashboardContacts.stream().filter(c -> c.getDisease() == disease).count());
        countValues.put(CURRENT_CONTACTS, (int) dashboardContacts.stream().filter(c -> c.getDisease() == disease).count());
        diseaseMap.put(disease, countValues);
    }
    Map<Disease, Map<String, Integer>> orderedDiseaseMap = diseaseMap.entrySet().stream().sorted(Map.Entry.comparingByValue((o1, o2) -> -Integer.compare(o1.get(CURRENT_CONTACTS), o2.get(CURRENT_CONTACTS)))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
    DashboardContactFollowUpDto dashboardContactFollowUp = calculateContactFollowUpStatistics(dashboardContacts, dashboardCriteria.getDateTo());
    DashboardContactStoppedFollowUpDto dashboardContactStoppedFollowUp = calculateContactStoppedFollowUpStatistics(dashboardContacts);
    DashboardContactVisitDto dashboardContactVisit = calculateContactVisitStatistics(dashboardContacts, previousDashboardContacts);
    return new DashboardContactStatisticDto(contactsCount, newContactsCount, newContactsPercentage, symptomaticContactsCount, symptomaticContactsPercentage, contactClassificationConfirmedCount, contactClassificationConfirmedPercentage, contactClassificationUnconfirmedCount, contactClassificationUnconfirmedPercentage, contactClassificationNotAContactCount, contactClassificationNotAContactPercentage, orderedDiseaseMap, dashboardContactFollowUp, dashboardContactStoppedFollowUp, dashboardContactVisit);
}
Also used : FollowUpStatus(de.symeda.sormas.api.contact.FollowUpStatus) FeatureType(de.symeda.sormas.api.feature.FeatureType) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) DashboardCaseMeasureDto(de.symeda.sormas.api.dashboard.DashboardCaseMeasureDto) RolesAllowed(javax.annotation.security.RolesAllowed) Date(java.util.Date) DashboardContactStoppedFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactStoppedFollowUpDto) DashboardEventDto(de.symeda.sormas.api.dashboard.DashboardEventDto) VisitStatus(de.symeda.sormas.api.visit.VisitStatus) BigDecimal(java.math.BigDecimal) Map(java.util.Map) FeatureConfigurationFacadeEjb(de.symeda.sormas.backend.feature.FeatureConfigurationFacadeEjb) DashboardCaseDto(de.symeda.sormas.api.dashboard.DashboardCaseDto) OutbreakCriteria(de.symeda.sormas.api.outbreak.OutbreakCriteria) Stateless(javax.ejb.Stateless) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) District(de.symeda.sormas.backend.infrastructure.district.District) DiseaseBurdenDto(de.symeda.sormas.api.disease.DiseaseBurdenDto) Collectors(java.util.stream.Collectors) ContactClassification(de.symeda.sormas.api.contact.ContactClassification) PREVIOUS_CONTACTS(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto.PREVIOUS_CONTACTS) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) DashboardFacade(de.symeda.sormas.api.dashboard.DashboardFacade) List(java.util.List) DashboardQuarantineDataDto(de.symeda.sormas.api.dashboard.DashboardQuarantineDataDto) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) DashboardContactStatisticDto(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto) EventFacadeEjb(de.symeda.sormas.backend.event.EventFacadeEjb) DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) Optional(java.util.Optional) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) OutbreakFacadeEjb(de.symeda.sormas.backend.outbreak.OutbreakFacadeEjb) CaseReferenceDefinition(de.symeda.sormas.api.caze.CaseReferenceDefinition) CaseMeasure(de.symeda.sormas.api.CaseMeasure) CaseClassification(de.symeda.sormas.api.caze.CaseClassification) DateHelper(de.symeda.sormas.api.utils.DateHelper) HashMap(java.util.HashMap) CaseFacadeEjb(de.symeda.sormas.backend.caze.CaseFacadeEjb) EventStatus(de.symeda.sormas.api.event.EventStatus) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DashboardContactVisitDto(de.symeda.sormas.api.dashboard.DashboardContactVisitDto) CURRENT_CONTACTS(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto.CURRENT_CONTACTS) EventCriteria(de.symeda.sormas.api.event.EventCriteria) DashboardCriteria(de.symeda.sormas.api.dashboard.DashboardCriteria) DashboardContactFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactFollowUpDto) LocalBean(javax.ejb.LocalBean) DiseaseConfigurationFacadeEjb(de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb) SampleFacadeEjb(de.symeda.sormas.backend.sample.SampleFacadeEjb) EpiCurveGrouping(de.symeda.sormas.api.dashboard.EpiCurveGrouping) EJB(javax.ejb.EJB) DataHelper(de.symeda.sormas.api.utils.DataHelper) ContactStatus(de.symeda.sormas.api.contact.ContactStatus) CriteriaDateType(de.symeda.sormas.api.utils.criteria.CriteriaDateType) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) DateUtils(org.apache.commons.lang3.time.DateUtils) DashboardCaseStatisticDto(de.symeda.sormas.api.dashboard.DashboardCaseStatisticDto) PresentCondition(de.symeda.sormas.api.person.PresentCondition) UserRight(de.symeda.sormas.api.user.UserRight) Disease(de.symeda.sormas.api.Disease) TreeMap(java.util.TreeMap) ContactFacadeEjb(de.symeda.sormas.backend.contact.ContactFacadeEjb) Disease(de.symeda.sormas.api.Disease) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DashboardContactStoppedFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactStoppedFollowUpDto) DashboardContactVisitDto(de.symeda.sormas.api.dashboard.DashboardContactVisitDto) TreeMap(java.util.TreeMap) DashboardContactFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactFollowUpDto) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) DashboardContactStatisticDto(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 3 with DashboardContactDto

use of de.symeda.sormas.api.dashboard.DashboardContactDto in project SORMAS-Project by hzi-braunschweig.

the class DashboardFacadeEjb method calculateContactVisitStatistics.

private DashboardContactVisitDto calculateContactVisitStatistics(List<DashboardContactDto> contacts, List<DashboardContactDto> previousContacts) {
    Map<VisitStatus, Integer> visitStatusMap = new EnumMap<>(VisitStatus.class);
    Map<VisitStatus, Integer> previousVisitStatusMap = new EnumMap<>(VisitStatus.class);
    // only visits that needed to be done, i.e. at most the amount of follow-up days
    int doneEssentialVisitsCount = 0;
    int previousDoneEssentialVisitsCount = 0;
    Date now = new Date();
    int totalFollowUpDays = 0;
    int previousTotalFollowUpDays = 0;
    for (DashboardContactDto contact : contacts) {
        for (VisitStatus visitStatus : contact.getVisitStatusMap().keySet()) {
            int value = 0;
            if (visitStatusMap.containsKey(visitStatus)) {
                value = visitStatusMap.get(visitStatus);
            }
            visitStatusMap.put(visitStatus, value + contact.getVisitStatusMap().get(visitStatus));
        }
        if (contact.getFollowUpUntil() != null) {
            int contactFollowUpDays = Math.min(DateHelper.getDaysBetween(contact.getReportDate(), now), DateHelper.getDaysBetween(contact.getReportDate(), contact.getFollowUpUntil()));
            totalFollowUpDays += contactFollowUpDays;
            int visitCount = contact.getVisitStatusMap().values().stream().reduce(0, Integer::sum);
            doneEssentialVisitsCount += (Math.min(visitCount, contactFollowUpDays));
        }
    }
    for (DashboardContactDto contact : previousContacts) {
        for (VisitStatus visitStatus : contact.getVisitStatusMap().keySet()) {
            int value = 0;
            if (previousVisitStatusMap.containsKey(visitStatus)) {
                value = previousVisitStatusMap.get(visitStatus);
            }
            previousVisitStatusMap.put(visitStatus, value + contact.getVisitStatusMap().get(visitStatus));
        }
        if (contact.getFollowUpUntil() != null) {
            int contactFollowUpDays = Math.min(DateHelper.getDaysBetween(contact.getReportDate(), now), DateHelper.getDaysBetween(contact.getReportDate(), contact.getFollowUpUntil()));
            previousTotalFollowUpDays += contactFollowUpDays;
            int visitCount = contact.getVisitStatusMap().values().stream().reduce(0, Integer::sum);
            previousDoneEssentialVisitsCount += (Math.min(visitCount, contactFollowUpDays));
        }
    }
    int visitsCount = visitStatusMap.values().stream().reduce(0, Integer::sum);
    int missedVisitsCount = totalFollowUpDays - doneEssentialVisitsCount;
    int unavailableVisitsCount = Optional.ofNullable(visitStatusMap.get(VisitStatus.UNAVAILABLE)).orElse(0).intValue();
    int uncooperativeVisitsCount = Optional.ofNullable(visitStatusMap.get(VisitStatus.UNCOOPERATIVE)).orElse(0).intValue();
    int cooperativeVisitsCount = Optional.ofNullable(visitStatusMap.get(VisitStatus.COOPERATIVE)).orElse(0).intValue();
    int previousMissedVisitsCount = previousTotalFollowUpDays - previousDoneEssentialVisitsCount;
    int previousUnavailableVisitsCount = Optional.ofNullable(previousVisitStatusMap.get(VisitStatus.UNAVAILABLE)).orElse(0).intValue();
    int previousUncooperativeVisitsCount = Optional.ofNullable(previousVisitStatusMap.get(VisitStatus.UNCOOPERATIVE)).orElse(0).intValue();
    int previousCooperativeVisitsCount = Optional.ofNullable(previousVisitStatusMap.get(VisitStatus.COOPERATIVE)).orElse(0).intValue();
    int missedVisitsGrowth = calculateGrowth(missedVisitsCount, previousMissedVisitsCount);
    int unavailableVisitsGrowth = calculateGrowth(unavailableVisitsCount, previousUnavailableVisitsCount);
    int uncooperativeVisitsGrowth = calculateGrowth(uncooperativeVisitsCount, previousUncooperativeVisitsCount);
    int cooperativeVisitsGrowth = calculateGrowth(cooperativeVisitsCount, previousCooperativeVisitsCount);
    return new DashboardContactVisitDto(visitsCount, missedVisitsCount, missedVisitsGrowth, unavailableVisitsCount, unavailableVisitsGrowth, uncooperativeVisitsCount, uncooperativeVisitsGrowth, cooperativeVisitsCount, cooperativeVisitsGrowth, previousMissedVisitsCount, previousUnavailableVisitsCount, previousUncooperativeVisitsCount, previousCooperativeVisitsCount);
}
Also used : DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) DashboardContactVisitDto(de.symeda.sormas.api.dashboard.DashboardContactVisitDto) EnumMap(java.util.EnumMap) Date(java.util.Date) VisitStatus(de.symeda.sormas.api.visit.VisitStatus)

Example 4 with DashboardContactDto

use of de.symeda.sormas.api.dashboard.DashboardContactDto in project SORMAS-Project by hzi-braunschweig.

the class DashboardFacadeEjb method calculateContactFollowUpStatistics.

private DashboardContactFollowUpDto calculateContactFollowUpStatistics(List<DashboardContactDto> contacts, Date dateTo) {
    List<DashboardContactDto> followUpContacts = contacts.stream().filter(c -> c.getFollowUpStatus() == FollowUpStatus.FOLLOW_UP).collect(Collectors.toList());
    int followUpContactsCount = followUpContacts.size();
    int cooperativeContactsCount = (int) followUpContacts.stream().filter(c -> c.getLastVisitStatus() == VisitStatus.COOPERATIVE).count();
    int uncooperativeContactsCount = (int) followUpContacts.stream().filter(c -> c.getLastVisitStatus() == VisitStatus.UNCOOPERATIVE).count();
    int unavailableContactsCount = (int) followUpContacts.stream().filter(c -> c.getLastVisitStatus() == VisitStatus.UNAVAILABLE).count();
    int notVisitedContactsCount = (int) followUpContacts.stream().filter(c -> c.getLastVisitStatus() == null).count();
    int cooperativeContactsPercentage = calculatePercentage(cooperativeContactsCount, followUpContactsCount);
    int uncooperativeContactsPercentage = calculatePercentage(uncooperativeContactsCount, followUpContactsCount);
    int unavailableContactsPercentage = calculatePercentage(unavailableContactsCount, followUpContactsCount);
    int notVisitedContactsPercentage = calculatePercentage(notVisitedContactsCount, followUpContactsCount);
    int missedVisitsOneDayCount = 0;
    int missedVisitsTwoDaysCount = 0;
    int missedVisitsThreeDaysCount = 0;
    int missedVisitsGtThreeDaysCount = 0;
    for (DashboardContactDto contact : followUpContacts) {
        Date lastVisitDateTime = contact.getLastVisitDateTime() != null ? contact.getLastVisitDateTime() : contact.getReportDate();
        Date referenceDate = dateTo.after(new Date()) ? new Date() : dateTo;
        int missedDays = DateHelper.getFullDaysBetween(lastVisitDateTime, referenceDate);
        switch(missedDays <= 3 ? missedDays : 4) {
            case 1:
                missedVisitsOneDayCount++;
                break;
            case 2:
                missedVisitsTwoDaysCount++;
                break;
            case 3:
                missedVisitsThreeDaysCount++;
                break;
            case 4:
                missedVisitsGtThreeDaysCount++;
                break;
            default:
                break;
        }
    }
    return new DashboardContactFollowUpDto(followUpContactsCount, cooperativeContactsCount, cooperativeContactsPercentage, uncooperativeContactsCount, uncooperativeContactsPercentage, unavailableContactsCount, unavailableContactsPercentage, notVisitedContactsCount, notVisitedContactsPercentage, missedVisitsOneDayCount, missedVisitsTwoDaysCount, missedVisitsThreeDaysCount, missedVisitsGtThreeDaysCount);
}
Also used : FollowUpStatus(de.symeda.sormas.api.contact.FollowUpStatus) FeatureType(de.symeda.sormas.api.feature.FeatureType) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) DashboardCaseMeasureDto(de.symeda.sormas.api.dashboard.DashboardCaseMeasureDto) RolesAllowed(javax.annotation.security.RolesAllowed) Date(java.util.Date) DashboardContactStoppedFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactStoppedFollowUpDto) DashboardEventDto(de.symeda.sormas.api.dashboard.DashboardEventDto) VisitStatus(de.symeda.sormas.api.visit.VisitStatus) BigDecimal(java.math.BigDecimal) Map(java.util.Map) FeatureConfigurationFacadeEjb(de.symeda.sormas.backend.feature.FeatureConfigurationFacadeEjb) DashboardCaseDto(de.symeda.sormas.api.dashboard.DashboardCaseDto) OutbreakCriteria(de.symeda.sormas.api.outbreak.OutbreakCriteria) Stateless(javax.ejb.Stateless) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) District(de.symeda.sormas.backend.infrastructure.district.District) DiseaseBurdenDto(de.symeda.sormas.api.disease.DiseaseBurdenDto) Collectors(java.util.stream.Collectors) ContactClassification(de.symeda.sormas.api.contact.ContactClassification) PREVIOUS_CONTACTS(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto.PREVIOUS_CONTACTS) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) DashboardFacade(de.symeda.sormas.api.dashboard.DashboardFacade) List(java.util.List) DashboardQuarantineDataDto(de.symeda.sormas.api.dashboard.DashboardQuarantineDataDto) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) DashboardContactStatisticDto(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto) EventFacadeEjb(de.symeda.sormas.backend.event.EventFacadeEjb) DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) Optional(java.util.Optional) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) OutbreakFacadeEjb(de.symeda.sormas.backend.outbreak.OutbreakFacadeEjb) CaseReferenceDefinition(de.symeda.sormas.api.caze.CaseReferenceDefinition) CaseMeasure(de.symeda.sormas.api.CaseMeasure) CaseClassification(de.symeda.sormas.api.caze.CaseClassification) DateHelper(de.symeda.sormas.api.utils.DateHelper) HashMap(java.util.HashMap) CaseFacadeEjb(de.symeda.sormas.backend.caze.CaseFacadeEjb) EventStatus(de.symeda.sormas.api.event.EventStatus) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DashboardContactVisitDto(de.symeda.sormas.api.dashboard.DashboardContactVisitDto) CURRENT_CONTACTS(de.symeda.sormas.api.dashboard.DashboardContactStatisticDto.CURRENT_CONTACTS) EventCriteria(de.symeda.sormas.api.event.EventCriteria) DashboardCriteria(de.symeda.sormas.api.dashboard.DashboardCriteria) DashboardContactFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactFollowUpDto) LocalBean(javax.ejb.LocalBean) DiseaseConfigurationFacadeEjb(de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb) SampleFacadeEjb(de.symeda.sormas.backend.sample.SampleFacadeEjb) EpiCurveGrouping(de.symeda.sormas.api.dashboard.EpiCurveGrouping) EJB(javax.ejb.EJB) DataHelper(de.symeda.sormas.api.utils.DataHelper) ContactStatus(de.symeda.sormas.api.contact.ContactStatus) CriteriaDateType(de.symeda.sormas.api.utils.criteria.CriteriaDateType) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) DateUtils(org.apache.commons.lang3.time.DateUtils) DashboardCaseStatisticDto(de.symeda.sormas.api.dashboard.DashboardCaseStatisticDto) PresentCondition(de.symeda.sormas.api.person.PresentCondition) UserRight(de.symeda.sormas.api.user.UserRight) Disease(de.symeda.sormas.api.Disease) TreeMap(java.util.TreeMap) ContactFacadeEjb(de.symeda.sormas.backend.contact.ContactFacadeEjb) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) DashboardContactFollowUpDto(de.symeda.sormas.api.dashboard.DashboardContactFollowUpDto) Date(java.util.Date)

Example 5 with DashboardContactDto

use of de.symeda.sormas.api.dashboard.DashboardContactDto in project SORMAS-Project by hzi-braunschweig.

the class ContactService method getContactsForDashboard.

public List<DashboardContactDto> getContactsForDashboard(Region region, District district, Disease disease, Date from, Date to) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<DashboardContactDto> cq = cb.createQuery(DashboardContactDto.class);
    Root<Contact> contact = cq.from(getElementClass());
    ContactQueryContext cqc = new ContactQueryContext(cb, cq, contact);
    Join<Contact, Case> caze = cqc.getJoins().getCaze();
    Predicate filter = createDefaultFilter(cb, contact);
    filter = CriteriaBuilderHelper.and(cb, filter, createUserFilter(cqc, null));
    Predicate dateFilter = buildDateFilter(cb, contact, from, to);
    if (filter != null) {
        filter = cb.and(filter, dateFilter);
    } else {
        filter = dateFilter;
    }
    filter = CriteriaBuilderHelper.and(cb, filter, getRegionDistrictDiseasePredicate(region, district, disease, cb, contact, caze));
    if (filter != null) {
        cq.where(filter);
        cq.multiselect(contact.get(AbstractDomainObject.ID), contact.get(Contact.REPORT_DATE_TIME), contact.get(Contact.CONTACT_STATUS), contact.get(Contact.CONTACT_CLASSIFICATION), contact.get(Contact.FOLLOW_UP_STATUS), contact.get(Contact.FOLLOW_UP_UNTIL), contact.get(Contact.DISEASE), contact.get(Contact.QUARANTINE_FROM), contact.get(Contact.QUARANTINE_TO));
        List<DashboardContactDto> dashboardContacts = em.createQuery(cq).getResultList();
        if (!dashboardContacts.isEmpty()) {
            List<Long> dashboardContactIds = dashboardContacts.stream().map(DashboardContactDto::getId).collect(Collectors.toList());
            CriteriaQuery<DashboardVisit> visitsCq = cb.createQuery(DashboardVisit.class);
            Root<Contact> visitsCqRoot = visitsCq.from(getElementClass());
            Join<Contact, Visit> visitsJoin = visitsCqRoot.join(Contact.VISITS, JoinType.LEFT);
            Join<Visit, Symptoms> visitSymptomsJoin = visitsJoin.join(Visit.SYMPTOMS, JoinType.LEFT);
            visitsCq.where(CriteriaBuilderHelper.and(cb, contact.get(AbstractDomainObject.ID).in(dashboardContactIds), cb.isNotEmpty(visitsCqRoot.get(Contact.VISITS))));
            visitsCq.multiselect(visitsCqRoot.get(AbstractDomainObject.ID), visitSymptomsJoin.get(Symptoms.SYMPTOMATIC), visitsJoin.get(Visit.VISIT_STATUS), visitsJoin.get(Visit.VISIT_DATE_TIME));
            List<DashboardVisit> contactVisits = em.createQuery(visitsCq).getResultList();
            // Add visit information to the DashboardContactDtos
            for (DashboardContactDto dashboardContact : dashboardContacts) {
                List<DashboardVisit> visits = contactVisits.stream().filter(v -> v.getContactId() == dashboardContact.getId()).collect(Collectors.toList());
                DashboardVisit lastVisit = visits.stream().max((v1, v2) -> v1.getVisitDateTime().compareTo(v2.getVisitDateTime())).orElse(null);
                if (lastVisit != null) {
                    dashboardContact.setLastVisitDateTime(lastVisit.getVisitDateTime());
                    dashboardContact.setLastVisitStatus(lastVisit.getVisitStatus());
                    dashboardContact.setSymptomatic(lastVisit.isSymptomatic());
                    List<VisitStatus> visitStatuses = visits.stream().map(DashboardVisit::getVisitStatus).collect(Collectors.toList());
                    Map<VisitStatus, Integer> frequency = new EnumMap<>(VisitStatus.class);
                    for (VisitStatus status : VisitStatus.values()) {
                        int freq = Collections.frequency(visitStatuses, status);
                        frequency.put(status, freq);
                    }
                    dashboardContact.setVisitStatusMap(frequency);
                }
            }
            return dashboardContacts;
        }
    }
    return Collections.emptyList();
}
Also used : FollowUpStatus(de.symeda.sormas.api.contact.FollowUpStatus) VisitService(de.symeda.sormas.backend.visit.VisitService) Arrays(java.util.Arrays) VisitStatus(de.symeda.sormas.api.visit.VisitStatus) ExternalDataUpdateException(de.symeda.sormas.api.externaldata.ExternalDataUpdateException) StringUtils(org.apache.commons.lang3.StringUtils) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) DocumentRelatedEntityType(de.symeda.sormas.api.document.DocumentRelatedEntityType) Visit(de.symeda.sormas.backend.visit.Visit) EpiData(de.symeda.sormas.backend.epidata.EpiData) From(javax.persistence.criteria.From) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) EnumMap(java.util.EnumMap) Transactional(javax.transaction.Transactional) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) ExternalDataDto(de.symeda.sormas.api.externaldata.ExternalDataDto) JurisdictionHelper(de.symeda.sormas.backend.util.JurisdictionHelper) DeletableAdo(de.symeda.sormas.backend.common.DeletableAdo) SampleService(de.symeda.sormas.backend.sample.SampleService) Sample(de.symeda.sormas.backend.sample.Sample) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Community(de.symeda.sormas.backend.infrastructure.community.Community) CaseUserFilterCriteria(de.symeda.sormas.backend.caze.CaseUserFilterCriteria) MapContactDto(de.symeda.sormas.api.contact.MapContactDto) Selection(javax.persistence.criteria.Selection) CaseReferenceDto(de.symeda.sormas.api.caze.CaseReferenceDto) Case(de.symeda.sormas.backend.caze.Case) LocalBean(javax.ejb.LocalBean) ContactListEntryDtoResultTransformer(de.symeda.sormas.backend.contact.transformers.ContactListEntryDtoResultTransformer) FieldConstraints(de.symeda.sormas.api.utils.FieldConstraints) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ChangeDateFilterBuilder(de.symeda.sormas.backend.common.ChangeDateFilterBuilder) TaskCriteria(de.symeda.sormas.api.task.TaskCriteria) DataHelper(de.symeda.sormas.api.utils.DataHelper) Task(de.symeda.sormas.backend.task.Task) ContactLogic(de.symeda.sormas.api.contact.ContactLogic) Disease(de.symeda.sormas.api.Disease) FollowUpLogic(de.symeda.sormas.api.followup.FollowUpLogic) HealthConditions(de.symeda.sormas.backend.clinicalcourse.HealthConditions) Subquery(javax.persistence.criteria.Subquery) FeatureTypeProperty(de.symeda.sormas.api.feature.FeatureTypeProperty) FeatureType(de.symeda.sormas.api.feature.FeatureType) VisitFacadeEjb(de.symeda.sormas.backend.visit.VisitFacadeEjb) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) Join(javax.persistence.criteria.Join) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Date(java.util.Date) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) ChangeDateBuilder(de.symeda.sormas.backend.common.ChangeDateBuilder) AbstractCoreAdoService(de.symeda.sormas.backend.common.AbstractCoreAdoService) ContactProximity(de.symeda.sormas.api.contact.ContactProximity) ExposureService(de.symeda.sormas.backend.exposure.ExposureService) UserRole(de.symeda.sormas.api.user.UserRole) SormasToSormasShareInfoFacadeEjb(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfoFacadeEjb) VaccinationStatus(de.symeda.sormas.api.caze.VaccinationStatus) Stateless(javax.ejb.Stateless) DocumentService(de.symeda.sormas.backend.document.DocumentService) CaseService(de.symeda.sormas.backend.caze.CaseService) DiseaseConfigurationFacadeEjbLocal(de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb.DiseaseConfigurationFacadeEjbLocal) Person(de.symeda.sormas.backend.person.Person) Timestamp(java.sql.Timestamp) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Collectors(java.util.stream.Collectors) ContactClassification(de.symeda.sormas.api.contact.ContactClassification) List(java.util.List) EditPermissionType(de.symeda.sormas.api.EditPermissionType) ContactDto(de.symeda.sormas.api.contact.ContactDto) Optional(java.util.Optional) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Location(de.symeda.sormas.backend.location.Location) SormasToSormasShareInfoService(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfoService) TaskService(de.symeda.sormas.backend.task.TaskService) DateHelper(de.symeda.sormas.api.utils.DateHelper) EntityRelevanceStatus(de.symeda.sormas.api.EntityRelevanceStatus) ExternalDataUtil(de.symeda.sormas.backend.util.ExternalDataUtil) HashSet(java.util.HashSet) DeletionDetails(de.symeda.sormas.api.common.DeletionDetails) CriteriaUpdate(javax.persistence.criteria.CriteriaUpdate) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) CaseJoins(de.symeda.sormas.backend.caze.CaseJoins) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) EpiDataService(de.symeda.sormas.backend.epidata.EpiDataService) ContactListEntryDto(de.symeda.sormas.api.contact.ContactListEntryDto) ContactStatus(de.symeda.sormas.api.contact.ContactStatus) Event(de.symeda.sormas.backend.event.Event) SormasToSormasShareInfo(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfo) ExternalJournalService(de.symeda.sormas.backend.externaljournal.ExternalJournalService) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) Comparator(java.util.Comparator) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections) Visit(de.symeda.sormas.backend.visit.Visit) Case(de.symeda.sormas.backend.caze.Case) Predicate(javax.persistence.criteria.Predicate) EnumMap(java.util.EnumMap) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) VisitStatus(de.symeda.sormas.api.visit.VisitStatus) DashboardContactDto(de.symeda.sormas.api.dashboard.DashboardContactDto) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms)

Aggregations

DashboardContactDto (de.symeda.sormas.api.dashboard.DashboardContactDto)8 Date (java.util.Date)8 VisitStatus (de.symeda.sormas.api.visit.VisitStatus)7 EnumMap (java.util.EnumMap)7 Disease (de.symeda.sormas.api.Disease)5 ContactClassification (de.symeda.sormas.api.contact.ContactClassification)5 ContactStatus (de.symeda.sormas.api.contact.ContactStatus)5 FollowUpStatus (de.symeda.sormas.api.contact.FollowUpStatus)5 DateHelper (de.symeda.sormas.api.utils.DateHelper)5 List (java.util.List)5 Map (java.util.Map)5 ContactCriteria (de.symeda.sormas.api.contact.ContactCriteria)4 FeatureType (de.symeda.sormas.api.feature.FeatureType)4 DataHelper (de.symeda.sormas.api.utils.DataHelper)4 District (de.symeda.sormas.backend.infrastructure.district.District)4 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 EJB (javax.ejb.EJB)4 LocalBean (javax.ejb.LocalBean)4