private Device initializeDevice(Patient p) {
    log("Initializing device");

    Device d = newDevice();
    d.setManufacturer(deviceInfo.getManufacturerName());
    d.setModel(deviceInfo.getModelNumber());
    d.setVersion(deviceInfo.getFirmwareRevision());
    NarrativeDt n = getNarrative(d);
    n.setDiv(
        n.getDiv()
            .getValueAsString()
            .substring(5, n.getDiv().getValueAsString().length() - 6)
            .concat("<strong>Serial Number: </strong>")
            .concat(deviceInfo.getSerialNumber())
            .concat("<br /><strong>Name: </strong>")
            .concat(deviceInfo.getName())
            .concat("<br /><strong>MAC Address: </strong>")
            .concat(deviceInfo.getAddress()));
    d.setText(n);
    CodeableConceptDt type = new CodeableConceptDt();
    // TODO find valid url
    type.addCoding()
        .setCode("15-102")
        .setDisplay("Glukose-Analysegerät")
        .setSystem("http://umdns.org");
    d.setType(type);

    d.setPatient(newResourceReference(p.getId()));
    d.setIdentifier(Arrays.asList(getIdentifier("/devices/", "" + deviceInfo.getSysID())));

    log("Initializing device done");

    return d;
  }
  private DeviceMetric initializeDeviceMetric(Device d) {
    log("Initializing device metric");

    DeviceMetric metric = new DeviceMetric();
    CodeableConceptDt metricType = new CodeableConceptDt();
    // TODO is this the right code and display?
    metricType
        .addCoding()
        .setCode("160020")
        .setSystem("https://rtmms.nist.gov")
        .setDisplay("MDC_CONC_GLU_GEN");
    metric.setType(metricType);
    metric.setSource(newResourceReference(d.getId()));
    metric.setText(getNarrative(metric));
    metric.setCategory(DeviceMetricCategoryEnum.MEASUREMENT);
    CodeableConceptDt unitConcept = new CodeableConceptDt();
    unitConcept
        .addCoding()
        .setDisplay("MDC_DIM_MILLI_MOLE_PER_L")
        .setSystem("https://rtmms.nist.gov")
        .setCode("266866");
    metric.setUnit(unitConcept);
    // TODO what is the identifier here??? especially the value
    metric.setIdentifier(
        new IdentifierDt().setSystem(SYSTEM_URL + "/metric/").setValue("glucosemillimolperliter"));

    log("Initializing device metric done.");
    return metric;
  }
 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 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 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 CodeableConceptDt createCode(int unit) {
    log("get code done");
    CodeableConceptDt codeableConceptDt = new CodeableConceptDt();
    CodingDt coding = new CodingDt();

    String code = "", display = "";
    switch (unit) {
      case GlucoseRecord.UNIT_kgpl:
        code = "2339-0";
        display = "Glucose [Mass/volume] in Blood";
        break;
      case GlucoseRecord.UNIT_molpl:
        code = "15074-8";
        display = "Glucose [Moles/volume] in Blood";
        break;
    }

    codeableConceptDt.addCoding().setSystem("http://loinc.org").setCode(code).setDisplay(display);
    log("get code done");
    return codeableConceptDt;
  }
  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);
  }