Exemplo n.º 1
0
        private FlagEnum <TreeNodeDifference> GetDifference(PrtgNode first, PrtgNode second)
        {
            var differences = new List <TreeNodeDifference>();

            if (first == null && second == null)
            {
                return(TreeNodeDifference.None);
            }

            if (first != null && second == null)
            {
                return(TreeNodeDifference.Removed);
            }

            if (first == null && second != null)
            {
                return(TreeNodeDifference.Added);
            }

            if (first.GetType() != second.GetType())
            {
                differences.Add(TreeNodeDifference.Type);
            }

            //Collections don't have values, so there's no point checking value properties for them
            if (first.Value != null && second.Value != null)
            {
                Debug.Assert(first.Value.Id == second.Value.Id, "If two nodes had different IDs one of them should have been replaced in the comparison visitor with null.");

                if (first.Value.ParentId != second.Value.ParentId)
                {
                    differences.Add(TreeNodeDifference.ParentId);
                }

                if (first.Value.Name != second.Value.Name)
                {
                    differences.Add(TreeNodeDifference.Name);
                }
            }

            if (first.Children.Count > 0 && second.Children.Count == 0 || second.Children.Count > 0 && first.Children.Count == 0)
            {
                differences.Add(TreeNodeDifference.HasChildren);
            }

            if (first.Children.Count != second.Children.Count)
            {
                differences.Add(TreeNodeDifference.NumberOfChildren);
            }

            return(new FlagEnum <TreeNodeDifference>(differences.ToArray()));
        }
Exemplo n.º 2
0
        private FlagEnum <TreeNodeDifference> GetDifference(PrtgNode first, PrtgNode second, TreeNodeDifference[] interestedDifferences)
        {
            var differences = new List <TreeNodeDifference>();

            if (first == null && second == null)
            {
                return(TreeNodeDifference.None);
            }

            if (first != null && second == null)
            {
                differences.Add(TreeNodeDifference.Removed);
            }

            if (first == null && second != null)
            {
                differences.Add(TreeNodeDifference.Added);
            }

            if (first != null && second != null)
            {
                if (first.GetType() != second.GetType())
                {
                    differences.Add(TreeNodeDifference.Type);
                }

                //Collections don't have values, so there's no point checking value properties for them
                if (first.Value != null && second.Value != null)
                {
                    Debug.Assert(first.Value.Id == second.Value.Id, "If two nodes had different IDs one of them should have been replaced in the comparison visitor with null.");

                    if (first.Value.ParentId != second.Value.ParentId)
                    {
                        differences.Add(TreeNodeDifference.ParentId);
                    }

                    if (first.Value.Name != second.Value.Name)
                    {
                        differences.Add(TreeNodeDifference.Name);
                    }

                    if (first.Value is ISensorOrDeviceOrGroupOrProbe && second.Value is ISensorOrDeviceOrGroupOrProbe)
                    {
                        if (((ISensorOrDeviceOrGroupOrProbe)first.Value).Position !=
                            ((ISensorOrDeviceOrGroupOrProbe)second.Value).Position)
                        {
                            differences.Add(TreeNodeDifference.Position);
                        }
                    }

                    if (first.Value is PropertyValuePair && second.Value is PropertyValuePair)
                    {
                        if (!string.Equals(((PropertyValuePair)first.Value).Value?.ToString(), ((PropertyValuePair)second.Value).Value?.ToString()))
                        {
                            differences.Add(TreeNodeDifference.Value);
                        }
                    }
                }

                if ((first.Children.Count > 0 && second.Children.Count == 0 || second.Children.Count > 0 && first.Children.Count == 0))
                {
                    differences.Add(TreeNodeDifference.HasChildren);
                }

                if (first.Children.Count != second.Children.Count)
                {
                    differences.Add(TreeNodeDifference.NumberOfChildren);
                }
            }

            Debug.Assert(
                (differences.Contains(TreeNodeDifference.Added) ||
                 differences.Contains(TreeNodeDifference.Removed)) &&
                differences.Count > 1 ?
                false :
                true,
                "Cannot contain more than one difference when the difference is Added or Removed"
                );

            if (interestedDifferences != null && interestedDifferences.Length > 0)
            {
                var interestedFlags = new FlagEnum <TreeNodeDifference>(interestedDifferences);

                if (interestedFlags != TreeNodeDifference.None)
                {
                    var proposedDifferences = new FlagEnum <TreeNodeDifference>(differences.ToArray());

                    var result = proposedDifferences & interestedFlags;

                    return(result);
                }
            }

            return(new FlagEnum <TreeNodeDifference>(differences.ToArray()));
        }