private Obs getObsGroup() {
    ConceptDatatype datatype = new ConceptDatatype();
    datatype.setUuid(ConceptDatatype.CODED_UUID);
    datatype.setHl7Abbreviation(ConceptDatatype.CODED);

    Concept concept = new Concept(1);
    concept.setDatatype(datatype);
    concept.addName(new ConceptName("MedSet", Locale.ENGLISH));

    ConceptSource source = new ConceptSource();
    source.setName("LOCAL");

    ConceptMap map = new ConceptMap();
    map.setSourceCode("100");
    map.setSource(source);

    concept.addConceptMapping(map);

    Date dateCreated = new Date(213231421890234L);

    Obs obs = new Obs(1);
    obs.setConcept(concept);
    obs.setDateCreated(dateCreated);

    return obs;
  }
 public String getTRValueSetCode(Concept concept) {
   for (ConceptMap mapping : concept.getConceptMappings()) {
     if (mapping.getConceptMapType().getUuid().equals(ConceptMapType.SAME_AS_MAP_TYPE_UUID)) {
       return mapping.getConceptReferenceTerm().getCode();
     }
   }
   for (ConceptName conceptName : concept.getShortNames()) {
     return conceptName.getName();
   }
   return concept.getName().getName();
 }
 public CodeableConceptDt addTRCoding(Concept concept) {
   CodeableConceptDt codeableConcept = new CodeableConceptDt();
   Collection<ConceptMap> conceptMappings = concept.getConceptMappings();
   for (ConceptMap mapping : conceptMappings) {
     if (mapping.getConceptMapType().getUuid().equals(ConceptMapType.SAME_AS_MAP_TYPE_UUID)) {
       addTRCodingsForReferenceTerms(concept, idMappingsRepository, codeableConcept, mapping);
     }
   }
   addTRCodingForConcept(concept, idMappingsRepository, codeableConcept);
   return codeableConcept;
 }
  public List<String> getAncestors(
      UiSessionContext context,
      @RequestParam("termId") String termId,
      @RequestParam("conceptId") String conceptId,
      @RequestParam("updateBy") String updateBy)
      throws Exception {

    Locale locale = context.getLocale();
    ConceptService conceptService = (ConceptService) Context.getService(ConceptService.class);

    ConceptManagementAppsProperties conceptManagementAppsProperties =
        new ConceptManagementAppsProperties();
    ConceptSource conceptSource =
        conceptService.getConceptSourceByUuid(
            conceptManagementAppsProperties.getSnomedCTConceptSourceUuidGlobalProperty(
                ConceptManagementAppsConstants.SNOMED_CT_CONCEPT_SOURCE_UUID_GP));

    if (StringUtils.equals(updateBy, "conceptUpdate")) {

      Collection<ConceptMap> mappings =
          conceptService.getConcept(Integer.parseInt(conceptId)).getConceptMappings();

      for (ConceptMap conceptMap : mappings) {

        if (StringUtils.equals(
                conceptMap.getConceptReferenceTerm().getConceptSource().getUuid(),
                conceptSource.getUuid())
            && StringUtils.equals(
                conceptMap.getConceptMapType().getUuid(),
                ConceptManagementAppsConstants.SAME_AS_CONCEPT_MAP_TYPE_UUID)) {
          termId =
              Integer.toString(conceptMap.getConceptReferenceTerm().getConceptReferenceTermId());
        }
      }
      return getRefTermAncestors(context, termId, locale, conceptSource);
    } else {
      return getRefTermAncestors(context, termId, locale, conceptSource);
    }
  }
 private void addTRCodingsForReferenceTerms(
     Concept concept,
     IdMappingsRepository idMappingsRepository,
     CodeableConceptDt codeableConcept,
     ConceptMap mapping) {
   ConceptReferenceTerm conceptReferenceTerm = mapping.getConceptReferenceTerm();
   IdMapping idMapping = idMappingsRepository.findByInternalId(conceptReferenceTerm.getUuid());
   if (idMapping != null) {
     addFHIRCoding(
         codeableConcept,
         conceptReferenceTerm.getCode(),
         idMapping.getUri(),
         concept.getName().getName());
   }
 }
  /**
   * Gets the display string for a concept map.
   *
   * @param conceptMap the concept map object.
   * @return the display string.
   */
  @Override
  @PropertyGetter("display")
  public String getDisplayString(ConceptMap conceptMap) {
    if (conceptMap.getConceptReferenceTerm() == null
        || conceptMap.getConceptReferenceTerm().getConceptSource() == null) {
      return "";
    }

    String display =
        conceptMap.getConceptReferenceTerm().getConceptSource().getName()
            + ": "
            + conceptMap.getConceptReferenceTerm().getCode();
    if (!StringUtils.isBlank(conceptMap.getConceptReferenceTerm().getName())) {
      display += " (" + conceptMap.getConceptReferenceTerm().getName() + ")";
    }
    return display;
  }
  @Test
  public void shouldEvaluateOBRORUR01TemplateForOBSGroup() throws Exception {
    // given
    Encounter encounter = new Encounter();

    Date date = new Date();
    encounter.setEncounterDatetime(date);
    encounter.setUuid("ENCOUNTER UUID");

    EncounterType encounterType = new EncounterType();
    encounterType.setName("ENCOUNTER TYPE NAME");
    encounter.setEncounterType(encounterType);

    Location location = new Location(1);
    location.setUuid("LOCATION UUID");
    location.setName("LOCATION NAME");
    encounter.setLocation(location);

    Person provider = new Person(1);
    provider.setUuid("PROVIDER UUID");
    provider.addName(
        new PersonName("PROVIDER GIVENNAME", "PROVIDER MIDDLENAME", "PROVIDER FAMILYNAME"));
    encounter.setProvider(provider);

    ConceptSource source = new ConceptSource();
    source.setName("AMPATH");

    ConceptMap map = new ConceptMap();
    map.setSourceCode("200");
    map.setSource(source);

    ConceptDatatype datatype = new ConceptDatatype();
    datatype.setUuid(ConceptDatatype.NUMERIC_UUID);
    datatype.setHl7Abbreviation(ConceptDatatype.NUMERIC);

    ConceptNumeric concept = new ConceptNumeric();
    concept.setDatatype(datatype);
    concept.addConceptMapping(map);
    concept.addName(new ConceptName("NumericConcept", Locale.ENGLISH));
    concept.setUnits("mg");

    Date dateCreated = new Date(213231421890234L);

    Obs obs = new Obs(2);
    obs.setConcept(concept);
    obs.setDateCreated(dateCreated);
    obs.setValueNumeric(10d);

    obs.setObsGroup(getObsGroup());
    obs.getObsGroup().addGroupMember(obs);

    encounter.addObs(obs);

    obs = new Obs(3);
    obs.setConcept(concept);
    obs.setDateCreated(dateCreated);
    obs.setValueNumeric(23d);
    encounter.addObs(obs);

    Map<String, Object> bindings = new HashMap<String, Object>();
    bindings.put("encounter", encounter);
    bindings.put("implementationId", "MVP");

    // when
    HL7Template hl7Template = hl7QueryService.getHL7TemplateByName("Generic Obs Group");
    String evaluatedTemplate = hl7QueryService.evaluateTemplate(hl7Template, bindings);

    // then
    evaluatedTemplate = StringUtils.deleteWhitespace(evaluatedTemplate);
    Assert.assertEquals(
        "<ORU_R01.ORDER_OBSERVATION><OBR><OBR.1>0</OBR.1><OBR.4><CE.1>100</CE.1>"
            + "<CE.2>MEDICALRECORDOBSERVATIONS</CE.2><CE.3>LOCAL</CE.3></OBR.4><OBR.18>0</OBR.18>"
            + "<OBR.29><EIP.2><EI.3>ENCOUNTERUUID</EI.3></EIP.2></OBR.29></OBR><ORU_R01.OBSERVATION>"
            + "<OBX><OBX.1>1</OBX.1><OBX.2>NM</OBX.2><OBX.3><CE.1>200</CE.1><CE.2>NumericConcept</CE.2>"
            + "<CE.3>AMPATH</CE.3></OBX.3><OBX.5>23.0</OBX.5><OBX.6><CE.1>mg</CE.1><CE.3>UCUM</CE.3></OBX.6>"
            + "<OBX.14><TS.1>"
            + new HL7TemplateFunctions().formatDate(dateCreated, null)
            + "</TS.1>"
            + "</OBX.14></OBX></ORU_R01.OBSERVATION></ORU_R01.ORDER_OBSERVATION>"
            + "<ORU_R01.ORDER_OBSERVATION><OBR><OBR.1>2</OBR.1><OBR.4><CE.1>100</CE.1><CE.2>MedSet</CE.2>"
            + "<CE.3>LOCAL</CE.3></OBR.4><OBR.18>0</OBR.18><OBR.29><EIP.2><EI.3>ENCOUNTERUUID</EI.3></EIP.2></OBR.29"
            + "></OBR><ORU_R01.OBSERVATION><OBX><OBX.1>2</OBX.1><OBX.2>NM</OBX.2><OBX.3><CE.1>200</CE.1>"
            + "<CE.2>NumericConcept</CE.2><CE.3>AMPATH</CE.3></OBX.3><OBX.5>10.0</OBX.5><OBX.6><CE.1>mg</CE.1>"
            + "<CE.3>UCUM</CE.3></OBX.6><OBX.14><TS.1>"
            + new HL7TemplateFunctions().formatDate(dateCreated, null)
            + "</TS.1></OBX.14></OBX></ORU_R01.OBSERVATION></ORU_R01.ORDER_OBSERVATION>",
        evaluatedTemplate);
  }