Search in sources :

Example 6 with SortProperty

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

the class RegionFacadeEjb method getIndexList.

@Override
public List<RegionIndexDto> getIndexList(RegionCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Region> cq = cb.createQuery(Region.class);
    Root<Region> region = cq.from(Region.class);
    Join<Region, Area> area = region.join(Region.AREA, JoinType.LEFT);
    Join<Region, Country> country = region.join(Region.COUNTRY, JoinType.LEFT);
    Predicate filter = null;
    if (criteria != null) {
        filter = service.buildCriteriaFilter(criteria, cb, region);
    }
    if (filter != null) {
        cq.where(filter);
    }
    if (CollectionUtils.isNotEmpty(sortProperties)) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case Region.NAME:
                case Region.EPID_CODE:
                case Region.GROWTH_RATE:
                case Region.EXTERNAL_ID:
                    expression = region.get(sortProperty.propertyName);
                    break;
                case Region.AREA:
                    expression = area.get(Area.NAME);
                    break;
                case RegionIndexDto.COUNTRY:
                    expression = country.get(Country.DEFAULT_NAME);
                    break;
                default:
                    throw new IllegalArgumentException(sortProperty.propertyName);
            }
            order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
        }
        cq.orderBy(order);
    } else {
        cq.orderBy(cb.asc(region.get(Region.NAME)));
    }
    cq.select(region);
    return QueryHelper.getResultList(em, cq, first, max, this::toIndexDto);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Area(de.symeda.sormas.backend.infrastructure.area.Area) SortProperty(de.symeda.sormas.api.utils.SortProperty) Country(de.symeda.sormas.backend.infrastructure.country.Country)

Example 7 with SortProperty

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

the class SubcontinentFacadeEjb method getIndexList.

@Override
public List<SubcontinentIndexDto> getIndexList(SubcontinentCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Subcontinent> cq = cb.createQuery(Subcontinent.class);
    Root<Subcontinent> subcontinent = cq.from(Subcontinent.class);
    Join<Subcontinent, Continent> continent = subcontinent.join(Subcontinent.CONTINENT, JoinType.LEFT);
    Predicate filter = null;
    if (criteria != null) {
        filter = service.buildCriteriaFilter(criteria, cb, subcontinent);
    }
    if (filter != null) {
        cq.where(filter);
    }
    if (CollectionUtils.isNotEmpty(sortProperties)) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case SubcontinentIndexDto.DISPLAY_NAME:
                    expression = subcontinent.get(Subcontinent.DEFAULT_NAME);
                    break;
                case SubcontinentDto.CONTINENT:
                    expression = continent.get(Continent.DEFAULT_NAME);
                    break;
                case SubcontinentDto.EXTERNAL_ID:
                    expression = subcontinent.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.asc(subcontinent.get(Subcontinent.DEFAULT_NAME)));
    }
    cq.select(subcontinent);
    return QueryHelper.getResultList(em, cq, first, max, this::toIndexDto);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Continent(de.symeda.sormas.backend.infrastructure.continent.Continent) SortProperty(de.symeda.sormas.api.utils.SortProperty)

Example 8 with SortProperty

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

the class LabMessageFacadeEjb method getIndexList.

@Override
public List<LabMessageIndexDto> getIndexList(LabMessageCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<LabMessageIndexDto> cq = cb.createQuery(LabMessageIndexDto.class);
    Root<LabMessage> labMessage = cq.from(LabMessage.class);
    Join<LabMessage, User> userJoin = labMessage.join(LabMessage.ASSIGNEE, JoinType.LEFT);
    cq.multiselect(labMessage.get(LabMessage.UUID), labMessage.get(LabMessage.TYPE), labMessage.get(LabMessage.MESSAGE_DATE_TIME), labMessage.get(LabMessage.LAB_NAME), labMessage.get(LabMessage.LAB_POSTAL_CODE), labMessage.get(LabMessage.TESTED_DISEASE), labMessage.get(LabMessage.SAMPLE_OVERALL_TEST_RESULT), labMessage.get(LabMessage.PERSON_FIRST_NAME), labMessage.get(LabMessage.PERSON_LAST_NAME), labMessage.get(LabMessage.PERSON_BIRTH_DATE_YYYY), labMessage.get(LabMessage.PERSON_BIRTH_DATE_MM), labMessage.get(LabMessage.PERSON_BIRTH_DATE_DD), labMessage.get(LabMessage.PERSON_POSTAL_CODE), labMessage.get(LabMessage.STATUS), userJoin.get(User.UUID), userJoin.get(User.FIRST_NAME), userJoin.get(User.LAST_NAME));
    Predicate filter = null;
    if (criteria != null) {
        filter = labMessageService.buildCriteriaFilter(cb, labMessage, criteria);
    }
    if (filter != null) {
        cq.where(filter);
    }
    // Distinct is necessary here to avoid duplicate results due to the user role join in taskService.createAssigneeFilter
    cq.distinct(true);
    List<Order> order = new ArrayList<>();
    if (!CollectionUtils.isEmpty(sortProperties)) {
        for (SortProperty sortProperty : sortProperties) {
            if (LabMessageIndexDto.PERSON_BIRTH_DATE.equals(sortProperty.propertyName)) {
                Expression<?> birthdateYYYY = labMessage.get(LabMessage.PERSON_BIRTH_DATE_YYYY);
                order.add(sortProperty.ascending ? cb.asc(birthdateYYYY) : cb.desc(birthdateYYYY));
                Expression<?> birthdateMM = labMessage.get(LabMessage.PERSON_BIRTH_DATE_MM);
                order.add(sortProperty.ascending ? cb.asc(birthdateMM) : cb.desc(birthdateMM));
                Expression<?> birthdateDD = labMessage.get(LabMessage.PERSON_BIRTH_DATE_DD);
                order.add(sortProperty.ascending ? cb.asc(birthdateDD) : cb.desc(birthdateDD));
            } else if (VALID_SORT_PROPERTY_NAMES.contains(sortProperty.propertyName)) {
                Expression<?> expression = labMessage.get(sortProperty.propertyName);
                order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
            }
        }
    }
    order.add(cb.desc(labMessage.get(LabMessage.MESSAGE_DATE_TIME)));
    cq.orderBy(order);
    return QueryHelper.getResultList(em, cq, first, max);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) User(de.symeda.sormas.backend.user.User) ArrayList(java.util.ArrayList) LabMessageIndexDto(de.symeda.sormas.api.labmessage.LabMessageIndexDto) Predicate(javax.persistence.criteria.Predicate) SortProperty(de.symeda.sormas.api.utils.SortProperty) Expression(javax.persistence.criteria.Expression)

Example 9 with SortProperty

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

the class DevModeView method generateContacts.

private void generateContacts() {
    initializeRandomGenerator();
    ContactGenerationConfig config = contactGeneratorConfigBinder.getBean();
    Disease disease = config.getDisease();
    List<Disease> diseases = disease == null ? FacadeProvider.getDiseaseConfigurationFacade().getAllDiseases(true, true, true) : null;
    if (disease == null) {
        disease = random(diseases);
        Notification.show("", "Automatically chosen disease: " + disease.getName(), Notification.Type.TRAY_NOTIFICATION);
    }
    List<String> personUuids = new ArrayList<>();
    List<CaseReferenceDto> cases = null;
    List<DistrictIndexDto> districts = config.getDistrict() == null ? FacadeProvider.getDistrictFacade().getIndexList(new DistrictCriteria().region(config.getRegion()), 0, Math.min(config.getContactCount() * 2, 50), Arrays.asList(new SortProperty(DistrictDto.NAME))) : null;
    if (!config.isCreateWithoutSourceCases()) {
        cases = FacadeProvider.getCaseFacade().getRandomCaseReferences(new CaseCriteria().region(config.getRegion()).district(config.getDistrict()).disease(disease), config.getContactCount() * 2, random());
        if (cases == null) {
            Notification.show("Error", I18nProperties.getString(Strings.messageMissingCases), Notification.Type.ERROR_MESSAGE);
            return;
        }
    }
    float baseOffset = random().nextFloat();
    int daysBetween = (int) ChronoUnit.DAYS.between(config.startDate, config.endDate);
    long dt = System.nanoTime();
    for (int i = 0; i < config.getContactCount(); i++) {
        fieldVisibilityCheckers = FieldVisibilityCheckers.withDisease(disease).andWithCountry(FacadeProvider.getConfigFacade().getCountryLocale());
        LocalDateTime referenceDateTime = getReferenceDateTime(i, config.getContactCount(), baseOffset, disease, config.getStartDate(), daysBetween);
        PersonDto person;
        if (config.isCreateMultipleContactsPerPerson() && !personUuids.isEmpty() && randomPercent(25)) {
            String personUuid = random(personUuids);
            person = FacadeProvider.getPersonFacade().getPersonByUuid(personUuid);
        } else {
            person = PersonDto.build();
            fillEntity(person, referenceDateTime);
            person.setSymptomJournalStatus(null);
            setPersonName(person);
            if (config.isCreateMultipleContactsPerPerson()) {
                personUuids.add(person.getUuid());
            }
        }
        CaseReferenceDto contactCase = null;
        if (!config.isCreateWithoutSourceCases()) {
            contactCase = random(cases);
        }
        ContactDto contact = ContactDto.build();
        contact.setPerson(person.toReference());
        fillEntity(contact, referenceDateTime);
        if (contactCase != null) {
            contact.setCaze(contactCase);
        }
        contact.setDisease(disease);
        if (contact.getDisease() == Disease.OTHER) {
            contact.setDiseaseDetails("RD " + (random().nextInt(20) + 1));
        }
        UserReferenceDto userReference = UserProvider.getCurrent().getUserReference();
        contact.setReportingUser(userReference);
        contact.setReportDateTime(Date.from(referenceDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        if (districts != null) {
            DistrictIndexDto district = random(districts);
            contact.setRegion(district.getRegion());
            contact.setDistrict(district.toReference());
        } else {
            contact.setRegion(config.getRegion());
            contact.setDistrict(config.getDistrict());
        }
        if (contact.getLastContactDate() != null && contact.getLastContactDate().after(contact.getReportDateTime())) {
            contact.setLastContactDate(contact.getReportDateTime());
        }
        if (FollowUpStatus.CANCELED.equals(contact.getFollowUpStatus()) || FollowUpStatus.LOST.equals(contact.getFollowUpStatus())) {
            contact.setFollowUpComment("-");
        }
        // description
        contact.setDescription("Contact generated using DevMode on " + LocalDate.now());
        FacadeProvider.getPersonFacade().savePerson(person);
        contact = FacadeProvider.getContactFacade().save(contact);
        if (FacadeProvider.getDiseaseConfigurationFacade().hasFollowUp(contact.getDisease())) {
            contact.setFollowUpStatus(random(FollowUpStatus.values()));
        } else {
            contact.setFollowUpStatus(FollowUpStatus.NO_FOLLOW_UP);
        }
        contact.setFollowUpUntil(contact.getFollowUpStatus() == FollowUpStatus.NO_FOLLOW_UP ? null : randomDate(referenceDateTime));
        // Create visits
        if (config.isCreateWithVisits() && FacadeProvider.getDiseaseConfigurationFacade().hasFollowUp(contact.getDisease()) && FollowUpStatus.NO_FOLLOW_UP != contact.getFollowUpStatus()) {
            Date latestFollowUpDate = contact.getFollowUpUntil().before(new Date()) ? contact.getFollowUpUntil() : new Date();
            Date contactStartDate = ContactLogic.getStartDate(contact);
            int followUpCount = random().nextInt(DateHelper.getDaysBetween(contactStartDate, latestFollowUpDate) + 1);
            if (followUpCount > 0) {
                int[] followUpDays = random().ints(1, followUpCount + 1).distinct().limit(followUpCount).toArray();
                List<LocalDateTime> followUpDates = new ArrayList<>();
                for (int day : followUpDays) {
                    followUpDates.add(DateHelper8.toLocalDate(contactStartDate).atStartOfDay().plusDays(day - 1).plusMinutes(random().nextInt(60 * 24 + 1)));
                }
                for (LocalDateTime date : followUpDates) {
                    VisitDto visit = VisitDto.build(contact.getPerson(), contact.getDisease(), VisitOrigin.USER);
                    fillEntity(visit, date);
                    visit.setVisitUser(userReference);
                    visit.setVisitDateTime(DateHelper8.toDate(date));
                    visit.setDisease(contact.getDisease());
                    if (visit.getVisitStatus() == null) {
                        visit.setVisitStatus(VisitStatus.COOPERATIVE);
                    }
                    FacadeProvider.getVisitFacade().saveVisit(visit);
                }
            }
        }
    }
    dt = System.nanoTime() - dt;
    long perContact = dt / config.getContactCount();
    String msg = String.format("Generating %,d contacts took %,d  ms (%,d ms per contact)", config.getContactCount(), dt / 1_000_000, perContact / 1_000_000);
    logger.info(msg);
    Notification.show("", msg, Notification.Type.TRAY_NOTIFICATION);
}
Also used : LocalDateTime(java.time.LocalDateTime) Disease(de.symeda.sormas.api.Disease) DistrictIndexDto(de.symeda.sormas.api.infrastructure.district.DistrictIndexDto) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) DistrictCriteria(de.symeda.sormas.api.infrastructure.district.DistrictCriteria) Date(java.util.Date) LocalDate(java.time.LocalDate) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) SortProperty(de.symeda.sormas.api.utils.SortProperty) CaseCriteria(de.symeda.sormas.api.caze.CaseCriteria) ContactDto(de.symeda.sormas.api.contact.ContactDto) VisitDto(de.symeda.sormas.api.visit.VisitDto) CaseReferenceDto(de.symeda.sormas.api.caze.CaseReferenceDto)

Example 10 with SortProperty

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

the class AreasGrid method setLazyDataProvider.

public void setLazyDataProvider() {
    DataProvider<AreaDto, AreaCriteria> dataProvider = DataProvider.fromFilteringCallbacks(query -> FacadeProvider.getAreaFacade().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.getAreaFacade().count(query.getFilter().orElse(null));
    });
    setDataProvider(dataProvider);
    setSelectionMode(SelectionMode.NONE);
}
Also used : AreaCriteria(de.symeda.sormas.api.infrastructure.area.AreaCriteria) SortProperty(de.symeda.sormas.api.utils.SortProperty) AreaDto(de.symeda.sormas.api.infrastructure.area.AreaDto)

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