public FhirImplementationGuide Convert(ImplementationGuide ig, SummaryType?summaryType = null, bool includeVocabulary = true) { var parserSettings = new fhir_stu3.Hl7.Fhir.Serialization.ParserSettings(); parserSettings.AcceptUnknownMembers = true; parserSettings.AllowUnrecognizedEnums = true; parserSettings.DisallowXsiAttributesOnRoot = false; var fhirXmlParser = new FhirXmlParser(parserSettings); var fhirJsonParser = new FhirJsonParser(parserSettings); string url = string.Format("ImplementationGuide/{0}", ig.Id); if (!string.IsNullOrEmpty(ig.Identifier) && (ig.Identifier.StartsWith("http://") || ig.Identifier.StartsWith("https://"))) { url = ig.Identifier.TrimEnd('/') + "/" + url.TrimStart('/'); } var fhirImplementationGuide = new FhirImplementationGuide() { Id = ig.Id.ToString(), Name = ig.Name, Url = url }; // Status if (ig.PublishStatus == PublishStatus.GetPublishedStatus(this.tdb)) { fhirImplementationGuide.Status = PublicationStatus.Active; } else if (ig.PublishStatus == PublishStatus.GetRetiredStatus(this.tdb) || ig.PublishStatus == PublishStatus.GetDeprecatedStatus(this.tdb)) { fhirImplementationGuide.Status = PublicationStatus.Retired; } else { fhirImplementationGuide.Status = PublicationStatus.Draft; } if (summaryType == null || summaryType == SummaryType.Data) { // Package FhirImplementationGuide.PackageComponent package = new FhirImplementationGuide.PackageComponent(); package.Name = "Profiles in this Implementation Guide"; fhirImplementationGuide.Package.Add(package); // Page: Create a page for the implementation guide. This is required by the fhir ig publisher fhirImplementationGuide.Page = new FhirImplementationGuide.PageComponent(); fhirImplementationGuide.Page.Kind = FhirImplementationGuide.GuidePageKind.Page; fhirImplementationGuide.Page.Title = ig.GetDisplayName(); fhirImplementationGuide.Page.Source = string.Format("{0}://{1}/IG/View/{2}", this.scheme, this.authority, ig.Id); // Add profiles to the implementation guide List <Template> templates = ig.GetRecursiveTemplates(this.tdb, inferred: false); var profileResources = (from t in templates.OrderBy(y => y.ImpliedTemplateId) select new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("StructureDefinition/{0}", t.FhirId()), Display = t.Name } }); package.Resource.AddRange(profileResources); if (includeVocabulary) { // Add value sets to the implementation guide var valueSetsIds = (from t in templates join tc in this.tdb.TemplateConstraints.AsNoTracking() on t.Id equals tc.TemplateId where tc.ValueSetId != null select tc.ValueSetId) .Distinct() .ToList(); var valueSets = (from vs in this.tdb.ValueSets join vsi in valueSetsIds on vs.Id equals vsi select vs).ToList(); var valueSetResources = (from vs in valueSets where vs.GetIdentifier() != null && !vs.GetIdentifier().StartsWith("http://hl7.org/fhir/ValueSet/") // Ignore value sets in the base spec select new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("ValueSet/{0}", vs.GetFhirId()), Display = vs.Name } }); package.Resource.AddRange(valueSetResources); } // Add each of the individual FHIR resources added as files to the IG foreach (var file in ig.Files) { var fileData = file.GetLatestData(); Resource resource = null; try { string fileContent = System.Text.Encoding.UTF8.GetString(fileData.Data); if (file.MimeType == "application/xml" || file.MimeType == "text/xml") { resource = fhirXmlParser.Parse <Resource>(fileContent); } else if (file.MimeType == "application/json") { resource = fhirJsonParser.Parse <Resource>(fileContent); } } catch { } if (resource == null || string.IsNullOrEmpty(resource.Id)) { continue; } var packageFile = new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("{0}/{1}", resource.ResourceType, resource.Id), Display = GetResourceName(resource, file.FileName) } }; package.Resource.Add(packageFile); } // Add each of the samples generated for the template/profile var templateExamples = (from t in templates join ts in this.tdb.TemplateSamples on t.Id equals ts.TemplateId select new { Template = t, Sample = ts }); foreach (var templateExample in templateExamples) { Resource resource = null; try { resource = fhirXmlParser.Parse <Resource>(templateExample.Sample.XmlSample); } catch { } try { if (resource == null) { resource = fhirJsonParser.Parse <Resource>(templateExample.Sample.XmlSample); } } catch { } if (resource == null || string.IsNullOrEmpty(resource.Id)) { continue; } var packageExample = new FhirImplementationGuide.ResourceComponent() { Example = true, Source = new ResourceReference() { Reference = string.Format("{0}/{1}", resource.ResourceType, resource.Id), Display = GetResourceName(resource, templateExample.Sample.Name) }, ExampleFor = new ResourceReference() { Reference = string.Format("StructureDefinition/{0}", templateExample.Template.Bookmark), Display = templateExample.Template.Name } }; package.Resource.Add(packageExample); } } return(fhirImplementationGuide); }
public FhirImplementationGuide Convert(ImplementationGuide ig, SummaryType?summaryType = null, bool includeVocabulary = true) { var fhirImplementationGuide = new FhirImplementationGuide() { Id = ig.Id.ToString(), Name = ig.Name, Url = string.Format("ImplementationGuide/{0}", ig.Id) }; // Status if (ig.PublishStatus == PublishStatus.GetPublishedStatus(this.tdb)) { fhirImplementationGuide.Status = ConformanceResourceStatus.Active; } else if (ig.PublishStatus == PublishStatus.GetRetiredStatus(this.tdb) || ig.PublishStatus == PublishStatus.GetDeprecatedStatus(this.tdb)) { fhirImplementationGuide.Status = ConformanceResourceStatus.Retired; } else { fhirImplementationGuide.Status = ConformanceResourceStatus.Draft; } if (summaryType == null || summaryType == SummaryType.Data) { // Package FhirImplementationGuide.PackageComponent package = new FhirImplementationGuide.PackageComponent(); package.Name = "Profiles in this Implementation Guide"; fhirImplementationGuide.Package.Add(package); // Page: Create a page for the implementation guide. This is required by the fhir ig publisher fhirImplementationGuide.Page = new FhirImplementationGuide.PageComponent(); fhirImplementationGuide.Page.Kind = FhirImplementationGuide.GuidePageKind.Page; fhirImplementationGuide.Page.Title = ig.GetDisplayName(); fhirImplementationGuide.Page.Source = string.Format("{0}://{1}/IG/View/{2}", this.scheme, this.authority, ig.Id); // Add profiles to the implementation guide List <Template> templates = ig.GetRecursiveTemplates(this.tdb, inferred: false); var profileResources = (from t in templates select new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("StructureDefinition/{0}", t.FhirId()), Display = t.Name } }); package.Resource.AddRange(profileResources); if (includeVocabulary) { // Add value sets to the implementation guide var valueSets = (from t in templates join tc in this.tdb.TemplateConstraints on t.Id equals tc.TemplateId where tc.ValueSet != null select tc.ValueSet).Distinct().ToList(); var valueSetResources = (from vs in valueSets select new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("ValueSet/{0}", vs.Id.ToString()), Display = vs.Name } }); package.Resource.AddRange(valueSetResources); } // Add each of the individual FHIR resources added as files to the IG foreach (var file in ig.Files) { var fileData = file.GetLatestData(); Resource resource = null; try { string fileContent = System.Text.Encoding.UTF8.GetString(fileData.Data); if (file.MimeType == "application/xml" || file.MimeType == "text/xml") { resource = FhirParser.ParseResourceFromXml(fileContent); } else if (file.MimeType == "application/json") { resource = FhirParser.ParseResourceFromJson(fileContent); } } catch { } if (resource == null || string.IsNullOrEmpty(resource.Id)) { continue; } var packageFile = new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("{0}/{1}", resource.ResourceType, resource.Id), Display = GetResourceName(resource, file.FileName) } }; package.Resource.Add(packageFile); } // Add each of the samples generated for the template/profile var templateExamples = (from t in templates join ts in this.tdb.TemplateSamples on t.Id equals ts.TemplateId select new { Template = t, Sample = ts }); foreach (var templateExample in templateExamples) { Resource resource = null; try { resource = FhirParser.ParseResourceFromXml(templateExample.Sample.XmlSample); } catch { } try { if (resource == null) { resource = FhirParser.ParseResourceFromJson(templateExample.Sample.XmlSample); } } catch { } if (resource == null || string.IsNullOrEmpty(resource.Id)) { continue; } var packageExample = new FhirImplementationGuide.ResourceComponent() { Example = true, Source = new ResourceReference() { Reference = string.Format("{0}/{1}", resource.ResourceType, resource.Id), Display = GetResourceName(resource, templateExample.Sample.Name) } }; package.Resource.Add(packageExample); } } return(fhirImplementationGuide); }