示例#1
0
        private static List <ResourceReference> AllBaseReferences(this Base FhirBase)
        {
            //Cache
            if (ClassPropertyMappingListCache == null)
            {
                ClassPropertyMappingListCache = new Dictionary <int, IEnumerable <Hl7.Fhir.Introspection.PropertyMapping> >();
            }

            var ReferenceResultList = new List <ResourceReference>();

            if (FhirBase == null)
            {
                return(ReferenceResultList);
            }

            //If DomainResource of Extention then drill through all extentions and collect all referances
            if (FhirBase is DomainResource DomainResource)
            {
                ReferenceResultList.AddRange(DomainResource.Extension.AllExtensionListReferences());
            }
            else if (FhirBase is Extension Extension)
            {
                ReferenceResultList.AddRange(Extension.AllExtensionReferences());
            }

            //Cache ClassMappings's PropertyList as likley to have same resource again in the bundle entries
            IEnumerable <Hl7.Fhir.Introspection.PropertyMapping> PropertyMappingList = ClassPropertyMappingListCache.SingleOrDefault(x => x.Key == FhirBase.TypeName.GetHashCode()).Value;

            if (PropertyMappingList == null)
            {
                var ClassMapping = Hl7.Fhir.Introspection.ClassMapping.Create(FhirBase.GetType());
                PropertyMappingList = ClassMapping.PropertyMappings.Where(t => t.ElementType == typeof(ResourceReference) || t.ElementType.BaseType == typeof(BackboneElement));
                ClassPropertyMappingListCache.Add(FhirBase.TypeName.GetHashCode(), PropertyMappingList);
            }

            foreach (var PropertyItem in PropertyMappingList)
            {
                if (PropertyItem.ElementType.BaseType == typeof(BackboneElement))
                {
                    if (PropertyItem.IsCollection)
                    {
                        var PropertyCollection = PropertyItem.GetValue(FhirBase) as System.Collections.IEnumerable;
                        foreach (var CollectionItem in PropertyCollection)
                        {
                            var BackboneElement = CollectionItem as BackboneElement;
                            ReferenceResultList.AddRange(BackboneElement.AllBaseReferences());
                        }
                    }
                    else
                    {
                        var BackboneElement = PropertyItem.GetValue(FhirBase) as BackboneElement;
                        ReferenceResultList.AddRange(BackboneElement.AllBaseReferences());
                    }
                }
                else
                {
                    if (PropertyItem.GetValue(FhirBase) is ResourceReference rr)
                    {
                        ReferenceResultList.Add(rr);
                    }
                }
            }
            return(ReferenceResultList);
        }
        private static List <ResourceReference> AllBaseReferences(this Base FhirBase, Dictionary <int, IEnumerable <Hl7.Fhir.Introspection.PropertyMapping> > ClassPropertyMappingListCache)
        {
            var ReferenceResultList = new List <ResourceReference>();

            if (FhirBase == null)
            {
                return(ReferenceResultList);
            }

            //If DomainResource or Extension then drill through all extensions and collect all references
            if (FhirBase is DomainResource DomainResource)
            {
                ReferenceResultList.AddRange(DomainResource.Extension.AllExtensionListReferences());
            }
            else if (FhirBase is Extension Extension)
            {
                ReferenceResultList.AddRange(Extension.AllExtensionReferences());
            }

            //Cache ClassMappings's PropertyList as likely to have same resource again in the bundle entries
            IEnumerable <Hl7.Fhir.Introspection.PropertyMapping> PropertyMappingList = ClassPropertyMappingListCache.SingleOrDefault(x => x.Key == FhirBase.TypeName.GetHashCode()).Value;

            if (PropertyMappingList == null)
            {
                var ClassMapping = Hl7.Fhir.Introspection.ClassMapping.Create(FhirBase.GetType());
                PropertyMappingList = ClassMapping.PropertyMappings.Where(t => t.ElementType == typeof(ResourceReference) || t.ElementType.BaseType == typeof(BackboneElement));
                ClassPropertyMappingListCache.Add(FhirBase.TypeName.GetHashCode(), PropertyMappingList);
            }
            int Count = 0;

            foreach (var PropertyItem in PropertyMappingList)
            {
                if (PropertyItem.ElementType.BaseType == typeof(BackboneElement))
                {
                    if (PropertyItem.IsCollection)
                    {
                        try
                        {
                            var PropertyCollection = PropertyItem.GetValue(FhirBase) as System.Collections.IEnumerable;
                            foreach (var CollectionItem in PropertyCollection)
                            {
                                var BackboneElement = CollectionItem as BackboneElement;
                                ReferenceResultList.AddRange(BackboneElement.AllBaseReferences(ClassPropertyMappingListCache));
                            }
                        }
                        catch (System.Exception)
                        {
                            //Why Does 'PropertyItem.GetValue(FhirBase)' throw an exception?
                            //for no we are ignoring the exception as not sure what else to do, could be some
                            //ResourceReferences in transaction bundles are not updated.
                        }
                    }
                    else
                    {
                        try
                        {
                            var BackboneElement = PropertyItem.GetValue(FhirBase) as BackboneElement;
                            ReferenceResultList.AddRange(BackboneElement.AllBaseReferences(ClassPropertyMappingListCache));
                        }
                        catch (System.Exception)
                        {
                            //Why Does 'PropertyItem.GetValue(FhirBase)' throw an exception?
                            //for no we are ignoring the exception as not sure what else to do, could be some
                            //ResourceReferences in transaction bundles are not updated.
                        }
                    }
                }
                else
                {
                    try
                    {
                        if (PropertyItem.GetValue(FhirBase) is ResourceReference SingleRef)
                        {
                            ReferenceResultList.Add(SingleRef);
                        }
                        if (PropertyItem.GetValue(FhirBase) is ICollection <ResourceReference> ResourceReferenceList)
                        {
                            foreach (ResourceReference Ref in ResourceReferenceList)
                            {
                                ReferenceResultList.Add(Ref);
                            }
                        }
                    }
                    catch (System.Exception)
                    {
                        //Why Does 'PropertyItem.GetValue(FhirBase)' throw an exception?
                        //for no we are ignoring the exception as not sure what else to do, could be some
                        //ResourceReferences in translation bundles are not updated.
                    }
                }
                Count++;
            }
            return(ReferenceResultList);
        }