Search in sources :

Example 1 with BAGExportCaseDto

use of de.symeda.sormas.api.bagexport.BAGExportCaseDto in project SORMAS-Project by hzi-braunschweig.

the class BAGExportFacadeEjbTest method testCaseExport.

@Test
public void testCaseExport() {
    final TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
    final UserDto user = creator.createUser(rdcf, UserRole.SURVEILLANCE_SUPERVISOR);
    PersonDto personDto = creator.createPerson("James", "Smith", p -> {
        LocationDto homeAddress = p.getAddress();
        homeAddress.setStreet("Home street");
        homeAddress.setHouseNumber("11A");
        homeAddress.setCity("Home city");
        homeAddress.setPostalCode("12345");
        p.setPhone("12345678");
        p.setEmailAddress("test@email.com");
        p.setSex(Sex.MALE);
        p.setBirthdateYYYY(1978);
        p.setBirthdateMM(10);
        p.setBirthdateDD(22);
        p.setOccupationType(OccupationType.ACCOMMODATION_AND_FOOD_SERVICES);
        LocationDto workPlaceAddress = LocationDto.build();
        workPlaceAddress.setAddressType(PersonAddressType.PLACE_OF_WORK);
        workPlaceAddress.setStreet("Work street");
        workPlaceAddress.setHouseNumber("12W");
        workPlaceAddress.setCity("Work city");
        workPlaceAddress.setPostalCode("54321");
        p.getAddresses().add(workPlaceAddress);
        LocationDto exposureAddress = LocationDto.build();
        exposureAddress.setAddressType(PersonAddressType.PLACE_OF_EXPOSURE);
        exposureAddress.setStreet("Exposure street");
        exposureAddress.setHouseNumber("13E");
        exposureAddress.setCity("Exposure city");
        exposureAddress.setPostalCode("098765");
        p.getAddresses().add(exposureAddress);
        LocationDto isolationAddress = LocationDto.build();
        isolationAddress.setAddressType(PersonAddressType.PLACE_OF_ISOLATION);
        isolationAddress.setStreet("Isolation street");
        isolationAddress.setHouseNumber("14I");
        isolationAddress.setCity("Isolation city");
        isolationAddress.setPostalCode("76543");
        p.getAddresses().add(isolationAddress);
    });
    Date symptomDate = new Date();
    Date contactTracingDate = DateHelper.subtractDays(new Date(), 10);
    Date quarantineFromDate = DateHelper.subtractDays(new Date(), 11);
    Date quarantineToDate = DateHelper.subtractDays(new Date(), 1);
    Date followupDate = DateHelper.addDays(new Date(), 10);
    CaseDataDto cazeDto = creator.createCase(user.toReference(), personDto.toReference(), Disease.CORONAVIRUS, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf, c -> {
        c.setCaseIdIsm(123456);
        c.getSymptoms().setAgitation(SymptomState.YES);
        c.getSymptoms().setOnsetDate(symptomDate);
        c.setContactTracingFirstContactDate(contactTracingDate);
        c.setWasInQuarantineBeforeIsolation(YesNoUnknown.NO);
        c.setQuarantineFrom(quarantineFromDate);
        c.setQuarantineReasonBeforeIsolation(QuarantineReason.OTHER_REASON);
        c.setQuarantineReasonBeforeIsolationDetails("Test quarantine details");
        c.setQuarantine(QuarantineType.OTHER);
        c.setQuarantineTypeDetails("Test quarantine");
        c.setFollowUpStatus(FollowUpStatus.FOLLOW_UP);
        c.setOverwriteFollowUpUntil(true);
        c.setFollowUpUntil(followupDate);
        c.setQuarantineTo(quarantineToDate);
        c.setEndOfIsolationReason(EndOfIsolationReason.OTHER);
        c.setEndOfIsolationReasonDetails("Test end of iso");
    });
    Date sampleDate = DateHelper.subtractDays(new Date(), 5);
    SampleDto sample = creator.createSample(cazeDto.toReference(), user.toReference(), new Facility(), s -> {
        s.setSampleDateTime(sampleDate);
        s.setSamplingReason(SamplingReason.OTHER_REASON);
        s.setSamplingReasonDetails("Test reason");
    });
    Date testDate = DateHelper.subtractDays(new Date(), 4);
    creator.createPathogenTest(sample.toReference(), PathogenTestType.RAPID_TEST, Disease.CORONAVIRUS, testDate, new Facility(), user.toReference(), PathogenTestResultType.POSITIVE, "", true);
    Case caze = getCaseService().getByUuid(cazeDto.getUuid());
    List<BAGExportCaseDto> caseList = getBAGExportFacade().getCaseExportList(Collections.emptySet(), 0, 100);
    BAGExportCaseDto firstCase = caseList.get(0);
    assertThat(firstCase.getCaseIdIsm(), is(123456));
    assertThat(firstCase.getCaseId(), is(caze.getId()));
    assertThat(firstCase.getLastName(), is("Smith"));
    assertThat(firstCase.getFirstName(), is("James"));
    assertThat(firstCase.getHomeAddressStreet(), is("Home street"));
    assertThat(firstCase.getHomeAddressHouseNumber(), is("11A"));
    assertThat(firstCase.getHomeAddressCity(), is("Home city"));
    assertThat(firstCase.getHomeAddressPostalCode(), is("12345"));
    assertThat(firstCase.getHomeAddressCountry(), isEmptyOrNullString());
    assertThat(firstCase.getPhoneNumber(), is("12345678"));
    assertThat(firstCase.getMobileNumber(), isEmptyOrNullString());
    assertThat(firstCase.getEmailAddress(), is("test@email.com"));
    assertThat(firstCase.getSex(), is(Sex.MALE));
    assertThat(firstCase.getBirthDate().getDateOfBirthYYYY(), is(1978));
    assertThat(firstCase.getBirthDate().getDateOfBirthMM(), is(10));
    assertThat(firstCase.getBirthDate().getDateOfBirthDD(), is(22));
    assertThat(firstCase.getOccupationType(), is(OccupationType.ACCOMMODATION_AND_FOOD_SERVICES));
    assertThat(firstCase.getWorkPlaceName(), isEmptyOrNullString());
    assertThat(firstCase.getWorkPlaceStreet(), is("Work street"));
    assertThat(firstCase.getWorkPlaceStreetNumber(), is("12W"));
    assertThat(firstCase.getWorkPlaceCity(), is("Work city"));
    assertThat(firstCase.getWorkPlacePostalCode(), is("54321"));
    assertThat(firstCase.getWorkPlaceCountry(), isEmptyOrNullString());
    assertThat(firstCase.getSymptomatic(), is(YesNoUnknown.YES));
    assertThat(firstCase.getPcrReason(), is(SamplingReason.OTHER_REASON));
    assertThat(firstCase.getOtherPcrReason(), is("Test reason"));
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    assertThat(dateFormat.format(firstCase.getSymptomOnsetDate()), is(dateFormat.format(symptomDate)));
    assertThat(dateFormat.format(firstCase.getSampleDate()), is(dateFormat.format(sampleDate)));
    assertThat(dateFormat.format(firstCase.getLabReportDate()), is(dateFormat.format(testDate)));
    assertThat(firstCase.getTestType(), is(PathogenTestType.RAPID_TEST));
    assertThat(firstCase.getTestResult(), is(PathogenTestResultType.POSITIVE));
    assertThat(firstCase.getContactCaseLinkCaseYn(), is(nullValue()));
    assertThat(firstCase.getContactCaseLinkCaseId(), is(nullValue()));
    assertThat(firstCase.getContactCaseLinkCaseIdIsm(), is(nullValue()));
    assertThat(firstCase.getContactCaseLinkContactDate(), is(nullValue()));
    assertThat(firstCase.getExposureLocationYn(), is(YesNoUnknown.YES));
    assertThat(firstCase.getActivityMappingYn(), isEmptyOrNullString());
    assertThat(firstCase.getExposureCountry(), isEmptyOrNullString());
    assertThat(firstCase.getExposureLocationCity(), is("Exposure city"));
    assertThat(firstCase.getExposureLocationTypeDetails(), isEmptyOrNullString());
    assertThat(firstCase.getExposureLocationName(), isEmptyOrNullString());
    assertThat(firstCase.getExposureLocationStreet(), is("Exposure street"));
    assertThat(firstCase.getExposureLocationStreetNumber(), is("13E"));
    assertThat(firstCase.getExposureLocationCity(), is("Exposure city"));
    assertThat(firstCase.getExposureLocationPostalCode(), is("098765"));
    assertThat(firstCase.getExposureLocationCountry(), isEmptyOrNullString());
    assertThat(dateFormat.format(firstCase.getContactTracingContactDate()), is(dateFormat.format(contactTracingDate)));
    assertThat(firstCase.getIsolationType(), is(QuarantineType.OTHER));
    assertThat(firstCase.getIsolationTypeDetails(), is("Test quarantine"));
    assertThat(firstCase.getIsolationLocationStreet(), is("Isolation street"));
    assertThat(firstCase.getIsolationLocationStreetNumber(), is("14I"));
    assertThat(firstCase.getIsolationLocationCity(), is("Isolation city"));
    assertThat(firstCase.getIsolationLocationPostalCode(), is("76543"));
    assertThat(firstCase.getIsolationLocationCountry(), isEmptyOrNullString());
    assertThat(dateFormat.format(firstCase.getFollowUpStartDate()), is(dateFormat.format(quarantineFromDate)));
    assertThat(dateFormat.format(firstCase.getEndOfIsolationDate()), is(dateFormat.format(quarantineToDate)));
    assertThat(firstCase.getEndOfIsolationReason(), is(EndOfIsolationReason.OTHER));
    assertThat(firstCase.getEndOfIsolationReasonDetails(), is("Test end of iso"));
}
Also used : CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) UserDto(de.symeda.sormas.api.user.UserDto) Date(java.util.Date) BAGExportCaseDto(de.symeda.sormas.api.bagexport.BAGExportCaseDto) Case(de.symeda.sormas.backend.caze.Case) TestDataCreator(de.symeda.sormas.backend.TestDataCreator) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) SampleDto(de.symeda.sormas.api.sample.SampleDto) SimpleDateFormat(java.text.SimpleDateFormat) LocationDto(de.symeda.sormas.api.location.LocationDto) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Example 2 with BAGExportCaseDto

use of de.symeda.sormas.api.bagexport.BAGExportCaseDto in project SORMAS-Project by hzi-braunschweig.

the class BAGExportFacadeEjb method getCaseExportList.

@Override
public List<BAGExportCaseDto> getCaseExportList(Collection<String> selectedRows, int first, int max) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<BAGExportCaseDto> cq = cb.createQuery(BAGExportCaseDto.class);
    Root<Case> caseRoot = cq.from(Case.class);
    CaseJoins caseJoins = new CaseJoins(caseRoot);
    Join<Case, Person> person = caseJoins.getPerson();
    PersonQueryContext personQueryContext = new PersonQueryContext(cb, cq, caseJoins.getPersonJoins());
    Join<Person, Location> homeAddress = caseJoins.getPersonAddress();
    Expression<String> homeAddressCountry = cb.literal(TODO_VALUE);
    Expression<String> mobileNumber = cb.literal(TODO_VALUE);
    Expression<String> activityMappingYn = cb.literal(TODO_VALUE);
    cq.multiselect(caseRoot.get(Case.CASE_ID_ISM), caseRoot.get(Case.ID), person.get(Person.ID), person.get(Person.LAST_NAME), person.get(Person.FIRST_NAME), homeAddress.get(Location.STREET), homeAddress.get(Location.HOUSE_NUMBER), homeAddress.get(Location.CITY), homeAddress.get(Location.POSTAL_CODE), homeAddressCountry, personQueryContext.getSubqueryExpression(PersonQueryContext.PERSON_PHONE_SUBQUERY), mobileNumber, personQueryContext.getSubqueryExpression(PersonQueryContext.PERSON_EMAIL_SUBQUERY), person.get(Person.SEX), person.get(Person.BIRTHDATE_DD), person.get(Person.BIRTHDATE_MM), person.get(Person.BIRTHDATE_YYYY), person.get(Person.OCCUPATION_TYPE), caseJoins.getSymptoms().get(Symptoms.SYMPTOMATIC), caseJoins.getSymptoms().get(Symptoms.ONSET_DATE), activityMappingYn, caseRoot.get(Case.CONTACT_TRACING_FIRST_CONTACT_DATE), caseRoot.get(Case.WAS_IN_QUARANTINE_BEFORE_ISOLATION), caseRoot.get(Case.QUARANTINE_REASON_BEFORE_ISOLATION), caseRoot.get(Case.QUARANTINE_REASON_BEFORE_ISOLATION_DETAILS), caseRoot.get(Case.QUARANTINE), caseRoot.get(Case.QUARANTINE_TYPE_DETAILS), caseRoot.get(Case.QUARANTINE_FROM), caseRoot.get(Case.QUARANTINE_TO), caseRoot.get(Case.END_OF_ISOLATION_REASON), caseRoot.get(Case.END_OF_ISOLATION_REASON_DETAILS));
    if (CollectionUtils.isNotEmpty(selectedRows)) {
        cq.where(CriteriaBuilderHelper.andInValues(selectedRows, null, cb, caseRoot.get(Case.UUID)));
    }
    List<BAGExportCaseDto> exportList = QueryHelper.getResultList(em, cq, first, max);
    Map<Long, List<Location>> personAddresses = new HashMap<>();
    Map<Long, List<Sample>> samples = new HashMap<>();
    if (exportList.size() > 0) {
        List<Long> caseIds = exportList.stream().map(BAGExportCaseDto::getCaseId).collect(Collectors.toList());
        List<Long> personIds = exportList.stream().map(BAGExportCaseDto::getPersonId).collect(Collectors.toList());
        // get addresses
        CriteriaQuery<Object[]> addressesCq = cb.createQuery(Object[].class);
        Root<Location> addressesRoot = addressesCq.from(Location.class);
        addressesCq.where(addressesRoot.get(Location.PERSON).get(Person.ID).in(personIds));
        addressesCq.multiselect(addressesRoot.get(Location.PERSON).get(Person.ID), addressesRoot);
        List<Object[]> personIdLocationList = em.createQuery(addressesCq).getResultList();
        personIdLocationList.forEach(personIdLocation -> {
            Long personId = (Long) personIdLocation[0];
            Location location = (Location) personIdLocation[1];
            List<Location> personLocations = personAddresses.get(personId);
            if (personLocations == null) {
                personLocations = new ArrayList<>();
            }
            personLocations.add(location);
            personAddresses.put(personId, personLocations);
        });
        // get samples
        CriteriaQuery<Sample> samplesCq = cb.createQuery(Sample.class);
        Root<Sample> samplesRoot = samplesCq.from(Sample.class);
        Path<Object> caseIdExpr = samplesRoot.join(Sample.ASSOCIATED_CASE, JoinType.LEFT).get(Case.ID);
        samplesCq.where(caseIdExpr.in(caseIds)).orderBy(cb.asc(samplesRoot.get(Sample.REPORT_DATE_TIME)));
        List<Sample> samplesList = em.createQuery(samplesCq).setHint(ModelConstants.HINT_HIBERNATE_READ_ONLY, true).getResultList();
        samples.putAll(samplesList.stream().collect(Collectors.groupingBy(s -> s.getAssociatedCase().getId())));
    }
    exportList.forEach(caze -> {
        List<Location> addresses = personAddresses.getOrDefault(caze.getPersonId(), Collections.emptyList());
        addresses.stream().filter(a -> PersonAddressType.PLACE_OF_WORK.equals(a.getAddressType())).findFirst().ifPresent(workAddress -> {
            caze.setWorkPlaceName(TODO_VALUE);
            caze.setWorkPlaceStreet(workAddress.getStreet());
            caze.setWorkPlaceStreetNumber(workAddress.getHouseNumber());
            caze.setWorkPlaceCity(workAddress.getCity());
            caze.setWorkPlacePostalCode(workAddress.getPostalCode());
            caze.setWorkPlaceCountry(TODO_VALUE);
        });
        caze.setExposureLocationYn(YesNoUnknown.NO);
        addresses.stream().filter(a -> PersonAddressType.PLACE_OF_EXPOSURE.equals(a.getAddressType())).findFirst().ifPresent(exposureAddress -> {
            caze.setExposureLocationYn(YesNoUnknown.YES);
            caze.setExposureCountry(TODO_VALUE);
            caze.setExposureLocationType(exposureAddress.getFacilityType());
            caze.setExposureLocationFlightDetail(exposureAddress.getFacilityDetails());
            caze.setExposureLocationName(TODO_VALUE);
            caze.setExposureLocationStreet(exposureAddress.getStreet());
            caze.setExposureLocationStreetNumber(exposureAddress.getHouseNumber());
            caze.setExposureLocationCity(exposureAddress.getCity());
            caze.setExposureLocationPostalCode(exposureAddress.getPostalCode());
            caze.setExposureLocationFlightDetail(TODO_VALUE);
        });
        addresses.stream().filter(a -> PersonAddressType.PLACE_OF_ISOLATION.equals(a.getAddressType())).findFirst().ifPresent(isolationAddress -> {
            caze.setIsolationLocationStreet(isolationAddress.getStreet());
            caze.setIsolationLocationStreetNumber(isolationAddress.getHouseNumber());
            caze.setIsolationLocationCity(isolationAddress.getCity());
            caze.setIsolationLocationPostalCode(isolationAddress.getPostalCode());
            caze.setIsolationLocationCountry(TODO_VALUE);
        });
        List<Sample> caseSamples = samples.get(caze.getCaseId());
        if (caseSamples != null && caseSamples.size() > 0) {
            Sample firstSample = caseSamples.get(0);
            caze.setSampleDate(firstSample.getSampleDateTime());
            caze.setPcrReason(firstSample.getSamplingReason());
            caze.setOtherPcrReason(firstSample.getSamplingReasonDetails());
            List<PathogenTest> sortedTests = firstSample.getPathogenTests().stream().sorted(Comparator.comparing(PathogenTest::getTestDateTime)).collect(Collectors.toList());
            Optional<PathogenTest> positiveTest = sortedTests.stream().filter(t -> t.getTestResult() == PathogenTestResultType.POSITIVE).findFirst();
            if (positiveTest.isPresent()) {
                setCasePathogenTestData(caze, positiveTest.get());
            } else if (sortedTests.size() > 0) {
                setCasePathogenTestData(caze, sortedTests.get(0));
            }
        }
    });
    return exportList;
}
Also used : Join(javax.persistence.criteria.Join) RolesAllowed(javax.annotation.security.RolesAllowed) BAGExportFacade(de.symeda.sormas.api.bagexport.BAGExportFacade) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) Date(java.util.Date) HashMap(java.util.HashMap) PathogenTest(de.symeda.sormas.backend.sample.PathogenTest) ArrayList(java.util.ArrayList) BAGExportContactDto(de.symeda.sormas.api.bagexport.BAGExportContactDto) YesNoUnknown(de.symeda.sormas.api.utils.YesNoUnknown) Case(de.symeda.sormas.backend.caze.Case) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) LocalBean(javax.ejb.LocalBean) Symptoms(de.symeda.sormas.backend.symptoms.Symptoms) ContactJoins(de.symeda.sormas.backend.contact.ContactJoins) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) CaseJoins(de.symeda.sormas.backend.caze.CaseJoins) Path(javax.persistence.criteria.Path) Root(javax.persistence.criteria.Root) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Contact(de.symeda.sormas.backend.contact.Contact) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Person(de.symeda.sormas.backend.person.Person) Collection(java.util.Collection) BAGExportCaseDto(de.symeda.sormas.api.bagexport.BAGExportCaseDto) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Collectors(java.util.stream.Collectors) UserRight(de.symeda.sormas.api.user.UserRight) List(java.util.List) PersonAddressType(de.symeda.sormas.api.person.PersonAddressType) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) PersonQueryContext(de.symeda.sormas.backend.person.PersonQueryContext) Optional(java.util.Optional) Comparator(java.util.Comparator) Location(de.symeda.sormas.backend.location.Location) Collections(java.util.Collections) Sample(de.symeda.sormas.backend.sample.Sample) HashMap(java.util.HashMap) Case(de.symeda.sormas.backend.caze.Case) ArrayList(java.util.ArrayList) List(java.util.List) CaseJoins(de.symeda.sormas.backend.caze.CaseJoins) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Sample(de.symeda.sormas.backend.sample.Sample) PathogenTest(de.symeda.sormas.backend.sample.PathogenTest) BAGExportCaseDto(de.symeda.sormas.api.bagexport.BAGExportCaseDto) PersonQueryContext(de.symeda.sormas.backend.person.PersonQueryContext) Person(de.symeda.sormas.backend.person.Person) Location(de.symeda.sormas.backend.location.Location)

Aggregations

BAGExportCaseDto (de.symeda.sormas.api.bagexport.BAGExportCaseDto)2 Case (de.symeda.sormas.backend.caze.Case)2 Date (java.util.Date)2 BAGExportContactDto (de.symeda.sormas.api.bagexport.BAGExportContactDto)1 BAGExportFacade (de.symeda.sormas.api.bagexport.BAGExportFacade)1 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)1 LocationDto (de.symeda.sormas.api.location.LocationDto)1 PersonAddressType (de.symeda.sormas.api.person.PersonAddressType)1 PersonDto (de.symeda.sormas.api.person.PersonDto)1 PathogenTestResultType (de.symeda.sormas.api.sample.PathogenTestResultType)1 SampleDto (de.symeda.sormas.api.sample.SampleDto)1 UserDto (de.symeda.sormas.api.user.UserDto)1 UserRight (de.symeda.sormas.api.user.UserRight)1 YesNoUnknown (de.symeda.sormas.api.utils.YesNoUnknown)1 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)1 TestDataCreator (de.symeda.sormas.backend.TestDataCreator)1 CaseJoins (de.symeda.sormas.backend.caze.CaseJoins)1 CriteriaBuilderHelper (de.symeda.sormas.backend.common.CriteriaBuilderHelper)1 Contact (de.symeda.sormas.backend.contact.Contact)1 ContactJoins (de.symeda.sormas.backend.contact.ContactJoins)1