예제 #1
0
        /// <summary>
        /// Implements a LevelOrder traversal algorithm to write the tree model data
        /// into a SQLite database file.
        /// </summary>
        /// <param name="solutionRoot"></param>
        private int WriteToFile(ISolutionModel solutionRoot
                                , SQLiteCommand cmd)
        {
            int result = 0;
            int iKey   = 0;

            var items = TreeLib.BreadthFirst.Traverse.LevelOrder <IItemModel>(solutionRoot.Root
                                                                              , (i) =>
            {
                var it = i as IItemChildrenModel;

                if (it != null)
                {
                    return(it.Children);
                }

                // Emulate an emtpy list if items have no children
                return(new List <IItemChildrenModel>());
            });

            foreach (var item in items)
            {
                int        iLevel  = item.Level;
                IItemModel current = item.Node;
                current.Id = iKey++;

                long parentId = (current.Parent == null ? -1 : current.Parent.Id);

                if (cmd != null)
                {
                    cmd.Parameters.AddWithValue("@id", current.Id);
                    cmd.Parameters.AddWithValue("@parent", parentId);
                    cmd.Parameters.AddWithValue("@level", iLevel);
                    cmd.Parameters.AddWithValue("@name", current.DisplayName);
                    cmd.Parameters.AddWithValue("@itemtypeid", (int)(current.ItemType));

                    result += cmd.ExecuteNonQuery();
                }
                else
                {
                    Console.WriteLine(string.Format("{0,4} - {1} ({2})"
                                                    , iLevel, current.GetStackPath(), current.ItemType.ToString()));
                }
            }

            return(result);
        }