Search in sources :

Example 1 with ContactQueryContext

use of de.symeda.sormas.backend.contact.ContactQueryContext in project SORMAS-Project by hzi-braunschweig.

the class CaseService method createUserFilter.

@SuppressWarnings("rawtypes")
public Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Case> casePath, CaseUserFilterCriteria userFilterCriteria) {
    User currentUser = getCurrentUser();
    if (currentUser == null) {
        return null;
    }
    Predicate filterResponsible = null;
    Predicate filter = null;
    final JurisdictionLevel jurisdictionLevel = currentUser.getCalculatedJurisdictionLevel();
    if (jurisdictionLevel != JurisdictionLevel.NATION && !currentUser.hasAnyUserRole(UserRole.REST_USER, UserRole.REST_EXTERNAL_VISITS_USER)) {
        // whoever created the case or is assigned to it is allowed to access it
        if (userFilterCriteria == null || (userFilterCriteria.getIncludeCasesFromOtherJurisdictions())) {
            filterResponsible = cb.equal(casePath.get(Case.REPORTING_USER).get(User.ID), currentUser.getId());
            filterResponsible = cb.or(filterResponsible, cb.equal(casePath.get(Case.SURVEILLANCE_OFFICER).get(User.ID), currentUser.getId()));
            filterResponsible = cb.or(filterResponsible, cb.equal(casePath.get(Case.CASE_OFFICER).get(User.ID), currentUser.getId()));
        }
        switch(jurisdictionLevel) {
            case REGION:
                final Region region = currentUser.getRegion();
                if (region != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.REGION).get(Region.ID), region.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_REGION).get(Region.ID), region.getId()));
                }
                break;
            case DISTRICT:
                final District district = currentUser.getDistrict();
                if (district != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.DISTRICT).get(District.ID), district.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_DISTRICT).get(District.ID), district.getId()));
                }
                break;
            case HEALTH_FACILITY:
                final Facility healthFacility = currentUser.getHealthFacility();
                if (healthFacility != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.HEALTH_FACILITY).get(Facility.ID), healthFacility.getId()));
                }
                break;
            case COMMUNITY:
                final Community community = currentUser.getCommunity();
                if (community != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.COMMUNITY).get(Community.ID), community.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_COMMUNITY).get(Community.ID), community.getId()));
                }
                break;
            case POINT_OF_ENTRY:
                final PointOfEntry pointOfEntry = currentUser.getPointOfEntry();
                if (pointOfEntry != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.POINT_OF_ENTRY).get(PointOfEntry.ID), pointOfEntry.getId()));
                }
                break;
            case LABORATORY:
                final Subquery<Long> sampleSubQuery = cq.subquery(Long.class);
                final Root<Sample> sampleRoot = sampleSubQuery.from(Sample.class);
                final SampleJoins joins = new SampleJoins(sampleRoot);
                final Join cazeJoin = joins.getCaze();
                sampleSubQuery.where(cb.and(cb.equal(cazeJoin, casePath), sampleService.createUserFilterWithoutAssociations(cb, joins)));
                sampleSubQuery.select(sampleRoot.get(Sample.ID));
                filter = CriteriaBuilderHelper.or(cb, filter, cb.exists(sampleSubQuery));
                break;
            default:
        }
        // get all cases based on the user's contact association
        if (userFilterCriteria == null || (!userFilterCriteria.isExcludeCasesFromContacts() && Boolean.TRUE.equals(userFilterCriteria.getIncludeCasesFromOtherJurisdictions()))) {
            filter = CriteriaBuilderHelper.or(cb, filter, contactService.createUserFilterWithoutCase(new ContactQueryContext(cb, cq, casePath.join(Case.CONTACTS, JoinType.LEFT))));
        }
        // all users (without specific restrictions) get access to cases that have been made available to the whole country
        if ((userFilterCriteria == null || userFilterCriteria.getIncludeCasesFromOtherJurisdictions()) && !featureConfigurationFacade.isFeatureDisabled(FeatureType.NATIONAL_CASE_SHARING)) {
            filter = CriteriaBuilderHelper.or(cb, filter, cb.isTrue(casePath.get(Case.SHARED_TO_COUNTRY)));
        }
    }
    // only show cases of a specific disease if a limited disease is set
    if (currentUser.getLimitedDisease() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(casePath.get(Case.DISEASE), currentUser.getLimitedDisease()));
    }
    // port health users can only see port health cases
    if (UserRole.isPortHealthUser(currentUser.getUserRoles())) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(casePath.get(Case.CASE_ORIGIN), CaseOrigin.POINT_OF_ENTRY));
    }
    filter = CriteriaBuilderHelper.or(cb, filter, filterResponsible);
    return filter;
}
Also used : User(de.symeda.sormas.backend.user.User) Sample(de.symeda.sormas.backend.sample.Sample) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Join(javax.persistence.criteria.Join) Predicate(javax.persistence.criteria.Predicate) Region(de.symeda.sormas.backend.infrastructure.region.Region) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) PointOfEntry(de.symeda.sormas.backend.infrastructure.pointofentry.PointOfEntry) District(de.symeda.sormas.backend.infrastructure.district.District) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) Community(de.symeda.sormas.backend.infrastructure.community.Community)

Example 2 with ContactQueryContext

use of de.symeda.sormas.backend.contact.ContactQueryContext 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 3 with ContactQueryContext

use of de.symeda.sormas.backend.contact.ContactQueryContext 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 4 with ContactQueryContext

use of de.symeda.sormas.backend.contact.ContactQueryContext in project SORMAS-Project by hzi-braunschweig.

the class SampleService method getJurisdictionSelections.

public List<Selection<?>> getJurisdictionSelections(SampleQueryContext qc) {
    final CriteriaBuilder cb = qc.getCriteriaBuilder();
    final SampleJoins joins = qc.getJoins();
    final CriteriaQuery cq = qc.getQuery();
    return Arrays.asList(JurisdictionHelper.booleanSelector(cb, inJurisdictionOrOwned(qc)), JurisdictionHelper.booleanSelector(cb, cb.and(cb.isNotNull(joins.getCaze()), caseService.inJurisdictionOrOwned(new CaseQueryContext(cb, cq, joins.getCaseJoins())))), JurisdictionHelper.booleanSelector(cb, cb.and(cb.isNotNull(joins.getContact()), contactService.inJurisdictionOrOwned(new ContactQueryContext(cb, cq, joins.getContactJoins())))), JurisdictionHelper.booleanSelector(cb, cb.and(cb.isNotNull(joins.getContact()), cb.isNotNull(joins.getContactJoins().getCaze()), caseService.inJurisdictionOrOwned(new CaseQueryContext(cb, cq, joins.getContactJoins().getCaseJoins())))), JurisdictionHelper.booleanSelector(cb, cb.and(cb.isNotNull(joins.getEventParticipant()), eventParticipantService.inJurisdictionOrOwned(new EventParticipantQueryContext(cb, cq, joins.getEventParticipantJoins())))));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext)

Example 5 with ContactQueryContext

use of de.symeda.sormas.backend.contact.ContactQueryContext in project SORMAS-Project by hzi-braunschweig.

the class CaseService method createUserFilter.

@SuppressWarnings("rawtypes")
public Predicate createUserFilter(CaseQueryContext caseQueryContext, CaseUserFilterCriteria userFilterCriteria) {
    User currentUser = getCurrentUser();
    if (currentUser == null) {
        return null;
    }
    final CriteriaQuery<?> cq = caseQueryContext.getQuery();
    final CriteriaBuilder cb = caseQueryContext.getCriteriaBuilder();
    final From<?, Case> casePath = caseQueryContext.getRoot();
    Predicate filterResponsible = null;
    Predicate filter = null;
    final JurisdictionLevel jurisdictionLevel = currentUser.getJurisdictionLevel();
    if (jurisdictionLevel != JurisdictionLevel.NATION && !currentUser.hasUserRole(UserRole.REST_USER)) {
        // whoever created the case or is assigned to it is allowed to access it
        if (userFilterCriteria == null || (userFilterCriteria.getIncludeCasesFromOtherJurisdictions())) {
            filterResponsible = cb.equal(casePath.get(Case.REPORTING_USER).get(User.ID), currentUser.getId());
            filterResponsible = cb.or(filterResponsible, cb.equal(casePath.get(Case.SURVEILLANCE_OFFICER).get(User.ID), currentUser.getId()));
            filterResponsible = cb.or(filterResponsible, cb.equal(casePath.get(Case.CASE_OFFICER).get(User.ID), currentUser.getId()));
        } else {
            // make sure we don't see all cases just because no filter is defined at all
            filterResponsible = cb.disjunction();
        }
        switch(jurisdictionLevel) {
            case REGION:
                final Region region = currentUser.getRegion();
                if (region != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.REGION).get(Region.ID), region.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_REGION).get(Region.ID), region.getId()));
                }
                break;
            case DISTRICT:
                final District district = currentUser.getDistrict();
                if (district != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.DISTRICT).get(District.ID), district.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_DISTRICT).get(District.ID), district.getId()));
                }
                break;
            case HEALTH_FACILITY:
                final Facility healthFacility = currentUser.getHealthFacility();
                if (healthFacility != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.HEALTH_FACILITY).get(Facility.ID), healthFacility.getId()));
                }
                break;
            case COMMUNITY:
                final Community community = currentUser.getCommunity();
                if (community != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.COMMUNITY).get(Community.ID), community.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_COMMUNITY).get(Community.ID), community.getId()));
                }
                break;
            case POINT_OF_ENTRY:
                final PointOfEntry pointOfEntry = currentUser.getPointOfEntry();
                if (pointOfEntry != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.POINT_OF_ENTRY).get(PointOfEntry.ID), pointOfEntry.getId()));
                }
                break;
            case LABORATORY:
                final Subquery<Long> sampleSubQuery = cq.subquery(Long.class);
                final Root<Sample> sampleRoot = sampleSubQuery.from(Sample.class);
                final SampleJoins joins = new SampleJoins(sampleRoot);
                final Join cazeJoin = joins.getCaze();
                sampleSubQuery.where(cb.and(cb.equal(cazeJoin, casePath), sampleService.createUserFilterWithoutAssociations(cb, joins)));
                sampleSubQuery.select(sampleRoot.get(Sample.ID));
                filter = CriteriaBuilderHelper.or(cb, filter, cb.exists(sampleSubQuery));
                break;
            default:
        }
        // get all cases based on the user's contact association
        if (userFilterCriteria == null || (!userFilterCriteria.isExcludeCasesFromContacts() && Boolean.TRUE.equals(userFilterCriteria.getIncludeCasesFromOtherJurisdictions()))) {
            ContactQueryContext contactQueryContext = new ContactQueryContext(cb, cq, new ContactJoins(caseQueryContext.getJoins().getContacts()));
            filter = CriteriaBuilderHelper.or(cb, filter, contactService.createUserFilterWithoutCase(contactQueryContext));
        }
        // all users (without specific restrictions) get access to cases that have been made available to the whole country
        if ((userFilterCriteria == null || userFilterCriteria.getIncludeCasesFromOtherJurisdictions()) && !featureConfigurationFacade.isFeatureDisabled(FeatureType.NATIONAL_CASE_SHARING)) {
            filter = CriteriaBuilderHelper.or(cb, filter, cb.isTrue(casePath.get(Case.SHARED_TO_COUNTRY)));
        }
    }
    // only show cases of a specific disease if a limited disease is set
    if (currentUser.getLimitedDisease() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(casePath.get(Case.DISEASE), currentUser.getLimitedDisease()));
    }
    // port health users can only see port health cases
    if (UserRole.isPortHealthUser(currentUser.getUserRoles())) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(casePath.get(Case.CASE_ORIGIN), CaseOrigin.POINT_OF_ENTRY));
    }
    filter = CriteriaBuilderHelper.or(cb, filter, filterResponsible);
    if (featureConfigurationFacade.isPropertyValueTrue(FeatureType.LIMITED_SYNCHRONIZATION, FeatureTypeProperty.EXCLUDE_NO_CASE_CLASSIFIED_CASES) && RequestContextHolder.isMobileSync()) {
        final Predicate limitedCaseSyncPredicate = cb.not(cb.and(cb.equal(casePath.get(Case.CASE_CLASSIFICATION), CaseClassification.NO_CASE), cb.or(cb.notEqual(casePath.get(Case.REPORTING_USER), currentUser), cb.and(cb.equal(casePath.get(Case.REPORTING_USER), currentUser), cb.isNull(casePath.get(Case.CREATION_VERSION))))));
        filter = CriteriaBuilderHelper.and(cb, filter, limitedCaseSyncPredicate);
    }
    return filter;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) User(de.symeda.sormas.backend.user.User) ContactJoins(de.symeda.sormas.backend.contact.ContactJoins) Sample(de.symeda.sormas.backend.sample.Sample) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Join(javax.persistence.criteria.Join) Predicate(javax.persistence.criteria.Predicate) Region(de.symeda.sormas.backend.infrastructure.region.Region) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) PointOfEntry(de.symeda.sormas.backend.infrastructure.pointofentry.PointOfEntry) District(de.symeda.sormas.backend.infrastructure.district.District) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) Community(de.symeda.sormas.backend.infrastructure.community.Community)

Aggregations

ContactQueryContext (de.symeda.sormas.backend.contact.ContactQueryContext)19 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)17 Predicate (javax.persistence.criteria.Predicate)13 CaseQueryContext (de.symeda.sormas.backend.caze.CaseQueryContext)12 Contact (de.symeda.sormas.backend.contact.Contact)10 ContactJoins (de.symeda.sormas.backend.contact.ContactJoins)10 EventParticipantQueryContext (de.symeda.sormas.backend.event.EventParticipantQueryContext)7 TravelEntryQueryContext (de.symeda.sormas.backend.travelentry.TravelEntryQueryContext)6 User (de.symeda.sormas.backend.user.User)6 Case (de.symeda.sormas.backend.caze.Case)5 ArrayList (java.util.ArrayList)5 JurisdictionLevel (de.symeda.sormas.api.user.JurisdictionLevel)4 CaseJoins (de.symeda.sormas.backend.caze.CaseJoins)4 EventParticipant (de.symeda.sormas.backend.event.EventParticipant)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 EventParticipantJoins (de.symeda.sormas.backend.event.EventParticipantJoins)3 EventQueryContext (de.symeda.sormas.backend.event.EventQueryContext)3 ImmunizationJoins (de.symeda.sormas.backend.immunization.ImmunizationJoins)3