public Block AddRoot()
 {
     Block systemRoot = new Block()
         {
             Id = Guid.NewGuid(),
             BlockName = "_SystemRoot",
             Path = HierarchyId.GetRoot(),
             Level = HierarchyId.GetRoot().GetLevel()
         };
     context.Blocks.Add(systemRoot);
     return systemRoot;
 }
        public Block AppendSubservice(string ns)
        {
            Block systemRoot = context.Blocks.SingleOrDefault(x => x.Path == HierarchyId.GetRoot());
            if (systemRoot == null)
            {
                systemRoot = AddRoot();
            }

            var subservice = new Block { Id = Guid.NewGuid(), BlockName = ns, Path = GetAppendPath(systemRoot) };
            subservice.Level = subservice.Path.GetLevel();
            context.Blocks.Add(subservice);
            context.SaveChanges();
            return subservice;
        }
 private TreeItem Map(Block blockToMap)
 {
     var newItem = new TreeItem()
     {
         Name = blockToMap.BlockName,
         IsChoice = blockToMap.IsChoice,
         Order = blockToMap.Order,
         SubItems = new List<TreeItem>()
     };
     newItem.SubItems.AddRange(blockToMap.AttributeList.Select(x => new TreeItem()
         {
             Name = x.AttributeName,
             Order = x.Order,
             Placeholder = x.Placeholder
         }));
     foreach (Block block in GetChildren(blockToMap))
     {
         newItem.SubItems.Add(Map(block));
     }
     newItem.SubItems.Sort(new TreeNodeComparer());
     return newItem;
 }
        private void Dive(TreeItem currentNode, HierarchyId currentPath)
        {
            var newBlock = new Block
            {
                Id = Guid.NewGuid(),
                Path = currentPath,
                Level = currentPath.GetLevel(),
                BlockName = currentNode.Name,
                IsChoice = currentNode.IsChoice,
                Order = currentNode.Order,
                AttributeList = new Collection<AttributeMetadata>()
            };
            var subBlocks = currentNode.SubItems.Where(x => x.SubItems != null).ToArray();

            //блок
            for (int i = 1; i <= subBlocks.Length; i++)
            {
                var subItem = currentNode.SubItems[i - 1];
                Dive(subItem, HierarchyId.Parse(currentPath + i.ToString() + "/"));
            }
            /*var subAttributes = currentNode.SubItems.Where(x => x.SubItems == null).Select(x => new AttributeMetadata
                {
                    Id = Guid.NewGuid(),
                    AttributeName = x.Name,
                    BlockId = newBlock.Id,
                    Placeholder = x.Placeholder,
                });*/
            foreach (var node in currentNode.SubItems.Where(x => x.SubItems == null))
            {
                newBlock.AttributeList.Add(new AttributeMetadata
                {
                    Id = Guid.NewGuid(),
                    AttributeName = node.Name,
                    Placeholder = node.Placeholder,
                    Order = node.Order
                });
            }
            context.Blocks.Add(newBlock);
        }
 public IEnumerable<Block> GetChildren(Block parent)
 {
     return context.Blocks.Where(
         x => x.Path.IsDescendantOf(parent.Path) && x.Level == parent.Level + 1);
 }
 /// <summary>
 /// Возвращает путь для вставки в конец
 /// </summary>
 /// <param name="parentBlock"></param>
 /// <returns></returns>
 public HierarchyId GetAppendPath(Block parentBlock)
 {
     var index = context.Blocks.Count(x => x.Level == parentBlock.Level + 1
                                           && x.Path.IsDescendantOf(parentBlock.Path)) + 1;
     HierarchyId newItemPath = HierarchyId.Parse(parentBlock.Path + index.ToString() + "/");
     return newItemPath;
 }