Search in sources :

Example 1 with ImmunizationQueryContext

use of de.symeda.sormas.backend.immunization.ImmunizationQueryContext in project SORMAS-Project by hzi-braunschweig.

the class PersonService method getAllAfter.

@Override
public // todo refactor this to use the create user filter form persons
List<Person> getAllAfter(Date date, Integer batchSize, String lastSynchronizedUuid) {
    User user = getCurrentUser();
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    final CriteriaQuery<Person> personsQuery = cb.createQuery(Person.class);
    final Root<Person> personsRoot = personsQuery.from(Person.class);
    final PersonQueryContext personQueryContext = new PersonQueryContext(cb, personsQuery, personsRoot);
    final PersonJoins joins = personQueryContext.getJoins();
    // persons by district
    Join<Person, Location> address = joins.getAddress();
    Predicate districtFilter = cb.equal(address.get(Location.DISTRICT), user.getDistrict());
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(personQueryContext, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        districtFilter = cb.and(districtFilter, dateFilter);
    }
    personsQuery.where(districtFilter);
    List<Person> districtResultList = getBatchedQueryResults(cb, personsQuery, personsRoot, batchSize);
    // persons by case
    CriteriaQuery<Person> casePersonsQuery = cb.createQuery(Person.class);
    Root<Case> casePersonsRoot = casePersonsQuery.from(Case.class);
    Join<Person, Person> casePersonsSelect = casePersonsRoot.join(Case.PERSON);
    casePersonsSelect.fetch(Person.ADDRESS);
    casePersonsQuery.select(casePersonsSelect);
    Predicate casePersonsFilter = caseService.createUserFilter(new CaseQueryContext(cb, casePersonsQuery, new CaseJoins(casePersonsRoot)));
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, casePersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        if (batchSize == null) {
            // include case change dates: When a case is relocated it may become available to another user and this will have to include the person as-well
            Predicate caseDateFilter = caseService.createChangeDateFilter(cb, casePersonsRoot, DateHelper.toTimestampUpper(date));
            dateFilter = cb.or(dateFilter, caseDateFilter);
        }
        if (casePersonsFilter != null) {
            casePersonsFilter = cb.and(casePersonsFilter, dateFilter);
        } else {
            casePersonsFilter = dateFilter;
        }
    }
    if (casePersonsFilter != null) {
        casePersonsQuery.where(casePersonsFilter);
    }
    casePersonsQuery.distinct(true);
    List<Person> casePersonsResultList = getBatchedQueryResults(cb, casePersonsQuery, casePersonsSelect, batchSize);
    // persons by contact
    CriteriaQuery<Person> contactPersonsQuery = cb.createQuery(Person.class);
    Root<Contact> contactPersonsRoot = contactPersonsQuery.from(Contact.class);
    Join<Person, Person> contactPersonsSelect = contactPersonsRoot.join(Contact.PERSON);
    contactPersonsSelect.fetch(Person.ADDRESS);
    contactPersonsQuery.select(contactPersonsSelect);
    Predicate contactPersonsFilter = contactService.createUserFilter(new ContactQueryContext(cb, contactPersonsQuery, new ContactJoins(contactPersonsRoot)));
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, contactPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        if (batchSize == null) {
            Predicate contactDateFilter = contactService.createChangeDateFilter(cb, contactPersonsRoot, date);
            dateFilter = cb.or(dateFilter, contactDateFilter);
        }
        contactPersonsFilter = and(cb, contactPersonsFilter, dateFilter);
    }
    if (contactPersonsFilter != null) {
        contactPersonsQuery.where(contactPersonsFilter);
    }
    contactPersonsQuery.distinct(true);
    List<Person> contactPersonsResultList = getBatchedQueryResults(cb, contactPersonsQuery, contactPersonsSelect, batchSize);
    // persons by event participant
    CriteriaQuery<Person> eventPersonsQuery = cb.createQuery(Person.class);
    Root<EventParticipant> eventPersonsRoot = eventPersonsQuery.from(EventParticipant.class);
    Join<Person, Person> eventPersonsSelect = eventPersonsRoot.join(EventParticipant.PERSON);
    eventPersonsSelect.fetch(Person.ADDRESS);
    eventPersonsQuery.select(eventPersonsSelect);
    Predicate eventPersonsFilter = eventParticipantService.createUserFilter(new EventParticipantQueryContext(cb, eventPersonsQuery, new EventParticipantJoins(eventPersonsRoot)));
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, eventPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        if (batchSize == null) {
            Predicate eventParticipantDateFilter = eventParticipantService.createChangeDateFilter(cb, eventPersonsRoot, DateHelper.toTimestampUpper(date));
            dateFilter = cb.or(dateFilter, eventParticipantDateFilter);
        }
        eventPersonsFilter = and(cb, eventPersonsFilter, dateFilter);
    }
    if (eventPersonsFilter != null) {
        eventPersonsQuery.where(eventPersonsFilter);
    }
    eventPersonsQuery.distinct(true);
    List<Person> eventPersonsResultList = getBatchedQueryResults(cb, eventPersonsQuery, eventPersonsSelect, batchSize);
    // persons by immunization
    List<Person> immunizationPersonsResultList = new ArrayList<>();
    if (!featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
        CriteriaQuery<Person> immunizationPersonsQuery = cb.createQuery(Person.class);
        Root<Immunization> immunizationPersonsRoot = immunizationPersonsQuery.from(Immunization.class);
        Join<Immunization, Person> immunizationPersonsSelect = immunizationPersonsRoot.join(Immunization.PERSON);
        immunizationPersonsSelect.fetch(Person.ADDRESS);
        immunizationPersonsQuery.select(immunizationPersonsSelect);
        Predicate immunizationPersonsFilter = immunizationService.createUserFilter(new ImmunizationQueryContext(cb, immunizationPersonsQuery, new ImmunizationJoins(immunizationPersonsRoot)));
        // date range
        if (date != null) {
            Predicate dateFilter = createChangeDateFilter(cb, immunizationPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
            if (batchSize == null) {
                Predicate immunizationDateFilter = immunizationService.createChangeDateFilter(cb, immunizationPersonsRoot, DateHelper.toTimestampUpper(date));
                dateFilter = cb.or(dateFilter, immunizationDateFilter);
            }
            immunizationPersonsFilter = and(cb, immunizationPersonsFilter, dateFilter);
        }
        if (immunizationPersonsFilter != null) {
            immunizationPersonsQuery.where(immunizationPersonsFilter);
        }
        immunizationPersonsQuery.distinct(true);
        immunizationPersonsResultList = getBatchedQueryResults(cb, immunizationPersonsQuery, immunizationPersonsSelect, batchSize);
    }
    List<Person> travelEntryPersonsResultList = new ArrayList<>();
    // if a batch size is given, this is a sync from the mobile app where travel entries are not relevant for now
    if (batchSize == null) {
        // persons by travel entries
        CriteriaQuery<Person> tepQuery = cb.createQuery(Person.class);
        Root<TravelEntry> tepRoot = tepQuery.from(TravelEntry.class);
        Join<TravelEntry, Person> tepSelect = tepRoot.join(TravelEntry.PERSON);
        tepSelect.fetch(Person.ADDRESS);
        tepQuery.select(tepSelect);
        Predicate tepFilter = travelEntryService.createUserFilter(new TravelEntryQueryContext(cb, tepQuery, new TravelEntryJoins(tepRoot)));
        // date range
        if (date != null) {
            Predicate dateFilter = createChangeDateFilter(cb, tepSelect, DateHelper.toTimestampUpper(date));
            Predicate travelEntryDateFilter = travelEntryService.createChangeDateFilter(cb, tepRoot, DateHelper.toTimestampUpper(date));
            tepFilter = and(cb, tepFilter, cb.or(dateFilter, travelEntryDateFilter));
        }
        if (tepFilter != null) {
            tepQuery.where(tepFilter);
        }
        tepQuery.distinct(true);
        travelEntryPersonsResultList = em.createQuery(tepQuery).getResultList();
    }
    return Stream.of(districtResultList, casePersonsResultList, contactPersonsResultList, eventPersonsResultList, immunizationPersonsResultList, travelEntryPersonsResultList).flatMap(List<Person>::stream).distinct().sorted(new ChangeDateUuidComparator<>()).limit(batchSize == null ? Long.MAX_VALUE : batchSize).collect(Collectors.toList());
}
Also used : TravelEntry(de.symeda.sormas.backend.travelentry.TravelEntry) User(de.symeda.sormas.backend.user.User) TravelEntryJoins(de.symeda.sormas.backend.travelentry.TravelEntryJoins) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Case(de.symeda.sormas.backend.caze.Case) EventParticipantJoins(de.symeda.sormas.backend.event.EventParticipantJoins) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ImmunizationQueryContext(de.symeda.sormas.backend.immunization.ImmunizationQueryContext) CaseJoins(de.symeda.sormas.backend.caze.CaseJoins) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Immunization(de.symeda.sormas.backend.immunization.entity.Immunization) ContactJoins(de.symeda.sormas.backend.contact.ContactJoins) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext) Contact(de.symeda.sormas.backend.contact.Contact) ImmunizationJoins(de.symeda.sormas.backend.immunization.ImmunizationJoins) TravelEntryQueryContext(de.symeda.sormas.backend.travelentry.TravelEntryQueryContext) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Location(de.symeda.sormas.backend.location.Location)

Example 2 with ImmunizationQueryContext

use of de.symeda.sormas.backend.immunization.ImmunizationQueryContext in project SORMAS-Project by hzi-braunschweig.

the class PersonService method getSimilarPersonDtos.

public List<SimilarPersonDto> getSimilarPersonDtos(PersonSimilarityCriteria criteria, Integer limit) {
    setSimilarityThresholdQuery();
    boolean activeEntriesOnly = configFacade.isDuplicateChecksExcludePersonsOfArchivedEntries();
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    final CriteriaQuery<Person> personQuery = cb.createQuery(Person.class);
    final Root<Person> personRoot = personQuery.from(Person.class);
    final PersonQueryContext personQueryContext = new PersonQueryContext(cb, personQuery, personRoot);
    final PersonJoins joins = personQueryContext.getJoins();
    Join<Person, Case> personCaseJoin = joins.getCaze();
    Join<Person, Contact> personContactJoin = joins.getContact();
    Join<Person, EventParticipant> personEventParticipantJoin = joins.getEventParticipant();
    Join<Person, Immunization> personImmunizationJoin = joins.getImmunization();
    Join<Person, TravelEntry> personTravelEntryJoin = joins.getTravelEntry();
    // Persons of active cases
    Predicate personSimilarityFilter = buildSimilarityCriteriaFilter(criteria, cb, personRoot);
    Predicate activeCasesFilter = activeEntriesOnly ? caseService.createActiveCasesFilter(cb, personCaseJoin) : caseService.createDefaultFilter(cb, personCaseJoin);
    Predicate caseUserFilter = caseService.createUserFilter(new CaseQueryContext(cb, personQuery, personQueryContext.getJoins().getCaseJoins()));
    Predicate personCasePredicate = and(cb, personCaseJoin.get(Case.ID).isNotNull(), activeCasesFilter, caseUserFilter);
    // Persons of active contacts
    final ContactQueryContext contactQueryContext = new ContactQueryContext(cb, personQuery, joins.getContactJoins());
    Predicate activeContactsFilter = activeEntriesOnly ? contactService.createActiveContactsFilter(contactQueryContext) : contactService.createDefaultFilter(cb, personContactJoin);
    Predicate contactUserFilter = contactService.createUserFilter(contactQueryContext, null);
    Predicate personContactPredicate = and(cb, personContactJoin.get(Contact.ID).isNotNull(), contactUserFilter, activeContactsFilter);
    // Persons of event participants in active events
    final EventParticipantQueryContext eventParticipantQueryContext = new EventParticipantQueryContext(cb, personQuery, joins.getEventParticipantJoins());
    Predicate activeEventParticipantsFilter = activeEntriesOnly ? eventParticipantService.createActiveEventParticipantsInActiveEventsFilter(eventParticipantQueryContext) : eventParticipantService.createDefaultInUndeletedEventsFilter(eventParticipantQueryContext);
    Predicate eventParticipantUserFilter = eventParticipantService.createUserFilter(eventParticipantQueryContext);
    Predicate personEventParticipantPredicate = and(cb, personEventParticipantJoin.get(EventParticipant.ID).isNotNull(), activeEventParticipantsFilter, eventParticipantUserFilter);
    // Persons of active immunizations
    Predicate personImmunizationPredicate = null;
    if (!featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
        Predicate activeImmunizationsFilter = activeEntriesOnly ? immunizationService.createActiveImmunizationsFilter(cb, personImmunizationJoin) : immunizationService.createDefaultFilter(cb, personImmunizationJoin);
        Predicate immunizationUserFilter = immunizationService.createUserFilter(new ImmunizationQueryContext(cb, personQuery, joins.getImmunizationJoins()));
        personImmunizationPredicate = and(cb, personImmunizationJoin.get(Immunization.ID).isNotNull(), immunizationUserFilter, activeImmunizationsFilter);
    }
    // Persons of active travel entries
    Predicate activeTravelEntriesFilter = activeEntriesOnly ? travelEntryService.createActiveTravelEntriesFilter(cb, personTravelEntryJoin) : travelEntryService.createDefaultFilter(cb, personTravelEntryJoin);
    Predicate travelEntryUserFilter = travelEntryService.createUserFilter(new TravelEntryQueryContext(cb, personQuery, joins.getTravelEntryJoins()));
    Predicate personTravelEntryPredicate = and(cb, personTravelEntryJoin.get(TravelEntry.ID).isNotNull(), travelEntryUserFilter, activeTravelEntriesFilter);
    Predicate finalPredicate = CriteriaBuilderHelper.or(cb, personCasePredicate, personContactPredicate, personEventParticipantPredicate, personImmunizationPredicate, personTravelEntryPredicate);
    personQuery.where(and(cb, personSimilarityFilter, finalPredicate));
    personQuery.distinct(true);
    TypedQuery<Person> query = em.createQuery(personQuery);
    if (limit != null) {
        query.setMaxResults(limit);
    }
    List<Person> persons = query.getResultList();
    List<Long> personsInJurisdiction = getInJurisdictionIDs(persons);
    return persons.stream().filter(p -> personsInJurisdiction.contains(p.getId())).map(this::toSimilarPersonDto).collect(Collectors.toList());
}
Also used : TravelEntry(de.symeda.sormas.backend.travelentry.TravelEntry) Case(de.symeda.sormas.backend.caze.Case) Predicate(javax.persistence.criteria.Predicate) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) ImmunizationQueryContext(de.symeda.sormas.backend.immunization.ImmunizationQueryContext) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Immunization(de.symeda.sormas.backend.immunization.entity.Immunization) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext) Contact(de.symeda.sormas.backend.contact.Contact) TravelEntryQueryContext(de.symeda.sormas.backend.travelentry.TravelEntryQueryContext) EventParticipant(de.symeda.sormas.backend.event.EventParticipant)

Example 3 with ImmunizationQueryContext

use of de.symeda.sormas.backend.immunization.ImmunizationQueryContext in project SORMAS-Project by hzi-braunschweig.

the class PersonService method getAllUuids.

@Override
public List<String> getAllUuids() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    // persons by LGA
    CriteriaQuery<String> lgaQuery = cb.createQuery(String.class);
    Root<Person> lgaRoot = lgaQuery.from(Person.class);
    Join<Person, Location> address = lgaRoot.join(Person.ADDRESS);
    lgaQuery.select(lgaRoot.get(Person.UUID));
    Predicate lgaFilter = cb.equal(address.get(Location.DISTRICT), getCurrentUser().getDistrict());
    lgaQuery.where(lgaFilter);
    List<String> lgaResultList = em.createQuery(lgaQuery).getResultList();
    // persons by case
    CriteriaQuery<String> casePersonsQuery = cb.createQuery(String.class);
    Root<Case> casePersonsRoot = casePersonsQuery.from(Case.class);
    Join<Person, Person> casePersonsSelect = casePersonsRoot.join(Case.PERSON);
    casePersonsQuery.select(casePersonsSelect.get(Person.UUID));
    Predicate casePersonsFilter = caseService.createUserFilter(new CaseQueryContext(cb, casePersonsQuery, new CaseJoins(casePersonsRoot)));
    if (casePersonsFilter != null) {
        casePersonsQuery.where(casePersonsFilter);
    }
    casePersonsQuery.distinct(true);
    List<String> casePersonsResultList = em.createQuery(casePersonsQuery).getResultList();
    // persons by contact
    CriteriaQuery<String> contactPersonsQuery = cb.createQuery(String.class);
    Root<Contact> contactPersonsRoot = contactPersonsQuery.from(Contact.class);
    Join<Person, Person> contactPersonsSelect = contactPersonsRoot.join(Contact.PERSON);
    contactPersonsQuery.select(contactPersonsSelect.get(Person.UUID));
    Predicate contactPersonsFilter = contactService.createUserFilter(new ContactQueryContext(cb, contactPersonsQuery, new ContactJoins(contactPersonsRoot)));
    if (contactPersonsFilter != null) {
        contactPersonsQuery.where(contactPersonsFilter);
    }
    contactPersonsQuery.distinct(true);
    List<String> contactPersonsResultList = em.createQuery(contactPersonsQuery).getResultList();
    // persons by event participant
    CriteriaQuery<String> eventPersonsQuery = cb.createQuery(String.class);
    Root<EventParticipant> eventPersonsRoot = eventPersonsQuery.from(EventParticipant.class);
    Join<Person, Person> eventPersonsSelect = eventPersonsRoot.join(EventParticipant.PERSON);
    eventPersonsQuery.select(eventPersonsSelect.get(Person.UUID));
    Predicate eventPersonsFilter = eventParticipantService.createUserFilter(new EventParticipantQueryContext(cb, eventPersonsQuery, new EventParticipantJoins(eventPersonsRoot)));
    if (eventPersonsFilter != null) {
        eventPersonsQuery.where(eventPersonsFilter);
    }
    eventPersonsQuery.distinct(true);
    List<String> eventPersonsResultList = em.createQuery(eventPersonsQuery).getResultList();
    // persons by immunization
    List<String> immunizationPersonsResultList = new ArrayList<>();
    if (!featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
        CriteriaQuery<String> immunizationPersonsQuery = cb.createQuery(String.class);
        Root<Immunization> immunizationPersonsRoot = immunizationPersonsQuery.from(Immunization.class);
        Join<Immunization, Person> immunizationPersonsSelect = immunizationPersonsRoot.join(Immunization.PERSON);
        immunizationPersonsQuery.select(immunizationPersonsSelect.get(Person.UUID));
        Predicate immunizationPersonsFilter = immunizationService.createUserFilter(new ImmunizationQueryContext(cb, immunizationPersonsQuery, new ImmunizationJoins(immunizationPersonsRoot)));
        if (immunizationPersonsFilter != null) {
            immunizationPersonsQuery.where(immunizationPersonsFilter);
        }
        immunizationPersonsQuery.distinct(true);
        immunizationPersonsResultList = em.createQuery(immunizationPersonsQuery).getResultList();
    }
    // persons by travel entry
    CriteriaQuery<String> travelEntryPersonsQuery = cb.createQuery(String.class);
    Root<TravelEntry> travelEntryPersonsRoot = travelEntryPersonsQuery.from(TravelEntry.class);
    Join<TravelEntry, Person> travelEntryPersonsSelect = travelEntryPersonsRoot.join(TravelEntry.PERSON);
    travelEntryPersonsQuery.select(travelEntryPersonsSelect.get(Person.UUID));
    Predicate travelEntryPersonsFilter = travelEntryService.createUserFilter(new TravelEntryQueryContext(cb, travelEntryPersonsQuery, new TravelEntryJoins(travelEntryPersonsRoot)));
    if (travelEntryPersonsFilter != null) {
        travelEntryPersonsQuery.where(travelEntryPersonsFilter);
    }
    travelEntryPersonsQuery.distinct(true);
    List<String> travelEntryPersonsResultList = em.createQuery(travelEntryPersonsQuery).getResultList();
    return Stream.of(lgaResultList, casePersonsResultList, contactPersonsResultList, eventPersonsResultList, immunizationPersonsResultList, travelEntryPersonsResultList).flatMap(List<String>::stream).distinct().collect(Collectors.toList());
}
Also used : TravelEntry(de.symeda.sormas.backend.travelentry.TravelEntry) TravelEntryJoins(de.symeda.sormas.backend.travelentry.TravelEntryJoins) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Case(de.symeda.sormas.backend.caze.Case) EventParticipantJoins(de.symeda.sormas.backend.event.EventParticipantJoins) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ImmunizationQueryContext(de.symeda.sormas.backend.immunization.ImmunizationQueryContext) CaseJoins(de.symeda.sormas.backend.caze.CaseJoins) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Immunization(de.symeda.sormas.backend.immunization.entity.Immunization) ContactJoins(de.symeda.sormas.backend.contact.ContactJoins) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext) Contact(de.symeda.sormas.backend.contact.Contact) ImmunizationJoins(de.symeda.sormas.backend.immunization.ImmunizationJoins) TravelEntryQueryContext(de.symeda.sormas.backend.travelentry.TravelEntryQueryContext) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Location(de.symeda.sormas.backend.location.Location)

Example 4 with ImmunizationQueryContext

use of de.symeda.sormas.backend.immunization.ImmunizationQueryContext in project SORMAS-Project by hzi-braunschweig.

the class PersonService method createUserFilter.

@SuppressWarnings("rawtypes")
public Predicate createUserFilter(PersonQueryContext personQueryContext, PersonCriteria personCriteria) {
    final CriteriaBuilder cb = personQueryContext.getCriteriaBuilder();
    final CriteriaQuery cq = personQueryContext.getQuery();
    final PersonJoins joins = personQueryContext.getJoins();
    final boolean fullImmunizationModuleUsed = !featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED);
    // 1. Define filters per association lazy to avoid superfluous joins
    final Supplier<Predicate> caseFilter = () -> CriteriaBuilderHelper.and(cb, caseService.createUserFilter(new CaseQueryContext(cb, cq, joins.getCaseJoins()), new CaseUserFilterCriteria()), caseService.createDefaultFilter(cb, joins.getCaze()));
    final Supplier<Predicate> contactFilter = () -> {
        final Predicate contactUserFilter = contactService.createUserFilter(new ContactQueryContext(cb, cq, joins.getContactJoins()), new ContactCriteria().includeContactsFromOtherJurisdictions(false));
        return CriteriaBuilderHelper.and(cb, contactUserFilter, contactService.createDefaultFilter(cb, joins.getContact()));
    };
    final Supplier<Predicate> eventParticipantFilter = () -> CriteriaBuilderHelper.and(cb, eventParticipantService.createUserFilter(new EventParticipantQueryContext(cb, cq, joins.getEventParticipantJoins()), new EventUserFilterCriteria().includeUserCaseAndEventParticipantFilter(false).forceRegionJurisdiction(true)), eventParticipantService.createDefaultFilter(cb, joins.getEventParticipant()));
    final Supplier<Predicate> immunizationFilter = fullImmunizationModuleUsed ? () -> CriteriaBuilderHelper.and(cb, immunizationService.createUserFilter(new ImmunizationQueryContext(cb, cq, joins.getImmunizationJoins())), immunizationService.createDefaultFilter(cb, joins.getImmunization())) : () -> null;
    final Supplier<Predicate> travelEntryFilter = () -> CriteriaBuilderHelper.and(cb, travelEntryService.createUserFilter(new TravelEntryQueryContext(cb, cq, joins.getTravelEntryJoins())), travelEntryService.createDefaultFilter(cb, joins.getTravelEntry()));
    // 2. Define the Joins on associations where needed
    PersonAssociation personAssociation = Optional.ofNullable(personCriteria).map(e -> e.getPersonAssociation()).orElse(PersonCriteria.DEFAULT_ASSOCIATION);
    switch(personAssociation) {
        case ALL:
            return CriteriaBuilderHelper.or(cb, caseFilter.get(), contactFilter.get(), eventParticipantFilter.get(), fullImmunizationModuleUsed ? immunizationFilter.get() : null, travelEntryFilter.get());
        case CASE:
            return caseFilter.get();
        case CONTACT:
            return contactFilter.get();
        case EVENT_PARTICIPANT:
            return eventParticipantFilter.get();
        case IMMUNIZATION:
            if (!fullImmunizationModuleUsed) {
                throw new UnsupportedOperationException("Filtering persons by immunizations is not supported when the reduced immunization module is used.");
            }
            return immunizationFilter.get();
        case TRAVEL_ENTRY:
            return travelEntryFilter.get();
        default:
            throw new IllegalArgumentException(personAssociation.toString());
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) FeatureType(de.symeda.sormas.api.feature.FeatureType) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) VisitService(de.symeda.sormas.backend.visit.VisitService) Arrays(java.util.Arrays) ApproximateAgeType(de.symeda.sormas.api.person.ApproximateAgeType) GeocodingService(de.symeda.sormas.backend.geocoding.GeocodingService) Join(javax.persistence.criteria.Join) SIMILARITY_OPERATOR(de.symeda.sormas.backend.ExtendedPostgreSQL94Dialect.SIMILARITY_OPERATOR) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) EventParticipantJoins(de.symeda.sormas.backend.event.EventParticipantJoins) Date(java.util.Date) CriteriaBuilderHelper.andEquals(de.symeda.sormas.backend.common.CriteriaBuilderHelper.andEquals) PersonAssociation(de.symeda.sormas.api.person.PersonAssociation) Immunization(de.symeda.sormas.backend.immunization.entity.Immunization) ExternalDataUpdateException(de.symeda.sormas.api.externaldata.ExternalDataUpdateException) StringUtils(org.apache.commons.lang3.StringUtils) TravelEntryQueryContext(de.symeda.sormas.backend.travelentry.TravelEntryQueryContext) TransactionAttributeType(javax.ejb.TransactionAttributeType) Predicate(javax.persistence.criteria.Predicate) EventUserFilterCriteria(de.symeda.sormas.backend.event.EventUserFilterCriteria) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GeoLatLon(de.symeda.sormas.api.geo.GeoLatLon) ImmunizationJoins(de.symeda.sormas.backend.immunization.ImmunizationJoins) ContactJoins(de.symeda.sormas.backend.contact.ContactJoins) From(javax.persistence.criteria.From) CriteriaBuilderHelper.andEqualsReferenceDto(de.symeda.sormas.backend.common.CriteriaBuilderHelper.andEqualsReferenceDto) Path(javax.persistence.criteria.Path) Contact(de.symeda.sormas.backend.contact.Contact) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ParameterExpression(javax.persistence.criteria.ParameterExpression) CaseService(de.symeda.sormas.backend.caze.CaseService) Transactional(javax.transaction.Transactional) Timestamp(java.sql.Timestamp) SimilarPersonDto(de.symeda.sormas.api.person.SimilarPersonDto) Sex(de.symeda.sormas.api.person.Sex) Region(de.symeda.sormas.backend.infrastructure.region.Region) TravelEntryJoins(de.symeda.sormas.backend.travelentry.TravelEntryJoins) PersonHelper(de.symeda.sormas.api.person.PersonHelper) District(de.symeda.sormas.backend.infrastructure.district.District) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext) List(java.util.List) Query(javax.persistence.Query) UserService(de.symeda.sormas.backend.user.UserService) Stream(java.util.stream.Stream) User(de.symeda.sormas.backend.user.User) CoreAdo(de.symeda.sormas.backend.common.CoreAdo) ExternalDataDto(de.symeda.sormas.api.externaldata.ExternalDataDto) Optional(java.util.Optional) PersonSimilarityCriteria(de.symeda.sormas.api.person.PersonSimilarityCriteria) Location(de.symeda.sormas.backend.location.Location) FeatureConfigurationFacadeEjbLocal(de.symeda.sormas.backend.feature.FeatureConfigurationFacadeEjb.FeatureConfigurationFacadeEjbLocal) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Community(de.symeda.sormas.backend.infrastructure.community.Community) CaseUserFilterCriteria(de.symeda.sormas.backend.caze.CaseUserFilterCriteria) ImmunizationQueryContext(de.symeda.sormas.backend.immunization.ImmunizationQueryContext) CaseClassification(de.symeda.sormas.api.caze.CaseClassification) DateHelper(de.symeda.sormas.api.utils.DateHelper) Supplier(java.util.function.Supplier) TypedQuery(javax.persistence.TypedQuery) ExternalDataUtil(de.symeda.sormas.backend.util.ExternalDataUtil) EventParticipantService(de.symeda.sormas.backend.event.EventParticipantService) ArrayList(java.util.ArrayList) Case(de.symeda.sormas.backend.caze.Case) AdoServiceWithUserFilter(de.symeda.sormas.backend.common.AdoServiceWithUserFilter) ChangeDateUuidComparator(de.symeda.sormas.backend.util.ChangeDateUuidComparator) TransactionAttribute(javax.ejb.TransactionAttribute) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) CriteriaBuilderHelper.and(de.symeda.sormas.backend.common.CriteriaBuilderHelper.and) LocalBean(javax.ejb.LocalBean) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) LinkedList(java.util.LinkedList) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) CaseJoins(de.symeda.sormas.backend.caze.CaseJoins) ConfigFacadeEjbLocal(de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ChangeDateFilterBuilder(de.symeda.sormas.backend.common.ChangeDateFilterBuilder) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) PersonCriteria(de.symeda.sormas.api.person.PersonCriteria) ManualMessageLogService(de.symeda.sormas.backend.common.messaging.ManualMessageLogService) DataHelper(de.symeda.sormas.api.utils.DataHelper) ImmunizationService(de.symeda.sormas.backend.immunization.ImmunizationService) TravelEntryService(de.symeda.sormas.backend.travelentry.services.TravelEntryService) Disease(de.symeda.sormas.api.Disease) ContactService(de.symeda.sormas.backend.contact.ContactService) Subquery(javax.persistence.criteria.Subquery) FeatureTypeProperty(de.symeda.sormas.api.feature.FeatureTypeProperty) TravelEntry(de.symeda.sormas.backend.travelentry.TravelEntry) Collections(java.util.Collections) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext) EventUserFilterCriteria(de.symeda.sormas.backend.event.EventUserFilterCriteria) CaseUserFilterCriteria(de.symeda.sormas.backend.caze.CaseUserFilterCriteria) Predicate(javax.persistence.criteria.Predicate) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) TravelEntryQueryContext(de.symeda.sormas.backend.travelentry.TravelEntryQueryContext) ImmunizationQueryContext(de.symeda.sormas.backend.immunization.ImmunizationQueryContext) PersonAssociation(de.symeda.sormas.api.person.PersonAssociation)

Aggregations

Case (de.symeda.sormas.backend.caze.Case)4 CaseQueryContext (de.symeda.sormas.backend.caze.CaseQueryContext)4 Contact (de.symeda.sormas.backend.contact.Contact)4 ContactQueryContext (de.symeda.sormas.backend.contact.ContactQueryContext)4 EventParticipant (de.symeda.sormas.backend.event.EventParticipant)4 EventParticipantQueryContext (de.symeda.sormas.backend.event.EventParticipantQueryContext)4 ImmunizationQueryContext (de.symeda.sormas.backend.immunization.ImmunizationQueryContext)4 Immunization (de.symeda.sormas.backend.immunization.entity.Immunization)4 TravelEntry (de.symeda.sormas.backend.travelentry.TravelEntry)4 TravelEntryQueryContext (de.symeda.sormas.backend.travelentry.TravelEntryQueryContext)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 CaseJoins (de.symeda.sormas.backend.caze.CaseJoins)3 ContactJoins (de.symeda.sormas.backend.contact.ContactJoins)3 EventParticipantJoins (de.symeda.sormas.backend.event.EventParticipantJoins)3 ImmunizationJoins (de.symeda.sormas.backend.immunization.ImmunizationJoins)3 Location (de.symeda.sormas.backend.location.Location)3 TravelEntryJoins (de.symeda.sormas.backend.travelentry.TravelEntryJoins)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3