@SuppressWarnings("rawtypes")
 private String describeValidationParameters(Parameters pin) {
   CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
   for (ParametersParameterComponent p : pin.getParameter()) {
     if (p.hasValue() && p.getValue() instanceof PrimitiveType) {
       b.append(p.getName() + "=" + ((PrimitiveType) p.getValue()).asStringValue());
     } else if (p.hasValue() && p.getValue() instanceof Coding) {
       b.append("system=" + ((Coding) p.getValue()).getSystem());
       b.append("code=" + ((Coding) p.getValue()).getCode());
       b.append("display=" + ((Coding) p.getValue()).getDisplay());
     } else if (p.hasValue() && p.getValue() instanceof CodeableConcept) {
       if (((CodeableConcept) p.getValue()).hasCoding()) {
         Coding c = ((CodeableConcept) p.getValue()).getCodingFirstRep();
         b.append("system=" + c.getSystem());
         b.append("code=" + c.getCode());
         b.append("display=" + c.getDisplay());
       } else if (((CodeableConcept) p.getValue()).hasText()) {
         b.append("text=" + ((CodeableConcept) p.getValue()).getText());
       }
     } else if (p.hasResource() && (p.getResource() instanceof ValueSet)) {
       b.append("valueset=" + getVSSummary((ValueSet) p.getResource()));
     }
   }
   return b.toString();
 }
 protected void codedTriple(Subject subject, String predicate, CodeableConcept cc) {
   for (Coding c : cc.getCoding()) {
     String s = getLinkedForm(c);
     if (s != null)
       subject.predicate(
           predicate, new StringType(s), c.hasDisplay() ? c.getDisplay() : cc.getText());
   }
 }
 protected void codedTriple(
     Section section, String subject, String predicate, CodeableConcept cc) {
   for (Coding c : cc.getCoding()) {
     String s = getLinkedForm(c);
     if (s != null)
       section.triple(subject, predicate, s, c.hasDisplay() ? c.getDisplay() : cc.getText());
   }
 }
 protected String getLinkedForm(Coding c) {
   if (c.hasSystem()) {
     if (c.getSystem().equals("http://loinc.org")) {
       prefixes.put("loinc", "http://loinc.org/");
       return "loinc:" + c.getCode();
     }
   }
   return null;
 }
 private String cacheId(Coding coding) {
   return "|"
       + coding.getSystem()
       + "|"
       + coding.getVersion()
       + "|"
       + coding.getCode()
       + "|"
       + coding.getDisplay();
 }
 private ValidationResult verifyCodeInternal(ValueSet vs, CodeableConcept code) throws Exception {
   for (Coding c : code.getCoding()) {
     ValidationResult res = verifyCodeInternal(vs, c.getSystem(), c.getCode(), c.getDisplay());
     if (res.isOk()) return res;
   }
   if (code.getCoding().isEmpty())
     return new ValidationResult(IssueSeverity.ERROR, "None code provided");
   else
     return new ValidationResult(
         IssueSeverity.ERROR, "None of the codes are in the specified value set");
 }
  private ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateCodeIsInContains(
      List<ValueSetExpansionContainsComponent> contains,
      String theSystem,
      String theCode,
      Coding theCoding,
      CodeableConcept theCodeableConcept) {
    for (ValueSetExpansionContainsComponent 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 (Coding 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(
      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);
  }
 @Override
 public ValidationResult validateCode(Coding code, ValueSet vs) {
   if (codeSystems.containsKey(code.getSystem()) && codeSystems.get(code.getSystem()) != null)
     try {
       return verifyCodeInCodeSystem(
           codeSystems.get(code.getSystem()), code.getSystem(), code.getCode(), code.getDisplay());
     } catch (Exception e) {
       return new ValidationResult(
           IssueSeverity.FATAL,
           "Error validating code \""
               + code
               + "\" in system \""
               + code.getSystem()
               + "\": "
               + e.getMessage());
     }
   else if (vs.hasExpansion())
     try {
       return verifyCodeInternal(vs, code.getSystem(), code.getCode(), code.getDisplay());
     } catch (Exception e) {
       return new ValidationResult(
           IssueSeverity.FATAL,
           "Error validating code \""
               + code
               + "\" in system \""
               + code.getSystem()
               + "\": "
               + e.getMessage());
     }
   else
     try {
       return verifyCodeExternal(vs, code, true);
     } catch (Exception e) {
       return new ValidationResult(
           IssueSeverity.WARNING,
           "Error validating code \""
               + code
               + "\" in system \""
               + code.getSystem()
               + "\": "
               + e.getMessage());
     }
 }