Search in sources :

Example 1 with FacilityCriteria

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

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

the class DevModeView method generateSamples.

private void generateSamples() {
    initializeRandomGenerator();
    SampleGenerationConfig config = sampleGeneratorConfigBinder.getBean();
    float baseOffset = random().nextFloat();
    int daysBetween = (int) ChronoUnit.DAYS.between(config.startDate, config.endDate);
    FacilityCriteria facilityCriteria = new FacilityCriteria();
    facilityCriteria.region(config.getRegion());
    facilityCriteria.district(config.getDistrict());
    long dt = System.nanoTime();
    UserReferenceDto user = UserProvider.getCurrent().getUserReference();
    List<CaseReferenceDto> cases = FacadeProvider.getCaseFacade().getRandomCaseReferences(new CaseCriteria().region(config.getRegion()).district(config.getDistrict()).disease(config.getDisease()), config.getSampleCount() * 2, random());
    if (nonNull(cases)) {
        for (int i = 0; i < config.getSampleCount(); i++) {
            CaseReferenceDto caseReference = random(cases);
            List<Disease> diseases = FacadeProvider.getDiseaseConfigurationFacade().getAllDiseases(true, true, true);
            Disease disease = config.getDisease();
            if (disease == null) {
                disease = random(diseases);
                config.setDisease(disease);
            }
            LocalDateTime referenceDateTime = getReferenceDateTime(i, config.getSampleCount(), baseOffset, config.getDisease(), config.getStartDate(), daysBetween);
            SampleDto sample = SampleDto.build(user, caseReference);
            sample.setSamplePurpose(config.getSamplePurpose());
            Date date = Date.from(referenceDateTime.toLocalDate().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            sample.setSampleDateTime(date);
            sample.setSampleMaterial(config.getSampleMaterial());
            sample.setFieldSampleID(UUID.randomUUID().toString());
            sample.setComment(random(sampleComments));
            sample.setLab(config.getLaboratory());
            if (config.isRequestPathogenTestsToBePerformed()) {
                Set pathogenTestTypes = new HashSet<PathogenTestType>();
                int until = randomInt(1, PathogenTestType.values().length);
                for (int j = 0; j < until; j++) {
                    pathogenTestTypes.add(PathogenTestType.values()[j]);
                }
                sample.setPathogenTestingRequested(true);
                sample.setRequestedPathogenTests(pathogenTestTypes);
            }
            if (config.isRequestAdditionalTestsToBePerformed()) {
                Set additionalTestTypes = new HashSet<AdditionalTestType>();
                int until = randomInt(1, AdditionalTestType.values().length);
                for (int j = 0; j < until; j++) {
                    additionalTestTypes.add(AdditionalTestType.values()[j]);
                }
                sample.setAdditionalTestingRequested(true);
                sample.setRequestedAdditionalTests(additionalTestTypes);
            }
            if (config.isSendDispatch()) {
                sample.setShipped(true);
                sample.setShipmentDate(date);
                sample.setShipmentDetails(random(sampleShipmentDetails));
            }
            if (config.isReceived()) {
                sample.setReceived(true);
                sample.setReceivedDate(date);
                sample.setSpecimenCondition(random(SpecimenCondition.values()));
            }
            SampleDto sampleDto = FacadeProvider.getSampleFacade().saveSample(sample);
            if (config.isRequestAdditionalTestsToBePerformed()) {
                createAdditionalTest(sampleDto, date);
            }
        }
        dt = System.nanoTime() - dt;
        long perSample = dt / config.getSampleCount();
        String msg = String.format("Generating %d samples took %.2f  s (%.1f ms per sample)", config.getSampleCount(), (double) dt / 1_000_000_000, (double) perSample / 1_000_000);
        logger.info(msg);
        Notification.show("", msg, Notification.Type.TRAY_NOTIFICATION);
    } else {
        String msg = "No Sample has been generated because cases is null ";
        logger.info(msg);
        Notification.show("", msg, Notification.Type.TRAY_NOTIFICATION);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) HashSet(java.util.HashSet) Disease(de.symeda.sormas.api.Disease) Date(java.util.Date) LocalDate(java.time.LocalDate) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) CaseCriteria(de.symeda.sormas.api.caze.CaseCriteria) FacilityCriteria(de.symeda.sormas.api.infrastructure.facility.FacilityCriteria) SampleDto(de.symeda.sormas.api.sample.SampleDto) CaseReferenceDto(de.symeda.sormas.api.caze.CaseReferenceDto) HashSet(java.util.HashSet)

Example 3 with FacilityCriteria

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

the class FacilityFacadeEjbTest method testGetIndexListMappedSorting.

@Test
public void testGetIndexListMappedSorting() {
    FacilityCriteria facilityCriteria = new FacilityCriteria();
    // 0. No sortProperties
    List<FacilityIndexDto> result = getFacilityFacade().getIndexList(facilityCriteria, null, null, new ArrayList<>());
    assertThat(result, is(empty()));
    List<SortProperty> allSortProperties = new ArrayList<>();
    allSortProperties.add(new SortProperty(FacilityDto.NAME));
    allSortProperties.add(new SortProperty(FacilityDto.TYPE));
    allSortProperties.add(new SortProperty(FacilityDto.REGION));
    allSortProperties.add(new SortProperty(FacilityDto.DISTRICT));
    allSortProperties.add(new SortProperty(FacilityDto.COMMUNITY));
    allSortProperties.add(new SortProperty(FacilityDto.CITY));
    allSortProperties.add(new SortProperty(FacilityDto.LATITUDE));
    allSortProperties.add(new SortProperty(FacilityDto.LONGITUDE));
    allSortProperties.add(new SortProperty(FacilityDto.EXTERNAL_ID));
    // 1. Sort by every property
    for (SortProperty sortProperty : allSortProperties) {
        getFacilityFacade().getIndexList(facilityCriteria, null, null, Collections.singletonList(sortProperty));
        assertThat(sortProperty.toString(), result, is(empty()));
    }
    // 2. Sort by all properties at once
    getFacilityFacade().getIndexList(facilityCriteria, null, null, allSortProperties);
    assertThat(result, is(empty()));
}
Also used : SortProperty(de.symeda.sormas.api.utils.SortProperty) FacilityIndexDto(de.symeda.sormas.api.infrastructure.facility.FacilityIndexDto) ArrayList(java.util.ArrayList) FacilityCriteria(de.symeda.sormas.api.infrastructure.facility.FacilityCriteria) Test(org.junit.Test) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest)

Example 4 with FacilityCriteria

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

the class InfrastructureImporterTest method testDontImportDuplicateInfrastructure.

@Test
public void testDontImportDuplicateInfrastructure() throws IOException, InvalidColumnException, InterruptedException, CsvValidationException, URISyntaxException {
    RDCF rdcf = new TestDataCreator().createRDCF("Default Region", "Default District", "Default Community", "Default Facility");
    UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Default", "User", UserRole.ADMIN);
    // Import region
    File regionCsvFile = new File(getClass().getClassLoader().getResource("sormas_region_import_test.csv").toURI());
    InfrastructureImporter importer = new InfrastructureImporterExtension(regionCsvFile, user, InfrastructureType.REGION);
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importer.runImport());
    assertEquals(2, getRegionFacade().count(new RegionCriteria()));
    // Import district
    File districtCsvFile = new File(getClass().getClassLoader().getResource("sormas_district_import_test.csv").toURI());
    importer = new InfrastructureImporterExtension(districtCsvFile, user, InfrastructureType.DISTRICT);
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importer.runImport());
    assertEquals(2, getDistrictFacade().count(new DistrictCriteria()));
    // Import community
    File communityCsvFile = new File(getClass().getClassLoader().getResource("sormas_community_import_test.csv").toURI());
    importer = new InfrastructureImporterExtension(communityCsvFile, user, InfrastructureType.COMMUNITY);
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importer.runImport());
    assertEquals(2, getCommunityFacade().count(new CommunityCriteria()));
    // Import facility
    File facilityCsvFile = new File(getClass().getClassLoader().getResource("sormas_facility_import_test.csv").toURI());
    importer = new InfrastructureImporterExtension(facilityCsvFile, user, InfrastructureType.FACILITY);
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importer.runImport());
    assertEquals(3, getFacilityFacade().count(new FacilityCriteria()));
    // Import point of entry
    File poeCsvFile = new File(getClass().getClassLoader().getResource("sormas_poe_import_test.csv").toURI());
    importer = new InfrastructureImporterExtension(poeCsvFile, user, InfrastructureType.POINT_OF_ENTRY);
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importer.runImport());
    assertEquals(1, getPointOfEntryFacade().count(new PointOfEntryCriteria()));
}
Also used : PointOfEntryCriteria(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryCriteria) RDCF(de.symeda.sormas.ui.TestDataCreator.RDCF) RegionCriteria(de.symeda.sormas.api.infrastructure.region.RegionCriteria) UserDto(de.symeda.sormas.api.user.UserDto) TestDataCreator(de.symeda.sormas.ui.TestDataCreator) DistrictCriteria(de.symeda.sormas.api.infrastructure.district.DistrictCriteria) FacilityCriteria(de.symeda.sormas.api.infrastructure.facility.FacilityCriteria) File(java.io.File) CommunityCriteria(de.symeda.sormas.api.infrastructure.community.CommunityCriteria) AbstractBeanTest(de.symeda.sormas.ui.AbstractBeanTest) Test(org.junit.Test)

Example 5 with FacilityCriteria

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

the class DevModeView method generateEvents.

private void generateEvents() {
    initializeRandomGenerator();
    EventGenerationConfig config = eventGeneratorConfigBinder.getBean();
    int generatedParticipants = 0;
    int generatedCases = 0;
    int generatedContacts = 0;
    Disease disease = config.getDisease();
    float baseOffset = random().nextFloat();
    int daysBetween = (int) ChronoUnit.DAYS.between(config.startDate, config.endDate);
    // this should be adjusted to be much more complex
    FacilityCriteria facilityCriteria = new FacilityCriteria();
    facilityCriteria.region(config.getRegion());
    facilityCriteria.district(config.getDistrict());
    List<FacilityIndexDto> healthFacilities = FacadeProvider.getFacilityFacade().getIndexList(facilityCriteria, 0, (int) (config.getMaxParticipantsPerEvent() * config.getPercentageOfCases() / 100), null);
    // Filter list, so that only health facilities meant for accomodation are selected
    healthFacilities.removeIf(el -> (!el.getType().isAccommodation()));
    long dt = System.nanoTime();
    for (int i = 0; i < config.getEventCount(); i++) {
        LocalDateTime referenceDateTime;
        EventDto event = EventDto.build();
        // disease
        if (disease != null) {
            // reset
            event.setDisease(disease);
            if (event.getDisease() == Disease.OTHER) {
                event.setDiseaseDetails("RD " + (random().nextInt(20) + 1));
            }
            referenceDateTime = getReferenceDateTime(i, config.getEventCount(), baseOffset, disease, config.getStartDate(), daysBetween);
            fieldVisibilityCheckers = FieldVisibilityCheckers.withDisease(disease).andWithCountry(FacadeProvider.getConfigFacade().getCountryLocale());
        } else {
            referenceDateTime = getReferenceDateTime(i, config.getEventCount(), baseOffset, Disease.OTHER, config.getStartDate(), daysBetween);
            fieldVisibilityCheckers = FieldVisibilityCheckers.withDisease(Disease.OTHER).andWithCountry(FacadeProvider.getConfigFacade().getCountryLocale());
        }
        // title
        event.setEventTitle(random(eventTitles));
        // description
        event.setEventDesc("Event generated using DevMode on " + LocalDate.now());
        // report
        UserReferenceDto userReference = UserProvider.getCurrent().getUserReference();
        event.setReportingUser(userReference);
        event.setReportDateTime(Date.from(referenceDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        // region & district
        event.getEventLocation().setRegion(config.getRegion());
        event.getEventLocation().setDistrict(config.getDistrict());
        // status
        event.setEventStatus(EventStatus.EVENT);
        FacadeProvider.getEventFacade().save(event);
        // EventParticipants
        int numParticipants = randomInt(config.getMinParticipantsPerEvent(), config.getMaxParticipantsPerEvent());
        for (int j = 0; j < numParticipants; j++) {
            EventParticipantDto eventParticipant = EventParticipantDto.build(event.toReference(), UserProvider.getCurrent().getUserReference());
            // person
            // instead of creating new persons everytime, it would be nice if some persons came of the original database
            PersonDto person = PersonDto.build();
            fillEntity(person, referenceDateTime);
            person.setSymptomJournalStatus(null);
            setPersonName(person);
            FacadeProvider.getPersonFacade().savePerson(person);
            eventParticipant.setPerson(person);
            eventParticipant.setInvolvementDescription("Participant");
            if (disease != null) {
                // generate cases for some participants
                if (randomPercent(config.getPercentageOfCases()) && !healthFacilities.isEmpty()) {
                    CaseDataDto caze = CaseDataDto.buildFromEventParticipant(eventParticipant, person, event.getDisease());
                    fillEntity(caze, referenceDateTime);
                    caze.setDisease(event.getDisease());
                    caze.setReportingUser(UserProvider.getCurrent().getUserReference());
                    caze.setReportDate(Date.from(referenceDateTime.atZone(ZoneId.systemDefault()).toInstant()));
                    caze.setCaseOrigin(CaseOrigin.IN_COUNTRY);
                    caze.setResponsibleRegion(config.getRegion());
                    caze.setResponsibleDistrict(config.getDistrict());
                    FacilityIndexDto facility = random(healthFacilities);
                    caze.setHealthFacility(facility.toReference());
                    caze.setFacilityType(facility.getType());
                    caze.setAdditionalDetails("Case generated using DevMode on " + LocalDate.now());
                    FacadeProvider.getCaseFacade().save(caze);
                    eventParticipant.setResultingCase(caze.toReference());
                    generatedCases++;
                }
                // generate contacts for some participants
                List<CaseReferenceDto> cases = FacadeProvider.getCaseFacade().getRandomCaseReferences(new CaseCriteria().region(config.getRegion()).district(config.getDistrict()).disease(event.getDisease()), numParticipants * 2, random());
                int numContacts = randomInt(config.getMinContactsPerParticipant(), config.getMaxContactsPerParticipant());
                for (int k = 0; (k < numContacts && (cases != null)); k++) {
                    ContactDto contact = ContactDto.build(eventParticipant);
                    contact.setDisease(event.getDisease());
                    contact.setCaze(random(cases));
                    contact.setReportingUser(UserProvider.getCurrent().getUserReference());
                    contact.setReportDateTime(Date.from(referenceDateTime.atZone(ZoneId.systemDefault()).toInstant()));
                    contact.setDescription("Contact generated using DevMode on " + LocalDate.now());
                    FacadeProvider.getContactFacade().save(contact);
                    generatedContacts++;
                }
            }
            FacadeProvider.getEventParticipantFacade().saveEventParticipant(eventParticipant);
            generatedParticipants++;
        }
    }
    dt = System.nanoTime() - dt;
    long perCase = dt / config.getEventCount();
    String msg = String.format("Generating %d events with a total of %d participants (%d contacts, %d cases) took %.2f  s (%.1f ms per event)", config.getEventCount(), generatedParticipants, generatedContacts, generatedCases, (double) dt / 1_000_000_000, (double) perCase / 1_000_000);
    logger.info(msg);
    Notification.show("", msg, Notification.Type.TRAY_NOTIFICATION);
}
Also used : LocalDateTime(java.time.LocalDateTime) Disease(de.symeda.sormas.api.Disease) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) FacilityIndexDto(de.symeda.sormas.api.infrastructure.facility.FacilityIndexDto) PersonDto(de.symeda.sormas.api.person.PersonDto) EventDto(de.symeda.sormas.api.event.EventDto) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) CaseCriteria(de.symeda.sormas.api.caze.CaseCriteria) ContactDto(de.symeda.sormas.api.contact.ContactDto) FacilityCriteria(de.symeda.sormas.api.infrastructure.facility.FacilityCriteria) CaseReferenceDto(de.symeda.sormas.api.caze.CaseReferenceDto)

Aggregations

FacilityCriteria (de.symeda.sormas.api.infrastructure.facility.FacilityCriteria)7 FacilityIndexDto (de.symeda.sormas.api.infrastructure.facility.FacilityIndexDto)4 Disease (de.symeda.sormas.api.Disease)3 UserReferenceDto (de.symeda.sormas.api.user.UserReferenceDto)3 SortProperty (de.symeda.sormas.api.utils.SortProperty)3 LocalDateTime (java.time.LocalDateTime)3 CaseCriteria (de.symeda.sormas.api.caze.CaseCriteria)2 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)2 CaseReferenceDto (de.symeda.sormas.api.caze.CaseReferenceDto)2 PersonDto (de.symeda.sormas.api.person.PersonDto)2 Test (org.junit.Test)2 ContactDto (de.symeda.sormas.api.contact.ContactDto)1 EventDto (de.symeda.sormas.api.event.EventDto)1 EventParticipantDto (de.symeda.sormas.api.event.EventParticipantDto)1 CommunityCriteria (de.symeda.sormas.api.infrastructure.community.CommunityCriteria)1 DistrictCriteria (de.symeda.sormas.api.infrastructure.district.DistrictCriteria)1 FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)1 PointOfEntryCriteria (de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryCriteria)1 RegionCriteria (de.symeda.sormas.api.infrastructure.region.RegionCriteria)1 SampleDto (de.symeda.sormas.api.sample.SampleDto)1