HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public Set<Long> findSimilarMedicationsIds( final long medicationId, @Nonnull final Collection<Long> routeIds, @Nonnull final DateTime when) { Preconditions.checkNotNull(medicationId, "routeIds is required"); Preconditions.checkNotNull(medicationId, "when is required"); final Alias.Effective<Medication> referenceMedication = Alias.forEffectiveEntity(Medication.class); final Alias.Effective<MedicationVersion> referenceMedicationVersion = Alias.forEffectiveEntity(MedicationVersion.class); final Alias.Effective<MedicationGeneric> referenceGeneric = Alias.forEffectiveEntity(MedicationGeneric.class); final Alias.Effective<MedicationRoute> childRoute = Alias.forEffectiveEntity(MedicationRoute.class); final Alias.Effective<MedicationRouteRelation> medicationRouteChildRelation = Alias.forEffectiveEntity(MedicationRouteRelation.class); final Alias.Effective<AtcClassification> referenceAtcClassification = Alias.forEffectiveEntity(AtcClassification.class); final Alias.Effective<AtcClassification> referenceTopAtcClassification = Alias.forEffectiveEntity(AtcClassification.class); final Alias.Effective<AtcClassification> topAtcClassification = Alias.forEffectiveEntity(AtcClassification.class); return getHibernateTemplate().execute( requestSession -> { final List<Long> results = new Hql() .select( medication.id() ) .from( referenceMedication.innerJoin("versions").as(referenceMedicationVersion), referenceMedicationVersion.leftOuterJoin("medicationGeneric").as(referenceGeneric), referenceMedicationVersion.leftOuterJoin("atcClassification").as(referenceAtcClassification), referenceAtcClassification.leftOuterJoin("topParent").as(referenceTopAtcClassification), medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("medicationGeneric").as(medicationGeneric), medicationVersion.innerJoin("atcClassification").as(atcClassification), atcClassification.innerJoin("topParent").as(topAtcClassification), medication.innerJoin("routes").as(medicationRouteLink), medicationRouteLink.innerJoin("route").as(medicationRoute), medicationRoute.leftOuterJoin("childRelations").as(medicationRouteChildRelation) .with(medicationRouteChildRelation.notDeletedAndEffectiveAt(when)), medicationRouteChildRelation.leftOuterJoin("childRoute").as(childRoute) .with(childRoute.notDeletedAndEffectiveAt(when)) ) .where( or( medication.id().eq(medicationId), and( referenceMedication.id().eq(medicationId), medicationGeneric.id().eq(referenceGeneric.id()), topAtcClassification.id().eq(referenceTopAtcClassification.id()), or( CollectionUtils.isEmpty(routeIds), medicationRoute.id().in(routeIds), childRoute.id().in(routeIds)) )), effectiveEntities(medicationVersion, referenceMedicationVersion).notDeletedAndEffectiveAt(when) ) .orderBy(medicationVersion.get("name")) .buildQuery(requestSession, Long.class) .list(); return new HashSet<>(results); }); }  @Required void setCatalogDao(final CatalogDao catalogDao); @Required void setDoseFormDtoMapper(final DoseFormDtoMapper doseFormDtoMapper); @Required void setMedicationsBo(final MedicationsBo medicationsBo); @Required void setTherapyDisplayProvider(final TherapyDisplayProvider therapyDisplayProvider); @Override Map<Long, MedicationHolderDto> loadMedicationsMap(final DateTime when); @Override MedicationDataDto getMedicationData(
      final long medicationId,
      final String careProviderId,
      @Nonnull final DateTime when); @Override Map<Long, MedicationDataDto> getMedicationDataMap(
      @Nonnull final Set<Long> medicationIds,
      final String careProviderId,
      @Nonnull final DateTime when); @Override List<Long> getMedicationIdsWithIngredientId(final long ingredientId, final DateTime when); @Override Set<Long> getMedicationIdsWithIngredientRule(
      final MedicationRuleEnum medicationRuleEnum,
      final DateTime when); @Override List<MedicationRouteDto> getMedicationRoutes(final long medicationId, final DateTime when); @Override String getMedicationExternalId(final String externalSystem, final long medicationId, final DateTime when); @Override Set<Long> findSimilarMedicationsIds(
      final long medicationId,
      @Nonnull final Collection<Long> routeIds,
      @Nonnull final DateTime when); @Override boolean isProductBasedMedication(final long medicationId); @Override List<MedicationSupplyCandidateDto> getMedicationSupplyCandidates(
      final long medicationId,
      final long routeId,
      final DateTime when); @Override List<MedicationDto> getMedicationChildProducts(
      final long medicationId,
      @Nonnull final Collection<Long> routeIds,
      @Nonnull final DateTime when); @Override MedicationDto getMedicationById(final Long medicationId, final DateTime when); @Override Map<Long, MedicationDto> getMedicationsMap(final Set<Long> medicationIds, final DateTime when); @Override DoseFormDto getDoseFormByCode(final String doseFormCode, final DateTime when); @Override Map<String, String> getMedicationExternalValues(
      final String externalSystem, final MedicationsExternalValueType valueType, final Set<String> valuesSet); @Override List<MedicationRouteDto> getRoutes(final DateTime when); @Override List<DoseFormDto> getDoseForms(final DateTime when); @Override List<String> getMedicationBasicUnits(); @Override Map<Long, Pair<String, Integer>> getCustomGroupNameSortOrderMap(
      final String careProviderId, final Set<Long> medicationIds); @Override List<String> getCustomGroupNames(final String careProviderId); @Override TherapyTemplatesDto getTherapyTemplates(
      @Nonnull final String patientId,
      @Nonnull final String userId,
      @Nonnull final TherapyTemplateModeEnum templateMode,
      final String careProviderId,
      final Double referenceWeight,
      final Double patientHeight,
      @Nonnull final Locale locale); @Override long saveTherapyTemplate(
      final TherapyTemplateDto templateDto,
      final TherapyTemplateModeEnum templateMode,
      final String userId); @Override void deleteTherapyTemplate(final long templateId); @Override Map<Long, MedicationLevelEnum> getMedicationsLevels(final Set<Long> medicationIds); @Override String getPatientLastLinkName(final long patientId); @Override void savePatientLastLinkName(final long patientId, final String lastLinkName); @Override Map<ActionReasonType, List<CodedNameDto>> getActionReasons(
      @Nonnull final DateTime when,
      final ActionReasonType type); @Override List<MentalHealthTemplateDto> getMentalHealthTemplates(); @Override List<MentalHealthTemplateMemberDto> getMentalHealthTemplateMembers(@Nonnull final Collection<Long> mentalHealthTemplateIds); @Override Map<Long, MedicationRouteDto> loadRoutesMap(final DateTime when); @Override Collection<MedicationsWarningDto> getCustomWarningsForMedication(
      @Nonnull final Set<Long> medicationIds,
      @Nonnull final DateTime when); @Override Long getMedicationIdForBarcode(@Nonnull final String barcode);  }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testFindSimilarMedications.xml") public void testFindSimilarMedications() { final List<Long> routeIds = Collections.singletonList(11L); final Set<Long> medicationIds = medicationsDao.findSimilarMedicationsIds( 1L, routeIds, new DateTime(2013, 1, 1, 10, 15)); assertEquals(4L, (long)medicationIds.size()); assertTrue(medicationIds.contains(1L)); assertTrue(medicationIds.contains(2L)); assertTrue(medicationIds.contains(4L)); assertTrue(medicationIds.contains(7L)); }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testFindSimilarMedications.xml") public void testFindSimilarMedicationsMultipleRoutes() { final List<Long> routeIds = new ArrayList<>(); routeIds.add(11L); routeIds.add(12L); routeIds.add(2L); final Set<Long> medicationIds = medicationsDao.findSimilarMedicationsIds( 4L, routeIds, new DateTime(2013, 1, 1, 10, 15)); assertEquals(5L, (long)medicationIds.size()); assertTrue(medicationIds.contains(1L)); assertTrue(medicationIds.contains(2L)); assertTrue(medicationIds.contains(4L)); assertTrue(medicationIds.contains(5L)); assertTrue(medicationIds.contains(7L)); }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testFindSimilarMedications.xml") public void testFindSimilarMedicationsMultipleRoutesNoSimilar() { final List<Long> routeIds = new ArrayList<>(); routeIds.add(12L); routeIds.add(13L); final Set<Long> medicationIds = medicationsDao.findSimilarMedicationsIds( 3L, routeIds, new DateTime(2013, 1, 1, 10, 15)); assertEquals(1L, medicationIds.size()); assertTrue(medicationIds.contains(3L)); }