private OrganisationHierarchyCacheItem ConvertRelationshipsToOrganisationTreeItem(IReadOnlyCollection <Relationship> relationships, Guid rootNode)
        {
            //make sure root node is in list and top level
            if (relationships.All(rel => rootNode != rel.ParentNodeId) ||
                relationships.Any(rel => rel.ChildNodeId == rootNode))
            {
                const string error = "The expected root node for the tree should be a parent and should not appear as a child in any relationship.";
                _logger.Error(OperationConvertRelationships,
                              new LogItem("Error", error),
                              new LogItem("rootNode", rootNode.ToString()));
                throw new InvalidOperationException(error);
            }

            var organisationTreeItem = new OrganisationHierarchyCacheItem
            {
                OrganisationId = rootNode
            };

            organisationTreeItem.Metadata = relationships.First(x => x.ParentNodeId == organisationTreeItem.OrganisationId)
                                            .ParentNodeMetadata;

            RecursivelyPopulateChildrenForOrganisationTreeItems(relationships, organisationTreeItem);

            return(organisationTreeItem);
        }
        private static List <OrganisationHierarchyCacheItem> RecursivelyPopulateChildrenForOrganisationTreeItems(IReadOnlyCollection <Relationship> relationships, OrganisationHierarchyCacheItem organisationTreeItem)
        {
            //when we enter, we know the current parentNodeId and metadata being passed in is populated.
            //so we need to now populate the children.

            var directChildRelationshipsForParent = relationships.Where(x => x.ParentNodeId == organisationTreeItem.OrganisationId)
                                                    .Distinct(new RelationshipEqualityComparer()).ToList();

            if (directChildRelationshipsForParent.Any())//continue recursion downward
            {
                organisationTreeItem.ChildOrganisations = new List <OrganisationHierarchyCacheItem>();

                foreach (var relationship in directChildRelationshipsForParent)
                {
                    var childOrgToAdd = new OrganisationHierarchyCacheItem
                    {
                        OrganisationId = relationship.ChildNodeId,
                        Metadata       = relationship.ChildNodeMetadata
                    };

                    organisationTreeItem.ChildOrganisations.AddRange(RecursivelyPopulateChildrenForOrganisationTreeItems(relationships, childOrgToAdd));
                }
            }

            return(new List <OrganisationHierarchyCacheItem> {
                organisationTreeItem
            });
        }
Exemplo n.º 3
0
        public static bool ContainsOrganisationId(this OrganisationHierarchyCacheItem organisationHierarchyCacheItem, Guid id)
        {
            if (organisationHierarchyCacheItem == null)
            {
                return(false);
            }

            if (organisationHierarchyCacheItem.OrganisationId == id)
            {
                return(true);
            }

            if (organisationHierarchyCacheItem.ChildOrganisations == null)
            {
                return(false);
            }

            foreach (var childTree in organisationHierarchyCacheItem.ChildOrganisations)
            {
                var result = childTree.ContainsOrganisationId(id);

                if (result) //do not just return result as this will end recursion if false.
                {
                    return(true);
                }
            }

            return(false);
        }
 public void AddOrUpdate(Guid key, OrganisationHierarchyCacheItem item)
 {
     _cacheList[key] = item;
 }