Search in sources :

Example 1 with Facility

use of de.symeda.sormas.backend.infrastructure.facility.Facility 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 Facility

use of de.symeda.sormas.backend.infrastructure.facility.Facility in project SORMAS-Project by hzi-braunschweig.

the class DefaultEntitiesCreator method createDefaultLaboratory.

public Facility createDefaultLaboratory(Region region, District district, Community community) {
    Facility laboratory = new Facility();
    laboratory.setUuid(DataHelper.createUuid());
    laboratory.setName(I18nProperties.getCaption(Captions.defaultLaboratory, "Default Laboratory"));
    laboratory.setCommunity(community);
    laboratory.setDistrict(district);
    laboratory.setRegion(region);
    laboratory.setType(FacilityType.LABORATORY);
    return laboratory;
}
Also used : Facility(de.symeda.sormas.backend.infrastructure.facility.Facility)

Example 3 with Facility

use of de.symeda.sormas.backend.infrastructure.facility.Facility in project SORMAS-Project by hzi-braunschweig.

the class DefaultEntitiesCreator method createDefaultFacility.

public Facility createDefaultFacility(Region region, District district, Community community) {
    Facility facility = new Facility();
    facility.setUuid(DataHelper.createUuid());
    facility.setType(FacilityType.HOSPITAL);
    facility.setName(I18nProperties.getCaption(Captions.defaultFacility, "Default Health Facility"));
    facility.setCommunity(community);
    facility.setDistrict(district);
    facility.setRegion(region);
    return facility;
}
Also used : Facility(de.symeda.sormas.backend.infrastructure.facility.Facility)

Example 4 with Facility

use of de.symeda.sormas.backend.infrastructure.facility.Facility in project SORMAS-Project by hzi-braunschweig.

the class CaseFacadeEjb method onCaseChanged.

public void onCaseChanged(CaseDataDto existingCase, Case newCase, boolean syncShares) {
    // If its a new case and the case is new and the geo coordinates of the case's
    // health facility are null, set its coordinates to the case's report
    // coordinates, if available. Else if case report coordinates are null set them
    // to the facility's coordinates
    Facility facility = newCase.getHealthFacility();
    if (existingCase == null && facility != null && !FacilityHelper.isOtherOrNoneHealthFacility(facility.getUuid())) {
        if ((facility.getLatitude() == null || facility.getLongitude() == null) && newCase.getReportLat() != null && newCase.getReportLon() != null) {
            facility.setLatitude(newCase.getReportLat());
            facility.setLongitude(newCase.getReportLon());
            facilityService.ensurePersisted(facility);
        } else if (newCase.getReportLat() == null && newCase.getReportLon() == null && newCase.getReportLatLonAccuracy() == null) {
            newCase.setReportLat(facility.getLatitude());
            newCase.setReportLon(facility.getLongitude());
        }
    }
    // Clear facility type if no facility or home was selected
    if (newCase.getHealthFacility() == null || FacilityDto.NONE_FACILITY_UUID.equals(newCase.getHealthFacility().getUuid())) {
        newCase.setFacilityType(null);
    }
    // Generate epid number if missing or incomplete
    FieldVisibilityCheckers fieldVisibilityCheckers = FieldVisibilityCheckers.withCountry(configFacade.getCountryLocale());
    if (fieldVisibilityCheckers.isVisible(CaseDataDto.class, CaseDataDto.EPID_NUMBER) && !CaseLogic.isCompleteEpidNumber(newCase.getEpidNumber())) {
        newCase.setEpidNumber(generateEpidNumber(newCase.getEpidNumber(), newCase.getUuid(), newCase.getDisease(), newCase.getReportDate(), newCase.getResponsibleDistrict().getUuid()));
    }
    // update the plague type based on symptoms
    if (newCase.getDisease() == Disease.PLAGUE) {
        PlagueType plagueType = DiseaseHelper.getPlagueTypeForSymptoms(SymptomsFacadeEjb.toDto(newCase.getSymptoms()));
        if (plagueType != newCase.getPlagueType() && plagueType != null) {
            newCase.setPlagueType(plagueType);
        }
    }
    District survOffDistrict = newCase.getSurveillanceOfficer() != null ? newCase.getSurveillanceOfficer().getDistrict() : null;
    if (survOffDistrict == null || (!survOffDistrict.equals(newCase.getResponsibleDistrict()) && !survOffDistrict.equals(newCase.getDistrict()))) {
        setResponsibleSurveillanceOfficer(newCase);
    }
    updateInvestigationByStatus(existingCase, newCase);
    updatePersonAndCaseByOutcome(existingCase, newCase);
    updateCaseAge(existingCase, newCase);
    // Change the disease of all contacts if the case disease or disease details have changed
    if (existingCase != null && (newCase.getDisease() != existingCase.getDisease() || !StringUtils.equals(newCase.getDiseaseDetails(), existingCase.getDiseaseDetails()))) {
        for (Contact contact : contactService.findBy(new ContactCriteria().caze(newCase.toReference()), null)) {
            if (contact.getDisease() != newCase.getDisease() || !StringUtils.equals(contact.getDiseaseDetails(), newCase.getDiseaseDetails())) {
                // Only do the change if it hasn't been done in the mobile app before
                contact.setDisease(newCase.getDisease());
                contact.setDiseaseDetails(newCase.getDiseaseDetails());
                contactService.ensurePersisted(contact);
            }
        }
    }
    if (existingCase != null && (newCase.getDisease() != existingCase.getDisease() || !Objects.equals(newCase.getReportDate(), existingCase.getReportDate()) || !Objects.equals(newCase.getSymptoms().getOnsetDate(), existingCase.getSymptoms().getOnsetDate()))) {
        // Update follow-up until and status of all contacts
        for (Contact contact : contactService.findBy(new ContactCriteria().caze(newCase.toReference()), null)) {
            contactService.updateFollowUpDetails(contact, false);
            contactService.udpateContactStatus(contact);
        }
        for (Contact contact : contactService.getAllByResultingCase(newCase)) {
            contactService.updateFollowUpDetails(contact, false);
            contactService.udpateContactStatus(contact);
        }
    }
    updateTasksOnCaseChanged(newCase, existingCase);
    // Update case classification if the feature is enabled
    CaseClassification classification = null;
    if (configFacade.isFeatureAutomaticCaseClassification()) {
        if (newCase.getCaseClassification() != CaseClassification.NO_CASE) {
            // calculate classification
            CaseDataDto newCaseDto = toDto(newCase);
            classification = caseClassificationFacade.getClassification(newCaseDto);
            // only update when classification by system changes - user may overwrite this
            if (classification != newCase.getSystemCaseClassification()) {
                newCase.setSystemCaseClassification(classification);
                // really a change? (user may have already set it)
                if (classification != newCase.getCaseClassification()) {
                    newCase.setCaseClassification(classification);
                    newCase.setClassificationUser(null);
                    newCase.setClassificationDate(new Date());
                }
            }
        }
    }
    // calculate reference definition for cases
    if (configFacade.isConfiguredCountry(CountryHelper.COUNTRY_CODE_GERMANY)) {
        boolean fulfilled = evaluateFulfilledCondition(toDto(newCase), classification);
        newCase.setCaseReferenceDefinition(fulfilled ? CaseReferenceDefinition.FULFILLED : CaseReferenceDefinition.NOT_FULFILLED);
    }
    // are not empty
    if (!newCase.getHospitalization().getPreviousHospitalizations().isEmpty() && YesNoUnknown.YES != newCase.getHospitalization().getHospitalizedPreviously()) {
        newCase.getHospitalization().setHospitalizedPreviously(YesNoUnknown.YES);
    }
    if (!newCase.getEpiData().getExposures().isEmpty() && !YesNoUnknown.YES.equals(newCase.getEpiData().getExposureDetailsKnown())) {
        newCase.getEpiData().setExposureDetailsKnown(YesNoUnknown.YES);
    }
    // Update completeness value
    newCase.setCompleteness(null);
    // changed
    if (existingCase != null && existingCase.getCaseClassification() != newCase.getCaseClassification()) {
        try {
            String message = String.format(I18nProperties.getString(MessageContents.CONTENT_CASE_CLASSIFICATION_CHANGED), DataHelper.getShortUuid(newCase.getUuid()), newCase.getCaseClassification().toString());
            notificationService.sendNotifications(NotificationType.CASE_CLASSIFICATION_CHANGED, JurisdictionHelper.getCaseRegions(newCase), null, MessageSubject.CASE_CLASSIFICATION_CHANGED, message);
        } catch (NotificationDeliveryFailedException e) {
            logger.error("NotificationDeliveryFailedException when trying to notify supervisors about the change of a case classification. ");
        }
    }
    // Unspecified VHF case has changed
    if (existingCase != null && existingCase.getDisease() == Disease.UNSPECIFIED_VHF && existingCase.getDisease() != newCase.getDisease()) {
        try {
            String message = String.format(I18nProperties.getString(MessageContents.CONTENT_DISEASE_CHANGED), DataHelper.getShortUuid(newCase.getUuid()), existingCase.getDisease().toString(), newCase.getDisease().toString());
            notificationService.sendNotifications(NotificationType.DISEASE_CHANGED, JurisdictionHelper.getCaseRegions(newCase), null, MessageSubject.DISEASE_CHANGED, message);
        } catch (NotificationDeliveryFailedException e) {
            logger.error("NotificationDeliveryFailedException when trying to notify supervisors about the change of a case disease.");
        }
    }
    // If the case is a newly created case or if it was not in a CONFIRMED status
    // and now the case is in a CONFIRMED status, notify related surveillance officers
    Set<CaseClassification> confirmedClassifications = CaseClassification.getConfirmedClassifications();
    if ((existingCase == null || !confirmedClassifications.contains(existingCase.getCaseClassification())) && confirmedClassifications.contains(newCase.getCaseClassification())) {
        sendConfirmedCaseNotificationsForEvents(newCase);
    }
    if (existingCase != null && syncShares && sormasToSormasFacade.isFeatureConfigured()) {
        syncSharesAsync(new ShareTreeCriteria(existingCase.getUuid()));
    }
    // This logic should be consistent with CaseDataForm.onQuarantineEndChange
    if (existingCase != null && existingCase.getQuarantineTo() != null && !existingCase.getQuarantineTo().equals(newCase.getQuarantineTo())) {
        newCase.setPreviousQuarantineTo(existingCase.getQuarantineTo());
    }
    if (existingCase == null) {
        vaccinationFacade.updateVaccinationStatuses(newCase);
    }
    // On German systems, correct and clean up reinfection data
    if (configFacade.isConfiguredCountry(CountryHelper.COUNTRY_CODE_GERMANY)) {
        newCase.setReinfectionDetails(cleanUpReinfectionDetails(newCase.getReinfectionDetails()));
        newCase.setReinfectionStatus(CaseLogic.calculateReinfectionStatus(newCase.getReinfectionDetails()));
    }
}
Also used : ShareTreeCriteria(de.symeda.sormas.api.sormastosormas.ShareTreeCriteria) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) CaseClassification(de.symeda.sormas.api.caze.CaseClassification) PlagueType(de.symeda.sormas.api.caze.PlagueType) FieldVisibilityCheckers(de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers) Date(java.util.Date) LocalDate(java.time.LocalDate) ExternalShareInfoCountAndLatestDate(de.symeda.sormas.backend.share.ExternalShareInfoCountAndLatestDate) Contact(de.symeda.sormas.backend.contact.Contact) ContactCriteria(de.symeda.sormas.api.contact.ContactCriteria) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) District(de.symeda.sormas.backend.infrastructure.district.District) NotificationDeliveryFailedException(de.symeda.sormas.backend.common.messaging.NotificationDeliveryFailedException)

Example 5 with Facility

use of de.symeda.sormas.backend.infrastructure.facility.Facility 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)

Aggregations

Facility (de.symeda.sormas.backend.infrastructure.facility.Facility)34 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)18 Test (org.junit.Test)18 SampleDto (de.symeda.sormas.api.sample.SampleDto)17 District (de.symeda.sormas.backend.infrastructure.district.District)16 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)14 PathogenTestDto (de.symeda.sormas.api.sample.PathogenTestDto)12 Region (de.symeda.sormas.backend.infrastructure.region.Region)12 Date (java.util.Date)10 PersonDto (de.symeda.sormas.api.person.PersonDto)8 UserDto (de.symeda.sormas.api.user.UserDto)8 TestDataCreator (de.symeda.sormas.backend.TestDataCreator)8 Community (de.symeda.sormas.backend.infrastructure.community.Community)8 UserReferenceDto (de.symeda.sormas.api.user.UserReferenceDto)7 UserRole (de.symeda.sormas.api.user.UserRole)6 Disease (de.symeda.sormas.api.Disease)5 Arrays (java.util.Arrays)5 List (java.util.List)5 Matchers.is (org.hamcrest.Matchers.is)5 PathogenTestResultType (de.symeda.sormas.api.sample.PathogenTestResultType)4