DynamicExportForm.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using InABox.Core;
  10. using InABox.Wpf;
  11. using InABox.WPF;
  12. using Syncfusion.Windows.Shared;
  13. using Xceed.Wpf.AvalonDock.Themes;
  14. namespace InABox.DynamicGrid
  15. {
  16. /// <summary>
  17. /// Interaction logic for DynamicExportForm.xaml
  18. /// </summary>
  19. public partial class DynamicExportForm : ThemableWindow
  20. {
  21. private readonly Type _entitytype;
  22. public DynamicExportForm(Type entitytype, IEnumerable<string> Selected)
  23. {
  24. InitializeComponent();
  25. Fields = Array.Empty<string>();
  26. ChildMappings = new();
  27. var codeProperty = EntityCode(entitytype);
  28. if (codeProperty != null)
  29. {
  30. var m2mTypes = DynamicGridUtils.GetManyToManyTypes(entitytype)
  31. .Where(x => x.GetInterfaces().Contains(typeof(IExportable)));
  32. var o2mTypes = DynamicGridUtils.GetOneToManyTypes(entitytype)
  33. .Where(x => x.GetInterfaces().Contains(typeof(IExportable)));
  34. foreach(var type in m2mTypes)
  35. {
  36. var otherType = CoreUtils.GetManyToManyOtherType(type, entitytype);
  37. var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(otherType.Name);
  38. AddOtherTab(type, result);
  39. }
  40. foreach(var type in o2mTypes)
  41. {
  42. var caption = type.GetCustomAttribute(typeof(Caption));
  43. var result = caption != null ?
  44. ((Caption)caption).Text :
  45. new Inflector.Inflector(new CultureInfo("en")).Pluralize(type.Name);
  46. AddOtherTab(type, result);
  47. }
  48. }
  49. _entitytype = entitytype;
  50. ShowHelpImage.Source = Properties.Resources.help.AsBitmapImage();
  51. var mappings = ImportFactory.ExtractMappings(_entitytype, ImportMappingType.Export);
  52. Mappings.HideBlank = true;
  53. Mappings.Items.AddRange(mappings);
  54. Mappings.Selected.AddRange(mappings.Where(x => x.Key || Selected.Contains(x.Property)));
  55. Mappings.Refresh(true, true);
  56. }
  57. private DynamicExportMappingGrid CreateOtherTab(Type tabType)
  58. {
  59. var mappingsGrid = new DynamicExportMappingGrid();
  60. var mappings = ImportFactory.ExtractMappings(tabType, ImportMappingType.Export);
  61. mappingsGrid.HideBlank = true;
  62. mappingsGrid.Items.AddRange(mappings);
  63. mappingsGrid.Selected.Clear();
  64. mappingsGrid.Refresh(true, true);
  65. return mappingsGrid;
  66. }
  67. private void AddOtherTab(Type tabType, string caption)
  68. {
  69. var tab = new TabItem() { Header = caption };
  70. var grid = CreateOtherTab(tabType);
  71. ChildMappings[tabType] = grid;
  72. tab.Content = grid;
  73. TabControl.Items.Add(tab);
  74. }
  75. private DynamicExportMappingGrid CurrentMappings
  76. {
  77. get => (TabControl.SelectedContent as DynamicExportMappingGrid)!;
  78. }
  79. public string[] Fields { get; private set; }
  80. private Dictionary<Type, DynamicExportMappingGrid> ChildMappings;
  81. private void ClearAll_Click(object sender, RoutedEventArgs e)
  82. {
  83. CurrentMappings.ClearAll();
  84. }
  85. private void SelectAll_Click(object sender, RoutedEventArgs e)
  86. {
  87. CurrentMappings.SelectAll();
  88. }
  89. private void HideBlank_Click(object sender, RoutedEventArgs e)
  90. {
  91. CurrentMappings.HideBlank = !CurrentMappings.HideBlank;
  92. HideBlank.Content = CurrentMappings.HideBlank ? "Show All" : "Hide Blank";
  93. CurrentMappings.Refresh(false, true);
  94. }
  95. private void Cancel_Click(object sender, RoutedEventArgs e)
  96. {
  97. DialogResult = false;
  98. }
  99. private void OK_Click(object sender, RoutedEventArgs e)
  100. {
  101. Fields = Mappings.Selected.Select(x => x.Property).ToArray();
  102. DialogResult = true;
  103. }
  104. private void ShowHelp_Click(object sender, RoutedEventArgs e)
  105. {
  106. Process.Start(new ProcessStartInfo("https://prs-software.com.au/wiki/index.php/Export_Data") { UseShellExecute = true });
  107. }
  108. private IProperty? EntityCode(Type entityType)
  109. {
  110. foreach(var property in DatabaseSchema.Properties(entityType))
  111. {
  112. var editor = property.Editor;
  113. if(editor is UniqueCodeEditor)
  114. {
  115. return property;
  116. }
  117. }
  118. return null;
  119. }
  120. public Dictionary<Type, IEnumerable<string>> GetChildFields()
  121. {
  122. return ChildMappings.ToDictionary(
  123. x => x.Key,
  124. x => x.Value.Selected.Select(x => x.Property));
  125. }
  126. }
  127. }