private boolean notcomplete(ValueSet vs) {
   if (!vs.hasExpansion()) return true;
   if (!vs.getExpansion()
       .getExtensionsByUrl("http://hl7.org/fhir/StructureDefinition/valueset-unclosed")
       .isEmpty()) return true;
   if (!vs.getExpansion()
       .getExtensionsByUrl("http://hl7.org/fhir/StructureDefinition/valueset-toocostly")
       .isEmpty()) return true;
   return false;
 }
 private ValidationResult verifyCodeInExpansion(ValueSet vs, String code) throws FHIRException {
   if (vs.getExpansion()
       .hasExtension("http://hl7.org/fhir/StructureDefinition/valueset-toocostly")) {
     throw new FHIRException("Unable to validate core - value set is too costly to expand");
   } else {
     ValueSetExpansionContainsComponent cc = findCode(vs.getExpansion().getContains(), code);
     if (cc == null)
       return new ValidationResult(
           IssueSeverity.ERROR, "Unknown Code " + code + " in " + vs.getUrl());
     return null;
   }
 }
 @Override
 public ValueSetExpansionComponent expandVS(ConceptSetComponent inc, boolean heirachical)
     throws TerminologyServiceException {
   ValueSet vs = new ValueSet();
   vs.setCompose(new ValueSetComposeComponent());
   vs.getCompose().getInclude().add(inc);
   ValueSetExpansionOutcome vse = expandVS(vs, true, heirachical);
   ValueSet valueset = vse.getValueset();
   if (valueset == null)
     throw new TerminologyServiceException("Error Expanding ValueSet: " + vse.getError());
   return valueset.getExpansion();
 }
 private ValidationResult verifyCodeInExpansion(
     ValueSet vs, String system, String code, String display) {
   ValueSetExpansionContainsComponent cc = findCode(vs.getExpansion().getContains(), code);
   if (cc == null)
     return new ValidationResult(
         IssueSeverity.ERROR, "Unknown Code " + code + " in " + vs.getUrl());
   if (display == null)
     return new ValidationResult(
         new ConceptDefinitionComponent().setCode(code).setDisplay(cc.getDisplay()));
   if (cc.hasDisplay()) {
     if (display.equalsIgnoreCase(cc.getDisplay()))
       return new ValidationResult(
           new ConceptDefinitionComponent().setCode(code).setDisplay(cc.getDisplay()));
     return new ValidationResult(
         IssueSeverity.WARNING,
         "Display Name for " + code + " must be '" + cc.getDisplay() + "'",
         new ConceptDefinitionComponent().setCode(code).setDisplay(cc.getDisplay()));
   }
   return null;
 }
  @Override
  public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateCode(
      IPrimitiveType<String> theValueSetIdentifier,
      IIdType theId,
      IPrimitiveType<String> theCode,
      IPrimitiveType<String> theSystem,
      IPrimitiveType<String> theDisplay,
      Coding theCoding,
      CodeableConcept theCodeableConcept,
      RequestDetails theRequestDetails) {

    List<IIdType> valueSetIds = Collections.emptyList();

    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 (!LogicUtil.multiXor(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 IdType("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.");
      }
      // String code = theCode.getValue();
      // String system = toStringOrNull(theSystem);
      LookupCodeResult result = myCodeSystemDao.lookupCode(theCode, theSystem, null, null);
      if (result.isFound()) {
        ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult retVal =
            new ValidateCodeResult(true, "Found code", result.getCodeDisplay());
        return retVal;
      }
    }

    //		if (valueSetIds.isEmpty()) {
    //			if (haveIdentifierParam) {
    //				myValidationSupport.expandValueSet(getContext(), include);
    //				if (myValidationSupport.isCodeSystemSupported(getContext(),
    // theValueSetIdentifier.getValue())) {
    //					String system = toStringOrNull(theSystem);
    //					String code = toStringOrNull(theCode);
    //					String display = toStringOrNull(theDisplay);
    //					CodeValidationResult result = myValidationSupport.validateCode(getContext(), system,
    // code, display);
    //					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 new ValidateCodeResult(true, "Code validates", result.getDisplay());
    //					}
    //				}
    //			}
    //		}

    for (IIdType nextId : valueSetIds) {
      ValueSet expansion = expand(nextId, null, theRequestDetails);
      List<ValueSetExpansionContainsComponent> 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);
  }