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;
}
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);
}
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);
}
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;
}
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;
}
Aggregations