BarcodeTaskFinder { public BarcodeTaskSearchDto getAdministrationTaskForBarcode( @Nonnull final String patientId, @Nonnull final String medicationBarcode, @Nonnull final DateTime when) { StringUtils.checkNotBlank(patientId, "patientId"); StringUtils.checkNotBlank(medicationBarcode, "barcode"); Preconditions.checkNotNull(when, "when"); final Long barcodeMedicationId = medicationsDao.getMedicationIdForBarcode(medicationBarcode); String barcodeTherapyId = null; if (barcodeMedicationId == null) { final boolean validTherapyId = TherapyIdUtils.isValidTherapyId(medicationBarcode); if (validTherapyId) { barcodeTherapyId = medicationBarcode; } else { return new BarcodeTaskSearchDto(BarcodeSearchResult.NO_MEDICATION); } } final List<TaskDto> dueTasks = medicationsTasksProvider.findAdministrationTasks( Collections.singleton(patientId), when.minusHours(1), when.plusHours(1)); final List<TaskDto> dueStartTasks = dueTasks.stream() .filter(t -> AdministrationTypeEnum.START.name().equals( t.getVariables().get(AdministrationTaskDef.ADMINISTRATION_TYPE.getName()))) .collect(Collectors.toList()); if (dueStartTasks.isEmpty()) { return new BarcodeTaskSearchDto(BarcodeSearchResult.NO_TASK); } final MultiValueMap<String, String> therapyIdTasksIdMap = buildTherapyIdTaskIdMap(dueStartTasks); final Set<String> matchingTherapyIds; if (barcodeMedicationId != null) { matchingTherapyIds = getTherapiesWithMatchingMedication( barcodeMedicationId, therapyIdTasksIdMap.keySet(), when); } else { matchingTherapyIds = getTherapiesWithMatchingOriginalTherapyIds(patientId, barcodeTherapyId, therapyIdTasksIdMap); } final Set<String> matchingTaskIds = matchingTherapyIds.stream() .flatMap(therapyId -> therapyIdTasksIdMap.get(therapyId).stream()) .collect(Collectors.toSet()); if (matchingTaskIds.size() == 1) { return new BarcodeTaskSearchDto( BarcodeSearchResult.TASK_FOUND, matchingTaskIds.iterator().next(), barcodeMedicationId); } else if (matchingTaskIds.size() > 1) { return new BarcodeTaskSearchDto(BarcodeSearchResult.MULTIPLE_TASKS); } else { return new BarcodeTaskSearchDto(BarcodeSearchResult.NO_TASK); } }  @Required void setMedicationsDao(final MedicationsDao medicationsDao); @Required void setMedicationsTasksProvider(final MedicationsTasksProvider medicationsTasksProvider); @Required void setMedicationsBo(final MedicationsBo medicationsBo); @Required void setMedicationsOpenEhrDao(final MedicationsOpenEhrDao medicationsOpenEhrDao); @Required void setMedicationsFinder(final MedicationsFinder medicationsFinder); BarcodeTaskSearchDto getAdministrationTaskForBarcode(
      @Nonnull final String patientId,
      @Nonnull final String medicationBarcode,
      @Nonnull final DateTime when);  }
@Test public void testGetAdministrationTaskForBarcodeNoMedication() { Mockito .when(medicationsDao.getMedicationIdForBarcode("222")) .thenReturn(null); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "222", testTime); assertEquals(BarcodeSearchResult.NO_MEDICATION, dto.getBarcodeSearchResult()); assertNull(dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeNoDueTasks() { Mockito.reset(medicationsDao); Mockito .when(medicationsDao.getMedicationIdForBarcode("222")) .thenReturn(222L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Collections.emptyList()); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "222", testTime); assertEquals(BarcodeSearchResult.NO_TASK, dto.getBarcodeSearchResult()); assertNull(dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeNoMatchingTherapy() { Mockito .when(medicationsDao.getMedicationIdForBarcode("222")) .thenReturn(222L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList(buildTaskDto("333", "uid1|Medication instruction"))); Mockito .when(medicationsOpenEhrDao.getTherapyInstructionPairs(Sets.newHashSet("uid1"))) .thenReturn(Lists.newArrayList(buildInstructionPair("uid1::1"))); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "222", testTime); assertEquals(BarcodeSearchResult.NO_TASK, dto.getBarcodeSearchResult()); assertNull(dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeDirectMedicationMatch() { Mockito .when(medicationsDao.getMedicationIdForBarcode("999")) .thenReturn(999L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList(buildTaskDto("333", "uid1|Medication instruction"))); final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair = buildInstructionPair( "uid1::1"); Mockito .when(medicationsOpenEhrDao.getTherapyInstructionPairs(Sets.newHashSet("uid1"))) .thenReturn(Lists.newArrayList(instructionPair)); Mockito .when(medicationsBo.getMedicationIds(instructionPair.getSecond().getOrder().get(0))) .thenReturn(Lists.newArrayList(999L)); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "999", testTime); assertEquals(BarcodeSearchResult.TASK_FOUND, dto.getBarcodeSearchResult()); assertEquals("333", dto.getTaskId()); assertEquals((Long)999L, dto.getMedicationId()); }
@Test public void testGetAdministrationTaskForBarcodeSimilarMedicationMatch() { Mockito .when(medicationsDao.getMedicationIdForBarcode("999")) .thenReturn(999L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList(buildTaskDto("333", "uid1|Medication instruction"))); final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair = buildInstructionPair( "uid1::1"); Mockito .when(medicationsOpenEhrDao.getTherapyInstructionPairs(Sets.newHashSet("uid1"))) .thenReturn(Lists.newArrayList(instructionPair)); Mockito .when(medicationsBo.getMedicationIds(instructionPair.getSecond().getOrder().get(0))) .thenReturn(Lists.newArrayList(888L)); Mockito .when(medicationsFinder.findMedicationProducts(888L, Lists.newArrayList(444L), testTime)) .thenReturn(Lists.newArrayList(buildMedicationDto(888L), buildMedicationDto(999L))); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "999", testTime); assertEquals(BarcodeSearchResult.TASK_FOUND, dto.getBarcodeSearchResult()); assertEquals("333", dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeNoSimilarMedicationMatch() { Mockito .when(medicationsDao.getMedicationIdForBarcode("999")) .thenReturn(999L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList(buildTaskDto("333", "uid1|Medication instruction"))); final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair = buildInstructionPair( "uid1::1"); Mockito .when(medicationsOpenEhrDao.getTherapyInstructionPairs(Sets.newHashSet("uid1"))) .thenReturn(Lists.newArrayList(instructionPair)); Mockito .when(medicationsBo.getMedicationIds(instructionPair.getSecond().getOrder().get(0))) .thenReturn(Lists.newArrayList(888L)); Mockito .when(medicationsFinder.findMedicationProducts(888L, Lists.newArrayList(444L), testTime)) .thenReturn(Lists.newArrayList(buildMedicationDto(888L), buildMedicationDto(777L))); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "999", testTime); assertEquals(BarcodeSearchResult.NO_TASK, dto.getBarcodeSearchResult()); assertNull(dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeMultipleTasksSingleTherapy() { Mockito .when(medicationsDao.getMedicationIdForBarcode("999")) .thenReturn(999L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList( buildTaskDto("333", "uid1|Medication instruction"), buildTaskDto("334", "uid1|Medication instruction"))); final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair = buildInstructionPair("uid1::1"); Mockito .when(medicationsOpenEhrDao.getTherapyInstructionPairs(Sets.newHashSet("uid1"))) .thenReturn(Lists.newArrayList(instructionPair)); Mockito .when(medicationsBo.getMedicationIds(instructionPair.getSecond().getOrder().get(0))) .thenReturn(Lists.newArrayList(888L)); Mockito .when(medicationsFinder.findMedicationProducts(888L, Lists.newArrayList(444L), testTime)) .thenReturn(Lists.newArrayList(buildMedicationDto(888L), buildMedicationDto(999L))); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "999", testTime); assertEquals(BarcodeSearchResult.MULTIPLE_TASKS, dto.getBarcodeSearchResult()); assertNull(dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeMultipleTasksMultipleTherapies() { Mockito .when(medicationsDao.getMedicationIdForBarcode("999")) .thenReturn(999L); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList( buildTaskDto("333", "uid1|Medication instruction"), buildTaskDto("334", "uid2|Medication instruction"))); final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair1 = buildInstructionPair( "uid1::1"); final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair2 = buildInstructionPair( "uid2::1"); Mockito .when(medicationsOpenEhrDao.getTherapyInstructionPairs(Sets.newHashSet("uid1", "uid2"))) .thenReturn(Lists.newArrayList(instructionPair1, instructionPair2)); Mockito .when(medicationsBo.getMedicationIds(instructionPair1.getSecond().getOrder().get(0))) .thenReturn(Lists.newArrayList(999L)); Mockito .when(medicationsBo.getMedicationIds(instructionPair2.getSecond().getOrder().get(0))) .thenReturn(Lists.newArrayList(888L)); Mockito .when(medicationsFinder.findMedicationProducts(888L, Lists.newArrayList(444L), testTime)) .thenReturn(Lists.newArrayList(buildMedicationDto(888L), buildMedicationDto(999L))); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode("1", "999", testTime); assertEquals(BarcodeSearchResult.MULTIPLE_TASKS, dto.getBarcodeSearchResult()); assertNull(dto.getTaskId()); }
@Test public void testGetAdministrationTaskForBarcodeByTherapyId() { Mockito .when(medicationsDao.getMedicationIdForBarcode("123e4567-e89b-12d3-a456-426655440001|Medication instruction")) .thenReturn(null); Mockito .when(medicationsBo.getOriginalTherapyId( "1", "123e4567-e89b-12d3-a456-426655440000")) .thenReturn("123e4567-e89b-12d3-a456-426655440001|Medication instruction"); Mockito .when(medicationsTasksProvider.findAdministrationTasks( Collections.singleton("1"), testTime.minusHours(1), testTime.plusHours(1))) .thenReturn(Lists.newArrayList(buildTaskDto("333", "123e4567-e89b-12d3-a456-426655440000|Medication instruction"))); final BarcodeTaskSearchDto dto = barcodeTaskFinder.getAdministrationTaskForBarcode( "1", "123e4567-e89b-12d3-a456-426655440001|Medication instruction", testTime); assertEquals(BarcodeSearchResult.TASK_FOUND, dto.getBarcodeSearchResult()); assertEquals("333", dto.getTaskId()); assertNull(dto.getMedicationId()); }