Search in sources :

Example 1 with Community

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);
    }
}
Also used : Region(de.symeda.sormas.backend.infrastructure.region.Region) FacilityCriteria(de.symeda.sormas.api.infrastructure.facility.FacilityCriteria) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) PointOfEntry(de.symeda.sormas.backend.infrastructure.pointofentry.PointOfEntry) District(de.symeda.sormas.backend.infrastructure.district.District) Community(de.symeda.sormas.backend.infrastructure.community.Community)

Example 2 with Community

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;
}
Also used : Community(de.symeda.sormas.backend.infrastructure.community.Community)

Example 3 with 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;
}
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 4 with Community

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate) UserRole(de.symeda.sormas.api.user.UserRole) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Community(de.symeda.sormas.backend.infrastructure.community.Community)

Example 5 with Community

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;
}
Also used : User(de.symeda.sormas.backend.user.User) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Community(de.symeda.sormas.backend.infrastructure.community.Community) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Community (de.symeda.sormas.backend.infrastructure.community.Community)34 District (de.symeda.sormas.backend.infrastructure.district.District)28 Region (de.symeda.sormas.backend.infrastructure.region.Region)26 Predicate (javax.persistence.criteria.Predicate)13 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)11 Facility (de.symeda.sormas.backend.infrastructure.facility.Facility)9 User (de.symeda.sormas.backend.user.User)8 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)7 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)6 PointOfEntry (de.symeda.sormas.backend.infrastructure.pointofentry.PointOfEntry)6 SortProperty (de.symeda.sormas.api.utils.SortProperty)5 ArrayList (java.util.ArrayList)5 Order (javax.persistence.criteria.Order)5 JurisdictionLevel (de.symeda.sormas.api.user.JurisdictionLevel)4 Location (de.symeda.sormas.backend.location.Location)4 Join (javax.persistence.criteria.Join)4 FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)3 PointOfEntryReferenceDto (de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryReferenceDto)3 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)3 Case (de.symeda.sormas.backend.caze.Case)3