public void addFHIRCoding(
     CodeableConceptDt codeableConcept, String code, String system, String display) {
   CodingDt coding = codeableConcept.addCoding();
   coding.setCode(code);
   coding.setSystem(system);
   coding.setDisplay(display);
 }
 public CodeableConceptDt addTRCodingOrDisplay(Concept concept) {
   CodeableConceptDt codeableConceptDt = addTRCoding(concept);
   if (CollectionUtils.isEmpty(codeableConceptDt.getCoding())) {
     CodingDt coding = codeableConceptDt.addCoding();
     coding.setDisplay(concept.getName().getName());
   }
   return codeableConceptDt;
 }
 public CodeableConceptDt getTRValueSetCodeableConcept(
     Concept concept, String valueSetURL, CodeableConceptDt codeableConcept) {
   CodingDt coding = codeableConcept.addCoding();
   if (null != idMappingsRepository.findByInternalId(concept.getUuid())) {
     coding.setCode(getTRValueSetCode(concept));
     coding.setSystem(valueSetURL);
   }
   coding.setDisplay(concept.getName().getName());
   return codeableConcept;
 }
  private ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateCodeIsInContains(
      List<ExpansionContains> contains,
      String theSystem,
      String theCode,
      CodingDt theCoding,
      CodeableConceptDt theCodeableConcept) {
    for (ExpansionContains nextCode : contains) {
      ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult result =
          validateCodeIsInContains(
              nextCode.getContains(), theSystem, theCode, theCoding, theCodeableConcept);
      if (result != null) {
        return result;
      }

      String system = nextCode.getSystem();
      String code = nextCode.getCode();

      if (isNotBlank(theCode)) {
        if (theCode.equals(code) && (isBlank(theSystem) || theSystem.equals(system))) {
          return new ValidateCodeResult(true, "Validation succeeded", nextCode.getDisplay());
        }
      } else if (theCoding != null) {
        if (StringUtils.equals(system, theCoding.getSystem())
            && StringUtils.equals(code, theCoding.getCode())) {
          return new ValidateCodeResult(true, "Validation succeeded", nextCode.getDisplay());
        }
      } else {
        for (CodingDt next : theCodeableConcept.getCoding()) {
          if (StringUtils.equals(system, next.getSystem())
              && StringUtils.equals(code, next.getCode())) {
            return new ValidateCodeResult(true, "Validation succeeded", nextCode.getDisplay());
          }
        }
      }
    }

    return null;
  }
  @Override
  public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateCode(
      UriDt theValueSetIdentifier,
      IIdType theId,
      CodeDt theCode,
      UriDt theSystem,
      StringDt theDisplay,
      CodingDt theCoding,
      CodeableConceptDt theCodeableConcept) {
    List<IIdType> valueSetIds;

    boolean haveCodeableConcept =
        theCodeableConcept != null && theCodeableConcept.getCoding().size() > 0;
    boolean haveCoding = theCoding != null && theCoding.isEmpty() == false;
    boolean haveCode = theCode != null && theCode.isEmpty() == false;

    if (!haveCodeableConcept && !haveCoding && !haveCode) {
      throw new InvalidRequestException("No code, coding, or codeableConcept provided to validate");
    }
    if (!(haveCodeableConcept ^ haveCoding ^ haveCode)) {
      throw new InvalidRequestException(
          "$validate-code can only validate (system AND code) OR (coding) OR (codeableConcept)");
    }

    boolean haveIdentifierParam =
        theValueSetIdentifier != null && theValueSetIdentifier.isEmpty() == false;
    if (theId != null) {
      valueSetIds = Collections.singletonList(theId);
    } else if (haveIdentifierParam) {
      Set<Long> ids =
          searchForIds(
              ValueSet.SP_IDENTIFIER, new TokenParam(null, theValueSetIdentifier.getValue()));
      valueSetIds = new ArrayList<IIdType>();
      for (Long next : ids) {
        valueSetIds.add(new IdDt("ValueSet", next));
      }
    } else {
      if (theCode == null || theCode.isEmpty()) {
        throw new InvalidRequestException(
            "Either ValueSet ID or ValueSet identifier or system and code must be provided. Unable to validate.");
      }
      Set<Long> ids =
          searchForIds(
              ValueSet.SP_CODE, new TokenParam(toStringOrNull(theSystem), theCode.getValue()));
      valueSetIds = new ArrayList<IIdType>();
      for (Long next : ids) {
        valueSetIds.add(new IdDt("ValueSet", next));
      }
    }

    for (IIdType nextId : valueSetIds) {
      ValueSet expansion = expand(nextId, null);
      List<ExpansionContains> contains = expansion.getExpansion().getContains();
      ValidateCodeResult result =
          validateCodeIsInContains(
              contains,
              toStringOrNull(theSystem),
              toStringOrNull(theCode),
              theCoding,
              theCodeableConcept);
      if (result != null) {
        if (theDisplay != null
            && isNotBlank(theDisplay.getValue())
            && isNotBlank(result.getDisplay())) {
          if (!theDisplay.getValue().equals(result.getDisplay())) {
            return new ValidateCodeResult(
                false, "Display for code does not match", result.getDisplay());
          }
        }
        return result;
      }
    }

    return new ValidateCodeResult(false, "Code not found", null);
  }