Skip to content

Commit

Permalink
#183 render region district filter based on jurisdiction
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony4m committed Jan 9, 2025
1 parent 34e2b59 commit b9b9921
Showing 1 changed file with 122 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,37 @@

package de.symeda.sormas.backend.ebs;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import de.symeda.sormas.api.Disease;
import de.symeda.sormas.api.EditPermissionType;
import de.symeda.sormas.api.common.DeletionDetails;
import de.symeda.sormas.api.document.DocumentRelatedEntityType;
import de.symeda.sormas.api.ebs.*;
import de.symeda.sormas.api.ebs.EbsCriteria;
import de.symeda.sormas.api.externaldata.ExternalDataDto;
import de.symeda.sormas.api.externaldata.ExternalDataUpdateException;
import de.symeda.sormas.api.externalsurveillancetool.ExternalSurveillanceToolException;
Expand All @@ -28,7 +54,12 @@
import de.symeda.sormas.api.user.JurisdictionLevel;
import de.symeda.sormas.api.utils.DateHelper;
import de.symeda.sormas.backend.caze.CaseService;
import de.symeda.sormas.backend.common.*;
import de.symeda.sormas.backend.common.AbstractCoreAdoService;
import de.symeda.sormas.backend.common.AbstractDomainObject;
import de.symeda.sormas.backend.common.ChangeDateBuilder;
import de.symeda.sormas.backend.common.ChangeDateFilterBuilder;
import de.symeda.sormas.backend.common.CriteriaBuilderHelper;
import de.symeda.sormas.backend.common.DeletableAdo;
import de.symeda.sormas.backend.document.DocumentService;
import de.symeda.sormas.backend.externalsurveillancetool.ExternalSurveillanceToolGatewayFacadeEjb;
import de.symeda.sormas.backend.infrastructure.community.Community;
Expand All @@ -43,18 +74,9 @@
import de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfoService;
import de.symeda.sormas.backend.user.User;
import de.symeda.sormas.backend.user.UserService;
import de.symeda.sormas.backend.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;
import de.symeda.sormas.backend.util.ExternalDataUtil;
import de.symeda.sormas.backend.util.JurisdictionHelper;
import de.symeda.sormas.backend.util.QueryHelper;

@Stateless
@LocalBean
Expand Down Expand Up @@ -100,7 +122,6 @@ protected Predicate createRelevantDataFilter(CriteriaBuilder cb, CriteriaQuery c
return filter;
}


protected List<String> referencesToBeFetched() {
return Arrays.asList(Ebs.EBS_LOCATION);
}
Expand Down Expand Up @@ -157,7 +178,6 @@ public Map<Disease, Long> getEbsCountByDisease(EbsCriteria ebsCriteria) {
return results.stream().collect(Collectors.toMap(e -> (Disease) e[0], e -> (Long) e[1]));
}


@Override
public void archive(String entityUuid, Date endOfProcessingDate) {
super.archive(entityUuid, endOfProcessingDate);
Expand Down Expand Up @@ -192,8 +212,7 @@ public void setArchiveInExternalSurveillanceToolForEntities(List<String> entityU
public List<String> getSharedEbsUuids(List<String> entityUuids) {
List<Long> ebsIds = getEbsIds(entityUuids);
List<String> sharedEbsUuids = new ArrayList<>();
List<ExternalShareInfoCountAndLatestDate> ebsShareInfos =
externalShareInfoService.getShareCountAndLatestDate(ebsIds, ExternalShareInfo.EBS);
List<ExternalShareInfoCountAndLatestDate> ebsShareInfos = externalShareInfoService.getShareCountAndLatestDate(ebsIds, ExternalShareInfo.EBS);
ebsShareInfos.forEach(shareInfo -> {
if (shareInfo.getLatestStatus() != ExternalShareStatus.DELETED) {
sharedEbsUuids.add(shareInfo.getAssociatedObjectUuid());
Expand Down Expand Up @@ -286,7 +305,6 @@ protected Predicate createUserFilterInternal(CriteriaBuilder cb, CriteriaQuery c
return createUserFilter(new EbsQueryContext(cb, cq, from));
}


protected EbsJoins toJoins(From<?, Ebs> adoPath) {
return new EbsJoins(adoPath);
}
Expand All @@ -310,6 +328,19 @@ public Predicate createUserFilter(final EbsQueryContext queryContext, final EbsU
final EbsJoins ebsJoins = queryContext.getJoins();
final From<?, Ebs> ebsJoin = queryContext.getRoot();

if (jurisdictionLevel != JurisdictionLevel.NATION) {
if (currentUser.getRegion() != null || currentUser.getDistrict() != null || currentUser.getCommunity() != null) {

// Apply region filter if exists
if (currentUser.getRegion() != null) {
Predicate regionPredicate = cb.equal(ebsJoins.getLocation().get(Location.REGION).get(Region.ID), currentUser.getRegion().getId());
Predicate districtPredicate =
cb.equal(ebsJoins.getLocation().get(Location.DISTRICT).get(District.ID), currentUser.getDistrict().getId());
filter = cb.and(regionPredicate, districtPredicate);
}
}
}

// if (RequestContextHolder.isMobileSync()) {
// Predicate limitedChangeDatePredicate = CriteriaBuilderHelper.and(cb, createLimitedChangeDateFilter(cb, ebsJoin));
// if (limitedChangeDatePredicate != null) {
Expand All @@ -335,8 +366,7 @@ protected <T extends ChangeDateBuilder<T>> T addChangeDates(T builder, EbsJoins
builder = super.addChangeDates(builder, ebsFrom, includeExtendedChangeDateFilters).add(ebsFrom, Ebs.EBS_LOCATION);

if (includeExtendedChangeDateFilters) {
builder.add(ebsFrom, Ebs.SORMAS_TO_SORMAS_ORIGIN_INFO)
.add(ebsFrom, Ebs.SORMAS_TO_SORMAS_SHARES);
builder.add(ebsFrom, Ebs.SORMAS_TO_SORMAS_ORIGIN_INFO).add(ebsFrom, Ebs.SORMAS_TO_SORMAS_SHARES);
}

return builder;
Expand All @@ -362,7 +392,6 @@ public void deletePermanent(Ebs ebs) {
super.deletePermanent(ebs);
}


private void deleteEbsInExternalSurveillanceTool(Ebs ebs) {
try {
ebsFacade.deleteEbsInExternalSurveillanceTool(ebs);
Expand All @@ -386,36 +415,61 @@ public Predicate buildCriteriaFilter(EbsCriteria ebsCriteria, EbsQueryContext eb
CriteriaBuilder cb = ebsQueryContext.getCriteriaBuilder();
From<?, Ebs> from = ebsQueryContext.getRoot();
final EbsJoins joins = ebsQueryContext.getJoins();

User currentUser = getCurrentUser();
if (currentUser == null) {
return null;
}
final JurisdictionLevel jurisdictionLevel = currentUser.getJurisdictionLevel();
Predicate filter = null;
if (jurisdictionLevel != JurisdictionLevel.NATION) {
if (currentUser.getRegion() != null) {
Predicate regionPredicate = cb.equal(joins.getLocation().get(Location.REGION).get(Region.ID), currentUser.getRegion().getId());
filter = regionPredicate;

if (currentUser.getDistrict() != null) {
Predicate districtPredicate =
cb.equal(joins.getLocation().get(Location.DISTRICT).get(District.ID), currentUser.getDistrict().getId());
filter = cb.and(filter, districtPredicate);
}

if (currentUser.getCommunity() != null) {
Predicate communityPredicate =
cb.equal(joins.getLocation().get(Location.COMMUNITY).get(Community.ID), currentUser.getCommunity().getId());
filter = cb.and(filter, communityPredicate);
}
}
}

if (ebsCriteria.getRiskAssessment() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getRiskAssessment().get(RiskAssessment.RISK_ASSESSMENT), ebsCriteria.getRiskAssessment()));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getRiskAssessment().get(RiskAssessment.RISK_ASSESSMENT), ebsCriteria.getRiskAssessment()));
}
if (ebsCriteria.getReportDateTime() != null) {
filter = CriteriaBuilderHelper.and(
cb, filter,
cb.between(
from.get(Ebs.REPORT_DATE_TIME),
DateHelper.getStartOfDay(ebsCriteria.getReportDateTime()),
DateHelper.getEndOfDay(ebsCriteria.getReportDateTime())
)
);
cb,
filter,
cb.between(
from.get(Ebs.REPORT_DATE_TIME),
DateHelper.getStartOfDay(ebsCriteria.getReportDateTime()),
DateHelper.getEndOfDay(ebsCriteria.getReportDateTime())));
}
if (ebsCriteria.getTriagingDto() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getTriaging().get(Ebs.TRIAGING), ebsCriteria.getTriagingDto()));
}
if (ebsCriteria.getSignalVerificationDto() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getSignalVerification().get(Ebs.SIGNAL_VERIFICATION), ebsCriteria.getSignalVerificationDto()));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getSignalVerification().get(Ebs.SIGNAL_VERIFICATION), ebsCriteria.getSignalVerificationDto()));
}
if (ebsCriteria.getSourceInformation() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Ebs.SOURCE_INFORMATION), ebsCriteria.getSourceInformation()));
}
if (ebsCriteria.getTriagingDecision() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getTriaging().get(Triaging.TRIAGING_DECISION), ebsCriteria.getTriagingDecision()));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getTriaging().get(Triaging.TRIAGING_DECISION), ebsCriteria.getTriagingDecision()));
}
if (ebsCriteria.getTriageDate() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getTriaging().get(Triaging.DATE_OF_DECISION),DateHelper.getStartOfDay( ebsCriteria.getTriageDate())));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getTriaging().get(Triaging.DATE_OF_DECISION), DateHelper.getStartOfDay(ebsCriteria.getTriageDate())));
}
if (ebsCriteria.getCategoryOfInformant() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Ebs.CATEGORY_OF_INFORMANT), ebsCriteria.getCategoryOfInformant()));
Expand All @@ -427,13 +481,16 @@ public Predicate buildCriteriaFilter(EbsCriteria ebsCriteria, EbsQueryContext eb
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Ebs.INFORMANT_TEL), ebsCriteria.getInformantTel()));
}
if (ebsCriteria.getSignalCategory() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getTriaging().get(Triaging.SIGNAL_CATEGORY), ebsCriteria.getSignalCategory()));
filter =
CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getTriaging().get(Triaging.SIGNAL_CATEGORY), ebsCriteria.getSignalCategory()));
}
if (ebsCriteria.getVerified() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getSignalVerification().get(SignalVerification.VERIFIED), ebsCriteria.getVerified()));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getSignalVerification().get(SignalVerification.VERIFIED), ebsCriteria.getVerified()));
}
if (ebsCriteria.getDeath() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getSignalVerification().get(SignalVerification.NUMBER_OF_DEATH), ebsCriteria.getDeath()));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getSignalVerification().get(SignalVerification.NUMBER_OF_DEATH), ebsCriteria.getDeath()));
}
if (ebsCriteria.getPersonRegistering() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Ebs.PERSON_REGISTERING), ebsCriteria.getPersonRegistering()));
Expand All @@ -442,19 +499,28 @@ public Predicate buildCriteriaFilter(EbsCriteria ebsCriteria, EbsQueryContext eb
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Ebs.PERSON_DESIGNATION), ebsCriteria.getPersonDesignation()));
}
if (ebsCriteria.getVerificationSent() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getSignalVerification().get(SignalVerification.VERIFICATION_SENT), ebsCriteria.getVerificationSent()));
filter = CriteriaBuilderHelper.and(
cb,
filter,
cb.equal(joins.getSignalVerification().get(SignalVerification.VERIFICATION_SENT), ebsCriteria.getVerificationSent()));
}
if (ebsCriteria.getVerifiedDate() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getSignalVerification().get(SignalVerification.VERIFICATION_COMPLETE_DATE), ebsCriteria.getVerifiedDate()));
filter = CriteriaBuilderHelper.and(
cb,
filter,
cb.equal(joins.getSignalVerification().get(SignalVerification.VERIFICATION_COMPLETE_DATE), ebsCriteria.getVerifiedDate()));
}
if (ebsCriteria.getRiskAssessment() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getRiskAssessment().get(RiskAssessment.RISK_ASSESSMENT), ebsCriteria.getRiskAssessment()));
filter = CriteriaBuilderHelper
.and(cb, filter, cb.equal(joins.getRiskAssessment().get(RiskAssessment.RISK_ASSESSMENT), ebsCriteria.getRiskAssessment()));
}
if (ebsCriteria.getActionInitiated() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getEbsAlert().get(EbsAlert.ACTION_INITIATED), ebsCriteria.getActionInitiated()));
filter =
CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getEbsAlert().get(EbsAlert.ACTION_INITIATED), ebsCriteria.getActionInitiated()));
}
if (ebsCriteria.getResponseStatus() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getEbsAlert().get(EbsAlert.RESPONSE_STATUS), ebsCriteria.getResponseStatus()));
filter =
CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getEbsAlert().get(EbsAlert.RESPONSE_STATUS), ebsCriteria.getResponseStatus()));
}
if (ebsCriteria.getRegion() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getRegion().get(Region.UUID), ebsCriteria.getRegion().getUuid()));
Expand All @@ -470,22 +536,19 @@ public Predicate buildCriteriaFilter(EbsCriteria ebsCriteria, EbsQueryContext eb
}

filter = CriteriaBuilderHelper.and(
cb,
filter,
externalShareInfoService.buildShareCriteriaFilter(
ebsCriteria,
ebsQueryContext.getQuery(),
cb,
filter,
externalShareInfoService.buildShareCriteriaFilter(
ebsCriteria,
ebsQueryContext.getQuery(),
cb,
from,
ExternalShareInfo.EBS,
(latestShareDate) -> createChangeDateFilter(cb, joins, latestShareDate)
)
);
from,
ExternalShareInfo.EBS,
(latestShareDate) -> createChangeDateFilter(cb, joins, latestShareDate)));

return filter;
}


/**
* Creates a filter that excludes all ebss that are either {@link Ebs#isArchived()} or {@link DeletableAdo#isDeleted()}.
*/
Expand All @@ -508,18 +571,19 @@ public Predicate createDefaultFilter(CriteriaBuilder cb, From<?, Ebs> root) {
return cb.isFalse(root.get(Ebs.DELETED));
}


public Predicate inJurisdictionOrOwned(CriteriaBuilder cb, CriteriaQuery<?> cq, From<?, Ebs> from) {
return inJurisdictionOrOwned(new EbsQueryContext(cb, cq, from));
}

public Predicate inJurisdictionOrOwned(EbsQueryContext qc) {
return inJurisdictionOrOwned(qc, getCurrentUser());
}

public boolean inJurisdictionOrOwned(Ebs event) {
return inJurisdictionOrOwned(event, getCurrentUser());
}
public Predicate inJurisdictionOrOwned(EbsQueryContext qc, User user) {

public Predicate inJurisdictionOrOwned(EbsQueryContext qc, User user) {
return EbsJurisdictionPredicateValidator.of(qc, user).inJurisdictionOrOwned();
}

Expand All @@ -537,7 +601,8 @@ public boolean inJurisdiction(Ebs ebs) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Boolean> cq = cb.createQuery(Boolean.class);
Root<Ebs> root = cq.from(Ebs.class);
cq.multiselect(JurisdictionHelper.booleanSelector(cb, inJurisdictionOrOwned(new EbsQueryContext(cb, cq, root), userService.getCurrentUser())));
cq.multiselect(
JurisdictionHelper.booleanSelector(cb, inJurisdictionOrOwned(new EbsQueryContext(cb, cq, root), userService.getCurrentUser())));
cq.where(cb.equal(root.get(Ebs.UUID), ebs.getUuid()));
return em.createQuery(cq).getResultList().stream().anyMatch(aBoolean -> aBoolean);
}
Expand Down Expand Up @@ -600,6 +665,7 @@ public String getUuidByCaseUuidOrPersonUuid(String searchTerm) {

return QueryHelper.getFirstResult(em, cq);
}

public boolean hasRegionAndDistrict(String ebsUuid) {

CriteriaBuilder cb = em.getCriteriaBuilder();
Expand Down

0 comments on commit b9b9921

Please sign in to comment.