@Test @DatabaseSetup("HibernateMedicationsDaoTest.testLoadMedicationsMap.xml") public void testGetMedicationDataMap() { final Map<Long, MedicationHolderDto> medicationsMap = medicationsDao.loadMedicationsMap(new DateTime(2016, 6, 12, 0, 0, 0)); assertEquals(1, medicationsMap.size()); final MedicationHolderDto dto = medicationsMap.get(1L); assertEquals(1L, (long)dto.getId()); assertEquals("Lekadol", dto.getName()); assertEquals("Lek", dto.getShortName()); assertEquals("Paracetamol", dto.getGenericName()); assertEquals(MedicationLevelEnum.VTM, dto.getMedicationLevel()); assertEquals(1L, (long)dto.getVtmId()); assertNull(dto.getVmpId()); assertNull(dto.getAmpId()); assertEquals(1L, dto.getDoseFormDto().getId()); assertEquals("Pill", dto.getDoseFormDto().getName()); assertEquals("C1", dto.getAtcGroupCode()); assertEquals("ATC 1", dto.getAtcGroupName()); assertEquals(1L, dto.getDefiningIngredient().getId()); assertEquals("Paracetamol", dto.getDefiningIngredient().getIngredientName()); assertEquals(Double.valueOf(2.0), dto.getDefiningIngredient().getStrengthNumerator()); assertEquals("mg", dto.getDefiningIngredient().getStrengthNumeratorUnit()); assertEquals(MedicationTypeEnum.MEDICATION, dto.getMedicationType()); assertEquals(MedicationRuleEnum.PARACETAMOL_MAX_DAILY_DOSE, dto.getMedicationRules().iterator().next()); assertTrue(dto.getFormularyCareProviders().contains("CP1")); assertTrue(dto.getFormularyCareProviders().contains("CP2")); assertTrue(dto.isActive()); assertFalse(dto.isAntibiotic()); assertTrue(dto.isFormulary()); assertTrue(dto.isInpatientMedication()); assertTrue(dto.isOutpatientMedication()); assertFalse(dto.isSuggestSwitchToOral()); assertFalse(dto.isMentalHealthDrug()); assertTrue(dto.isOrderable()); assertFalse(dto.isReviewReminder()); }
@Override public Map<Long, MedicationDataDto> getMedicationDataMap( @Nonnull final Set<Long> medicationIds, final String careProviderId, @Nonnull final DateTime when) { Preconditions.checkNotNull(medicationIds, "medicationIds is required"); Preconditions.checkNotNull(when, "when is required"); if (medicationIds.isEmpty()) { return Collections.emptyMap(); } final Alias.Effective<MedicationRoute> childRoute = Alias.forEffectiveEntity(MedicationRoute.class); final Alias.Effective<MedicationRouteRelation> medicationRouteChildRelation = Alias.forEffectiveEntity(MedicationRouteRelation.class); final Map<Long, MedicationDataDto> medicationDataMap = new HashMap<>(); getHibernateTemplate().execute( (HibernateCallback<Void>)requestSession -> { new Hql() .select( medication.id(), medicationIngredient.get("name"), medicationIngredient.id(), medicationIngredient.get("rule"), medicationIngredientLink.id(), medicationIngredientLink.get("strengthNumerator"), medicationIngredientLink.get("strengthNumeratorUnit"), medicationIngredientLink.get("strengthDenominator"), medicationIngredientLink.get("strengthDenominatorUnit"), medicationIngredientLink.get("descriptive"), medicationRoute.id(), medicationRoute.get("code"), medicationRoute.get("shortName"), medicationRoute.get("type"), medicationRouteLink.get("unlicensed"), medicationRouteLink.get("discretionary"), medicationRouteLink.get("bnfMaximum"), medicationRouteLink.get("bnfMaximumUnitType"), childRoute.id(), childRoute.get("code"), childRoute.get("shortName"), childRoute.get("type"), medicationRouteLink.get("defaultRoute"), doseForm, medicationBasicUnit.get("name"), medicationType.id(), medicationVersion.get("reviewReminder"), medicationVersion.get("controlledDrug"), medicationVersion.get("mentalHealthDrug"), medicationVersion.get("titration"), medicationVersion.get("medicationPackaging"), medicationVersion.get("roundingFactor"), medicationVersion.get("clinicalTrialMedication"), medicationVersion.get("unlicensedMedication"), medicationVersion.get("highAlertMedication"), medicationVersion.get("blackTriangleMedication"), medicationVersion.get("inpatientMedication"), medicationVersion.get("outpatientMedication"), medicationFormulary.get("id"), medicationVersion.get("expensiveDrug"), medicationVersion.get("price") ) .from( medication.leftOuterJoin("ingredients").as(medicationIngredientLink) .with(medicationIngredientLink.notDeletedAndEffectiveAt(when)), medicationIngredientLink.leftOuterJoin("ingredient").as(medicationIngredient) .with(medicationIngredient.notDeletedAndEffectiveAt(when)), medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("doseForm").as(doseForm), 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)), medicationVersion.leftOuterJoin("basicUnit").as(medicationBasicUnit), medication.leftOuterJoin("types").as(medicationType) .with( medicationType.get("type").eq(MedicationTypeEnum.ANTIBIOTIC), medicationType.notDeleted()), medication.leftOuterJoin("formulary").as(medicationFormulary) .with( medicationFormulary.notDeleted(), or( careProviderId == null, medicationFormulary.get("careProviderId").isNull(), medicationFormulary.get("careProviderId").eq(careProviderId)) ) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationRouteLink, medicationVersion).notDeletedAndEffectiveAt(when) ) .orderBy(medicationFormulary.get("careProviderId")) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { final Long medicationId = nextLong(); final String ingredientName = next(); final Long ingredientId = next(Long.class); final MedicationRuleEnum ingredientRuleEnum = next(MedicationRuleEnum.class); final Long medicationIngredientLinkId = next(); final Double strengthNumerator = next(); final String strengthNumeratorUnit = next(); final Double strengthDenominator = next(); final String strengthDenominatorUnit = next(); final Boolean descriptiveIngredient = next(); final MedicationDataDto dto = medicationDataMap.containsKey(medicationId) ? medicationDataMap.get(medicationId) : new MedicationDataDto(); if (ingredientName != null) { final MedicationIngredientDto medicationIngredientDto = new MedicationIngredientDto(); medicationIngredientDto.setIngredientName(ingredientName); medicationIngredientDto.setIngredientRule(ingredientRuleEnum); if (ingredientId != null) { medicationIngredientDto.setIngredientId(ingredientId); } medicationIngredientDto.setId(medicationIngredientLinkId); medicationIngredientDto.setStrengthNumerator(strengthNumerator); medicationIngredientDto.setStrengthNumeratorUnit(strengthNumeratorUnit); medicationIngredientDto.setStrengthDenominator(strengthDenominator); medicationIngredientDto.setStrengthDenominatorUnit(strengthDenominatorUnit); medicationIngredientDto.setDescriptive(descriptiveIngredient); if (descriptiveIngredient) { dto.setDescriptiveIngredient(medicationIngredientDto); } else if (!dto.getMedicationIngredients().contains(medicationIngredientDto)) { dto.getMedicationIngredients().add(medicationIngredientDto); } } final Long routeId = next(); final String routeCode = next(); final String routeShortName = next(); final MedicationRouteTypeEnum routeType = next(); final Boolean unlicensedRoute = next(); final Boolean discretionaryRoute = next(); final Integer bnfMaximum = next(); final BnfMaximumUnitType bnfMaximumUnitType = next(); final Long childRouteId = next(); final String childRouteCode = next(); final String childRouteShortName = next(); final MedicationRouteTypeEnum childRouteType = next(); Boolean defaultRoute = next(); final MedicationRouteDto routeDto = new MedicationRouteDto(); if (childRouteId != null) { defaultRoute = false; routeDto.setId(childRouteId); routeDto.setCode(childRouteCode); routeDto.setName(childRouteShortName); routeDto.setType(childRouteType); } else { routeDto.setId(routeId); routeDto.setCode(routeCode); routeDto.setName(routeShortName); routeDto.setType(routeType); } routeDto.setUnlicensedRoute(unlicensedRoute != null && unlicensedRoute); routeDto.setDiscretionary(discretionaryRoute != null && discretionaryRoute); if (bnfMaximum != null) { final BnfMaximumDto bnfMaximumDto = new BnfMaximumDto(); bnfMaximumDto.setQuantity(bnfMaximum); bnfMaximumDto.setQuantityUnit(bnfMaximumUnitType); routeDto.setBnfMaximumDto(bnfMaximumDto); } if (!dto.getRoutes().contains(routeDto)) { dto.getRoutes().add(routeDto); if (defaultRoute) { dto.setDefaultRoute(routeDto); } } else if (defaultRoute) { dto.setDefaultRoute(routeDto); } final DoseForm aDoseForm = next(); if (aDoseForm != null) { dto.setDoseForm(doseFormDtoMapper.map(aDoseForm, DoseFormDto.class)); dto.setDoseSplitFactor(aDoseForm.getSplitFactor()); } dto.setBasicUnit(nextString()); final Long medicationTypeAntibioticId = next(); dto.setAntibiotic(medicationTypeAntibioticId != null); dto.setReviewReminder(nextBoolean()); final Boolean controlledDrug = next(); dto.setControlledDrug(controlledDrug != null ? controlledDrug : false); final Boolean mentalHealthDrug = next(); dto.setMentalHealthDrug(mentalHealthDrug != null ? mentalHealthDrug : false); dto.setTitration(next()); dto.setMedicationPackaging(next()); dto.setRoundingFactor(next()); final Boolean clinicalTrialMedication = nextBoolean(); dto.setClinicalTrialMedication(clinicalTrialMedication != null ? clinicalTrialMedication : false); final Boolean unlicensedMedication = nextBoolean(); dto.setUnlicensedMedication(unlicensedMedication != null ? unlicensedMedication : false); final Boolean highAlertMedication = nextBoolean(); dto.setHighAlertMedication(highAlertMedication != null ? highAlertMedication : false); final Boolean blackTriangleMedication = nextBoolean(); dto.setBlackTriangleMedication(blackTriangleMedication != null ? blackTriangleMedication : false); final Boolean inpatientMedication = nextBoolean(); dto.setInpatientMedication(inpatientMedication != null ? inpatientMedication : false); final Boolean outpatientMedication = nextBoolean(); dto.setOutpatientMedication(outpatientMedication != null ? outpatientMedication : false); dto.setFormulary(nextLong() != null); final Boolean expensiveDrug = nextBoolean(); dto.setExpensiveDrug(expensiveDrug != null ? expensiveDrug : false); dto.setPrice(next()); medicationDataMap.put(medicationId, dto); return null; } }); new Hql() .select( medication.id(), medicationIndication.get("code"), medicationIndication.get("name") ) .from( medication.innerJoin("indications").as(medicationIndicationLink), medicationIndicationLink.innerJoin("indication").as(medicationIndication) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationIndicationLink, medicationIndication).notDeletedAndEffectiveAt(when) ) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { medicationDataMap.get(nextLong()).getIndications().add(new IndicationDto(nextString(), nextString())); return null; } }); return null; }); if (!medicationDataMap.isEmpty()) { final Map<Long, MedicationDto> medicationsMap = getMedicationsMap(medicationDataMap.keySet(), when); final Map<Long, List<MedicationDocumentDto>> medicationDocumentsMap = getMedicationDocumentsMap(medicationDataMap.keySet()); for (final Map.Entry<Long, MedicationDataDto> entry : medicationDataMap.entrySet()) { final Long medicationId = entry.getKey(); if (medicationsMap.containsKey(medicationId)) { entry.getValue().setMedication(medicationsMap.get(medicationId)); } if (medicationDocumentsMap.containsKey(medicationId)) { entry.getValue().setMedicationDocuments(medicationDocumentsMap.get(medicationId)); } } } return medicationDataMap; }
HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public Map<Long, MedicationDataDto> getMedicationDataMap( @Nonnull final Set<Long> medicationIds, final String careProviderId, @Nonnull final DateTime when) { Preconditions.checkNotNull(medicationIds, "medicationIds is required"); Preconditions.checkNotNull(when, "when is required"); if (medicationIds.isEmpty()) { return Collections.emptyMap(); } final Alias.Effective<MedicationRoute> childRoute = Alias.forEffectiveEntity(MedicationRoute.class); final Alias.Effective<MedicationRouteRelation> medicationRouteChildRelation = Alias.forEffectiveEntity(MedicationRouteRelation.class); final Map<Long, MedicationDataDto> medicationDataMap = new HashMap<>(); getHibernateTemplate().execute( (HibernateCallback<Void>)requestSession -> { new Hql() .select( medication.id(), medicationIngredient.get("name"), medicationIngredient.id(), medicationIngredient.get("rule"), medicationIngredientLink.id(), medicationIngredientLink.get("strengthNumerator"), medicationIngredientLink.get("strengthNumeratorUnit"), medicationIngredientLink.get("strengthDenominator"), medicationIngredientLink.get("strengthDenominatorUnit"), medicationIngredientLink.get("descriptive"), medicationRoute.id(), medicationRoute.get("code"), medicationRoute.get("shortName"), medicationRoute.get("type"), medicationRouteLink.get("unlicensed"), medicationRouteLink.get("discretionary"), medicationRouteLink.get("bnfMaximum"), medicationRouteLink.get("bnfMaximumUnitType"), childRoute.id(), childRoute.get("code"), childRoute.get("shortName"), childRoute.get("type"), medicationRouteLink.get("defaultRoute"), doseForm, medicationBasicUnit.get("name"), medicationType.id(), medicationVersion.get("reviewReminder"), medicationVersion.get("controlledDrug"), medicationVersion.get("mentalHealthDrug"), medicationVersion.get("titration"), medicationVersion.get("medicationPackaging"), medicationVersion.get("roundingFactor"), medicationVersion.get("clinicalTrialMedication"), medicationVersion.get("unlicensedMedication"), medicationVersion.get("highAlertMedication"), medicationVersion.get("blackTriangleMedication"), medicationVersion.get("inpatientMedication"), medicationVersion.get("outpatientMedication"), medicationFormulary.get("id"), medicationVersion.get("expensiveDrug"), medicationVersion.get("price") ) .from( medication.leftOuterJoin("ingredients").as(medicationIngredientLink) .with(medicationIngredientLink.notDeletedAndEffectiveAt(when)), medicationIngredientLink.leftOuterJoin("ingredient").as(medicationIngredient) .with(medicationIngredient.notDeletedAndEffectiveAt(when)), medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("doseForm").as(doseForm), 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)), medicationVersion.leftOuterJoin("basicUnit").as(medicationBasicUnit), medication.leftOuterJoin("types").as(medicationType) .with( medicationType.get("type").eq(MedicationTypeEnum.ANTIBIOTIC), medicationType.notDeleted()), medication.leftOuterJoin("formulary").as(medicationFormulary) .with( medicationFormulary.notDeleted(), or( careProviderId == null, medicationFormulary.get("careProviderId").isNull(), medicationFormulary.get("careProviderId").eq(careProviderId)) ) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationRouteLink, medicationVersion).notDeletedAndEffectiveAt(when) ) .orderBy(medicationFormulary.get("careProviderId")) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { final Long medicationId = nextLong(); final String ingredientName = next(); final Long ingredientId = next(Long.class); final MedicationRuleEnum ingredientRuleEnum = next(MedicationRuleEnum.class); final Long medicationIngredientLinkId = next(); final Double strengthNumerator = next(); final String strengthNumeratorUnit = next(); final Double strengthDenominator = next(); final String strengthDenominatorUnit = next(); final Boolean descriptiveIngredient = next(); final MedicationDataDto dto = medicationDataMap.containsKey(medicationId) ? medicationDataMap.get(medicationId) : new MedicationDataDto(); if (ingredientName != null) { final MedicationIngredientDto medicationIngredientDto = new MedicationIngredientDto(); medicationIngredientDto.setIngredientName(ingredientName); medicationIngredientDto.setIngredientRule(ingredientRuleEnum); if (ingredientId != null) { medicationIngredientDto.setIngredientId(ingredientId); } medicationIngredientDto.setId(medicationIngredientLinkId); medicationIngredientDto.setStrengthNumerator(strengthNumerator); medicationIngredientDto.setStrengthNumeratorUnit(strengthNumeratorUnit); medicationIngredientDto.setStrengthDenominator(strengthDenominator); medicationIngredientDto.setStrengthDenominatorUnit(strengthDenominatorUnit); medicationIngredientDto.setDescriptive(descriptiveIngredient); if (descriptiveIngredient) { dto.setDescriptiveIngredient(medicationIngredientDto); } else if (!dto.getMedicationIngredients().contains(medicationIngredientDto)) { dto.getMedicationIngredients().add(medicationIngredientDto); } } final Long routeId = next(); final String routeCode = next(); final String routeShortName = next(); final MedicationRouteTypeEnum routeType = next(); final Boolean unlicensedRoute = next(); final Boolean discretionaryRoute = next(); final Integer bnfMaximum = next(); final BnfMaximumUnitType bnfMaximumUnitType = next(); final Long childRouteId = next(); final String childRouteCode = next(); final String childRouteShortName = next(); final MedicationRouteTypeEnum childRouteType = next(); Boolean defaultRoute = next(); final MedicationRouteDto routeDto = new MedicationRouteDto(); if (childRouteId != null) { defaultRoute = false; routeDto.setId(childRouteId); routeDto.setCode(childRouteCode); routeDto.setName(childRouteShortName); routeDto.setType(childRouteType); } else { routeDto.setId(routeId); routeDto.setCode(routeCode); routeDto.setName(routeShortName); routeDto.setType(routeType); } routeDto.setUnlicensedRoute(unlicensedRoute != null && unlicensedRoute); routeDto.setDiscretionary(discretionaryRoute != null && discretionaryRoute); if (bnfMaximum != null) { final BnfMaximumDto bnfMaximumDto = new BnfMaximumDto(); bnfMaximumDto.setQuantity(bnfMaximum); bnfMaximumDto.setQuantityUnit(bnfMaximumUnitType); routeDto.setBnfMaximumDto(bnfMaximumDto); } if (!dto.getRoutes().contains(routeDto)) { dto.getRoutes().add(routeDto); if (defaultRoute) { dto.setDefaultRoute(routeDto); } } else if (defaultRoute) { dto.setDefaultRoute(routeDto); } final DoseForm aDoseForm = next(); if (aDoseForm != null) { dto.setDoseForm(doseFormDtoMapper.map(aDoseForm, DoseFormDto.class)); dto.setDoseSplitFactor(aDoseForm.getSplitFactor()); } dto.setBasicUnit(nextString()); final Long medicationTypeAntibioticId = next(); dto.setAntibiotic(medicationTypeAntibioticId != null); dto.setReviewReminder(nextBoolean()); final Boolean controlledDrug = next(); dto.setControlledDrug(controlledDrug != null ? controlledDrug : false); final Boolean mentalHealthDrug = next(); dto.setMentalHealthDrug(mentalHealthDrug != null ? mentalHealthDrug : false); dto.setTitration(next()); dto.setMedicationPackaging(next()); dto.setRoundingFactor(next()); final Boolean clinicalTrialMedication = nextBoolean(); dto.setClinicalTrialMedication(clinicalTrialMedication != null ? clinicalTrialMedication : false); final Boolean unlicensedMedication = nextBoolean(); dto.setUnlicensedMedication(unlicensedMedication != null ? unlicensedMedication : false); final Boolean highAlertMedication = nextBoolean(); dto.setHighAlertMedication(highAlertMedication != null ? highAlertMedication : false); final Boolean blackTriangleMedication = nextBoolean(); dto.setBlackTriangleMedication(blackTriangleMedication != null ? blackTriangleMedication : false); final Boolean inpatientMedication = nextBoolean(); dto.setInpatientMedication(inpatientMedication != null ? inpatientMedication : false); final Boolean outpatientMedication = nextBoolean(); dto.setOutpatientMedication(outpatientMedication != null ? outpatientMedication : false); dto.setFormulary(nextLong() != null); final Boolean expensiveDrug = nextBoolean(); dto.setExpensiveDrug(expensiveDrug != null ? expensiveDrug : false); dto.setPrice(next()); medicationDataMap.put(medicationId, dto); return null; } }); new Hql() .select( medication.id(), medicationIndication.get("code"), medicationIndication.get("name") ) .from( medication.innerJoin("indications").as(medicationIndicationLink), medicationIndicationLink.innerJoin("indication").as(medicationIndication) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationIndicationLink, medicationIndication).notDeletedAndEffectiveAt(when) ) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { medicationDataMap.get(nextLong()).getIndications().add(new IndicationDto(nextString(), nextString())); return null; } }); return null; }); if (!medicationDataMap.isEmpty()) { final Map<Long, MedicationDto> medicationsMap = getMedicationsMap(medicationDataMap.keySet(), when); final Map<Long, List<MedicationDocumentDto>> medicationDocumentsMap = getMedicationDocumentsMap(medicationDataMap.keySet()); for (final Map.Entry<Long, MedicationDataDto> entry : medicationDataMap.entrySet()) { final Long medicationId = entry.getKey(); if (medicationsMap.containsKey(medicationId)) { entry.getValue().setMedication(medicationsMap.get(medicationId)); } if (medicationDocumentsMap.containsKey(medicationId)) { entry.getValue().setMedicationDocuments(medicationDocumentsMap.get(medicationId)); } } } return medicationDataMap; } }
HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public Map<Long, MedicationDataDto> getMedicationDataMap( @Nonnull final Set<Long> medicationIds, final String careProviderId, @Nonnull final DateTime when) { Preconditions.checkNotNull(medicationIds, "medicationIds is required"); Preconditions.checkNotNull(when, "when is required"); if (medicationIds.isEmpty()) { return Collections.emptyMap(); } final Alias.Effective<MedicationRoute> childRoute = Alias.forEffectiveEntity(MedicationRoute.class); final Alias.Effective<MedicationRouteRelation> medicationRouteChildRelation = Alias.forEffectiveEntity(MedicationRouteRelation.class); final Map<Long, MedicationDataDto> medicationDataMap = new HashMap<>(); getHibernateTemplate().execute( (HibernateCallback<Void>)requestSession -> { new Hql() .select( medication.id(), medicationIngredient.get("name"), medicationIngredient.id(), medicationIngredient.get("rule"), medicationIngredientLink.id(), medicationIngredientLink.get("strengthNumerator"), medicationIngredientLink.get("strengthNumeratorUnit"), medicationIngredientLink.get("strengthDenominator"), medicationIngredientLink.get("strengthDenominatorUnit"), medicationIngredientLink.get("descriptive"), medicationRoute.id(), medicationRoute.get("code"), medicationRoute.get("shortName"), medicationRoute.get("type"), medicationRouteLink.get("unlicensed"), medicationRouteLink.get("discretionary"), medicationRouteLink.get("bnfMaximum"), medicationRouteLink.get("bnfMaximumUnitType"), childRoute.id(), childRoute.get("code"), childRoute.get("shortName"), childRoute.get("type"), medicationRouteLink.get("defaultRoute"), doseForm, medicationBasicUnit.get("name"), medicationType.id(), medicationVersion.get("reviewReminder"), medicationVersion.get("controlledDrug"), medicationVersion.get("mentalHealthDrug"), medicationVersion.get("titration"), medicationVersion.get("medicationPackaging"), medicationVersion.get("roundingFactor"), medicationVersion.get("clinicalTrialMedication"), medicationVersion.get("unlicensedMedication"), medicationVersion.get("highAlertMedication"), medicationVersion.get("blackTriangleMedication"), medicationVersion.get("inpatientMedication"), medicationVersion.get("outpatientMedication"), medicationFormulary.get("id"), medicationVersion.get("expensiveDrug"), medicationVersion.get("price") ) .from( medication.leftOuterJoin("ingredients").as(medicationIngredientLink) .with(medicationIngredientLink.notDeletedAndEffectiveAt(when)), medicationIngredientLink.leftOuterJoin("ingredient").as(medicationIngredient) .with(medicationIngredient.notDeletedAndEffectiveAt(when)), medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("doseForm").as(doseForm), 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)), medicationVersion.leftOuterJoin("basicUnit").as(medicationBasicUnit), medication.leftOuterJoin("types").as(medicationType) .with( medicationType.get("type").eq(MedicationTypeEnum.ANTIBIOTIC), medicationType.notDeleted()), medication.leftOuterJoin("formulary").as(medicationFormulary) .with( medicationFormulary.notDeleted(), or( careProviderId == null, medicationFormulary.get("careProviderId").isNull(), medicationFormulary.get("careProviderId").eq(careProviderId)) ) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationRouteLink, medicationVersion).notDeletedAndEffectiveAt(when) ) .orderBy(medicationFormulary.get("careProviderId")) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { final Long medicationId = nextLong(); final String ingredientName = next(); final Long ingredientId = next(Long.class); final MedicationRuleEnum ingredientRuleEnum = next(MedicationRuleEnum.class); final Long medicationIngredientLinkId = next(); final Double strengthNumerator = next(); final String strengthNumeratorUnit = next(); final Double strengthDenominator = next(); final String strengthDenominatorUnit = next(); final Boolean descriptiveIngredient = next(); final MedicationDataDto dto = medicationDataMap.containsKey(medicationId) ? medicationDataMap.get(medicationId) : new MedicationDataDto(); if (ingredientName != null) { final MedicationIngredientDto medicationIngredientDto = new MedicationIngredientDto(); medicationIngredientDto.setIngredientName(ingredientName); medicationIngredientDto.setIngredientRule(ingredientRuleEnum); if (ingredientId != null) { medicationIngredientDto.setIngredientId(ingredientId); } medicationIngredientDto.setId(medicationIngredientLinkId); medicationIngredientDto.setStrengthNumerator(strengthNumerator); medicationIngredientDto.setStrengthNumeratorUnit(strengthNumeratorUnit); medicationIngredientDto.setStrengthDenominator(strengthDenominator); medicationIngredientDto.setStrengthDenominatorUnit(strengthDenominatorUnit); medicationIngredientDto.setDescriptive(descriptiveIngredient); if (descriptiveIngredient) { dto.setDescriptiveIngredient(medicationIngredientDto); } else if (!dto.getMedicationIngredients().contains(medicationIngredientDto)) { dto.getMedicationIngredients().add(medicationIngredientDto); } } final Long routeId = next(); final String routeCode = next(); final String routeShortName = next(); final MedicationRouteTypeEnum routeType = next(); final Boolean unlicensedRoute = next(); final Boolean discretionaryRoute = next(); final Integer bnfMaximum = next(); final BnfMaximumUnitType bnfMaximumUnitType = next(); final Long childRouteId = next(); final String childRouteCode = next(); final String childRouteShortName = next(); final MedicationRouteTypeEnum childRouteType = next(); Boolean defaultRoute = next(); final MedicationRouteDto routeDto = new MedicationRouteDto(); if (childRouteId != null) { defaultRoute = false; routeDto.setId(childRouteId); routeDto.setCode(childRouteCode); routeDto.setName(childRouteShortName); routeDto.setType(childRouteType); } else { routeDto.setId(routeId); routeDto.setCode(routeCode); routeDto.setName(routeShortName); routeDto.setType(routeType); } routeDto.setUnlicensedRoute(unlicensedRoute != null && unlicensedRoute); routeDto.setDiscretionary(discretionaryRoute != null && discretionaryRoute); if (bnfMaximum != null) { final BnfMaximumDto bnfMaximumDto = new BnfMaximumDto(); bnfMaximumDto.setQuantity(bnfMaximum); bnfMaximumDto.setQuantityUnit(bnfMaximumUnitType); routeDto.setBnfMaximumDto(bnfMaximumDto); } if (!dto.getRoutes().contains(routeDto)) { dto.getRoutes().add(routeDto); if (defaultRoute) { dto.setDefaultRoute(routeDto); } } else if (defaultRoute) { dto.setDefaultRoute(routeDto); } final DoseForm aDoseForm = next(); if (aDoseForm != null) { dto.setDoseForm(doseFormDtoMapper.map(aDoseForm, DoseFormDto.class)); dto.setDoseSplitFactor(aDoseForm.getSplitFactor()); } dto.setBasicUnit(nextString()); final Long medicationTypeAntibioticId = next(); dto.setAntibiotic(medicationTypeAntibioticId != null); dto.setReviewReminder(nextBoolean()); final Boolean controlledDrug = next(); dto.setControlledDrug(controlledDrug != null ? controlledDrug : false); final Boolean mentalHealthDrug = next(); dto.setMentalHealthDrug(mentalHealthDrug != null ? mentalHealthDrug : false); dto.setTitration(next()); dto.setMedicationPackaging(next()); dto.setRoundingFactor(next()); final Boolean clinicalTrialMedication = nextBoolean(); dto.setClinicalTrialMedication(clinicalTrialMedication != null ? clinicalTrialMedication : false); final Boolean unlicensedMedication = nextBoolean(); dto.setUnlicensedMedication(unlicensedMedication != null ? unlicensedMedication : false); final Boolean highAlertMedication = nextBoolean(); dto.setHighAlertMedication(highAlertMedication != null ? highAlertMedication : false); final Boolean blackTriangleMedication = nextBoolean(); dto.setBlackTriangleMedication(blackTriangleMedication != null ? blackTriangleMedication : false); final Boolean inpatientMedication = nextBoolean(); dto.setInpatientMedication(inpatientMedication != null ? inpatientMedication : false); final Boolean outpatientMedication = nextBoolean(); dto.setOutpatientMedication(outpatientMedication != null ? outpatientMedication : false); dto.setFormulary(nextLong() != null); final Boolean expensiveDrug = nextBoolean(); dto.setExpensiveDrug(expensiveDrug != null ? expensiveDrug : false); dto.setPrice(next()); medicationDataMap.put(medicationId, dto); return null; } }); new Hql() .select( medication.id(), medicationIndication.get("code"), medicationIndication.get("name") ) .from( medication.innerJoin("indications").as(medicationIndicationLink), medicationIndicationLink.innerJoin("indication").as(medicationIndication) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationIndicationLink, medicationIndication).notDeletedAndEffectiveAt(when) ) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { medicationDataMap.get(nextLong()).getIndications().add(new IndicationDto(nextString(), nextString())); return null; } }); return null; }); if (!medicationDataMap.isEmpty()) { final Map<Long, MedicationDto> medicationsMap = getMedicationsMap(medicationDataMap.keySet(), when); final Map<Long, List<MedicationDocumentDto>> medicationDocumentsMap = getMedicationDocumentsMap(medicationDataMap.keySet()); for (final Map.Entry<Long, MedicationDataDto> entry : medicationDataMap.entrySet()) { final Long medicationId = entry.getKey(); if (medicationsMap.containsKey(medicationId)) { entry.getValue().setMedication(medicationsMap.get(medicationId)); } if (medicationDocumentsMap.containsKey(medicationId)) { entry.getValue().setMedicationDocuments(medicationDocumentsMap.get(medicationId)); } } } return medicationDataMap; }  }
HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public Map<Long, MedicationDataDto> getMedicationDataMap( @Nonnull final Set<Long> medicationIds, final String careProviderId, @Nonnull final DateTime when) { Preconditions.checkNotNull(medicationIds, "medicationIds is required"); Preconditions.checkNotNull(when, "when is required"); if (medicationIds.isEmpty()) { return Collections.emptyMap(); } final Alias.Effective<MedicationRoute> childRoute = Alias.forEffectiveEntity(MedicationRoute.class); final Alias.Effective<MedicationRouteRelation> medicationRouteChildRelation = Alias.forEffectiveEntity(MedicationRouteRelation.class); final Map<Long, MedicationDataDto> medicationDataMap = new HashMap<>(); getHibernateTemplate().execute( (HibernateCallback<Void>)requestSession -> { new Hql() .select( medication.id(), medicationIngredient.get("name"), medicationIngredient.id(), medicationIngredient.get("rule"), medicationIngredientLink.id(), medicationIngredientLink.get("strengthNumerator"), medicationIngredientLink.get("strengthNumeratorUnit"), medicationIngredientLink.get("strengthDenominator"), medicationIngredientLink.get("strengthDenominatorUnit"), medicationIngredientLink.get("descriptive"), medicationRoute.id(), medicationRoute.get("code"), medicationRoute.get("shortName"), medicationRoute.get("type"), medicationRouteLink.get("unlicensed"), medicationRouteLink.get("discretionary"), medicationRouteLink.get("bnfMaximum"), medicationRouteLink.get("bnfMaximumUnitType"), childRoute.id(), childRoute.get("code"), childRoute.get("shortName"), childRoute.get("type"), medicationRouteLink.get("defaultRoute"), doseForm, medicationBasicUnit.get("name"), medicationType.id(), medicationVersion.get("reviewReminder"), medicationVersion.get("controlledDrug"), medicationVersion.get("mentalHealthDrug"), medicationVersion.get("titration"), medicationVersion.get("medicationPackaging"), medicationVersion.get("roundingFactor"), medicationVersion.get("clinicalTrialMedication"), medicationVersion.get("unlicensedMedication"), medicationVersion.get("highAlertMedication"), medicationVersion.get("blackTriangleMedication"), medicationVersion.get("inpatientMedication"), medicationVersion.get("outpatientMedication"), medicationFormulary.get("id"), medicationVersion.get("expensiveDrug"), medicationVersion.get("price") ) .from( medication.leftOuterJoin("ingredients").as(medicationIngredientLink) .with(medicationIngredientLink.notDeletedAndEffectiveAt(when)), medicationIngredientLink.leftOuterJoin("ingredient").as(medicationIngredient) .with(medicationIngredient.notDeletedAndEffectiveAt(when)), medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("doseForm").as(doseForm), 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)), medicationVersion.leftOuterJoin("basicUnit").as(medicationBasicUnit), medication.leftOuterJoin("types").as(medicationType) .with( medicationType.get("type").eq(MedicationTypeEnum.ANTIBIOTIC), medicationType.notDeleted()), medication.leftOuterJoin("formulary").as(medicationFormulary) .with( medicationFormulary.notDeleted(), or( careProviderId == null, medicationFormulary.get("careProviderId").isNull(), medicationFormulary.get("careProviderId").eq(careProviderId)) ) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationRouteLink, medicationVersion).notDeletedAndEffectiveAt(when) ) .orderBy(medicationFormulary.get("careProviderId")) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { final Long medicationId = nextLong(); final String ingredientName = next(); final Long ingredientId = next(Long.class); final MedicationRuleEnum ingredientRuleEnum = next(MedicationRuleEnum.class); final Long medicationIngredientLinkId = next(); final Double strengthNumerator = next(); final String strengthNumeratorUnit = next(); final Double strengthDenominator = next(); final String strengthDenominatorUnit = next(); final Boolean descriptiveIngredient = next(); final MedicationDataDto dto = medicationDataMap.containsKey(medicationId) ? medicationDataMap.get(medicationId) : new MedicationDataDto(); if (ingredientName != null) { final MedicationIngredientDto medicationIngredientDto = new MedicationIngredientDto(); medicationIngredientDto.setIngredientName(ingredientName); medicationIngredientDto.setIngredientRule(ingredientRuleEnum); if (ingredientId != null) { medicationIngredientDto.setIngredientId(ingredientId); } medicationIngredientDto.setId(medicationIngredientLinkId); medicationIngredientDto.setStrengthNumerator(strengthNumerator); medicationIngredientDto.setStrengthNumeratorUnit(strengthNumeratorUnit); medicationIngredientDto.setStrengthDenominator(strengthDenominator); medicationIngredientDto.setStrengthDenominatorUnit(strengthDenominatorUnit); medicationIngredientDto.setDescriptive(descriptiveIngredient); if (descriptiveIngredient) { dto.setDescriptiveIngredient(medicationIngredientDto); } else if (!dto.getMedicationIngredients().contains(medicationIngredientDto)) { dto.getMedicationIngredients().add(medicationIngredientDto); } } final Long routeId = next(); final String routeCode = next(); final String routeShortName = next(); final MedicationRouteTypeEnum routeType = next(); final Boolean unlicensedRoute = next(); final Boolean discretionaryRoute = next(); final Integer bnfMaximum = next(); final BnfMaximumUnitType bnfMaximumUnitType = next(); final Long childRouteId = next(); final String childRouteCode = next(); final String childRouteShortName = next(); final MedicationRouteTypeEnum childRouteType = next(); Boolean defaultRoute = next(); final MedicationRouteDto routeDto = new MedicationRouteDto(); if (childRouteId != null) { defaultRoute = false; routeDto.setId(childRouteId); routeDto.setCode(childRouteCode); routeDto.setName(childRouteShortName); routeDto.setType(childRouteType); } else { routeDto.setId(routeId); routeDto.setCode(routeCode); routeDto.setName(routeShortName); routeDto.setType(routeType); } routeDto.setUnlicensedRoute(unlicensedRoute != null && unlicensedRoute); routeDto.setDiscretionary(discretionaryRoute != null && discretionaryRoute); if (bnfMaximum != null) { final BnfMaximumDto bnfMaximumDto = new BnfMaximumDto(); bnfMaximumDto.setQuantity(bnfMaximum); bnfMaximumDto.setQuantityUnit(bnfMaximumUnitType); routeDto.setBnfMaximumDto(bnfMaximumDto); } if (!dto.getRoutes().contains(routeDto)) { dto.getRoutes().add(routeDto); if (defaultRoute) { dto.setDefaultRoute(routeDto); } } else if (defaultRoute) { dto.setDefaultRoute(routeDto); } final DoseForm aDoseForm = next(); if (aDoseForm != null) { dto.setDoseForm(doseFormDtoMapper.map(aDoseForm, DoseFormDto.class)); dto.setDoseSplitFactor(aDoseForm.getSplitFactor()); } dto.setBasicUnit(nextString()); final Long medicationTypeAntibioticId = next(); dto.setAntibiotic(medicationTypeAntibioticId != null); dto.setReviewReminder(nextBoolean()); final Boolean controlledDrug = next(); dto.setControlledDrug(controlledDrug != null ? controlledDrug : false); final Boolean mentalHealthDrug = next(); dto.setMentalHealthDrug(mentalHealthDrug != null ? mentalHealthDrug : false); dto.setTitration(next()); dto.setMedicationPackaging(next()); dto.setRoundingFactor(next()); final Boolean clinicalTrialMedication = nextBoolean(); dto.setClinicalTrialMedication(clinicalTrialMedication != null ? clinicalTrialMedication : false); final Boolean unlicensedMedication = nextBoolean(); dto.setUnlicensedMedication(unlicensedMedication != null ? unlicensedMedication : false); final Boolean highAlertMedication = nextBoolean(); dto.setHighAlertMedication(highAlertMedication != null ? highAlertMedication : false); final Boolean blackTriangleMedication = nextBoolean(); dto.setBlackTriangleMedication(blackTriangleMedication != null ? blackTriangleMedication : false); final Boolean inpatientMedication = nextBoolean(); dto.setInpatientMedication(inpatientMedication != null ? inpatientMedication : false); final Boolean outpatientMedication = nextBoolean(); dto.setOutpatientMedication(outpatientMedication != null ? outpatientMedication : false); dto.setFormulary(nextLong() != null); final Boolean expensiveDrug = nextBoolean(); dto.setExpensiveDrug(expensiveDrug != null ? expensiveDrug : false); dto.setPrice(next()); medicationDataMap.put(medicationId, dto); return null; } }); new Hql() .select( medication.id(), medicationIndication.get("code"), medicationIndication.get("name") ) .from( medication.innerJoin("indications").as(medicationIndicationLink), medicationIndicationLink.innerJoin("indication").as(medicationIndication) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationIndicationLink, medicationIndication).notDeletedAndEffectiveAt(when) ) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { medicationDataMap.get(nextLong()).getIndications().add(new IndicationDto(nextString(), nextString())); return null; } }); return null; }); if (!medicationDataMap.isEmpty()) { final Map<Long, MedicationDto> medicationsMap = getMedicationsMap(medicationDataMap.keySet(), when); final Map<Long, List<MedicationDocumentDto>> medicationDocumentsMap = getMedicationDocumentsMap(medicationDataMap.keySet()); for (final Map.Entry<Long, MedicationDataDto> entry : medicationDataMap.entrySet()) { final Long medicationId = entry.getKey(); if (medicationsMap.containsKey(medicationId)) { entry.getValue().setMedication(medicationsMap.get(medicationId)); } if (medicationDocumentsMap.containsKey(medicationId)) { entry.getValue().setMedicationDocuments(medicationDocumentsMap.get(medicationId)); } } } return medicationDataMap; }  @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); }
HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public Map<Long, MedicationDataDto> getMedicationDataMap( @Nonnull final Set<Long> medicationIds, final String careProviderId, @Nonnull final DateTime when) { Preconditions.checkNotNull(medicationIds, "medicationIds is required"); Preconditions.checkNotNull(when, "when is required"); if (medicationIds.isEmpty()) { return Collections.emptyMap(); } final Alias.Effective<MedicationRoute> childRoute = Alias.forEffectiveEntity(MedicationRoute.class); final Alias.Effective<MedicationRouteRelation> medicationRouteChildRelation = Alias.forEffectiveEntity(MedicationRouteRelation.class); final Map<Long, MedicationDataDto> medicationDataMap = new HashMap<>(); getHibernateTemplate().execute( (HibernateCallback<Void>)requestSession -> { new Hql() .select( medication.id(), medicationIngredient.get("name"), medicationIngredient.id(), medicationIngredient.get("rule"), medicationIngredientLink.id(), medicationIngredientLink.get("strengthNumerator"), medicationIngredientLink.get("strengthNumeratorUnit"), medicationIngredientLink.get("strengthDenominator"), medicationIngredientLink.get("strengthDenominatorUnit"), medicationIngredientLink.get("descriptive"), medicationRoute.id(), medicationRoute.get("code"), medicationRoute.get("shortName"), medicationRoute.get("type"), medicationRouteLink.get("unlicensed"), medicationRouteLink.get("discretionary"), medicationRouteLink.get("bnfMaximum"), medicationRouteLink.get("bnfMaximumUnitType"), childRoute.id(), childRoute.get("code"), childRoute.get("shortName"), childRoute.get("type"), medicationRouteLink.get("defaultRoute"), doseForm, medicationBasicUnit.get("name"), medicationType.id(), medicationVersion.get("reviewReminder"), medicationVersion.get("controlledDrug"), medicationVersion.get("mentalHealthDrug"), medicationVersion.get("titration"), medicationVersion.get("medicationPackaging"), medicationVersion.get("roundingFactor"), medicationVersion.get("clinicalTrialMedication"), medicationVersion.get("unlicensedMedication"), medicationVersion.get("highAlertMedication"), medicationVersion.get("blackTriangleMedication"), medicationVersion.get("inpatientMedication"), medicationVersion.get("outpatientMedication"), medicationFormulary.get("id"), medicationVersion.get("expensiveDrug"), medicationVersion.get("price") ) .from( medication.leftOuterJoin("ingredients").as(medicationIngredientLink) .with(medicationIngredientLink.notDeletedAndEffectiveAt(when)), medicationIngredientLink.leftOuterJoin("ingredient").as(medicationIngredient) .with(medicationIngredient.notDeletedAndEffectiveAt(when)), medication.innerJoin("versions").as(medicationVersion), medicationVersion.leftOuterJoin("doseForm").as(doseForm), 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)), medicationVersion.leftOuterJoin("basicUnit").as(medicationBasicUnit), medication.leftOuterJoin("types").as(medicationType) .with( medicationType.get("type").eq(MedicationTypeEnum.ANTIBIOTIC), medicationType.notDeleted()), medication.leftOuterJoin("formulary").as(medicationFormulary) .with( medicationFormulary.notDeleted(), or( careProviderId == null, medicationFormulary.get("careProviderId").isNull(), medicationFormulary.get("careProviderId").eq(careProviderId)) ) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationRouteLink, medicationVersion).notDeletedAndEffectiveAt(when) ) .orderBy(medicationFormulary.get("careProviderId")) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { final Long medicationId = nextLong(); final String ingredientName = next(); final Long ingredientId = next(Long.class); final MedicationRuleEnum ingredientRuleEnum = next(MedicationRuleEnum.class); final Long medicationIngredientLinkId = next(); final Double strengthNumerator = next(); final String strengthNumeratorUnit = next(); final Double strengthDenominator = next(); final String strengthDenominatorUnit = next(); final Boolean descriptiveIngredient = next(); final MedicationDataDto dto = medicationDataMap.containsKey(medicationId) ? medicationDataMap.get(medicationId) : new MedicationDataDto(); if (ingredientName != null) { final MedicationIngredientDto medicationIngredientDto = new MedicationIngredientDto(); medicationIngredientDto.setIngredientName(ingredientName); medicationIngredientDto.setIngredientRule(ingredientRuleEnum); if (ingredientId != null) { medicationIngredientDto.setIngredientId(ingredientId); } medicationIngredientDto.setId(medicationIngredientLinkId); medicationIngredientDto.setStrengthNumerator(strengthNumerator); medicationIngredientDto.setStrengthNumeratorUnit(strengthNumeratorUnit); medicationIngredientDto.setStrengthDenominator(strengthDenominator); medicationIngredientDto.setStrengthDenominatorUnit(strengthDenominatorUnit); medicationIngredientDto.setDescriptive(descriptiveIngredient); if (descriptiveIngredient) { dto.setDescriptiveIngredient(medicationIngredientDto); } else if (!dto.getMedicationIngredients().contains(medicationIngredientDto)) { dto.getMedicationIngredients().add(medicationIngredientDto); } } final Long routeId = next(); final String routeCode = next(); final String routeShortName = next(); final MedicationRouteTypeEnum routeType = next(); final Boolean unlicensedRoute = next(); final Boolean discretionaryRoute = next(); final Integer bnfMaximum = next(); final BnfMaximumUnitType bnfMaximumUnitType = next(); final Long childRouteId = next(); final String childRouteCode = next(); final String childRouteShortName = next(); final MedicationRouteTypeEnum childRouteType = next(); Boolean defaultRoute = next(); final MedicationRouteDto routeDto = new MedicationRouteDto(); if (childRouteId != null) { defaultRoute = false; routeDto.setId(childRouteId); routeDto.setCode(childRouteCode); routeDto.setName(childRouteShortName); routeDto.setType(childRouteType); } else { routeDto.setId(routeId); routeDto.setCode(routeCode); routeDto.setName(routeShortName); routeDto.setType(routeType); } routeDto.setUnlicensedRoute(unlicensedRoute != null && unlicensedRoute); routeDto.setDiscretionary(discretionaryRoute != null && discretionaryRoute); if (bnfMaximum != null) { final BnfMaximumDto bnfMaximumDto = new BnfMaximumDto(); bnfMaximumDto.setQuantity(bnfMaximum); bnfMaximumDto.setQuantityUnit(bnfMaximumUnitType); routeDto.setBnfMaximumDto(bnfMaximumDto); } if (!dto.getRoutes().contains(routeDto)) { dto.getRoutes().add(routeDto); if (defaultRoute) { dto.setDefaultRoute(routeDto); } } else if (defaultRoute) { dto.setDefaultRoute(routeDto); } final DoseForm aDoseForm = next(); if (aDoseForm != null) { dto.setDoseForm(doseFormDtoMapper.map(aDoseForm, DoseFormDto.class)); dto.setDoseSplitFactor(aDoseForm.getSplitFactor()); } dto.setBasicUnit(nextString()); final Long medicationTypeAntibioticId = next(); dto.setAntibiotic(medicationTypeAntibioticId != null); dto.setReviewReminder(nextBoolean()); final Boolean controlledDrug = next(); dto.setControlledDrug(controlledDrug != null ? controlledDrug : false); final Boolean mentalHealthDrug = next(); dto.setMentalHealthDrug(mentalHealthDrug != null ? mentalHealthDrug : false); dto.setTitration(next()); dto.setMedicationPackaging(next()); dto.setRoundingFactor(next()); final Boolean clinicalTrialMedication = nextBoolean(); dto.setClinicalTrialMedication(clinicalTrialMedication != null ? clinicalTrialMedication : false); final Boolean unlicensedMedication = nextBoolean(); dto.setUnlicensedMedication(unlicensedMedication != null ? unlicensedMedication : false); final Boolean highAlertMedication = nextBoolean(); dto.setHighAlertMedication(highAlertMedication != null ? highAlertMedication : false); final Boolean blackTriangleMedication = nextBoolean(); dto.setBlackTriangleMedication(blackTriangleMedication != null ? blackTriangleMedication : false); final Boolean inpatientMedication = nextBoolean(); dto.setInpatientMedication(inpatientMedication != null ? inpatientMedication : false); final Boolean outpatientMedication = nextBoolean(); dto.setOutpatientMedication(outpatientMedication != null ? outpatientMedication : false); dto.setFormulary(nextLong() != null); final Boolean expensiveDrug = nextBoolean(); dto.setExpensiveDrug(expensiveDrug != null ? expensiveDrug : false); dto.setPrice(next()); medicationDataMap.put(medicationId, dto); return null; } }); new Hql() .select( medication.id(), medicationIndication.get("code"), medicationIndication.get("name") ) .from( medication.innerJoin("indications").as(medicationIndicationLink), medicationIndicationLink.innerJoin("indication").as(medicationIndication) ) .where( medication.id().in(medicationIds), effectiveEntities(medicationIndicationLink, medicationIndication).notDeletedAndEffectiveAt(when) ) .buildQuery(requestSession, Object[].class) .list( new TupleProcessor<Void>() { @Override protected Void process(final boolean hasNextTuple) throws ProcessingException { medicationDataMap.get(nextLong()).getIndications().add(new IndicationDto(nextString(), nextString())); return null; } }); return null; }); if (!medicationDataMap.isEmpty()) { final Map<Long, MedicationDto> medicationsMap = getMedicationsMap(medicationDataMap.keySet(), when); final Map<Long, List<MedicationDocumentDto>> medicationDocumentsMap = getMedicationDocumentsMap(medicationDataMap.keySet()); for (final Map.Entry<Long, MedicationDataDto> entry : medicationDataMap.entrySet()) { final Long medicationId = entry.getKey(); if (medicationsMap.containsKey(medicationId)) { entry.getValue().setMedication(medicationsMap.get(medicationId)); } if (medicationDocumentsMap.containsKey(medicationId)) { entry.getValue().setMedicationDocuments(medicationDocumentsMap.get(medicationId)); } } } return medicationDataMap; }  @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);  }