HibernateMedicationsDao extends HibernateDaoSupport implements MedicationsDao { @Override public 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) { StringUtils.checkNotBlank(patientId, "patientId is required"); StringUtils.checkNotBlank(userId, "userId is required"); Preconditions.checkNotNull(templateMode, "templateMode is required"); Preconditions.checkNotNull(locale, "locale is required"); return getHibernateTemplate().execute( session -> { final TherapyTemplatesDto allTemplatesDto = new TherapyTemplatesDto(); new Hql() .select( therapyTemplate.id(), therapyTemplate.get("version"), therapyTemplate.get("name"), therapyTemplate.get("type"), therapyTemplate.get("userId"), therapyTemplate.get("careProviderId"), therapyTemplate.get("patientId"), therapyTemplateElement.get("therapy"), therapyTemplateElement.get("completed") ) .from( therapyTemplate.innerJoin("therapyTemplateElements").as(therapyTemplateElement) ) .where( therapyTemplate.get("templateMode").eq(templateMode), or( therapyTemplate.get("userId").eq(userId), therapyTemplate.get("patientId").eq(patientId), and(careProviderId != null, therapyTemplate.get("careProviderId").eq(careProviderId)) ), permanentEntities(therapyTemplate, therapyTemplateElement).notDeleted() ) .orderBy(therapyTemplate.get("name")) .buildQuery(session, Object[].class) .list( new TwoLevelJoinProcessor.ToList<TherapyTemplateDto, TherapyTemplateElementDto>() { @Override protected TherapyTemplateDto mapParent() { final TherapyTemplateDto templateDto = new TherapyTemplateDto(); templateDto.setId(nextLong()); templateDto.setVersion(next(Integer.class)); templateDto.setName(nextString()); templateDto.setType(next(TherapyTemplateTypeEnum.class)); templateDto.setUserId(nextString()); templateDto.setCareProviderId(nextString()); templateDto.setPatientId(nextString()); if (templateDto.getType() == TherapyTemplateTypeEnum.ORGANIZATIONAL) { allTemplatesDto.getOrganizationTemplates().add(templateDto); } else if (templateDto.getType() == TherapyTemplateTypeEnum.USER) { allTemplatesDto.getUserTemplates().add(templateDto); } else if (templateDto.getType() == TherapyTemplateTypeEnum.PATIENT) { allTemplatesDto.getPatientTemplates().add(templateDto); } return templateDto; } @Override protected TherapyTemplateElementDto mapChild() { final TherapyTemplateElementDto templateElementDto = new TherapyTemplateElementDto(); final String therapyJson = next(); final TherapyDto therapyDto = JsonUtil.fromJson( therapyJson, TherapyDto.class, TherapyJsonDeserializer.INSTANCE.getTypeAdapters()); if (therapyDto instanceof ComplexTherapyDto && referenceWeight != null) { if (((ComplexTherapyDto)therapyDto).isContinuousInfusion()) { medicationsBo.fillInfusionRateFromFormula( (ComplexTherapyDto)therapyDto, referenceWeight, patientHeight); } else { medicationsBo.fillInfusionFormulaFromRate( (ComplexTherapyDto)therapyDto, referenceWeight, patientHeight); } } if (therapyDto.getRoutes() != null) { therapyDto.getRoutes() .stream() .filter(medicationRouteDto -> medicationRouteDto.getId() == 0) .forEach(medicationRouteDto -> medicationRouteDto.setId(Long.parseLong(medicationRouteDto.getCode()))); } if (therapyDto instanceof ComplexTherapyDto) { for (final InfusionIngredientDto ingredient : ((ComplexTherapyDto)therapyDto).getIngredientsList()) { if (ingredient.getQuantity() == null && ingredient.getQuantityDenominator() != null) { ingredient.setQuantity(ingredient.getQuantityDenominator()); ingredient.setQuantityUnit(ingredient.getQuantityDenominatorUnit()); ingredient.setQuantityDenominator(null); } } } therapyDisplayProvider.fillDisplayValues(therapyDto, true, true, true, locale, false); templateElementDto.setTherapy(therapyDto); templateElementDto.setCompleted(next(Boolean.class)); return templateElementDto; } @Override protected void associate( final TherapyTemplateDto parentDto, final TherapyTemplateElementDto childDto) { parentDto.getTemplateElements().add(childDto); } }); return allTemplatesDto; }); }  @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.testGetTherapyTemplates.xml") public void testGetTherapyTemplates1() { final TherapyTemplatesDto templatesDto = medicationsDao.getTherapyTemplates("555", "2", TherapyTemplateModeEnum.INPATIENT, "1", null, null, new Locale("en")); assertEquals(2L, (long)templatesDto.getUserTemplates().size()); final TherapyTemplateDto firstUserTemplate = templatesDto.getUserTemplates().get(0); assertEquals(TherapyTemplateTypeEnum.USER, firstUserTemplate.getType()); assertEquals("AAA", firstUserTemplate.getName()); assertEquals("2", firstUserTemplate.getUserId()); assertNull(firstUserTemplate.getCareProviderId()); assertEquals(2L, (long)firstUserTemplate.getTemplateElements().size()); assertTrue(firstUserTemplate.getTemplateElements().get(0).isCompleted()); assertFalse(firstUserTemplate.getTemplateElements().get(1).isCompleted()); final TherapyTemplateDto secondUserTemplate = templatesDto.getUserTemplates().get(1); assertEquals(TherapyTemplateTypeEnum.USER, secondUserTemplate.getType()); assertEquals("BBB", secondUserTemplate.getName()); assertEquals("2", secondUserTemplate.getUserId()); assertNull(secondUserTemplate.getCareProviderId()); assertEquals(1L, (long)secondUserTemplate.getTemplateElements().size()); assertTrue(secondUserTemplate.getTemplateElements().get(0).isCompleted()); assertEquals(1L, (long)templatesDto.getOrganizationTemplates().size()); final TherapyTemplateDto organizationTemplate = templatesDto.getOrganizationTemplates().get(0); assertEquals(TherapyTemplateTypeEnum.ORGANIZATIONAL, organizationTemplate.getType()); assertEquals("CCC", organizationTemplate.getName()); assertNull(organizationTemplate.getUserId()); assertEquals("1", organizationTemplate.getCareProviderId()); assertEquals(1L, (long)organizationTemplate.getTemplateElements().size()); assertTrue(organizationTemplate.getTemplateElements().get(0).isCompleted()); assertEquals(0L, (long)templatesDto.getPatientTemplates().size()); }
@Test @DatabaseSetup("HibernateMedicationsDaoTest.testGetTherapyTemplates.xml") public void testGetTherapyTemplates2() { final TherapyTemplatesDto templatesDto = medicationsDao.getTherapyTemplates("3", "2", TherapyTemplateModeEnum.INPATIENT, null, null, null, new Locale("en")); assertEquals(2L, (long)templatesDto.getUserTemplates().size()); assertEquals(0L, (long)templatesDto.getOrganizationTemplates().size()); assertEquals(1L, (long)templatesDto.getPatientTemplates().size()); final TherapyTemplateDto patientTemplate = templatesDto.getPatientTemplates().get(0); assertEquals(TherapyTemplateTypeEnum.PATIENT, patientTemplate.getType()); assertEquals("DDD", patientTemplate.getName()); assertNull(patientTemplate.getUserId()); assertEquals("3", patientTemplate.getPatientId()); assertEquals(1L, (long)patientTemplate.getTemplateElements().size()); assertTrue(patientTemplate.getTemplateElements().get(0).isCompleted()); }