DefaultMedicationsBo implements MedicationsBo, MedicationFromEhrConverter.MedicationDataProvider { DocumentationTherapiesDto getTherapiesForDocumentation( final String patientId, final String centralCaseId, final Interval centralCaseEffective, final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> instructionPairs, final Map<Long, MedicationDataForTherapyDto> medicationsDataMap, final boolean isOutpatient, final DateTime when, final Locale locale) { final List<TherapyDocumentationData> therapies = new ArrayList<>(); final List<TherapyDocumentationData> dischargeTherapies = new ArrayList<>(); final List<TherapyDocumentationData> admissionTherapies = new ArrayList<>(); final List<TherapyDto> taggedTherapiesForPrescription = new ArrayList<>(); if (!isOutpatient) { final List<MedicationOnDischargeComposition> dischargeCompositions = medicationsOpenEhrDao.findMedicationOnDischargeCompositions(patientId, centralCaseEffective); for (final MedicationOnDischargeComposition dischargeComposition : dischargeCompositions) { final TherapyDto convertedTherapy = convertInstructionToTherapyDtoWithDisplayValues( dischargeComposition, dischargeComposition.getMedicationDetail().getMedicationInstruction().get(0), null, null, when, true, locale); taggedTherapiesForPrescription.add(convertedTherapy); } } final Set<TherapyDocumentationData> alreadyHandled = new HashSet<>(); final boolean isOutpatientOrLastsOneDay = isOutpatient || Intervals.durationInDays(centralCaseEffective) <= 1; for (final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair : instructionPairs) { final DateTime therapyStart = DataValueUtils.getDateTime(instructionPair.getSecond().getOrder().get(0).getMedicationTiming().getStartDate()); final DateTime therapyEnd = DataValueUtils.getDateTime(instructionPair.getSecond().getOrder().get(0).getMedicationTiming().getStopDate()); final TherapyDto convertedTherapy = convertInstructionToTherapyDtoWithDisplayValues( instructionPair.getFirst(), instructionPair.getSecond(), null, null, when, true, locale); if (!areAllIngredientsSolutions(convertedTherapy)) { final Interval therapyInterval = new Interval(therapyStart, therapyEnd != null ? therapyEnd : Intervals.INFINITE.getEnd()); final boolean handled = handleSimilarAndLinkedTherapies( admissionTherapies, dischargeTherapies, therapies, alreadyHandled, instructionPair, convertedTherapy, medicationsDataMap, therapyInterval, centralCaseEffective.getEnd(), when); final TherapyDocumentationData therapy = createTherapyData(instructionPair, convertedTherapy, therapyInterval); alreadyHandled.add(therapy); if (!handled) { if (isOutpatientOrLastsOneDay) { if (therapyInterval.overlaps(Intervals.wholeDay(centralCaseEffective.getStart()))) { therapies.add(therapy); } } else { boolean isAdmission = false; if (therapyInterval.overlaps(Intervals.wholeDay(centralCaseEffective.getStart()))) { admissionTherapies.add(therapy); isAdmission = true; } boolean isDischarge = false; if (isDischargeTherapy(therapyInterval, centralCaseEffective.getEnd(), when)) { dischargeTherapies.add(therapy); isDischarge = true; } if (!isAdmission && !isDischarge) { if (therapyInterval.overlaps(centralCaseEffective)) { therapies.add(therapy); } } } } } } return new DocumentationTherapiesDto( getTherapyDisplayValuesForDocumentation(therapies, locale), getTherapyDisplayValuesForDocumentation(dischargeTherapies, locale), getTherapyDisplayValuesForDocumentation(admissionTherapies, locale), getTherapyDisplayValues(taggedTherapiesForPrescription, locale)); }  @Autowired void setMedicationsOpenEhrDao(final MedicationsOpenEhrDao medicationsOpenEhrDao); @Autowired void setMedicationsDao(final MedicationsDao medicationsDao); @Autowired void setTaggingOpenEhrDao(final TaggingOpenEhrDao taggingOpenEhrDao); @Autowired void setTherapyDisplayProvider(final TherapyDisplayProvider therapyDisplayProvider); @Autowired void setMedicationsValueHolder(final ValueHolder<Map<Long, MedicationHolderDto>> medicationsValueHolder); @Autowired void setMedicationRoutesValueHolder(final ValueHolder<Map<Long, MedicationRouteDto>> medicationRoutesValueHolder); @Autowired void setMedicationHolderDtoMapper(final MedicationHolderDtoMapper medicationHolderDtoMapper); @Autowired void setMedicationsTasksProvider(final MedicationsTasksProvider medicationsTasksProvider); @Autowired void setMedicationOnAdmissionHandler(final MedicationOnAdmissionHandler medicationOnAdmissionHandler); @Autowired void setAdministrationProvider(final AdministrationProvider administrationProvider); @Override Map<Long, MedicationDataForTherapyDto> getMedicationDataForTherapies(
      final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> instructionsList,
      @Nullable final String careProviderId); @Override boolean isTherapyModifiedFromLastReview(
      @Nonnull final MedicationInstructionInstruction instruction,
      @Nonnull final List<MedicationActionAction> actionsList,
      @Nonnull final DateTime compositionCreatedTime); @Override int getTherapyConsecutiveDay(
      final DateTime therapyStart,
      final DateTime therapyDay,
      final DateTime currentTime,
      final Integer pastDaysOfTherapy); @Override DateTime getOriginalTherapyStart(
      @Nonnull final String patientId,
      @Nonnull final MedicationOrderComposition composition); @Override Pair<MedicationOrderComposition, MedicationInstructionInstruction> getInstructionFromLink(
      @Nonnull final String patientId,
      @Nonnull final MedicationInstructionInstruction instruction,
      @Nonnull final EhrLinkType linkType,
      final boolean getLatestVersion); @Override Pair<MedicationOrderComposition, MedicationInstructionInstruction> getInstructionFromLink(
      final String patientId,
      final Link link,
      final boolean getLatestVersion); @Override boolean isTherapySuspended(final List<MedicationActionAction> actionsList); @Override void sortTherapyTemplates(final TherapyTemplatesDto templates); @Override int compareTherapiesForSort(final TherapyDto firstTherapy, final TherapyDto secondTherapy, final Collator collator); @Override boolean areInstructionsLinkedByUpdate(
      final Pair<MedicationOrderComposition, MedicationInstructionInstruction> instructionPair,
      final Pair<MedicationOrderComposition, MedicationInstructionInstruction> compareInstructionPair); @Override boolean doesInstructionHaveLinkToCompareInstruction(
      final MedicationInstructionInstruction instruction,
      final Pair<MedicationOrderComposition, MedicationInstructionInstruction> compareInstructionPair,
      final EhrLinkType linkType); @Override List<Long> getMedicationIds(final OrderActivity orderActivity); @Override List<Long> getMedicationIds(final MedicationActionAction medicationAction); @Override Long getMainMedicationId(final OrderActivity orderActivity); @Override boolean isMentalHealthMedication(final long medicationId); @Override boolean isTherapyActive(
      final List<String> daysOfWeek,
      final Integer dosingDaysFrequency,
      final Interval therapyInterval,
      final DateTime when); @Override boolean isTherapySuspended(final MedicationOrderComposition composition, final MedicationInstructionInstruction instruction); @Override TherapyChangeReasonDto getTherapySuspendReason(
      @Nonnull final MedicationOrderComposition composition,
      @Nonnull final MedicationInstructionInstruction instruction); @Override boolean isTherapyCancelledOrAborted(
      final MedicationOrderComposition composition, final MedicationInstructionInstruction instruction); @Override boolean isMedicationTherapyCompleted(
      final MedicationOrderComposition composition,
      final MedicationInstructionInstruction instruction); @Override MedicationActionAction getInstructionAction(
      final MedicationOrderComposition composition,
      final MedicationInstructionInstruction instruction,
      final MedicationActionEnum searchActionEnum,
      @Nullable final Interval searchInterval); @Override String getTherapyFormattedDisplay(
      final String patientId, final String therapyId, final DateTime when, final Locale locale); @Override TherapyDto getTherapy(
      final String patientId, final String compositionId, final String ehrOrderName, final DateTime when, final Locale locale); @Override TherapyDto convertInstructionToTherapyDto(
      @Nonnull final IspekComposition composition,
      @Nonnull final MedicationInstructionInstruction instruction,
      final DateTime currentTime); @Override TherapyDto convertInstructionToTherapyDtoWithDisplayValues(
      @Nonnull final IspekComposition composition,
      @Nonnull final MedicationInstructionInstruction instruction,
      final Double referenceWeight,
      final Double patientHeight,
      final DateTime currentTime,
      final boolean isToday,
      final Locale locale); @Override void fillDisplayValues(
      @Nonnull final TherapyDto therapy,
      final Double referenceWeight,
      final Double patientHeight,
      final boolean isToday,
      final Locale locale); @Override void fillInfusionFormulaFromRate(
      final ComplexTherapyDto therapy,
      final Double referenceWeight,
      final Double patientHeight); @Override void fillInfusionRateFromFormula(
      final ComplexTherapyDto therapy, final Double referenceWeight, final Double patientHeight); @Override double calculateBodySurfaceArea(final double heightInCm, final double weightInKg); @Override List<MedicationForWarningsSearchDto> getTherapiesForWarningsSearch(final String patientId, final DateTime when); @Override List<TherapyDto> getTherapies(
      final String patientId,
      final String centralCaseId,
      final Double referenceWeight,
      @Nullable final Locale locale,
      final DateTime when); @Override List<TherapyDto> getTherapies(
      final String patientId,
      final Interval searchInterval,
      final Double referenceWeight,
      final Double patientHeight,
      @Nullable final Locale locale,
      final DateTime when); @Override List<MedicationOnDischargeGroupDto> getMedicationOnDischargeGroups(
      final String patientId,
      final DateTime lastHospitalizationStart,
      @Nullable final Interval searchInterval,
      final Double referenceWeight,
      final Double patientHeight, // is this @Nullable ?
      @Nullable final Locale locale,
      final DateTime when); @Override List<MentalHealthTherapyDto> getMentalHealthTherapies(
      final String patientId,
      final Interval searchInterval,
      final DateTime when,
      final Locale locale); @Override List<TherapyDto> getLinkTherapyCandidates(
      @Nonnull final String patientId,
      @Nonnull final Double referenceWeight,
      final Double patientHeight,
      @Nonnull final DateTime when,
      @Nonnull final Locale locale); @Override List<TherapySurgeryReportElementDto> getTherapySurgeryReportElements(
      @Nonnull final String patientId,
      final Double patientHeight,
      @Nonnull final DateTime searchStart,
      @Nonnull final RoundsIntervalDto roundsIntervalDto,
      @Nonnull final Locale locale,
      @Nonnull final DateTime when); @Override List<MedicationForWarningsSearchDto> extractWarningsSearchDtos(
      @Nonnull final List<MedicationOrderComposition> compositions); @Override MedicationDto getMedication(final Long medicationId); @Override DoseFormDto getDoseForm(final String doseFormCode, final DateTime when); @Override DoseFormDto getMedicationDoseForm(final long medicationId); @Override MedicationRouteDto getMedicationRoute(final long routeId); @Override String getOriginalTherapyId(final String patientId, final String compositionUid); @Override String getOriginalTherapyId(@Nonnull final MedicationOrderComposition composition); @Override MedicationReferenceWeightComposition buildReferenceWeightComposition(
      final double weight,
      final DateTime when); @Override void sortTherapiesByMedicationTimingStart(
      final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> therapies, final boolean descending); @Override void deleteAdministration(final String patientId, final String compositionId, final String comment); @Override DocumentationTherapiesDto findTherapyGroupsForDocumentation(
      final String patientId,
      final String centralCaseId,
      final Interval centralCaseEffective,
      final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> instructionPairs,
      final boolean isOutpatient,
      final DateTime when,
      final Locale locale); @Override @Transactional @ServiceMethod(auditing = @Auditing(level = Level.FULL)) DateTime findPreviousTaskForTherapy(
      final String patientId,
      final String compositionUid,
      final String ehrOrderName,
      final DateTime when);  }
@Test public void testGetTherapiesForDocumentation() { final DateTime when = new DateTime(2014, 6, 16, 12, 0, 0); final MedicationDto medicationDto1 = new MedicationDto(); medicationDto1.setId(1L); medicationDto1.setName("Lekadol 20x500mg"); final HibernateMedicationsDao medicationsDaoMock = mock(HibernateMedicationsDao.class); medicationsBo.setMedicationsDao(medicationsDaoMock); final MedicationRouteDto route = new MedicationRouteDto(); route.setName("route1"); route.setId(1L); final MedicationRouteDto route2 = new MedicationRouteDto(); route2.setName("route2"); route2.setId(2L); final Map<Long, MedicationRouteDto> routes = new HashMap<>(); routes.put(2L, route2); routes.put(1L, route); Mockito.when(medicationRoutesValueHolder.getValue()).thenReturn(routes); final Map<Long, MedicationDataForTherapyDto> medicationsDataMap = new HashMap<>(); final MedicationDataForTherapyDto medicationData1 = new MedicationDataForTherapyDto(); medicationData1.setGenericName("Paracetamol"); medicationData1.setCustomGroupName("Group 1"); medicationData1.setAtcGroupCode("ATC 1"); medicationsDataMap.put(1L, medicationData1); final MedicationOrderComposition composition1 = MedicationsTestUtils.buildTestMedicationOrderComposition("uid1::1", new DateTime(2013, 11, 11, 0, 0, 0), "aaa"); final MedicationInstructionInstruction instruction1 = MedicationsTestUtils.buildTestMedicationInstruction("MedicationInstructionInstruction 1"); composition1.getMedicationDetail().getMedicationInstruction().add(instruction1); final OrderActivity orderActivity1 = MedicationsTestUtils.buildTestOrderActivity( "Lekadol", 1L, 500.0, "mg", null, null, null, null, 1L, "ORAL", "1", "TBL"); instruction1.getOrder().add(orderActivity1); final MedicationTimingCluster medicationTiming1 = MedicationsTestUtils.buildMedicationTimingCluster( 2L, null, null, null, new DateTime(2014, 6, 9, 13, 0, 0), new DateTime(2014, 6, 11, 13, 0, 0), null); orderActivity1.setMedicationTiming(medicationTiming1); final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> instructionPairs1 = new ArrayList<>(); instructionPairs1.add(Pair.of(composition1, instruction1)); final DocumentationTherapiesDto groups1 = medicationsBo.getTherapiesForDocumentation( null, null, Intervals.infiniteFrom(new DateTime(2014, 6, 9, 12, 0, 0)), instructionPairs1, medicationsDataMap, false, when, new Locale("en")); assertEquals(1L, groups1.getAdmissionTherapies().size()); final MedicationOrderComposition composition2 = MedicationsTestUtils.buildTestMedicationOrderComposition("uid1::1", new DateTime(2013, 11, 11, 0, 0, 0), "aaa"); final MedicationInstructionInstruction instruction2 = MedicationsTestUtils.buildTestMedicationInstruction("MedicationInstructionInstruction 1"); composition2.getMedicationDetail().getMedicationInstruction().add(instruction2); final OrderActivity orderActivity2 = MedicationsTestUtils.buildTestOrderActivity( "Lekadol", 1L, 1000.0, "mg", null, null, null, null, 1L, "ORAL", "1", "TBL"); instruction2.getOrder().add(orderActivity2); final MedicationTimingCluster medicationTiming2 = MedicationsTestUtils.buildMedicationTimingCluster( 2L, null, null, null, new DateTime(2014, 6, 11, 13, 0, 0), new DateTime(2014, 6, 12, 13, 0, 0), null); orderActivity2.setMedicationTiming(medicationTiming2); instructionPairs1.add(Pair.of(composition2, instruction2)); final DocumentationTherapiesDto groups2 = medicationsBo.getTherapiesForDocumentation( null, null, Intervals.infiniteFrom(new DateTime(2014, 6, 9, 12, 0, 0)), instructionPairs1, medicationsDataMap, false, when, new Locale("en")); assertEquals(1L, groups2.getAdmissionTherapies().size()); final MedicationOrderComposition composition3 = MedicationsTestUtils.buildTestMedicationOrderComposition("uid1::1", new DateTime(2013, 11, 11, 0, 0, 0), "aaa"); final MedicationInstructionInstruction instruction3 = MedicationsTestUtils.buildTestMedicationInstruction("MedicationInstructionInstruction 1"); composition3.getMedicationDetail().getMedicationInstruction().add(instruction3); final OrderActivity orderActivity3 = MedicationsTestUtils.buildTestOrderActivity( "Lekadol", 1L, 500.0, "mg", null, null, null, null, 1L, "ORAL", "1", "TBL"); instruction3.getOrder().add(orderActivity3); final MedicationTimingCluster medicationTiming3 = MedicationsTestUtils.buildMedicationTimingCluster( 2L, null, null, null, new DateTime(2014, 6, 12, 10, 0, 0), null, null); orderActivity3.setMedicationTiming(medicationTiming3); final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> instructionPairs2 = new ArrayList<>(); instructionPairs2.add(Pair.of(composition2, instruction2)); instructionPairs2.add(Pair.of(composition3, instruction3)); final DocumentationTherapiesDto groups3 = medicationsBo.getTherapiesForDocumentation( null, null, Intervals.infiniteFrom(new DateTime(2014, 6, 9, 12, 0, 0)), instructionPairs2, medicationsDataMap, false, when, new Locale("en")); assertEquals(1L, groups3.getDischargeTherapies().size()); assertEquals(0L, groups3.getAdmissionTherapies().size()); final DocumentationTherapiesDto groups4 = medicationsBo.getTherapiesForDocumentation( null, null, new Interval(new DateTime(2014, 6, 9, 12, 0, 0), new DateTime(2014, 6, 18, 12, 0, 0)), instructionPairs2, medicationsDataMap, false, when, new Locale("en")); assertEquals(1L, groups4.getDischargeTherapies().size()); assertEquals(0L, groups4.getTherapies().size()); final MedicationOrderComposition composition4 = MedicationsTestUtils.buildTestMedicationOrderComposition("uid1::1", new DateTime(2013, 11, 11, 0, 0, 0), "aaa"); final MedicationInstructionInstruction instruction4 = MedicationsTestUtils.buildTestMedicationInstruction("MedicationInstructionInstruction 1"); composition4.getMedicationDetail().getMedicationInstruction().add(instruction4); final OrderActivity orderActivity4 = MedicationsTestUtils.buildTestOrderActivity( "Lekadol", 1L, 500.0, "mg", null, null, null, null, 1L, "ORAL", "1", "TBL"); instruction4.getOrder().add(orderActivity4); final MedicationTimingCluster medicationTiming4 = MedicationsTestUtils.buildMedicationTimingCluster( 2L, null, null, null, new DateTime(2014, 6, 12, 10, 0, 0), new DateTime(2014, 6, 15, 10, 0, 0), null); orderActivity4.setMedicationTiming(medicationTiming4); final List<Pair<MedicationOrderComposition, MedicationInstructionInstruction>> instructionPairs4 = new ArrayList<>(); final Link linkToInstruction11 = OpenEhrRefUtils.getLinkToTdoTarget("update", EhrLinkType.UPDATE.getName(), composition2, instruction2); instruction2.getLinks().add(linkToInstruction11); final Link linkToInstruction22 = OpenEhrRefUtils.getLinkToTdoTarget("update", EhrLinkType.UPDATE.getName(), composition4, instruction4); instruction3.getLinks().add(linkToInstruction22); instructionPairs4.add(Pair.of(composition2, instruction2)); instructionPairs4.add(Pair.of(composition4, instruction4)); final DocumentationTherapiesDto groups5 = medicationsBo.getTherapiesForDocumentation( null, null, new Interval(new DateTime(2014, 6, 9, 12, 0, 0), new DateTime(2014, 6, 18, 12, 0, 0)), instructionPairs4, medicationsDataMap, false, when, new Locale("en")); assertEquals(1L, groups5.getTherapies().size()); }