ParameterTreeBuilder.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Xml;
  6. using Aitex.Core.UI.View.Common;
  7. using Caliburn.Micro.Core;
  8. using MECF.Framework.Common.CommonData;
  9. using MECF.Framework.Common.DataCenter;
  10. using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
  11. using MECF.Framework.UI.Client.CenterViews.Editors.Sequence;
  12. using MECF.Framework.UI.Client.CenterViews.Parameter;
  13. namespace MECF.Framework.UI.Client.CenterViews.Editors
  14. {
  15. public class ParameterTreeBuilder
  16. {
  17. static ParameterProvider _parameterProvider = new ParameterProvider();
  18. public static List<FileNode> GetFiles(string prefixPath, List<string> filenames)
  19. {
  20. List<FileNode> folders = new List<FileNode>();
  21. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  22. folders.Add(root);
  23. foreach (string filename in filenames)
  24. {
  25. string[] filesplits = filename.Split('\\');
  26. FileNode parent = root;
  27. if (filesplits.Length > 1)
  28. {
  29. for (var index = 0; index < filesplits.Length - 1; index++)
  30. {
  31. bool found = false;
  32. for (var j = 0; j < parent.Files.Count; j++)
  33. {
  34. if (parent.Files[j].Name == filesplits[index] && !parent.Files[j].IsFile)
  35. {
  36. found = true;
  37. parent = parent.Files[j];
  38. break;
  39. }
  40. }
  41. if (!found)
  42. {
  43. FileNode folder = new FileNode() { Name = filesplits[index], IsFile = false, PrefixPath = prefixPath };
  44. folder.FullPath = (parent.FullPath == string.Empty ? filesplits[index] : parent.FullPath + "\\" + filesplits[index]);
  45. folder.Parent = parent;
  46. parent.Files.Add(folder);
  47. parent = folder;
  48. }
  49. }
  50. }
  51. FileNode file = new FileNode() { Name = filesplits[filesplits.Length - 1], IsFile = true, PrefixPath = prefixPath };
  52. file.FullPath = (parent.FullPath == string.Empty ? filesplits[filesplits.Length - 1] : parent.FullPath + "\\" + filesplits[filesplits.Length - 1]);
  53. file.Parent = parent;
  54. parent.Files.Add(file);
  55. }
  56. return folders;
  57. }
  58. static string GetAttributeValue(string prefixPath, string parameterName, string attributeName)
  59. {
  60. string content = _parameterProvider.LoadParameter(prefixPath, parameterName);
  61. XmlDocument _doc = new XmlDocument();
  62. _doc.LoadXml(content);
  63. XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableParameterData") as XmlElement;
  64. return nodeData.GetAttribute(attributeName);
  65. }
  66. public static void CreateTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder)
  67. {
  68. selectionIsFolder = false;
  69. foreach (XmlElement ele in curElementNode.ChildNodes)
  70. {
  71. if (ele.Name == "File")
  72. {
  73. string fileName = ele.Attributes["Name"].Value;
  74. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  75. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  76. ? fileName
  77. : parent.FullPath + "\\" + fileName;
  78. ParameterProvider _parameterProvider = new ParameterProvider();
  79. string content = _parameterProvider.LoadParameter(prefixPath, fullPath);
  80. if ((content == null) || (content == ""))
  81. {
  82. continue;
  83. }
  84. FileNode file = new FileNode()
  85. {
  86. Name = fileName,
  87. IsFile = true,
  88. PrefixPath = prefixPath,
  89. Parent = parent,
  90. FullPath = fullPath,
  91. Creator = GetAttributeValue(prefixPath, fullPath, "CreatedBy"),
  92. Description = GetAttributeValue(prefixPath, fullPath, "Description"),
  93. CreatTime = GetAttributeValue(prefixPath, fullPath, "CreationTime"),
  94. ReviseTime = GetAttributeValue(prefixPath, fullPath, "LastRevisionTime"),
  95. Revisor = GetAttributeValue(prefixPath, fullPath, "LastRevisedBy"),
  96. Permission = GetAttributeValue(prefixPath, fullPath, "Permission"),
  97. Level = GetAttributeValue(prefixPath, fullPath, "Level"),
  98. IsChecked = GetAttributeValue(prefixPath, fullPath, "CheckResult") == "Correct",
  99. };
  100. string fileMode = string.Empty;
  101. string currentMode = QueryDataClient.Instance.Service.GetConfig("System.Engineeringmode")?.ToString();
  102. if (!string.IsNullOrEmpty(currentMode) && currentMode.Equals("Normal"))
  103. file.IsEnable = GetAttributeValue(prefixPath, fullPath, "NormalMode") != "Notuseable";
  104. else
  105. file.IsEnable = GetAttributeValue(prefixPath, fullPath, "EngineeringMode") != "Notuseable";
  106. if (!selectionIsFolder)
  107. {
  108. if (selectionName == file.FullPath)
  109. file.IsSelected = true;
  110. else
  111. file.IsSelected = false;
  112. FileNode node = file.Parent;
  113. while (node.FullPath.Contains("\\"))
  114. {
  115. node.IsExpanded = true;
  116. node = node.Parent;
  117. }
  118. parent.Files.Add(file);
  119. }
  120. }
  121. else if (ele.Name == "Folder")
  122. {
  123. string folderName = ele.Attributes["Name"].Value;
  124. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  125. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  126. ? folderName
  127. : parent.FullPath + "\\" + folderName;
  128. FileNode folder = new FileNode()
  129. {
  130. Name = folderName,
  131. IsFile = false,
  132. PrefixPath = prefixPath,
  133. Parent = parent,
  134. FullPath = fullPath,
  135. IsExpanded = !fullPath.Contains("\\"),
  136. IsChecked = true,
  137. IsEnable = true
  138. };
  139. parent.Files.Add(folder);
  140. if (selectionIsFolder && selectionName == folder.FullPath)
  141. {
  142. folder.IsSelected = true;
  143. FileNode node = folder;
  144. while (node.FullPath.Contains("\\"))
  145. {
  146. node.IsExpanded = true;
  147. node = node.Parent;
  148. }
  149. }
  150. CreateTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder);
  151. }
  152. }
  153. }
  154. static string GetRestoreAttributeValue(string prefixPath, string recipeName, string attributeName)
  155. {
  156. ParameterProvider _parameterProvider = new ParameterProvider();
  157. string content = _parameterProvider.LoadRestoreParameter(prefixPath, recipeName);
  158. XmlDocument _doc = new XmlDocument();
  159. _doc.LoadXml(content);
  160. XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableParameterData") as XmlElement;
  161. return nodeData.GetAttribute(attributeName);
  162. }
  163. public static void CreateRestoreTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder)
  164. {
  165. selectionIsFolder = false;
  166. foreach (XmlElement ele in curElementNode.ChildNodes)
  167. {
  168. if (ele.Name == "File")
  169. {
  170. string fileName = ele.Attributes["Name"].Value;
  171. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  172. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  173. ? fileName
  174. : parent.FullPath + "\\" + fileName;
  175. FileNode file = new FileNode()
  176. {
  177. Name = fileName,
  178. IsFile = true,
  179. PrefixPath = prefixPath,
  180. Parent = parent,
  181. FullPath = fullPath,
  182. Creator = GetRestoreAttributeValue(prefixPath, fullPath, "CreatedBy"),
  183. Description = GetRestoreAttributeValue(prefixPath, fullPath, "Description"),
  184. CreatTime = GetRestoreAttributeValue(prefixPath, fullPath, "CreationTime"),
  185. ReviseTime = GetRestoreAttributeValue(prefixPath, fullPath, "LastRevisionTime"),
  186. Revisor = GetRestoreAttributeValue(prefixPath, fullPath, "LastRevisedBy"),
  187. Permission = GetRestoreAttributeValue(prefixPath, fullPath, "RecipePermission"),
  188. Level = GetRestoreAttributeValue(prefixPath, fullPath, "RecipeLevel"),
  189. };
  190. if (!selectionIsFolder)
  191. {
  192. file.IsSelected = true;
  193. FileNode node = file.Parent;
  194. while (node.FullPath.Contains("\\"))
  195. {
  196. node.IsExpanded = true;
  197. node = node.Parent;
  198. }
  199. parent.Files.Add(file);
  200. }
  201. }
  202. else if (ele.Name == "Folder")
  203. {
  204. string folderName = ele.Attributes["Name"].Value;
  205. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  206. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  207. ? folderName
  208. : parent.FullPath + "\\" + folderName;
  209. FileNode folder = new FileNode()
  210. {
  211. Name = folderName,
  212. IsFile = false,
  213. PrefixPath = prefixPath,
  214. Parent = parent,
  215. FullPath = fullPath,
  216. IsExpanded = !fullPath.Contains("\\"),
  217. };
  218. parent.Files.Add(folder);
  219. if (selectionIsFolder && selectionName == folder.FullPath)
  220. {
  221. folder.IsSelected = true;
  222. FileNode node = folder;
  223. while (node.FullPath.Contains("\\"))
  224. {
  225. node.IsExpanded = true;
  226. node = node.Parent;
  227. }
  228. }
  229. CreateRestoreTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder);
  230. }
  231. }
  232. }
  233. public static List<FileNode> BuildFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml)
  234. {
  235. List<FileNode> folders = new List<FileNode>();
  236. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  237. folders.Add(root);
  238. XmlDocument doc = new XmlDocument();
  239. doc.LoadXml(fileListInXml);
  240. CreateTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder);
  241. return folders;
  242. }
  243. public static List<FileNode> BuildRestoreFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml)
  244. {
  245. List<FileNode> folders = new List<FileNode>();
  246. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  247. folders.Add(root);
  248. XmlDocument doc = new XmlDocument();
  249. doc.LoadXml(fileListInXml);
  250. CreateRestoreTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder);
  251. return folders;
  252. }
  253. public static ObservableCollection<FileNode> GetFileNodeParameterList(string prefixPath)
  254. {
  255. ObservableCollection<FileNode> fileList = new ObservableCollection<FileNode>();
  256. var tempfiles=_parameterProvider.GetFileNodeParameterList(prefixPath);
  257. FileNodeItemToFileNode(fileList, tempfiles);
  258. return fileList;
  259. }
  260. private static void FileNodeItemToFileNode(ObservableCollection<FileNode> fileList, List<FileNodeItem> fileNodeItems)
  261. {
  262. foreach (var item in fileNodeItems)
  263. {
  264. if (item.IsFile)
  265. {
  266. fileList.Add(new FileNode(item));
  267. }
  268. else
  269. {
  270. GetSubFiles(item).ForEach(x=>fileList.Add(x));
  271. }
  272. }
  273. }
  274. private static List<FileNode> GetSubFiles(FileNodeItem fileNodeItem)
  275. {
  276. List<FileNode> fileNodeList = new List<FileNode>();
  277. foreach (var item in fileNodeItem.Files)
  278. {
  279. if (item.IsFile)
  280. {
  281. fileNodeList.Add(new FileNode(item));
  282. }
  283. else
  284. {
  285. FileNode fileNode = new FileNode();
  286. GetSubFiles(item).ForEach(x => fileNode.Files.Add(x));
  287. fileNodeList.Add(fileNode);
  288. }
  289. }
  290. return fileNodeList;
  291. }
  292. }
  293. }