using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; using System.Windows; using System.Windows.Controls; using System.Xml; using Aitex.Core.UI.View.Common; using Caliburn.Micro.Core; using MECF.Framework.Common.CommonData; using MECF.Framework.Common.DataCenter; using MECF.Framework.UI.Client.CenterViews.Editors.Recipe; namespace MECF.Framework.UI.Client.CenterViews.Editors.Sequence { public class FileNode : PropertyChangedBase { public FileNode() { this.Files = new ObservableCollection(); this.IsFile = false; } public FileNode(FileNodeItem fileNodeItem) { IsFile = true; FullPath = fileNodeItem.FullPath; AllParentPath = fileNodeItem.AllParentPath; Name = fileNodeItem.Name; this.Files = new ObservableCollection(); PrefixPath = fileNodeItem.PrefixPath; Creator = fileNodeItem.Creator; Revisor = fileNodeItem.Revisor; Description = fileNodeItem.Description; CreatTime = fileNodeItem.CreatTime; ReviseTime = fileNodeItem.ReviseTime; Permission = fileNodeItem.Permission; Level = fileNodeItem.Level; if (fileNodeItem.Parent != null) Parent = new FileNode(fileNodeItem.Parent); } private string name = string.Empty; public string Name { get { return name; } set { name = value; NotifyOfPropertyChange("Name"); } } public string FullPath { get; set; } public string AllParentPath { get; set; } public FileNode Parent { get; set; } public ObservableCollection Files { get; set; } public bool IsFile { get; set; } public string PrefixPath { get; set; } private string creator = string.Empty; public string Creator { get { return creator; } set { creator = value; NotifyOfPropertyChange("Creator"); } } private string revisor = string.Empty; public string Revisor { get { return revisor; } set { revisor = value; NotifyOfPropertyChange("Revisor"); } } private string description = string.Empty; public string Description { get { return description; } set { description = value; NotifyOfPropertyChange("Description"); } } private string creatTime = string.Empty; public string CreatTime { get { return creatTime; } set { creatTime = value; NotifyOfPropertyChange("CreatTime"); } } private string reviseTime = string.Empty; public string ReviseTime { get { return reviseTime; } set { reviseTime = value; NotifyOfPropertyChange("ReviseTime"); } } private string permission = string.Empty; public string Permission { get { return permission; } set { permission = value; NotifyOfPropertyChange("Permission"); } } private string level = string.Empty; public string Level { get { return level; } set { level = value; NotifyOfPropertyChange("Level"); } } private bool _IsSelected; public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; NotifyOfPropertyChange("IsSelected"); } } private bool _isExpanded; public bool IsExpanded { get => _isExpanded; set { if (_isExpanded != value) { _isExpanded = value; NotifyOfPropertyChange("IsExpanded"); }; } } private Visibility _IsVisiable; public Visibility IsVisiable { get { return _IsVisiable; } set { _IsVisiable = value; NotifyOfPropertyChange("IsVisiable"); } } private bool _IsChecked; public bool IsChecked { get { return _IsChecked; } set { _IsChecked = value; NotifyOfPropertyChange("IsChecked"); } } private bool _IsBackupSelected; public bool IsBackupSelected { get { return _IsBackupSelected; } set { _IsBackupSelected = value; if (!IsFile) { foreach (var item in Files) { SetAllChild(item, value); } } NotifyOfPropertyChange("IsBackupSelected"); } } private void SetAllChild(FileNode fileNode, bool value) { if (!fileNode.IsFile) { SetAllChild(fileNode, value); } else { fileNode.IsBackupSelected = value; } } private bool _IsEnable; public bool IsEnable { get { return _IsEnable; } set { _IsEnable = value; NotifyOfPropertyChange("IsEnable"); } } } public class RecipeSequenceTreeBuilder { public static List GetFiles(string prefixPath, List filenames) { List folders = new List(); FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath }; folders.Add(root); foreach (string filename in filenames) { string[] filesplits = filename.Split('\\'); FileNode parent = root; if (filesplits.Length > 1) { for (var index = 0; index < filesplits.Length - 1; index++) { bool found = false; for (var j = 0; j < parent.Files.Count; j++) { if (parent.Files[j].Name == filesplits[index] && !parent.Files[j].IsFile) { found = true; parent = parent.Files[j]; break; } } if (!found) { FileNode folder = new FileNode() { Name = filesplits[index], IsFile = false, PrefixPath = prefixPath }; folder.FullPath = (parent.FullPath == string.Empty ? filesplits[index] : parent.FullPath + "\\" + filesplits[index]); folder.Parent = parent; parent.Files.Add(folder); parent = folder; } } } FileNode file = new FileNode() { Name = filesplits[filesplits.Length - 1], IsFile = true, PrefixPath = prefixPath }; file.FullPath = (parent.FullPath == string.Empty ? filesplits[filesplits.Length - 1] : parent.FullPath + "\\" + filesplits[filesplits.Length - 1]); file.Parent = parent; parent.Files.Add(file); } return folders; } static string GetAttributeValueByXmlDoc(XmlDocument _doc, string attributeName) { XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableRecipeData") as XmlElement; return nodeData.GetAttribute(attributeName); } static string GetAttributeValue(string prefixPath, string recipeName, string attributeName) { RecipeProvider _recipeProvider = new RecipeProvider(); string content = _recipeProvider.LoadRecipe(prefixPath, recipeName); XmlDocument _doc = new XmlDocument(); _doc.LoadXml(content); XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableRecipeData") as XmlElement; return nodeData.GetAttribute(attributeName); } public static void CreateTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder) { string currentMode = QueryDataClient.Instance.Service.GetConfig("System.Engineeringmode")?.ToString(); selectionIsFolder = false; foreach (XmlElement ele in curElementNode.ChildNodes) { if (ele.Name == "File") { string fileName = ele.Attributes["Name"].Value; fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1); string fullPath = string.IsNullOrEmpty(parent.FullPath) ? fileName : parent.FullPath + "\\" + fileName; RecipeProvider _recipeProvider = new RecipeProvider(); string content = _recipeProvider.LoadRecipe(prefixPath, fullPath); XmlDocument _doc = new XmlDocument(); _doc.LoadXml(content); if ((content == null) || (content == "")) { continue; } FileNode file = new FileNode() { Name = fileName, IsFile = true, PrefixPath = prefixPath, Parent = parent, FullPath = fullPath, Creator = GetAttributeValueByXmlDoc(_doc, "CreatedBy"), Description = GetAttributeValueByXmlDoc(_doc, "Description"), CreatTime = GetAttributeValueByXmlDoc(_doc, "CreationTime"), ReviseTime = GetAttributeValueByXmlDoc(_doc, "LastRevisionTime"), Revisor = GetAttributeValueByXmlDoc(_doc, "LastRevisedBy"), Permission = GetAttributeValueByXmlDoc(_doc, "RecipePermission"), Level = GetAttributeValueByXmlDoc(_doc, "RecipeLevel"), IsChecked = GetAttributeValueByXmlDoc(_doc, "CheckResult") == "Correct", }; string fileMode = string.Empty; if (!string.IsNullOrEmpty(currentMode) && currentMode.Equals("Normal")) file.IsEnable = GetAttributeValueByXmlDoc(_doc, "NormalMode") != "Notuseable"; else file.IsEnable = GetAttributeValueByXmlDoc(_doc, "EngineeringMode") != "Notuseable"; if (!selectionIsFolder) { if (selectionName == file.FullPath) file.IsSelected = true; else file.IsSelected = false; FileNode node = file.Parent; while (node.FullPath.Contains("\\")) { node.IsExpanded = true; node = node.Parent; } parent.Files.Add(file); } } else if (ele.Name == "Folder") { string folderName = ele.Attributes["Name"].Value; folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1); string fullPath = string.IsNullOrEmpty(parent.FullPath) ? folderName : parent.FullPath + "\\" + folderName; FileNode folder = new FileNode() { Name = folderName, IsFile = false, PrefixPath = prefixPath, Parent = parent, FullPath = fullPath, IsExpanded = !fullPath.Contains("\\"), IsChecked = true, IsEnable = true }; parent.Files.Add(folder); if (selectionIsFolder && selectionName == folder.FullPath) { folder.IsSelected = true; FileNode node = folder; while (node.FullPath.Contains("\\")) { node.IsExpanded = true; node = node.Parent; } } CreateTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder); } } } static string GetRestoreAttributeValue(string prefixPath, string recipeName, string attributeName) { RecipeProvider _recipeProvider = new RecipeProvider(); string content = _recipeProvider.LoadRestoreRecipe(prefixPath, recipeName); XmlDocument _doc = new XmlDocument(); _doc.LoadXml(content); XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableRecipeData") as XmlElement; return nodeData.GetAttribute(attributeName); } public static void CreateRestoreTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder) { selectionIsFolder = false; foreach (XmlElement ele in curElementNode.ChildNodes) { if (ele.Name == "File") { string fileName = ele.Attributes["Name"].Value; fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1); string fullPath = string.IsNullOrEmpty(parent.FullPath) ? fileName : parent.FullPath + "\\" + fileName; FileNode file = new FileNode() { Name = fileName, IsFile = true, PrefixPath = prefixPath, Parent = parent, FullPath = fullPath, Creator = GetRestoreAttributeValue(prefixPath, fullPath, "CreatedBy"), Description = GetRestoreAttributeValue(prefixPath, fullPath, "Description"), CreatTime = GetRestoreAttributeValue(prefixPath, fullPath, "CreationTime"), ReviseTime = GetRestoreAttributeValue(prefixPath, fullPath, "LastRevisionTime"), Revisor = GetRestoreAttributeValue(prefixPath, fullPath, "LastRevisedBy"), Permission = GetRestoreAttributeValue(prefixPath, fullPath, "RecipePermission"), Level = GetRestoreAttributeValue(prefixPath, fullPath, "RecipeLevel"), }; if (!selectionIsFolder) { file.IsSelected = true; FileNode node = file.Parent; while (node.FullPath.Contains("\\")) { node.IsExpanded = true; node = node.Parent; } parent.Files.Add(file); } } else if (ele.Name == "Folder") { string folderName = ele.Attributes["Name"].Value; folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1); string fullPath = string.IsNullOrEmpty(parent.FullPath) ? folderName : parent.FullPath + "\\" + folderName; FileNode folder = new FileNode() { Name = folderName, IsFile = false, PrefixPath = prefixPath, Parent = parent, FullPath = fullPath, IsExpanded = !fullPath.Contains("\\"), }; parent.Files.Add(folder); if (selectionIsFolder && selectionName == folder.FullPath) { folder.IsSelected = true; FileNode node = folder; while (node.FullPath.Contains("\\")) { node.IsExpanded = true; node = node.Parent; } } CreateRestoreTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder); } } } public static List BuildFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml) { List folders = new List(); FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath }; folders.Add(root); XmlDocument doc = new XmlDocument(); doc.LoadXml(fileListInXml); CreateTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder); return folders; } public static List BuildRestoreFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml) { List folders = new List(); FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath }; folders.Add(root); XmlDocument doc = new XmlDocument(); doc.LoadXml(fileListInXml); CreateRestoreTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder); return folders; } public static ObservableCollection GetFileNodeParameterList(string prefixPath) { RecipeProvider _recipeProvider = new RecipeProvider(); ObservableCollection fileList = new ObservableCollection(); var tempfiles = _recipeProvider.GetFileNodeParameterList(prefixPath); FileNodeItemToFileNode(fileList, tempfiles); FileNodeParent(fileList); FileNodeAllParentPath(fileList); return fileList; } private static void FileNodeAllParentPath(ObservableCollection fileList) { foreach (var item in fileList) { if (item.Files != null && item.Files.Count > 0) { FileNodeAllParentPath(item.Files); } if (item.Parent != null) { List allParentName = new List(); SetFileNodeAllParentPath(item, allParentName); if (allParentName != null && allParentName.Count > 0) { allParentName.RemoveAt(0); allParentName.Reverse(); item.AllParentPath = string.Join("\\", allParentName); } } } } private static void SetFileNodeAllParentPath(FileNode fileNode, List allParentName) { allParentName.Add(fileNode.Name); if (fileNode.Parent == null) { return; } SetFileNodeAllParentPath(fileNode.Parent, allParentName); } private static void FileNodeParent(ObservableCollection fileList) { foreach (var item in fileList) { if (item.Files == null || item.Files.Count == 0) { continue; } SetFileNodeParent(item); } } private static void SetFileNodeParent(FileNode fileNode) { if (fileNode.Files == null || fileNode.Files.Count == 0) { return; } foreach (var item in fileNode.Files) { item.Parent = fileNode; SetFileNodeParent(item); } } private static void FileNodeItemToFileNode(ObservableCollection fileList, List fileNodeItems) { foreach (var item in fileNodeItems) { if (item.IsFile) { fileList.Add(new FileNode(item)); } else { GetSubFiles(item).ForEach(x => fileList.Add(x)); } } } private static List GetSubFiles(FileNodeItem fileNodeItem) { List fileNodeList = new List(); foreach (var item in fileNodeItem.Files) { if (item.IsFile) { fileNodeList.Add(new FileNode(item)); } else { FileNode fileNode = new FileNode(); fileNode.Name = item.Name; fileNode.FullPath = item.FullPath; fileNode.PrefixPath = item.PrefixPath; fileNode.IsFile = false; GetSubFiles(item).ForEach(x => fileNode.Files.Add(x)); fileNodeList.Add(fileNode); } } return fileNodeList; } } }