RecipeSequenceTreeBuilder.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Xml;
  8. using Aitex.Core.UI.View.Common;
  9. using Caliburn.Micro.Core;
  10. using MECF.Framework.Common.CommonData;
  11. using MECF.Framework.Common.DataCenter;
  12. using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
  13. namespace MECF.Framework.UI.Client.CenterViews.Editors.Sequence
  14. {
  15. public class FileNode : PropertyChangedBase
  16. {
  17. public FileNode()
  18. {
  19. this.Files = new ObservableCollection<FileNode>();
  20. this.IsFile = false;
  21. }
  22. public FileNode(FileNodeItem fileNodeItem)
  23. {
  24. IsFile = true;
  25. FullPath = fileNodeItem.FullPath;
  26. AllParentPath = fileNodeItem.AllParentPath;
  27. Name = fileNodeItem.Name;
  28. this.Files = new ObservableCollection<FileNode>();
  29. PrefixPath = fileNodeItem.PrefixPath;
  30. Creator = fileNodeItem.Creator;
  31. Revisor = fileNodeItem.Revisor;
  32. Description = fileNodeItem.Description;
  33. CreatTime = fileNodeItem.CreatTime;
  34. ReviseTime = fileNodeItem.ReviseTime;
  35. Permission = fileNodeItem.Permission;
  36. Level = fileNodeItem.Level;
  37. if (fileNodeItem.Parent != null)
  38. Parent = new FileNode(fileNodeItem.Parent);
  39. }
  40. private string name = string.Empty;
  41. public string Name
  42. {
  43. get { return name; }
  44. set { name = value; NotifyOfPropertyChange("Name"); }
  45. }
  46. public string FullPath { get; set; }
  47. public string AllParentPath { get; set; }
  48. public FileNode Parent { get; set; }
  49. public ObservableCollection<FileNode> Files { get; set; }
  50. public bool IsFile { get; set; }
  51. public string PrefixPath { get; set; }
  52. private string creator = string.Empty;
  53. public string Creator
  54. {
  55. get { return creator; }
  56. set { creator = value; NotifyOfPropertyChange("Creator"); }
  57. }
  58. private string revisor = string.Empty;
  59. public string Revisor
  60. {
  61. get { return revisor; }
  62. set { revisor = value; NotifyOfPropertyChange("Revisor"); }
  63. }
  64. private string description = string.Empty;
  65. public string Description
  66. {
  67. get { return description; }
  68. set { description = value; NotifyOfPropertyChange("Description"); }
  69. }
  70. private string creatTime = string.Empty;
  71. public string CreatTime
  72. {
  73. get { return creatTime; }
  74. set { creatTime = value; NotifyOfPropertyChange("CreatTime"); }
  75. }
  76. private string reviseTime = string.Empty;
  77. public string ReviseTime
  78. {
  79. get { return reviseTime; }
  80. set { reviseTime = value; NotifyOfPropertyChange("ReviseTime"); }
  81. }
  82. private string permission = string.Empty;
  83. public string Permission
  84. {
  85. get { return permission; }
  86. set { permission = value; NotifyOfPropertyChange("Permission"); }
  87. }
  88. private string level = string.Empty;
  89. public string Level
  90. {
  91. get { return level; }
  92. set { level = value; NotifyOfPropertyChange("Level"); }
  93. }
  94. private bool _IsSelected;
  95. public bool IsSelected
  96. {
  97. get { return _IsSelected; }
  98. set { _IsSelected = value; NotifyOfPropertyChange("IsSelected"); }
  99. }
  100. private bool _isExpanded;
  101. public bool IsExpanded
  102. {
  103. get => _isExpanded;
  104. set
  105. {
  106. if (_isExpanded != value) {
  107. _isExpanded = value;
  108. NotifyOfPropertyChange("IsExpanded");
  109. };
  110. }
  111. }
  112. private Visibility _IsVisiable;
  113. public Visibility IsVisiable
  114. {
  115. get { return _IsVisiable; }
  116. set { _IsVisiable = value; NotifyOfPropertyChange("IsVisiable"); }
  117. }
  118. private bool _IsChecked;
  119. public bool IsChecked
  120. {
  121. get { return _IsChecked; }
  122. set { _IsChecked = value; NotifyOfPropertyChange("IsChecked"); }
  123. }
  124. private bool _IsBackupSelected;
  125. public bool IsBackupSelected
  126. {
  127. get { return _IsBackupSelected; }
  128. set
  129. {
  130. _IsBackupSelected = value;
  131. if (!IsFile)
  132. {
  133. foreach (var item in Files)
  134. {
  135. SetAllChild(item, value);
  136. }
  137. }
  138. NotifyOfPropertyChange("IsBackupSelected");
  139. }
  140. }
  141. private void SetAllChild(FileNode fileNode, bool value)
  142. {
  143. if (!fileNode.IsFile)
  144. {
  145. SetAllChild(fileNode, value);
  146. }
  147. else
  148. {
  149. fileNode.IsBackupSelected = value;
  150. }
  151. }
  152. private bool _IsEnable;
  153. public bool IsEnable
  154. {
  155. get { return _IsEnable; }
  156. set { _IsEnable = value; NotifyOfPropertyChange("IsEnable"); }
  157. }
  158. }
  159. public class RecipeSequenceTreeBuilder
  160. {
  161. public static List<FileNode> GetFiles(string prefixPath, List<string> filenames)
  162. {
  163. List<FileNode> folders = new List<FileNode>();
  164. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  165. folders.Add(root);
  166. foreach (string filename in filenames)
  167. {
  168. string[] filesplits = filename.Split('\\');
  169. FileNode parent = root;
  170. if (filesplits.Length > 1)
  171. {
  172. for (var index = 0; index < filesplits.Length - 1; index++)
  173. {
  174. bool found = false;
  175. for (var j = 0; j < parent.Files.Count; j++)
  176. {
  177. if (parent.Files[j].Name == filesplits[index] && !parent.Files[j].IsFile)
  178. {
  179. found = true;
  180. parent = parent.Files[j];
  181. break;
  182. }
  183. }
  184. if (!found)
  185. {
  186. FileNode folder = new FileNode() { Name = filesplits[index], IsFile = false, PrefixPath = prefixPath };
  187. folder.FullPath = (parent.FullPath == string.Empty ? filesplits[index] : parent.FullPath + "\\" + filesplits[index]);
  188. folder.Parent = parent;
  189. parent.Files.Add(folder);
  190. parent = folder;
  191. }
  192. }
  193. }
  194. FileNode file = new FileNode() { Name = filesplits[filesplits.Length - 1], IsFile = true, PrefixPath = prefixPath };
  195. file.FullPath = (parent.FullPath == string.Empty ? filesplits[filesplits.Length - 1] : parent.FullPath + "\\" + filesplits[filesplits.Length - 1]);
  196. file.Parent = parent;
  197. parent.Files.Add(file);
  198. }
  199. return folders;
  200. }
  201. static string GetAttributeValueByXmlDoc(XmlDocument _doc, string attributeName)
  202. {
  203. XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableRecipeData") as XmlElement;
  204. return nodeData.GetAttribute(attributeName);
  205. }
  206. static string GetAttributeValue(string prefixPath, string recipeName, string attributeName)
  207. {
  208. RecipeProvider _recipeProvider = new RecipeProvider();
  209. string content = _recipeProvider.LoadRecipe(prefixPath, recipeName);
  210. XmlDocument _doc = new XmlDocument();
  211. _doc.LoadXml(content);
  212. XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableRecipeData") as XmlElement;
  213. return nodeData.GetAttribute(attributeName);
  214. }
  215. public static void CreateTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder)
  216. {
  217. string currentMode = QueryDataClient.Instance.Service.GetConfig("System.Engineeringmode")?.ToString();
  218. selectionIsFolder = false;
  219. foreach (XmlElement ele in curElementNode.ChildNodes)
  220. {
  221. if (ele.Name == "File")
  222. {
  223. string fileName = ele.Attributes["Name"].Value;
  224. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  225. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  226. ? fileName
  227. : parent.FullPath + "\\" + fileName;
  228. RecipeProvider _recipeProvider = new RecipeProvider();
  229. string content = _recipeProvider.LoadRecipe(prefixPath, fullPath);
  230. XmlDocument _doc = new XmlDocument();
  231. _doc.LoadXml(content);
  232. if ((content == null) || (content == ""))
  233. {
  234. continue;
  235. }
  236. FileNode file = new FileNode()
  237. {
  238. Name = fileName,
  239. IsFile = true,
  240. PrefixPath = prefixPath,
  241. Parent = parent,
  242. FullPath = fullPath,
  243. Creator = GetAttributeValueByXmlDoc(_doc, "CreatedBy"),
  244. Description = GetAttributeValueByXmlDoc(_doc, "Description"),
  245. CreatTime = GetAttributeValueByXmlDoc(_doc, "CreationTime"),
  246. ReviseTime = GetAttributeValueByXmlDoc(_doc, "LastRevisionTime"),
  247. Revisor = GetAttributeValueByXmlDoc(_doc, "LastRevisedBy"),
  248. Permission = GetAttributeValueByXmlDoc(_doc, "RecipePermission"),
  249. Level = GetAttributeValueByXmlDoc(_doc, "RecipeLevel"),
  250. IsChecked = GetAttributeValueByXmlDoc(_doc, "CheckResult") == "Correct",
  251. };
  252. string fileMode = string.Empty;
  253. if (!string.IsNullOrEmpty(currentMode) && currentMode.Equals("Normal"))
  254. file.IsEnable = GetAttributeValueByXmlDoc(_doc, "NormalMode") != "Notuseable";
  255. else
  256. file.IsEnable = GetAttributeValueByXmlDoc(_doc, "EngineeringMode") != "Notuseable";
  257. if (!selectionIsFolder)
  258. {
  259. if (selectionName == file.FullPath)
  260. file.IsSelected = true;
  261. else
  262. file.IsSelected = false;
  263. FileNode node = file.Parent;
  264. while (node.FullPath.Contains("\\"))
  265. {
  266. node.IsExpanded = true;
  267. node = node.Parent;
  268. }
  269. parent.Files.Add(file);
  270. }
  271. }
  272. else if (ele.Name == "Folder")
  273. {
  274. string folderName = ele.Attributes["Name"].Value;
  275. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  276. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  277. ? folderName
  278. : parent.FullPath + "\\" + folderName;
  279. FileNode folder = new FileNode()
  280. {
  281. Name = folderName,
  282. IsFile = false,
  283. PrefixPath = prefixPath,
  284. Parent = parent,
  285. FullPath = fullPath,
  286. IsExpanded = !fullPath.Contains("\\"),
  287. IsChecked = true,
  288. IsEnable = true
  289. };
  290. parent.Files.Add(folder);
  291. if (selectionIsFolder && selectionName == folder.FullPath)
  292. {
  293. folder.IsSelected = true;
  294. FileNode node = folder;
  295. while (node.FullPath.Contains("\\"))
  296. {
  297. node.IsExpanded = true;
  298. node = node.Parent;
  299. }
  300. }
  301. CreateTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder);
  302. }
  303. }
  304. }
  305. static string GetRestoreAttributeValue(string prefixPath, string recipeName, string attributeName)
  306. {
  307. RecipeProvider _recipeProvider = new RecipeProvider();
  308. string content = _recipeProvider.LoadRestoreRecipe(prefixPath, recipeName);
  309. XmlDocument _doc = new XmlDocument();
  310. _doc.LoadXml(content);
  311. XmlElement nodeData = _doc.SelectSingleNode($"Aitex/TableRecipeData") as XmlElement;
  312. return nodeData.GetAttribute(attributeName);
  313. }
  314. public static void CreateRestoreTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder)
  315. {
  316. selectionIsFolder = false;
  317. foreach (XmlElement ele in curElementNode.ChildNodes)
  318. {
  319. if (ele.Name == "File")
  320. {
  321. string fileName = ele.Attributes["Name"].Value;
  322. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  323. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  324. ? fileName
  325. : parent.FullPath + "\\" + fileName;
  326. FileNode file = new FileNode()
  327. {
  328. Name = fileName,
  329. IsFile = true,
  330. PrefixPath = prefixPath,
  331. Parent = parent,
  332. FullPath = fullPath,
  333. Creator = GetRestoreAttributeValue(prefixPath, fullPath, "CreatedBy"),
  334. Description = GetRestoreAttributeValue(prefixPath, fullPath, "Description"),
  335. CreatTime = GetRestoreAttributeValue(prefixPath, fullPath, "CreationTime"),
  336. ReviseTime = GetRestoreAttributeValue(prefixPath, fullPath, "LastRevisionTime"),
  337. Revisor = GetRestoreAttributeValue(prefixPath, fullPath, "LastRevisedBy"),
  338. Permission = GetRestoreAttributeValue(prefixPath, fullPath, "RecipePermission"),
  339. Level = GetRestoreAttributeValue(prefixPath, fullPath, "RecipeLevel"),
  340. };
  341. if (!selectionIsFolder)
  342. {
  343. file.IsSelected = true;
  344. FileNode node = file.Parent;
  345. while (node.FullPath.Contains("\\"))
  346. {
  347. node.IsExpanded = true;
  348. node = node.Parent;
  349. }
  350. parent.Files.Add(file);
  351. }
  352. }
  353. else if (ele.Name == "Folder")
  354. {
  355. string folderName = ele.Attributes["Name"].Value;
  356. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  357. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  358. ? folderName
  359. : parent.FullPath + "\\" + folderName;
  360. FileNode folder = new FileNode()
  361. {
  362. Name = folderName,
  363. IsFile = false,
  364. PrefixPath = prefixPath,
  365. Parent = parent,
  366. FullPath = fullPath,
  367. IsExpanded = !fullPath.Contains("\\"),
  368. };
  369. parent.Files.Add(folder);
  370. if (selectionIsFolder && selectionName == folder.FullPath)
  371. {
  372. folder.IsSelected = true;
  373. FileNode node = folder;
  374. while (node.FullPath.Contains("\\"))
  375. {
  376. node.IsExpanded = true;
  377. node = node.Parent;
  378. }
  379. }
  380. CreateRestoreTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder);
  381. }
  382. }
  383. }
  384. public static List<FileNode> BuildFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml)
  385. {
  386. List<FileNode> folders = new List<FileNode>();
  387. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  388. folders.Add(root);
  389. XmlDocument doc = new XmlDocument();
  390. doc.LoadXml(fileListInXml);
  391. CreateTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder);
  392. return folders;
  393. }
  394. public static List<FileNode> BuildRestoreFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml)
  395. {
  396. List<FileNode> folders = new List<FileNode>();
  397. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  398. folders.Add(root);
  399. XmlDocument doc = new XmlDocument();
  400. doc.LoadXml(fileListInXml);
  401. CreateRestoreTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder);
  402. return folders;
  403. }
  404. public static ObservableCollection<FileNode> GetFileNodeParameterList(string prefixPath)
  405. {
  406. RecipeProvider _recipeProvider = new RecipeProvider();
  407. ObservableCollection<FileNode> fileList = new ObservableCollection<FileNode>();
  408. var tempfiles = _recipeProvider.GetFileNodeParameterList(prefixPath);
  409. FileNodeItemToFileNode(fileList, tempfiles);
  410. FileNodeParent(fileList);
  411. FileNodeAllParentPath(fileList);
  412. return fileList;
  413. }
  414. private static void FileNodeAllParentPath(ObservableCollection<FileNode> fileList)
  415. {
  416. foreach (var item in fileList)
  417. {
  418. if (item.Files != null && item.Files.Count > 0)
  419. {
  420. FileNodeAllParentPath(item.Files);
  421. }
  422. if (item.Parent != null)
  423. {
  424. List<string> allParentName = new List<string>();
  425. SetFileNodeAllParentPath(item, allParentName);
  426. if (allParentName != null && allParentName.Count > 0)
  427. {
  428. allParentName.RemoveAt(0);
  429. allParentName.Reverse();
  430. item.AllParentPath = string.Join("\\", allParentName);
  431. }
  432. }
  433. }
  434. }
  435. private static void SetFileNodeAllParentPath(FileNode fileNode, List<string> allParentName)
  436. {
  437. allParentName.Add(fileNode.Name);
  438. if (fileNode.Parent == null)
  439. {
  440. return;
  441. }
  442. SetFileNodeAllParentPath(fileNode.Parent, allParentName);
  443. }
  444. private static void FileNodeParent(ObservableCollection<FileNode> fileList)
  445. {
  446. foreach (var item in fileList)
  447. {
  448. if (item.Files == null || item.Files.Count == 0)
  449. {
  450. continue;
  451. }
  452. SetFileNodeParent(item);
  453. }
  454. }
  455. private static void SetFileNodeParent(FileNode fileNode)
  456. {
  457. if (fileNode.Files == null || fileNode.Files.Count == 0)
  458. {
  459. return;
  460. }
  461. foreach (var item in fileNode.Files)
  462. {
  463. item.Parent = fileNode;
  464. SetFileNodeParent(item);
  465. }
  466. }
  467. private static void FileNodeItemToFileNode(ObservableCollection<FileNode> fileList, List<FileNodeItem> fileNodeItems)
  468. {
  469. foreach (var item in fileNodeItems)
  470. {
  471. if (item.IsFile)
  472. {
  473. fileList.Add(new FileNode(item));
  474. }
  475. else
  476. {
  477. GetSubFiles(item).ForEach(x => fileList.Add(x));
  478. }
  479. }
  480. }
  481. private static List<FileNode> GetSubFiles(FileNodeItem fileNodeItem)
  482. {
  483. List<FileNode> fileNodeList = new List<FileNode>();
  484. foreach (var item in fileNodeItem.Files)
  485. {
  486. if (item.IsFile)
  487. {
  488. fileNodeList.Add(new FileNode(item));
  489. }
  490. else
  491. {
  492. FileNode fileNode = new FileNode();
  493. fileNode.Name = item.Name;
  494. fileNode.FullPath = item.FullPath;
  495. fileNode.PrefixPath = item.PrefixPath;
  496. fileNode.IsFile = false;
  497. GetSubFiles(item).ForEach(x => fileNode.Files.Add(x));
  498. fileNodeList.Add(fileNode);
  499. }
  500. }
  501. return fileNodeList;
  502. }
  503. }
  504. }