@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()); }
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); } }
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); } } }
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); } }  }
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); }
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);  }