Search in sources :

Example 1 with SampleJoins

use of de.symeda.sormas.backend.sample.SampleJoins 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 SampleJoins

use of de.symeda.sormas.backend.sample.SampleJoins in project SORMAS-Project by hzi-braunschweig.

the class ContactJurisdictionPredicateValidator method whenLaboratoryLevel.

@Override
protected Predicate whenLaboratoryLevel() {
    final Subquery<Long> sampleContactSubquery = cq.subquery(Long.class);
    final Root<Sample> sampleRoot = sampleContactSubquery.from(Sample.class);
    final SampleJoins sampleJoins = new SampleJoins(sampleRoot);
    final Join contactJoin = sampleJoins.getContact();
    SampleJurisdictionPredicateValidator sampleJurisdictionPredicateValidator = user != null ? SampleJurisdictionPredicateValidator.withoutAssociations(cb, sampleJoins, user) : SampleJurisdictionPredicateValidator.withoutAssociations(cb, sampleJoins, userPath);
    sampleContactSubquery.where(cb.and(cb.equal(contactJoin, joins.getRoot()), sampleJurisdictionPredicateValidator.inJurisdictionOrOwned()));
    sampleContactSubquery.select(sampleRoot.get(Sample.ID));
    return cb.exists(sampleContactSubquery);
}
Also used : Sample(de.symeda.sormas.backend.sample.Sample) Join(javax.persistence.criteria.Join) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) SampleJurisdictionPredicateValidator(de.symeda.sormas.backend.sample.SampleJurisdictionPredicateValidator)

Example 3 with SampleJoins

use of de.symeda.sormas.backend.sample.SampleJoins in project SORMAS-Project by hzi-braunschweig.

the class EventParticipantJurisdictionPredicateValidator method whenLaboratoryLevel.

@Override
protected Predicate whenLaboratoryLevel() {
    final Subquery<Long> sampleSubQuery = cq.subquery(Long.class);
    final Root<Sample> sampleRoot = sampleSubQuery.from(Sample.class);
    final SampleJoins sampleJoins = new SampleJoins(sampleRoot);
    final Join eventParticipant = sampleJoins.getEventParticipant();
    SampleJurisdictionPredicateValidator sampleJurisdictionPredicateValidator = user != null ? SampleJurisdictionPredicateValidator.withoutAssociations(cb, sampleJoins, user) : SampleJurisdictionPredicateValidator.withoutAssociations(cb, sampleJoins, userPath);
    sampleSubQuery.where(cb.and(cb.equal(eventParticipant, joins.getRoot()), sampleJurisdictionPredicateValidator.inJurisdictionOrOwned()));
    sampleSubQuery.select(sampleRoot.get(Sample.ID));
    return cb.exists(sampleSubQuery);
}
Also used : Sample(de.symeda.sormas.backend.sample.Sample) Join(javax.persistence.criteria.Join) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) SampleJurisdictionPredicateValidator(de.symeda.sormas.backend.sample.SampleJurisdictionPredicateValidator)

Example 4 with SampleJoins

use of de.symeda.sormas.backend.sample.SampleJoins 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)

Example 5 with SampleJoins

use of de.symeda.sormas.backend.sample.SampleJoins in project SORMAS-Project by hzi-braunschweig.

the class EventService method createUserFilter.

public Predicate createUserFilter(final EventQueryContext queryContext, final EventUserFilterCriteria eventUserFilterCriteria) {
    User currentUser = getCurrentUser();
    if (currentUser == null) {
        return null;
    }
    final JurisdictionLevel jurisdictionLevel = currentUser.getJurisdictionLevel();
    Predicate filter = null;
    @SuppressWarnings("rawtypes") final CriteriaQuery cq = queryContext.getQuery();
    final CriteriaBuilder cb = queryContext.getCriteriaBuilder();
    final EventJoins eventJoins = queryContext.getJoins();
    final From<?, Event> eventJoin = queryContext.getRoot();
    if (jurisdictionLevel != JurisdictionLevel.NATION && !currentUser.hasUserRole(UserRole.REST_USER)) {
        switch(jurisdictionLevel) {
            case REGION:
                if (currentUser.getRegion() != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(eventJoins.getLocation().get(Location.REGION), currentUser.getRegion()));
                }
                break;
            case DISTRICT:
                if (currentUser.getDistrict() != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(eventJoins.getLocation().get(Location.DISTRICT), currentUser.getDistrict()));
                }
                break;
            case COMMUNITY:
                if (currentUser.getCommunity() != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(eventJoins.getLocation().get(Location.COMMUNITY), currentUser.getCommunity()));
                }
                break;
            case HEALTH_FACILITY:
                if (currentUser.getHealthFacility() != null && currentUser.getHealthFacility().getDistrict() != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(eventJoins.getLocation().get(Location.DISTRICT), currentUser.getHealthFacility().getDistrict()));
                }
                break;
            case LABORATORY:
                final Subquery<Long> sampleSubQuery = cq.subquery(Long.class);
                final Root<Sample> sampleRoot = sampleSubQuery.from(Sample.class);
                final SampleJoins sampleJoins = new SampleJoins(sampleRoot);
                final Join eventParticipant = sampleJoins.getEventParticipant();
                final From<?, EventParticipant> eventParticipantJoin = eventJoins.getEventParticipants();
                SampleJurisdictionPredicateValidator sampleJurisdictionPredicateValidator = SampleJurisdictionPredicateValidator.withoutAssociations(cb, sampleJoins, currentUser);
                sampleSubQuery.where(cb.and(cb.equal(eventParticipant, eventParticipantJoin), sampleJurisdictionPredicateValidator.inJurisdictionOrOwned()));
                sampleSubQuery.select(sampleRoot.get(Sample.ID));
                filter = CriteriaBuilderHelper.or(cb, cb.exists(sampleSubQuery));
                break;
            default:
        }
        Predicate filterResponsible = cb.equal(eventJoins.getRoot().get(Event.REPORTING_USER), currentUser);
        filterResponsible = cb.or(filterResponsible, cb.equal(eventJoins.getRoot().get(Event.RESPONSIBLE_USER), currentUser));
        if (eventUserFilterCriteria != null && eventUserFilterCriteria.isIncludeUserCaseAndEventParticipantFilter()) {
            filter = CriteriaBuilderHelper.or(cb, filter, createCaseAndEventParticipantFilter(queryContext));
        }
        if (eventUserFilterCriteria != null && eventUserFilterCriteria.isForceRegionJurisdiction()) {
            filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(eventJoins.getLocation().get(Location.REGION), currentUser.getRegion()));
        }
        if (filter != null) {
            filter = CriteriaBuilderHelper.or(cb, filter, filterResponsible);
        } else {
            filter = filterResponsible;
        }
    }
    if (currentUser.getLimitedDisease() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.or(cb.equal(eventJoin.get(Event.DISEASE), currentUser.getLimitedDisease()), cb.isNull(eventJoin.get(Event.DISEASE))));
    }
    return filter;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) User(de.symeda.sormas.backend.user.User) Sample(de.symeda.sormas.backend.sample.Sample) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Join(javax.persistence.criteria.Join) Predicate(javax.persistence.criteria.Predicate) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) SampleJurisdictionPredicateValidator(de.symeda.sormas.backend.sample.SampleJurisdictionPredicateValidator)

Aggregations

Sample (de.symeda.sormas.backend.sample.Sample)8 SampleJoins (de.symeda.sormas.backend.sample.SampleJoins)8 Join (javax.persistence.criteria.Join)8 JurisdictionLevel (de.symeda.sormas.api.user.JurisdictionLevel)5 SampleJurisdictionPredicateValidator (de.symeda.sormas.backend.sample.SampleJurisdictionPredicateValidator)5 User (de.symeda.sormas.backend.user.User)5 Predicate (javax.persistence.criteria.Predicate)5 Community (de.symeda.sormas.backend.infrastructure.community.Community)3 District (de.symeda.sormas.backend.infrastructure.district.District)3 Region (de.symeda.sormas.backend.infrastructure.region.Region)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 ContactQueryContext (de.symeda.sormas.backend.contact.ContactQueryContext)2 Facility (de.symeda.sormas.backend.infrastructure.facility.Facility)2 PointOfEntry (de.symeda.sormas.backend.infrastructure.pointofentry.PointOfEntry)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 ContactJoins (de.symeda.sormas.backend.contact.ContactJoins)1 EventJoins (de.symeda.sormas.utils.EventJoins)1 From (javax.persistence.criteria.From)1