/// <summary> /// Set factor trees for specified locale. /// </summary> /// <param name="factorTrees">Factor trees.</param> /// <param name="locale">Locale.</param> /// <param name="getOnlyPublicFactors">Get only public factor trees or all from cache.</param> protected virtual void SetFactorTrees(FactorTreeNodeList factorTrees, ILocale locale, Boolean getOnlyPublicFactors) { String factorTreeType = getOnlyPublicFactors ? FactorTreesPermissionType.PublicFactorTrees.ToString() : FactorTreesPermissionType.AllFactorTrees.ToString(); FactorTrees[locale.ISOCode + "#" + factorTreeType] = factorTrees; }
/// <summary> /// Get factor trees for specified locale. /// </summary> /// <param name="locale">Locale.</param> /// <param name="getOnlyPublicFactors">Get only public factor trees or all from cache.</param> /// <returns>Factors for specified locale.</returns> protected virtual FactorTreeNodeList GetFactorTrees(ILocale locale, Boolean getOnlyPublicFactors) { String factorTreeType = getOnlyPublicFactors ? FactorTreesPermissionType.PublicFactorTrees.ToString() : FactorTreesPermissionType.AllFactorTrees.ToString(); FactorTreeNodeList factorTrees = null; if (FactorTrees.ContainsKey(locale.ISOCode + "#" + factorTreeType)) { factorTrees = (FactorTreeNodeList)(FactorTrees[locale.ISOCode + "#" + factorTreeType]); } return(factorTrees); }
/// <summary> /// Get all factor tree nodes that belongs /// to this factor tree node. /// This tree node is also included in the result. /// </summary> /// <returns> /// All factor tree nodes that belongs /// to this factor tree node. /// </returns> public FactorTreeNodeList GetAllChildTreeNodes() { FactorTreeNodeList factorTreeNodeList = new FactorTreeNodeList { this }; if (Children.IsNotEmpty()) { foreach (IFactorTreeNode child in Children) { factorTreeNodeList.AddRange(child.GetAllChildTreeNodes()); } } return(factorTreeNodeList); }
/// <summary> /// Get all leafs that belongs to this factor tree node. /// This tree node may also be included in the result. /// </summary> /// <returns>All leaf tree node that belongs to this factor tree node.</returns> public FactorTreeNodeList GetAllLeafTreeNodes() { FactorTreeNodeList leaves = new FactorTreeNodeList(); if (Children.IsEmpty()) { leaves.Add(this); } else { foreach (IFactorTreeNode child in Children) { leaves.AddRange(child.GetAllLeafTreeNodes()); } } return(leaves); }
/// <summary> /// Traverse a factor tree and creates a Dictionary for caching. /// </summary> /// <param name="factorTreeNodes">The Dictionary for caching.</param> /// <param name="children">Children factor trees to add to the dictionary.</param> private void AddFactorTreeChildren(Dictionary <int, IFactorTreeNode> factorTreeNodes, FactorTreeNodeList children) { if (children.IsNotEmpty()) { foreach (IFactorTreeNode child in children) { if (!factorTreeNodes.ContainsKey(child.Id)) { factorTreeNodes.Add(child.Id, child); } AddFactorTreeChildren(factorTreeNodes, child.Children); } } }
/// <summary> /// Get a factor tree node by the specified factor id. /// </summary> /// <param name="userContext"> /// Information about the user that makes this method call. /// </param> /// <param name="factorId">The specified factor id.</param> /// <returns>A factor tree node related to the specified factor id.</returns> public override IFactorTreeNode GetFactorTree(IUserContext userContext, int factorId) { IFactorTreeNode factorTreeNode = null; FactorTreeNodeList factorTrees = null; Dictionary <int, IFactorTreeNode> factorTreeNodes = new Dictionary <int, IFactorTreeNode>(); IFactorTreeSearchCriteria searchCriteria; searchCriteria = new FactorTreeSearchCriteria(); searchCriteria.FactorIds = new List <Int32>(); searchCriteria.FactorIds.Add(factorId); Boolean getOnlyPublicFactors = !IsUserAuthorizedToReadNonPublicFactors(userContext); // Get cached factor trees factorTrees = GetFactorTrees(userContext.Locale, getOnlyPublicFactors); factorTreeNode = GetFactorTreeNode(userContext.Locale, factorId, getOnlyPublicFactors); if (factorTrees.IsNull()) { // Get factor trees based on user role and rights (get public factors or all factors) factorTrees = base.GetFactorTrees(userContext); // Store factor trees in cache SetFactorTrees(factorTrees, userContext.Locale, getOnlyPublicFactors); foreach (IFactorTreeNode node in factorTrees) { if (!factorTreeNodes.ContainsKey(node.Id)) { factorTreeNodes.Add(node.Id, node); } AddFactorTreeChildren(factorTreeNodes, node.Children); } // Store factor tree nodes in cache SetFactorTreeNodes(factorTreeNodes, userContext.Locale, getOnlyPublicFactors); } if (factorTrees.IsNotNull()) { foreach (var factorTree in factorTrees) { if (factorTree.Id == factorId) { factorTreeNode = factorTree; break; } } } if (factorTreeNode.IsNull() && factorTreeNodes.IsNotNull()) { foreach (var factorTree in factorTreeNodes) { if (factorTree.Key == factorId) { factorTreeNode = factorTree.Value; break; } } } return(factorTreeNode); }