use of de.symeda.sormas.backend.infrastructure.community.Community in project SORMAS-Project by hzi-braunschweig.
the class StartupShutdownService method createDefaultInfrastructureData.
private void createDefaultInfrastructureData() {
if (!configFacade.isCreateDefaultEntities()) {
// return if isCreateDefaultEntities() is false
logger.info("Skipping the creation of default infrastructure data");
return;
}
// Region
Region region = null;
if (regionService.count() == 0) {
region = defaultEntitiesCreator.createDefaultRegion(false);
regionService.ensurePersisted(region);
}
// District
District district = null;
if (districtService.count() == 0) {
if (region == null) {
region = regionService.getAll().get(0);
}
district = defaultEntitiesCreator.createDefaultDistrict(region, false);
districtService.ensurePersisted(district);
region.getDistricts().add(district);
}
// Community
Community community = null;
if (communityService.count() == 0) {
if (district == null) {
district = districtService.getAll().get(0);
}
community = defaultEntitiesCreator.createDefaultCommunity(district, false);
communityService.ensurePersisted(community);
district.getCommunities().add(community);
}
// Facility
Facility facility;
FacilityCriteria facilityCriteria = new FacilityCriteria();
if (facilityFacade.count(facilityCriteria) == 0) {
if (community == null) {
community = communityService.getAll().get(0);
}
if (district == null) {
district = districtService.getAll().get(0);
}
if (region == null) {
region = regionService.getAll().get(0);
}
facility = defaultEntitiesCreator.createDefaultFacility(region, district, community);
facilityService.ensurePersisted(facility);
}
// Laboratory
Facility laboratory;
facilityCriteria.type(FacilityType.LABORATORY);
if (facilityFacade.count(facilityCriteria) == 0) {
if (community == null) {
community = communityService.getAll().get(0);
}
if (district == null) {
district = districtService.getAll().get(0);
}
if (region == null) {
region = regionService.getAll().get(0);
}
laboratory = defaultEntitiesCreator.createDefaultLaboratory(region, district, community);
facilityService.ensurePersisted(laboratory);
}
// Point of Entry
PointOfEntry pointOfEntry;
if (pointOfEntryService.count() == 0) {
if (district == null) {
district = districtService.getAll().get(0);
}
if (region == null) {
region = regionService.getAll().get(0);
}
pointOfEntry = defaultEntitiesCreator.createDefaultPointOfEntry(region, district);
pointOfEntryService.ensurePersisted(pointOfEntry);
}
}
use of de.symeda.sormas.backend.infrastructure.community.Community in project SORMAS-Project by hzi-braunschweig.
the class DefaultEntitiesCreator method createDefaultCommunity.
public Community createDefaultCommunity(District district, boolean randomUuid) {
Community community = new Community();
community.setUuid(createUuid(randomUuid, DefaultEntityHelper.DefaultInfrastructureUuidSeed.COMMUNITY));
community.setName(I18nProperties.getCaption(Captions.defaultCommunity, "Default Community"));
community.setDistrict(district);
return community;
}
use of de.symeda.sormas.backend.infrastructure.community.Community 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.infrastructure.community.Community in project SORMAS-Project by hzi-braunschweig.
the class UserService method getReferenceList.
/**
* Loads users filtered by combinable filter conditions.<br />
* Condition combination if parameter is set:<br />
* {@code ((regionUuids & districtUuids & communityUuids & filterByJurisdiction & userRoles) | includeSupervisors) & activeOnly}
*
* @see #createJurisdictionFilter(CriteriaBuilder, From)
* @param regionUuids
* @param districtUuids
* @param communityUuids
* @param includeSupervisors
* If set to {@code true}, all supervisors are returned independent of other filters.
* @param filterByJurisdiction
* @param activeOnly
* @param userRoles
*/
public List<UserReference> getReferenceList(List<String> regionUuids, List<String> districtUuids, List<String> communityUuids, boolean includeSupervisors, boolean filterByJurisdiction, boolean activeOnly, List<UserRole> userRoles) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserReference> cq = cb.createQuery(UserReference.class);
Root<UserReference> root = cq.from(UserReference.class);
Join<UserReference, UserRole> rolesJoin = root.join(User.USER_ROLES, JoinType.LEFT);
Root<User> userRoot = cq.from(User.class);
cq.select(root);
// WHERE inner AND
Predicate filter = null;
boolean userEntityJoinUsed = false;
if (CollectionUtils.isNotEmpty(regionUuids)) {
Join<User, Region> regionJoin = userRoot.join(User.REGION, JoinType.LEFT);
filter = CriteriaBuilderHelper.and(cb, filter, cb.in(regionJoin.get(AbstractDomainObject.UUID)).value(regionUuids));
userEntityJoinUsed = true;
}
if (CollectionUtils.isNotEmpty(districtUuids)) {
Join<User, District> districtJoin = userRoot.join(User.DISTRICT, JoinType.LEFT);
filter = CriteriaBuilderHelper.and(cb, filter, cb.in(districtJoin.get(AbstractDomainObject.UUID)).value(districtUuids));
userEntityJoinUsed = true;
}
if (filterByJurisdiction) {
filter = CriteriaBuilderHelper.and(cb, filter, createJurisdictionFilter(cb, userRoot));
userEntityJoinUsed = true;
}
if (CollectionUtils.isNotEmpty(userRoles)) {
filter = CriteriaBuilderHelper.and(cb, filter, rolesJoin.in(userRoles));
}
if (userEntityJoinUsed) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(root.get(UserReference.ID), userRoot.get(AbstractDomainObject.ID)));
}
// WHERE OR
if (includeSupervisors) {
Predicate supervisorFilter = rolesJoin.in(Arrays.asList(UserRole.CASE_SUPERVISOR, UserRole.CONTACT_SUPERVISOR, UserRole.SURVEILLANCE_SUPERVISOR, UserRole.ADMIN_SUPERVISOR));
filter = CriteriaBuilderHelper.or(cb, filter, supervisorFilter);
}
// WHERE outer AND
if (activeOnly) {
filter = CriteriaBuilderHelper.and(cb, filter, createDefaultFilter(cb, root));
}
if (CollectionUtils.isNotEmpty(communityUuids)) {
Join<User, Community> communityJoin = userRoot.join(User.COMMUNITY, JoinType.LEFT);
filter = CriteriaBuilderHelper.and(cb, filter, cb.in(communityJoin.get(AbstractDomainObject.UUID)).value(communityUuids));
}
if (filter != null) {
cq.where(filter);
}
cq.distinct(true);
cq.orderBy(cb.asc(root.get(AbstractDomainObject.ID)));
return em.createQuery(cq).setHint(ModelConstants.HINT_HIBERNATE_READ_ONLY, true).getResultList();
}
use of de.symeda.sormas.backend.infrastructure.community.Community in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormDataService method createUserFilter.
@Override
public Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, CampaignFormData> campaignPath) {
final User currentUser = getCurrentUser();
if (currentUser == null) {
return null;
}
Predicate filter = null;
final JurisdictionLevel jurisdictionLevel = currentUser.getCalculatedJurisdictionLevel();
if (jurisdictionLevel != JurisdictionLevel.NATION) {
switch(jurisdictionLevel) {
case REGION:
final Region region = currentUser.getRegion();
if (region != null) {
filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(campaignPath.get(CampaignFormData.REGION).get(Region.ID), region.getId()));
}
break;
case DISTRICT:
final District district = currentUser.getDistrict();
if (district != null) {
filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(campaignPath.get(CampaignFormData.DISTRICT).get(District.ID), district.getId()));
}
break;
case COMMUNITY:
final Community community = currentUser.getCommunity();
if (community != null) {
filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(campaignPath.get(CampaignFormData.COMMUNITY).get(Community.ID), community.getId()));
}
break;
default:
return null;
}
}
return filter;
}
Aggregations