FhirEncounterProvider implements EncounterProvider { @Override public List<EncounterDto> getPatientsActiveEncounters(@Nonnull final Collection<String> patientsIds) { Preconditions.checkNotNull(patientsIds, "patientId must not be blank"); if (patientsIds.isEmpty()) { return Collections.emptyList(); } final Bundle result; try { result = fhirClient .search() .forResource(Encounter.class) .include(Encounter.INCLUDE_PATIENT) .include(Encounter.INCLUDE_LOCATION) .include(Encounter.INCLUDE_PRACTITIONER) .where(Encounter.PATIENT.hasChainedProperty( Patient.IDENTIFIER.exactly().identifiers(buildFhirPatientsIdentifiers(patientsIds)))) .and(Encounter.STATUS.exactly().identifiers( buildFhirEncounterStatusIdentifiers( EnumSet.of( EncounterStateEnum.ARRIVED, EncounterStateEnum.IN_PROGRESS, EncounterStateEnum.ON_LEAVE, EncounterStateEnum.FINISHED)))) .sort().ascending(Encounter.DATE) .returnBundle(Bundle.class) .execute(); } catch (final Exception exception) { throw new SystemException( Dictionary.getEntry( "fhir.error.reading.encounter.data", DefinedLocaleHolder.INSTANCE.getCalculatedNotNullLocale()), exception); } return result.getEntry().stream() .filter(e -> e.getResource() instanceof Encounter) .map(e -> (Encounter)e.getResource()) .map(e -> getEncounterDto(e, getPatientIdentifier(e))) .collect(Collectors.toList()); }  @Required void setFhirClient(final IGenericClient fhirClient); @Required void setFhirPatientIdSystem(final String fhirPatientIdSystem); @Required void setFhirEncounterIdSystem(final String fhirEncounterIdSystem); @Required void setFhirWardIdSystem(final String fhirWardIdSystem); @Required void setFhirWardLocationPhysicalType(final String fhirWardLocationPhysicalType); @Required void setFhirLocationLocationPhysicalType(final String fhirLocationLocationPhysicalType); @Required void setFhirDoctorParticipantType(final String fhirDoctorParticipantType); @Override EncounterDto getPatientLatestEncounter(@Nonnull final String patientId); @Override List<EncounterDto> getPatientsActiveEncounters(@Nonnull final Collection<String> patientsIds);  }
@Test public void testGetPatientsActiveEncounters() { final Bundle result = new Bundle(); final Bundle.Entry entry1 = getEncounterResultBundleEntry( "encounter1", "patient1", EncounterClassEnum.OUTPATIENT, EncounterStateEnum.FINISHED, new DateTime(2016, 10, 1, 12, 0), new DateTime(2016, 10, 2, 13, 0), "ward1", "Test Ward", "Test Location", "Test Doctor"); result.addEntry(entry1); final Bundle.Entry entry2 = getEncounterResultBundleEntry( "encounter2", "patient2", EncounterClassEnum.INPATIENT, EncounterStateEnum.ON_LEAVE, new DateTime(2016, 10, 1, 12, 0), null, null, null, null, null); result.addEntry(entry2); final Bundle.Entry entry3 = getEncounterResultBundleEntry( "encounter3", "patient2", EncounterClassEnum.INPATIENT, EncounterStateEnum.FINISHED, new DateTime(2016, 9, 9, 9, 0), null, null, null, null, null); result.addEntry(entry3); mockFhirClientResult(result); final List<EncounterDto> encounterDtos = fhirEncounterProvider.getPatientsActiveEncounters(Lists.newArrayList("patient1", "patient2")); final EncounterDto encounterDto1 = encounterDtos.get(0); assertEquals("encounter1", encounterDto1.getId()); assertEquals(EncounterType.OUTPATIENT, encounterDto1.getType()); assertEquals(EncounterStatus.FINISHED, encounterDto1.getStatus()); assertEquals("Test Doctor", encounterDto1.getDoctor()); assertEquals("Test Location", encounterDto1.getLocation()); assertEquals("ward1", encounterDto1.getWard().getId()); assertEquals("Test Ward", encounterDto1.getWard().getName()); assertEquals(new DateTime(2016, 10, 1, 12, 0), encounterDto1.getStart()); assertEquals(new DateTime(2016, 10, 2, 13, 0), encounterDto1.getEnd()); final EncounterDto encounterDto2 = encounterDtos.get(1); assertEquals("encounter2", encounterDto2.getId()); assertEquals(EncounterType.INPATIENT, encounterDto2.getType()); assertEquals(EncounterStatus.ON_LEAVE, encounterDto2.getStatus()); assertNull(encounterDto2.getDoctor()); assertNull(encounterDto2.getLocation()); assertNull(encounterDto2.getWard()); assertEquals(new DateTime(2016, 10, 1, 12, 0), encounterDto2.getStart()); assertNull(encounterDto2.getEnd()); }
@Test public void testGetPatientsActiveEncountersNoPatientId() { expectedException.expect(SystemException.class); expectedException.expectMessage( "FHIR validation failed - No identifier for Patient with system \"patientIdSystem1\" defined"); final Bundle result = new Bundle(); final Bundle.Entry entry1 = getEncounterResultBundleEntry( "encounter1", null, EncounterClassEnum.OUTPATIENT, EncounterStateEnum.FINISHED, new DateTime(2016, 10, 1, 12, 0), new DateTime(2016, 10, 2, 13, 0), "ward1", "Test Ward", "Test Location", "Test Doctor"); result.addEntry(entry1); mockFhirClientResult(result); fhirEncounterProvider.getPatientsActiveEncounters(Lists.newArrayList("patient1")); }