HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public List<MedicationDto> getMedicationChildProducts( 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 Medication medicationEntity = getHibernateTemplate().load(MedicationImpl.class, medicationId); final MedicationLevelEnum medicationLevel = medicationEntity.getMedicationLevel(); return getHibernateTemplate().execute( requestSession -> { final Hql hql = new Hql() .select( medication.id(), medicationVersion.get("name"), medicationVersion.get("shortName"), medicationGeneric.get("name") ) .from( medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("medicationGeneric").as(medicationGeneric), medication.innerJoin("routes").as(medicationRouteLink), medicationRouteLink.innerJoin("route").as(medicationRoute) ) .where( medication.get("medicationLevel").eq(MedicationLevelEnum.AMP), or( CollectionUtils.isEmpty(routeIds), medicationRoute.id().in(routeIds) ), effectiveEntities(medicationVersion, medicationRouteLink).notDeletedAndEffectiveAt(when) ).orderBy(medicationVersion.get("name")); if (medicationLevel == MedicationLevelEnum.VTM) { hql.where(medication.get("vtmId").eq(medicationEntity.getVtmId())); } else if (medicationLevel == MedicationLevelEnum.VMP) { hql.where(medication.get("vmpId").eq(medicationEntity.getVmpId())); } else if (medicationLevel == MedicationLevelEnum.AMP) { hql.where(medication.get("ampId").eq(medicationEntity.getAmpId())); } else { throw new IllegalArgumentException("Medication level " + medicationLevel + " not yet supported."); } return hql .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<MedicationDto>() { @Override protected MedicationDto process(final boolean hasNextTuple) throws ProcessingException { final MedicationDto dto = new MedicationDto(); dto.setId(nextLong()); dto.setName(nextString()); dto.setShortName(nextString()); dto.setGenericName(nextString()); return dto; } }); }); }  @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.testMedicationsTree.xml") public void testGetMedicationProducts1() { final List<Long> routeIds = Collections.singletonList(1L); final List<MedicationDto> medicationProducts = medicationsDao.getMedicationChildProducts(1L, routeIds, new DateTime(2013, 11, 1, 0, 0, 0)); assertEquals(2, medicationProducts.size()); assertEquals(112L, (long)medicationProducts.get(0).getId()); assertEquals(111L, (long)medicationProducts.get(1).getId()); }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testMedicationsTree.xml") public void testGetMedicationProducts2() { final List<Long> routeIds = Collections.singletonList(2L); final List<MedicationDto> medicationProducts = medicationsDao.getMedicationChildProducts(1L, routeIds, new DateTime(2013, 11, 1, 0, 0, 0)); assertEquals(1, medicationProducts.size()); assertEquals(121L, (long)medicationProducts.get(0).getId()); }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testMedicationsTree.xml") public void testGetMedicationProducts3() { final List<Long> routeIds = Collections.singletonList(1L); final List<MedicationDto> medicationProducts = medicationsDao.getMedicationChildProducts(11L, routeIds, new DateTime(2013, 11, 1, 0, 0, 0)); assertEquals(2, medicationProducts.size()); assertEquals(112L, (long)medicationProducts.get(0).getId()); assertEquals(111L, (long)medicationProducts.get(1).getId()); }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testMedicationsTree.xml") public void testGetMedicationProducts4() { final List<Long> routeIds = Collections.singletonList(2L); final List<MedicationDto> medicationProducts = medicationsDao.getMedicationChildProducts(12L, routeIds, new DateTime(2013, 11, 1, 0, 0, 0)); assertEquals(1, medicationProducts.size()); assertEquals(121L, (long)medicationProducts.get(0).getId()); }