///<summary> /// Writes the associated XML of class Model T into a file. ///</summary> ///<param name="rootModel"></param> ///<param name="filename"></param> public static void WriteXmlToFile(string filename, ISolutionModel rootModel) { XmlWriter xmlWriter = null; try { var fileStream = new FileStream(filename, FileMode.Create); xmlWriter = XmlWriter.Create(fileStream, new XmlWriterSettings { Indent = true, IndentChars = " ", CloseOutput = true }); var dataContractSerializer = new DataContractSerializer(typeof(SolutionModel)); dataContractSerializer.WriteObject(xmlWriter, rootModel); } finally { if (xmlWriter != null) { xmlWriter.Close(); } } }
/// <summary> /// Inserts the data structure items (root item, projects, folders, files) of the solution /// into the SQLite database table 'solution'. /// </summary> /// <param name="solutionRoot"></param> /// <param name="db"></param> /// <returns></returns> public int InsertSolutionData(ISolutionModel solutionRoot , SQLiteDatabase db = null) { int recordCount = 0; if (db == null) { db = this; } // Insert into Database string query = "INSERT INTO solution ([id],[parent],[level],[name],[itemtypeid])VALUES(@id, @parent, @level, @name, @itemtypeid)"; // Write data out to database using (SQLiteCommand cmd = new SQLiteCommand(query, db.Connection)) { // https://www.jokecamp.com/blog/make-your-sqlite-bulk-inserts-very-fast-in-c/ using (var transaction = cmd.Connection.BeginTransaction()) { recordCount = WriteToFile(solutionRoot, cmd); transaction.Commit(); } } return(recordCount); }
/// <summary> /// Converts a viewmodel item into a model item and /// inserts it into the solution model structure. /// </summary> /// <param name="solutionModel"></param> /// <param name="parent"></param> /// <param name="item"></param> /// <returns></returns> private IItemModel ConvertToModel( ISolutionModel solutionModel , IItemChildrenModel parent , IItem item) { IItemModel modelNewChild = null; switch (item.ItemType) { case SolutionItemType.File: modelNewChild = solutionModel.AddChild(item.DisplayName, SolutionModelItemType.File, parent); break; case SolutionItemType.Folder: modelNewChild = solutionModel.AddChild(item.DisplayName, SolutionModelItemType.Folder, parent); break; case SolutionItemType.Project: modelNewChild = solutionModel.AddChild(item.DisplayName, SolutionModelItemType.Project, parent); break; case SolutionItemType.SolutionRootItem: default: throw new NotSupportedException(); } return(modelNewChild); }
///<summary> /// Writes the associated XML of class Model T into string and returns it. ///</summary> ///<param name="rootModel"></param> public static string WriteXmlToString(ISolutionModel rootModel) { using (var stringWriter = new StringWriter()) // Write Xml to string { XmlWriter xmlWriter = null; try { xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, IndentChars = " ", CloseOutput = true }); var dataContractSerializer = new DataContractSerializer(typeof(SolutionModel)); dataContractSerializer.WriteObject(xmlWriter, rootModel); } finally { if (xmlWriter != null) { xmlWriter.Close(); } } return(stringWriter.ToString()); } }
/// <summary> /// Read the Tree Model data structure from a SQLite database file back into memory. /// </summary> /// <param name="solutionRoot"></param> /// <param name="db"></param> /// <returns></returns> public int ReadSolutionData(ISolutionModel solutionRoot , SQLiteDatabase db = null) { if (db == null) { db = this; } int recordCount = 0; var query = "SELECT * FROM solution ORDER BY level, id"; using (SQLiteCommand cmd = new SQLiteCommand(query, db.Connection)) { using (SQLiteDataReader selectResult = cmd.ExecuteReader()) { Dictionary <long, IItemChildrenModel> mapKeyToItem = new Dictionary <long, IItemChildrenModel>(); if (selectResult.HasRows == true) { if (selectResult.Read() == true) { var root = solutionRoot.AddSolutionRootItem(selectResult["name"].ToString()); // .GetInt32(0) gets the Id of Root entry mapKeyToItem.Add(selectResult.GetInt32(0), root); recordCount++; } while (selectResult.Read() == true) { int iParentKey = selectResult.GetInt32(1); // Get parent key from next item IItemChildrenModel parent = null; if (mapKeyToItem.TryGetValue(iParentKey, out parent) == true) { var itemTypeId = (long)selectResult["itemtypeid"]; var item = solutionRoot.AddChild(selectResult["name"].ToString() , itemTypeId, parent); IItemChildrenModel parentItem = null; if ((parentItem = item as IItemChildrenModel) != null) { mapKeyToItem.Add(selectResult.GetInt32(0), parentItem); } recordCount++; } else { throw new Exception("Data written is corrupted."); } } } } } return(recordCount); }
/// <summary> /// Method is executed to save a solutions content into the filesystem /// (Save As dialog should be called before this function if required /// This method executes after a user approved a dialog to Save in this /// location with this name). /// </summary> /// <param name="sourcePath"></param> /// <param name="solutionRoot"></param> /// <returns></returns> private async Task <bool> SaveSolutionFileAsync(string sourcePath , ISolutionModel solutionRoot) { return(await Task.Run <bool>(() => { return SaveSolutionFile(sourcePath, solutionRoot); })); }
/// <summary> /// Method implements Level-Order traversal via <see cref="TreeLib"/> nuget /// package to convert the <see cref="SolutionModel"/> model in <paramref name="model"/> /// into a <see cref="ISolution"/> viewmodel in <paramref name="solutionRoot"/>. /// </summary> /// <param name="model"></param> /// <param name="solutionRoot"></param> /// <returns></returns> public ISolution ToViewModel(ISolutionModel model , ISolution solutionRoot) { solutionRoot.ResetToDefaults(); // Reset current viewmodel to construction time defaults ISolutionRootItemModel treeRootModel = model.Root; long itemId = 0; var items = TreeLib.BreadthFirst.Traverse.LevelOrder <IItemModel>(treeRootModel , (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>()); }); var dstIdItems = new Dictionary <long, IItemChildren>(); foreach (var item in items.Select(i => i.Node)) { item.Id = itemId++; if (item.Parent == null) { var rootItem = solutionRoot.AddSolutionRootItem(item.DisplayName); rootItem.SetId(item.Id); dstIdItems.Add(rootItem.GetId(), rootItem); } else { IItemChildren modelParentItem; IItem modelNewChild; dstIdItems.TryGetValue(item.Parent.Id, out modelParentItem); modelNewChild = ConvertToViewModel(solutionRoot, modelParentItem, item); modelNewChild.SetId(item.Id); // Store only items that can have children for later lock-up if (modelNewChild is IItemChildren) { dstIdItems.Add(modelNewChild.GetId(), modelNewChild as IItemChildren); } } } return(solutionRoot); }
/// <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); }
/// <summary> /// Method is executed to save a solutions content into the filesystem /// (Save As dialog should be called before this function if required /// This method executes after a user approved a dialog to Save in this /// location with this name). /// </summary> /// <param name="sourcePath"></param> /// <param name="solutionRoot"></param> /// <returns></returns> private bool SaveSolutionFile(string sourcePath , ISolutionModel solutionRoot) { SolutionModelsLib.SQLite.SolutionDB db = new SolutionModelsLib.SQLite.SolutionDB(); db.SetFileNameAndPath(sourcePath); Console.WriteLine("1) Writting data into SQLite file: '{0}'", db.DBFileNamePath); int recordCount = 0; int itemTypeCount = 0; try { // Overwrites the existing file (if any) db.OpenConnection(true); if (db.ConnectionState == false) { Console.WriteLine("ERROR: Cannot open Database connectiton.\n" + db.Status); return(false); } db.ReCreateDBTables(db); // Write itemtype enumeration into file var names = Enum.GetNames(typeof(SolutionModelsLib.Enums.SolutionModelItemType)); var values = Enum.GetValues(typeof(SolutionModelsLib.Enums.SolutionModelItemType)); itemTypeCount = db.InsertItemTypeEnumeration(names, values); // Write solution tree data file recordCount = db.InsertSolutionData(solutionRoot); } catch (Exception exp) { Console.WriteLine("\n\nAN ERROR OCURRED: " + exp.Message + "\n"); } finally { db.CloseConnection(); } Console.WriteLine("{0:000} records written to itemtype enumeration table...", itemTypeCount); Console.WriteLine("{0:000} records written to solution data table...", recordCount); return(true); }
private void LoadSolutionCommand_ExecutedAsync(ISolution solutionRoot) { var explorer = ServiceLocator.ServiceContainer.Instance.GetService <IExplorer>(); var result = explorer.FileOpen(solutionRoot.SolutionFileFilter, LastFileAccess, UserDocDir, solutionRoot.SolutionFileFilterDefault, SelectedFileExtFilterIndex); if (result == null) // User clicked Cancel ... { return; } LastFileAccess = result.Filepath; SelectedFileExtFilterIndex = result.SelectedFilterIndex; // Read model from file system and convert model to viewmodel var filename_ext = result.FileExtension; int recordCount = 0; ISolutionModel solutionModel = null; switch (filename_ext) { case ".solxml": solutionModel = Storage.ReadXmlFromFile <ISolutionModel>(result.Filepath); break; default: solutionModel = LoadSolutionFile(result.Filepath, out recordCount); break; } new ViewModelModelConverter().ToViewModel(solutionModel, solutionRoot); var rootItem = solutionRoot.GetRootItem(); // Show items below root by default if (rootItem != null) { rootItem.IsItemExpanded = true; } }
/// <summary> /// Method is executed to load a solutions content from the filesystem /// (Open file dialog should be called before this function if required). /// </summary> /// <param name="sourcePath"></param> /// <param name="recordCount"></param> /// <returns></returns> private ISolutionModel LoadSolutionFile(string sourcePath, out int recordCount) { recordCount = 0; ISolutionModel solutionRoot = null; var db = new SolutionModelsLib.SQLite.SolutionDB(); try { db.SetFileNameAndPath(sourcePath); db.OpenConnection(); if (db.ConnectionState == false) { MessageBox.Show("ERROR: Cannot open Database connectiton.\n" + db.Status); return(null); } solutionRoot = SolutionModelsLib.Factory.CreateSolutionModel(); // Select Result from Database var mapKeyToItem = db.ReadItemTypeEnum(); bool checkResult = CompareItemTypeEnums(mapKeyToItem); if (checkResult == false) { MessageBox.Show("ERROR: Cannot open file: itemtype enumeration is not consistent."); return(null); } recordCount = db.ReadSolutionData(solutionRoot, db); } catch (Exception exp) { MessageBox.Show("\n\nAN ERROR OCURRED: " + exp.Message + "\n"); } finally { db.CloseConnection(); } return(solutionRoot); }
protected CreateViewPageBaseModel CreateModel(IDictionaryService dictionary, ISolutionModel solution, ISite Site) { return(new CreateViewPageBaseModel(dictionary, solution, Site)); }
public CreateModulePageModel(IDictionaryService dictionary, ISolutionModel solutionModel, IServiceProvider serviceProvider) { this.dictionary = dictionary; this.solutionModel = solutionModel; this.serviceProvider = serviceProvider; }
public CreateViewPageBaseModel(IDictionaryService dictionary, ISolutionModel solutionModel, IServiceProvider serviceProvider) { _dictionary = dictionary; _solutionModel = solutionModel; _serviceProvider = serviceProvider; }
public CreateViewPageBaseModel(IDictionaryService dictionary, ISolutionModel solutionModel, IServiceProvider serviceProvider) { _dictionary = dictionary; _solutionModel = solutionModel; _serviceProvider = serviceProvider; }
protected CreateViewPageBaseModel CreateModel(IDictionaryService dictionary, ISolutionModel solution, ISite Site) { return new CreateViewPageBaseModel(dictionary, solution, Site); }
public SolutionTreeNodeControlTest(ISolutionModel viewModel) : base(viewModel) { }
public CreateFoundationalModulePageModel(IDictionaryService dictionary, ISolutionModel solutionModel, IServiceProvider serviceProvider) { this.dictionary = dictionary; this.solutionModel = solutionModel; this.serviceProvider = serviceProvider; }