DynamicImportMappingGrid.cs 7.2 KB

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