コード例 #1
0
        private MongoNode FindInItem(MongoNode item, string findText, bool previous = false)
        {
            MongoNode result = null;

            //查找下一个时,先查找父级
            if (!previous && FoundItem(item, findText))
            {
                item.IsSelected = true;
                return(item);
            }
            if (item.Children?.Any() ?? false)
            {
                var list = previous ? item.Children.Reverse() : item.Children;
                foreach (var child in list)
                {
                    result = FindInItem(child, findText, previous);
                    if (result != null)
                    {
                        item.IsExpanded = true;
                        return(result);
                    }
                }
            }
            //查找上一个时,后查找父级
            if (previous && FoundItem(item, findText))
            {
                item.IsSelected = true;
                return(item);
            }
            return(result);
        }
コード例 #2
0
        private MongoNode FindParentNext(MongoNode item, string findText, bool previous = false)
        {
            MongoNode result = null;
            var       parent = GetParentItem(item);

            if (parent != null)
            {
                var startIndex = parent.Children.IndexOf(item) + 1;
                var list       = previous ? parent.Children.Take(startIndex - 1).Reverse() : parent.Children.Skip(startIndex);
                foreach (var child in list)
                {
                    result = FindInItem(child, findText, previous);
                    if (result != null)
                    {
                        parent.IsExpanded = true;
                        return(result);
                    }
                }
                //向上查找父级节点
                if (previous && FoundItem(parent, findText))
                {
                    parent.IsSelected = true;
                    return(parent);
                }
                return(FindParentNext(parent, findText, previous));
            }
            return(result);
        }
コード例 #3
0
        private MongoNode FindNextItem(MongoNode item, string findText, bool previous = false)
        {
            item = item ?? Tree;
            MongoNode result = null;

            if (previous)
            {
                result = FindParentNext(item, findText, previous);
                return(result);
            }
            else
            {
                if (item.Children?.Any() ?? false)
                {
                    var list = previous ? item.Children.Reverse() : item.Children;
                    foreach (var child in list)
                    {
                        result = FindInItem(child, findText, previous);
                        if (result != null)
                        {
                            item.IsExpanded = true;
                            return(result);
                        }
                    }
                }
                return(FindParentNext(item, findText, previous));
            }
        }
コード例 #4
0
 private void InitExecutionTree()
 {
     Tree = new MongoNode();
     AddTreeNode("新增", new List <object>(), Tree);
     AddTreeNode("更新", new object(), Tree);
     AddTreeNode("重复", new object(), Tree);
     AddTreeNode("多余", new List <object>(), Tree);
     AddTreeNode("相同", new List <object>(), Tree);
 }
コード例 #5
0
 private string UpdateValueByChildrenCount(MongoNode node)
 {
     if (node.Type == nameof(Array))
     {
         node.Value = $"[{node.Children.Count}]";
     }
     else if (node.Type == nameof(Object))
     {
         node.Value = $"{{{node.Children.Count}}}";
     }
     return(node.Value);
 }
コード例 #6
0
 private bool FoundItem(MongoNode item, string findText)
 {
     if (item.Key?.ToLower().Contains(findText.ToLower()) ?? false)
     {
         return(true);
     }
     if (item.Value?.ToLower().Contains(findText.ToLower()) ?? false)
     {
         return(true);
     }
     return(false);
 }
コード例 #7
0
        private string GetNodeInnerJson(MongoNode node, bool isBreak = false)
        {
            if (node == null)
            {
                return(string.Empty);
            }
            var sb = new StringBuilder();

            if (node.Type == nameof(Object) || node.Type == nameof(Array))
            {
                //子节点
                if (node.Children != null && node.Children.Any())
                {
                    var indent    = "  ";//缩进
                    var itemJsons = new List <string>();
                    foreach (var item in node.Children)
                    {
                        //数组子元素不含Key,对象属性含Key
                        bool childWithKey = true;
                        if (node.Type == nameof(Array))
                        {
                            childWithKey = false;
                        }
                        var itemJson = GetNodeOutterJson(item, childWithKey);
                        var lines    = itemJson.Split(Environment.NewLine);
                        //每行加缩进
                        foreach (var line in lines)
                        {
                            sb.AppendLine().Append(indent).Append(line);
                        }
                        //容器节点各子节点分离,不含分隔符
                        if (!isBreak)
                        {
                            sb.Append(",");
                        }
                    }
                    //移除最后一个分隔符
                    if (!isBreak)
                    {
                        sb.Remove(sb.Length - 1, 1);
                    }
                    sb.AppendLine();
                }
            }
            else
            {
                //双引号转义
                sb.Append(node.Value?.Replace("\"", "\\\""));
            }
            return(sb.ToString());
        }
コード例 #8
0
        //TODO: 忽略Null值节点
        private string GetNodeOutterJson(MongoNode node, bool withKey = false)
        {
            if (node == null)
            {
                return(string.Empty);
            }
            //是否包含Key(顶级不含,数组元素不含,属性含)
            var keyOrEmpty = withKey ? $"\"{node.Key}\": " : null;
            //不同类型不同起止字符
            string valueOpen = null, valueClose = null;
            //容器节点各子节点分离,不含起止符
            var isBreak = node.Key == ContainerNodeText;

            if (!isBreak)
            {
                switch (node.Type)
                {
                case nameof(Object):
                    valueOpen  = "{";
                    valueClose = "}";
                    break;

                case nameof(Array):
                    valueOpen  = "[";
                    valueClose = "]";
                    break;

                case nameof(ObjectId):
                    valueOpen  = "ObjectId(\"";
                    valueClose = "\")";
                    break;

                case nameof(DateTime):
                    valueOpen  = "ISODate(\"";
                    valueClose = "\")";
                    break;

                case nameof(String):
                case "string":
                    valueOpen = valueClose = "\"";
                    break;

                default:
                    break;
                }
            }
            return($"{keyOrEmpty}{valueOpen}{GetNodeInnerJson(node, isBreak)}{valueClose}");
        }
コード例 #9
0
        private MongoNode GetParentItem(MongoNode item, MongoNode node = null)
        {
            MongoNode result = null;

            node = node ?? Tree;
            foreach (var child in node.Children)
            {
                if (child.Equals(item))
                {
                    return(node);
                }
                result = GetParentItem(item, child);
                if (result != null)
                {
                    return(result);
                }
            }
            return(null);
        }
コード例 #10
0
 private MongoNode GetSelectedItem(MongoNode node = null)
 {
     node = node ?? Tree;
     if (node.IsSelected)
     {
         return(node);
     }
     if (node.Children?.Any() ?? false)
     {
         foreach (var child in node.Children)
         {
             var result = GetSelectedItem(child);
             if (result != null)
             {
                 return(result);
             }
         }
     }
     return(null);
 }
コード例 #11
0
        private MongoNode AddBsonValueNode(string key, BsonValue v, MongoNode parent)
        {
            MongoNode node = new MongoNode {
                Key = key
            };

            if (v.IsBsonNull)
            {
                node = AddTreeNode(key, null, parent);
            }
            else if (v.IsBoolean)
            {
                node = AddTreeNode(key, v.ToBoolean(), parent);
            }
            else if (v.IsInt32)
            {
                node = AddTreeNode(key, v.ToInt32(), parent);
            }
            else if (v.IsInt32 || v.IsInt64)
            {
                node = AddTreeNode(key, v.ToInt64(), parent);
            }
            else if (v.IsDouble)
            {
                node = AddTreeNode(key, v.ToDouble(), parent);
            }
            else if (v.IsDecimal128 || v.IsNumeric)
            {
                node = AddTreeNode(key, v.ToDecimal(), parent);
            }
            else if (v.IsObjectId)
            {
                node = AddTreeNode(key, v.AsObjectId, parent);
            }
            else if (v.IsString || v.IsGuid)
            {
                node = AddTreeNode(key, v.ToString(), parent);
            }
            else if (v.IsValidDateTime || v.IsBsonDateTime)
            {
                node = AddTreeNode(key, v.ToLocalTime(), parent);
            }
            else if (v.IsString)
            {
                node = AddTreeNode(key, v.ToString(), parent);
            }
            else if (v.IsValidDateTime)
            {
                node = AddTreeNode(key, v.ToLocalTime(), parent);
            }
            else if (v.IsBsonArray)
            {
                var array = v.AsBsonArray;
                node.Value     = $"[{array.Count}]";
                node.Type      = "Array";
                node.ImagePath = ResourcesBase + @"/Images/arr.png";
                var i = 0;
                foreach (var item in array)
                {
                    AddBsonValueNode($"[{i}]", item, node);
                    i++;
                }
            }
            else
            {
                node = AddTreeNode(key, v.ToString(), parent);
            }

            return(node);
        }
コード例 #12
0
        private MongoNode AddTreeNode(string key, object value, MongoNode parent)
        {
            if (string.IsNullOrEmpty(key))
            {
                if (value is CacheSysProfileMode cache)
                {
                    key = $"{cache.Mode}:{cache.Item}";
                }
                else
                {
                    key = $"[{parent.Children.Count}]";
                }
            }
            var node = new MongoNode {
                Key = key, IsEnable = parent.IsEnable ?? true
            };

            if (value == null)
            {
                node.Value     = "null";
                node.Type      = "Null";
                node.ImagePath = ResourcesBase + @"/Images/null.png";
            }
            else if (value is ObjectId id)
            {
                if (id == ObjectId.Empty)
                {
                    node = null;
                }
                else
                {
                    node.Value     = value.ToString();
                    node.Type      = value.GetType().Name;
                    node.ImagePath = ResourcesBase + @"/Images/id.png";
                }
            }
            else if (value is bool)
            {
                node.Value     = value.ToString().ToLower();
                node.Type      = value.GetType().Name;
                node.ImagePath = ResourcesBase + @"/Images/id.png";
            }
            else if (value is string || value is int || value is long || value is double || value is decimal || value is ObjectId)
            {
                node.Value     = value.ToString();
                node.Type      = value.GetType().Name;
                node.ImagePath = ResourcesBase + @"/Images/id.png";
            }
            else if (value is DateTime t)
            {
                node.Value     = t.ToString("yyyy-MM-dd HH:mm:ss.fff");
                node.Type      = value.GetType().Name;
                node.ImagePath = ResourcesBase + @"/Images/id.png";
            }
            else if (value is System.Collections.ICollection c)
            {
                node.Value     = $"[{c.Count}]";
                node.Type      = nameof(Array);
                node.ImagePath = ResourcesBase + @"/Images/arr.png";
                var i = 0;
                foreach (var item in c)
                {
                    AddTreeNode(null, item, node);
                    i++;
                }
            }
            else if (value is BsonDocument d)
            {
                node.Value     = $"{{{d.ElementCount}}}";
                node.Type      = nameof(Object);
                node.ImagePath = ResourcesBase + @"/Images/obj.png";
                foreach (var item in d.Elements)
                {
                    AddTreeNode(item.Name, item.Value, node);
                }
            }
            else if (value is BsonValue v)
            {
                node = AddBsonValueNode(key, v, parent);
                if (node != null)
                {
                    node = null;
                }
            }
            else
            {
                var props = value.GetType().GetProperties();
                node.Value     = string.Empty;// $"{{{props.Length}}}";
                node.ImagePath = ResourcesBase + @"/Images/obj.png";
                node.Type      = nameof(Object);
                if (value is CacheSysProfileMode cache)
                {
                    node.Value += cache.Display;
                    var exists = parent.Children.Where(n => n.Key == node.Key);
                    if (exists.Any())
                    {
                        foreach (var item in exists)
                        {
                            item.IsEnable   = false;
                            item.Foreground = (SolidColorBrush)Application.Current.FindResource("RedBrush");
                        }
                        node.IsEnable   = false;
                        node.Foreground = (SolidColorBrush)Application.Current.FindResource("RedBrush");
                    }
                }
                foreach (var item in props)
                {
                    AddTreeNode(item.Name, item.GetValue(value), node);
                }
            }
            if (node != null)
            {
                MainWindowVM.DispatcherInvoke(() =>
                {
                    parent.Children.Add(node);
                });
            }
            return(node);
        }