Esempio n. 1
0
        /// <summary>
        /// Make all references in resource contained local references.
        /// </summary>
        /// <param name="b"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        DomainResource SetReferencesContained(Dictionary <String, DomainResource> entryDict,
                                              List <String> containNames,
                                              DomainResource dr)
        {
            String FixRef(String json)
            {
                Int32 start = 0;

                while (true)
                {
                    Int32 i = json.IndexOf("\"reference\":", start);
                    if (i < 0)
                    {
                        return(json);
                    }
                    Int32  refStart  = json.IndexOf("\"", i + 12) + 1;
                    String firstPart = json.Substring(0, refStart);
                    Int32  refEnd    = json.IndexOf("\"", refStart + 1);
                    String lastPart  = json.Substring(refEnd);
                    String reference = json.Substring(refStart, refEnd - refStart);
                    if (containNames.Contains(reference) == false)
                    {
                        containNames.Add(reference);
                    }
                    reference = reference.Replace("/", ".");
                    json      = firstPart + "#" + reference;
                    start     = json.Length;
                    json      = json + lastPart;
                }
            }

            String json = dr.ToFormatedJson();

            json = FixRef(json);

            FhirJsonParser parser = new FhirJsonParser();

            dr = parser.Parse <DomainResource>(json);
            return(dr);
        }
Esempio n. 2
0
        /// <summary>
        /// Note: This modifies the resource in b.
        /// </summary>
        void SplitExampleBundle(Bundle b, String prefix)
        {
            Dictionary <String, DomainResource> entryDict = new Dictionary <string, DomainResource>();

            void Write(Bundle.EntryComponent entry)
            {
                DomainResource dr      = (DomainResource)entry.Resource;
                String         profile = dr?.Meta?.Profile?.FirstOrDefault();

                if (profile == null)
                {
                    return;
                }
                if (profile.StartsWith("http://hl7.org/fhir/us/breast-radiology/") == false)
                {
                    return;
                }

                List <String> containNames = new List <string>();

                dr = this.SetReferencesContained(entryDict, containNames, dr);

                // If there are contained resource to add, parse this into a domain resource
                // add the contained resources and write it out again.
                if (containNames.Count > 0)
                {
                    foreach (String containName in containNames)
                    {
                        if (entryDict.TryGetValue(containName,
                                                  out DomainResource containedResource) == false)
                        {
                            throw new Exception($"Can not find bundle entry {containName}");
                        }
                        containedResource = this.SetReferencesContained(entryDict,
                                                                        containNames,
                                                                        containedResource);
                        dr.Contained.Add(containedResource);
                    }
                }

                String json = dr.ToFormatedJson();
                String path = this.ExamplePath(prefix, dr);

                File.WriteAllText(path, json);
            }

            foreach (Bundle.EntryComponent entry in b.Entry)
            {
                DomainResource dr = (DomainResource)entry.Resource;

                // make a copy so we dont change the original id.
                String         json   = dr.ToFormatedJson();
                FhirJsonParser parser = new FhirJsonParser();
                dr = parser.Parse <DomainResource>(json);

                dr.Id = $"{dr.TypeName}.{dr.Id}";
                entryDict.Add(entry.FullUrl, dr);
            }

            foreach (Bundle.EntryComponent entry in b.Entry)
            {
                Write(entry);
            }
        }