DynamicImportMappingGrid.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Media.Imaging;
  7. using InABox.Core;
  8. using InABox.WPF;
  9. namespace InABox.DynamicGrid;
  10. public class DynamicImportMappingGrid : DynamicGrid<ImportMapping>
  11. {
  12. private readonly BitmapImage create = Wpf.Resources.tick.AsBitmapImage();
  13. private readonly BitmapImage key = Wpf.Resources.key.AsBitmapImage();
  14. private readonly BitmapImage restrict = Wpf.Resources.warning.AsBitmapImage();
  15. public DynamicImportMappingGrid()
  16. {
  17. UniqueCode = "";
  18. HideBlank = false;
  19. Items = new List<ImportMapping>();
  20. }
  21. protected override void Init()
  22. {
  23. base.Init();
  24. ActionColumns.Add(new DynamicImageColumn(KeyImage, KeyAction) { Position = DynamicActionColumnPosition.Start, ToolTip = KeyToolTip });
  25. HiddenColumns.Add(x => x.Key);
  26. ActionColumns.Add(new DynamicImageColumn(LookupImage, LookupAction) { ToolTip = LookupToolTip });
  27. HiddenColumns.Add(x => x.Lookup);
  28. }
  29. protected override void DoReconfigure(DynamicGridOptions options)
  30. {
  31. base.DoReconfigure(options);
  32. options.RecordCount = true;
  33. options.DirectEdit = true;
  34. }
  35. public List<ImportMapping> Items { get; }
  36. public string UniqueCode { get; set; }
  37. public bool HideBlank { get; set; }
  38. public override void DeleteItems(params CoreRow[] rows)
  39. {
  40. var deletes = new List<ImportMapping>();
  41. foreach (var row in rows)
  42. deletes.Add(Items[row.Index]);
  43. Items.RemoveAll(x => deletes.Contains(x));
  44. }
  45. public override ImportMapping LoadItem(CoreRow row)
  46. {
  47. return Items[row.Index];
  48. }
  49. protected override void Reload(
  50. Filters<ImportMapping> criteria, Columns<ImportMapping> columns, ref SortOrder<ImportMapping>? sort,
  51. CancellationToken token, Action<CoreTable?, Exception?> action)
  52. {
  53. //Lookups.Clear();
  54. var result = new CoreTable();
  55. result.LoadColumns(typeof(ImportMapping));
  56. if (HideBlank)
  57. result.LoadRows(Items.Where(x => !string.IsNullOrWhiteSpace(x.Field) || !string.IsNullOrWhiteSpace(x.Constant)));
  58. else
  59. result.LoadRows(Items);
  60. foreach(var item in Items)
  61. {
  62. if(!item.Field.IsNullOrWhiteSpace() && !ImportFieldGenerator.Fields.Contains(item.Field))
  63. {
  64. item.Field = "";
  65. }
  66. }
  67. action.Invoke(result, null);
  68. }
  69. public override void SaveItem(ImportMapping item)
  70. {
  71. if (!Items.Contains(item))
  72. Items.Add(item);
  73. }
  74. private FrameworkElement? KeyToolTip(DynamicActionColumn column, CoreRow? row)
  75. {
  76. var result = "";
  77. if (row == null)
  78. result = "This column is used to indicate which fields form the unique key imported records";
  79. else
  80. {
  81. var key = row.Get<ImportMapping, bool>(x => x.Key);
  82. result = key
  83. ? "This field forms part (or all) of the unique key for imported records"
  84. : "";
  85. }
  86. return column.TextToolTip(result);
  87. }
  88. private FrameworkElement? LookupToolTip(DynamicActionColumn column, CoreRow? row)
  89. {
  90. var result = "";
  91. if (row == null)
  92. result = "This column determines whether or not lookup values are automatically created as required, or cause the import to fail.";
  93. else
  94. {
  95. var lookup = row.Get<ImportMapping, ImportLookupType>(x => x.Lookup);
  96. result = lookup == ImportLookupType.None
  97. ? ""
  98. : lookup == ImportLookupType.Create
  99. ? "Create Lookup Values as required"
  100. : "Restrict Importing for invalid / non-existent Lookup Values";
  101. }
  102. return column.TextToolTip(result);
  103. }
  104. public void Reset()
  105. {
  106. foreach (var item in Items)
  107. {
  108. item.Key = false;
  109. item.Field = "";
  110. item.Constant = "";
  111. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  112. }
  113. Refresh(false, true);
  114. }
  115. public void MatchFields()
  116. {
  117. foreach (var item in Items)
  118. {
  119. var field = ImportFieldGenerator.Fields.FirstOrDefault(x => String.Equals(item.Property.ToUpper(), x.ToUpper()));
  120. if (!String.IsNullOrWhiteSpace(field))
  121. {
  122. item.Field = field;
  123. if (item.Property.Equals(UniqueCode))
  124. item.Key = true;
  125. }
  126. else
  127. {
  128. item.Key = false;
  129. item.Field = "";
  130. item.Constant = "";
  131. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  132. }
  133. }
  134. Refresh(false, true);
  135. }
  136. private BitmapImage? KeyImage(CoreRow? arg)
  137. {
  138. if (arg == null)
  139. return key;
  140. return arg.Get<ImportMapping, bool>(x => x.Key) ? key : null;
  141. }
  142. private bool KeyAction(CoreRow? arg)
  143. {
  144. if (arg == null)
  145. return false;
  146. var item = Items[arg.Index];
  147. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  148. {
  149. if (item.Key)
  150. {
  151. item.Key = false;
  152. return true;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. else
  160. {
  161. item.Key = !item.Key;
  162. return true;
  163. }
  164. }
  165. private BitmapImage? LookupImage(CoreRow? arg)
  166. {
  167. if (arg == null)
  168. return restrict;
  169. var item = Items.FirstOrDefault(x => x.Property.Equals(arg.Get<ImportMapping, string>(c => c.Property)));
  170. if (item == null)
  171. return null;
  172. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  173. return null;
  174. return item.Lookup == ImportLookupType.Create
  175. ? create
  176. : item.Lookup == ImportLookupType.Restrict
  177. ? restrict
  178. : null;
  179. }
  180. private bool LookupAction(CoreRow? arg)
  181. {
  182. if (arg == null)
  183. return false;
  184. var property = arg.Get<ImportMapping, string>(c => c.Property);
  185. var item = Items.FirstOrDefault(x => x.Property.Equals(property));
  186. if (item == null || (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant)))
  187. return false;
  188. if (item.Lookup == ImportLookupType.Restrict)
  189. item.Lookup = ImportLookupType.Create;
  190. else if (item.Lookup == ImportLookupType.Create)
  191. item.Lookup = ImportLookupType.Restrict;
  192. else
  193. return false;
  194. return true;
  195. }
  196. }