Search in sources :

Example 21 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class CaseFacadeEjb method getCaseFollowUpList.

@Override
public List<CaseFollowUpDto> getCaseFollowUpList(CaseCriteria caseCriteria, Date referenceDate, int interval, Integer first, Integer max, List<SortProperty> sortProperties) {
    Date end = DateHelper.getEndOfDay(referenceDate);
    Date start = DateHelper.getStartOfDay(DateHelper.subtractDays(end, interval));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CaseFollowUpDto> cq = cb.createQuery(CaseFollowUpDto.class);
    Root<Case> caze = cq.from(Case.class);
    final CaseQueryContext caseQueryContext = new CaseQueryContext(cb, cq, caze);
    final CaseJoins joins = caseQueryContext.getJoins();
    cq.multiselect(caze.get(Case.UUID), caze.get(Case.CHANGE_DATE), joins.getPerson().get(Person.FIRST_NAME), joins.getPerson().get(Person.LAST_NAME), caze.get(Case.REPORT_DATE), joins.getSymptoms().get(Symptoms.ONSET_DATE), caze.get(Case.FOLLOW_UP_UNTIL), joins.getPerson().get(Person.SYMPTOM_JOURNAL_STATUS), caze.get(Case.DISEASE), JurisdictionHelper.booleanSelector(cb, service.inJurisdictionOrOwned(caseQueryContext)));
    Predicate filter = CriteriaBuilderHelper.and(cb, service.createUserFilter(caseQueryContext), service.createCriteriaFilter(caseCriteria, caseQueryContext));
    if (filter != null) {
        cq.where(filter);
    }
    cq.distinct(true);
    if (sortProperties != null && !sortProperties.isEmpty()) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case FollowUpDto.UUID:
                case FollowUpDto.REPORT_DATE:
                case FollowUpDto.FOLLOW_UP_UNTIL:
                    expression = caze.get(sortProperty.propertyName);
                    break;
                case FollowUpDto.FIRST_NAME:
                    expression = joins.getPerson().get(Person.FIRST_NAME);
                    order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
                    break;
                case FollowUpDto.SYMPTOM_JOURNAL_STATUS:
                    expression = joins.getPerson().get(Person.SYMPTOM_JOURNAL_STATUS);
                    break;
                case FollowUpDto.LAST_NAME:
                    expression = joins.getPerson().get(Person.LAST_NAME);
                    order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
                    break;
                default:
                    throw new IllegalArgumentException(sortProperty.propertyName);
            }
            order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
        }
        cq.orderBy(order);
    } else {
        cq.orderBy(cb.desc(caze.get(Case.CHANGE_DATE)));
    }
    List<CaseFollowUpDto> resultList = QueryHelper.getResultList(em, cq, first, max);
    if (!resultList.isEmpty()) {
        List<String> caseUuids = resultList.stream().map(FollowUpDto::getUuid).collect(Collectors.toList());
        CriteriaQuery<Object[]> visitsCq = cb.createQuery(Object[].class);
        Root<Case> visitsCqRoot = visitsCq.from(Case.class);
        Join<Case, Visit> visitsJoin = visitsCqRoot.join(Case.VISITS, JoinType.LEFT);
        Join<Visit, Symptoms> visitSymptomsJoin = visitsJoin.join(Visit.SYMPTOMS, JoinType.LEFT);
        visitsCq.where(CriteriaBuilderHelper.and(cb, caze.get(AbstractDomainObject.UUID).in(caseUuids), cb.isNotEmpty(visitsCqRoot.get(Case.VISITS)), cb.between(visitsJoin.get(Visit.VISIT_DATE_TIME), start, end)));
        visitsCq.multiselect(visitsCqRoot.get(Case.UUID), visitsJoin.get(Visit.VISIT_DATE_TIME), visitsJoin.get(Visit.VISIT_STATUS), visitsJoin.get(Visit.ORIGIN), visitSymptomsJoin.get(Symptoms.SYMPTOMATIC));
        // Sort by visit date so that we'll have the latest visit of each day
        visitsCq.orderBy(cb.asc(visitsJoin.get(Visit.VISIT_DATE_TIME)));
        visitsCq.orderBy(cb.asc(visitsJoin.get(Visit.VISIT_DATE_TIME)), cb.asc(visitsJoin.get(Visit.CREATION_DATE)));
        List<Object[]> visits = em.createQuery(visitsCq).getResultList();
        Map<String, CaseFollowUpDto> resultMap = resultList.stream().collect(Collectors.toMap(CaseFollowUpDto::getUuid, Function.identity()));
        Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
        for (CaseFollowUpDto caseFollowUpDto : resultMap.values()) {
            caseFollowUpDto.initVisitSize(interval + 1);
            pseudonymizer.pseudonymizeDto(CaseFollowUpDto.class, caseFollowUpDto, caseFollowUpDto.getInJurisdiction(), null);
        }
        for (Object[] v : visits) {
            int day = DateHelper.getDaysBetween(start, (Date) v[1]);
            VisitResultDto result = getVisitResult((VisitStatus) v[2], (VisitOrigin) v[3], (Boolean) v[4]);
            resultMap.get(v[0]).getVisitResults()[day - 1] = result;
        }
    }
    return resultList;
}
Also used : Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) Visit(de.symeda.sormas.backend.visit.Visit) ClinicalVisit(de.symeda.sormas.backend.clinicalcourse.ClinicalVisit) ArrayList(java.util.ArrayList) VisitResultDto(de.symeda.sormas.api.visit.VisitResultDto) CaseFollowUpDto(de.symeda.sormas.api.caze.CaseFollowUpDto) Predicate(javax.persistence.criteria.Predicate) SortProperty(de.symeda.sormas.api.utils.SortProperty) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) Date(java.util.Date) LocalDate(java.time.LocalDate) ExternalShareInfoCountAndLatestDate(de.symeda.sormas.backend.share.ExternalShareInfoCountAndLatestDate) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms)

Example 22 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class CampaignFacadeEjb method getIndexList.

@Override
public List<CampaignIndexDto> getIndexList(CampaignCriteria campaignCriteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CampaignIndexDto> cq = cb.createQuery(CampaignIndexDto.class);
    Root<Campaign> campaign = cq.from(Campaign.class);
    CampaignQueryContext queryContext = new CampaignQueryContext(cb, cq, campaign);
    cq.multiselect(campaign.get(Campaign.UUID), campaign.get(Campaign.NAME), campaign.get(Campaign.START_DATE), campaign.get(Campaign.END_DATE));
    Predicate filter = service.createUserFilter(queryContext);
    if (campaignCriteria != null) {
        Predicate criteriaFilter = service.buildCriteriaFilter(queryContext, campaignCriteria);
        filter = CriteriaBuilderHelper.and(cb, filter, criteriaFilter);
    }
    cq.where(filter);
    if (sortProperties != null && sortProperties.size() > 0) {
        List<Order> order = new ArrayList<Order>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case CampaignIndexDto.UUID:
                case CampaignIndexDto.NAME:
                case CampaignIndexDto.START_DATE:
                case CampaignIndexDto.END_DATE:
                    expression = campaign.get(sortProperty.propertyName);
                    break;
                default:
                    throw new IllegalArgumentException(sortProperty.propertyName);
            }
            order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
        }
        cq.orderBy(order);
    } else {
        cq.orderBy(cb.desc(campaign.get(Campaign.CHANGE_DATE)));
    }
    return QueryHelper.getResultList(em, cq, first, max);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ArrayList(java.util.ArrayList) CampaignIndexDto(de.symeda.sormas.api.campaign.CampaignIndexDto) Predicate(javax.persistence.criteria.Predicate) SortProperty(de.symeda.sormas.api.utils.SortProperty)

Example 23 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class ClinicalVisitFacadeEjb method getIndexList.

@Override
public List<ClinicalVisitIndexDto> getIndexList(ClinicalVisitCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ClinicalVisitIndexDto> cq = cb.createQuery(ClinicalVisitIndexDto.class);
    Root<ClinicalVisit> visit = cq.from(ClinicalVisit.class);
    ClinicalVisitJoins joins = new ClinicalVisitJoins(visit);
    cq.multiselect(visit.get(ClinicalVisit.UUID), visit.get(ClinicalVisit.VISIT_DATE_TIME), visit.get(ClinicalVisit.VISITING_PERSON), visit.get(ClinicalVisit.VISIT_REMARKS), joins.getSymptoms().get(Symptoms.TEMPERATURE), joins.getSymptoms().get(Symptoms.TEMPERATURE_SOURCE), joins.getSymptoms().get(Symptoms.BLOOD_PRESSURE_SYSTOLIC), joins.getSymptoms().get(Symptoms.BLOOD_PRESSURE_DIASTOLIC), joins.getSymptoms().get(Symptoms.HEART_RATE), joins.getSymptoms().get(Symptoms.ID), JurisdictionHelper.booleanSelector(cb, caseService.inJurisdictionOrOwned(new CaseQueryContext(cb, cq, joins.getCaseJoins()))));
    if (criteria != null) {
        cq.where(service.buildCriteriaFilter(criteria, cb, visit));
    }
    if (CollectionUtils.isNotEmpty(sortProperties)) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case ClinicalVisitDto.UUID:
                case ClinicalVisitDto.DISEASE:
                case ClinicalVisitDto.VISIT_DATE_TIME:
                case ClinicalVisitDto.CLINICAL_COURSE:
                    expression = visit.get(sortProperty.propertyName);
                    break;
                default:
                    throw new IllegalArgumentException(sortProperty.propertyName);
            }
            order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
        }
        cq.orderBy(order);
    } else {
        cq.orderBy(cb.desc(visit.get(ClinicalVisit.VISIT_DATE_TIME)));
    }
    List<ClinicalVisitIndexDto> results = QueryHelper.getResultList(em, cq, first, max);
    Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
    pseudonymizer.pseudonymizeDtoCollection(ClinicalVisitIndexDto.class, results, v -> v.getInJurisdiction(), null);
    return results;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) ArrayList(java.util.ArrayList) SortProperty(de.symeda.sormas.api.utils.SortProperty) ClinicalVisitIndexDto(de.symeda.sormas.api.clinicalcourse.ClinicalVisitIndexDto) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext)

Example 24 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class ContactFollowUpGrid method setDataProvider.

public void setDataProvider(Date referenceDate, int interval) {
    DataProvider<ContactFollowUpDto, ContactCriteria> dataProvider = DataProvider.fromFilteringCallbacks(query -> FacadeProvider.getContactFacade().getContactFollowUpList(query.getFilter().orElse(null), referenceDate, interval, query.getOffset(), query.getLimit(), query.getSortOrders().stream().map(sortOrder -> new SortProperty(sortOrder.getSorted(), sortOrder.getDirection() == SortDirection.ASCENDING)).collect(Collectors.toList())).stream(), query -> (int) FacadeProvider.getContactFacade().count(query.getFilter().orElse(null)));
    setDataProvider(dataProvider);
    setSelectionMode(SelectionMode.NONE);
}
Also used : SortProperty(de.symeda.sormas.api.utils.SortProperty) ContactFollowUpDto(de.symeda.sormas.api.contact.ContactFollowUpDto) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria)

Example 25 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class PointsOfEntryGrid method setLazyDataProvider.

public void setLazyDataProvider() {
    DataProvider<PointOfEntryDto, PointOfEntryCriteria> dataProvider = DataProvider.fromFilteringCallbacks(query -> FacadeProvider.getPointOfEntryFacade().getIndexList(query.getFilter().orElse(null), query.getOffset(), query.getLimit(), query.getSortOrders().stream().map(sortOrder -> new SortProperty(sortOrder.getSorted(), sortOrder.getDirection() == SortDirection.ASCENDING)).collect(Collectors.toList())).stream(), query -> {
        return (int) FacadeProvider.getPointOfEntryFacade().count(query.getFilter().orElse(null));
    });
    setDataProvider(dataProvider);
    setSelectionMode(SelectionMode.NONE);
}
Also used : PointOfEntryCriteria(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryCriteria) SortProperty(de.symeda.sormas.api.utils.SortProperty) PointOfEntryDto(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryDto)

Aggregations

SortProperty (de.symeda.sormas.api.utils.SortProperty)73 ArrayList (java.util.ArrayList)42 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)37 Order (javax.persistence.criteria.Order)37 Predicate (javax.persistence.criteria.Predicate)34 Date (java.util.Date)14 District (de.symeda.sormas.backend.infrastructure.district.District)13 Region (de.symeda.sormas.backend.infrastructure.region.Region)10 Pseudonymizer (de.symeda.sormas.backend.util.Pseudonymizer)10 PersonDto (de.symeda.sormas.api.person.PersonDto)9 Location (de.symeda.sormas.backend.location.Location)8 AbstractDomainObject (de.symeda.sormas.backend.common.AbstractDomainObject)7 Person (de.symeda.sormas.backend.person.Person)7 User (de.symeda.sormas.backend.user.User)7 Collectors (java.util.stream.Collectors)6 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)5 UserDto (de.symeda.sormas.api.user.UserDto)5 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)5 Case (de.symeda.sormas.backend.caze.Case)5 Selection (javax.persistence.criteria.Selection)5