/// <summary> /// Converts a <see cref="Concept"/> instance to an <see cref="FhirCodeableConcept"/> instance. /// </summary> /// <param name="concept">The concept to be converted to a <see cref="FhirCodeableConcept"/></param> /// <param name="preferredCodeSystem">The preferred code system for the codeable concept</param> /// <param name="nullIfNoPreferred">When true, instructs the system to return only code in preferred code system or nothing</param> /// <returns>Returns a FHIR codeable concept.</returns> public static FhirCodeableConcept ToFhirCodeableConcept(Concept concept, String preferredCodeSystem = null, bool nullIfNoPreferred = false) { traceSource.TraceEvent(EventLevel.Verbose, "Mapping concept"); if (concept == null) { return(null); } if (String.IsNullOrEmpty(preferredCodeSystem)) { return new FhirCodeableConcept { Coding = concept.LoadCollection <ConceptReferenceTerm>(nameof(Concept.ReferenceTerms)).Select(o => DataTypeConverter.ToCoding(o.LoadProperty <ReferenceTerm>(nameof(ConceptReferenceTerm.ReferenceTerm)))).ToList(), Text = concept.LoadCollection <ConceptName>(nameof(Concept.ConceptNames)).FirstOrDefault()?.Name } } ; else { var codeSystemService = ApplicationServiceContext.Current.GetService <IConceptRepositoryService>(); var refTerm = codeSystemService.GetConceptReferenceTerm(concept.Key.Value, preferredCodeSystem); if (refTerm == null && nullIfNoPreferred) { return(null); // No code in the preferred system, ergo, we will instead use our own } else if (refTerm == null) { return new FhirCodeableConcept { Coding = concept.LoadCollection <ConceptReferenceTerm>(nameof(Concept.ReferenceTerms)).Select(o => DataTypeConverter.ToCoding(o.LoadProperty <ReferenceTerm>(nameof(ConceptReferenceTerm.ReferenceTerm)))).ToList(), Text = concept.LoadCollection <ConceptName>(nameof(Concept.ConceptNames)).FirstOrDefault()?.Name } } ; else { return new FhirCodeableConcept { Coding = new List <FhirCoding>() { ToCoding(refTerm) }, Text = concept.LoadCollection <ConceptName>(nameof(Concept.ConceptNames)).FirstOrDefault()?.Name } }; } }