DynamicImportMappingGrid.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. foreach(var item in Items)
  57. {
  58. if(!item.Field.IsNullOrWhiteSpace() && !ImportFieldGenerator.Fields.Contains(item.Field))
  59. {
  60. item.Field = "";
  61. }
  62. }
  63. action.Invoke(result, null);
  64. }
  65. public override void SaveItem(ImportMapping item)
  66. {
  67. if (!Items.Contains(item))
  68. Items.Add(item);
  69. }
  70. private FrameworkElement? KeyToolTip(DynamicActionColumn column, CoreRow? row)
  71. {
  72. var result = "";
  73. if (row == null)
  74. result = "This column is used to indicate which fields form the unique key imported records";
  75. else
  76. {
  77. var key = row.Get<ImportMapping, bool>(x => x.Key);
  78. result = key
  79. ? "This field forms part (or all) of the unique key for imported records"
  80. : "";
  81. }
  82. return column.TextToolTip(result);
  83. }
  84. private FrameworkElement? LookupToolTip(DynamicActionColumn column, CoreRow? row)
  85. {
  86. var result = "";
  87. if (row == null)
  88. result = "This column determines whether or not lookup values are automatically created as required, or cause the import to fail.";
  89. else
  90. {
  91. var lookup = row.Get<ImportMapping, ImportLookupType>(x => x.Lookup);
  92. result = lookup == ImportLookupType.None
  93. ? ""
  94. : lookup == ImportLookupType.Create
  95. ? "Create Lookup Values as required"
  96. : "Restrict Importing for invalid / non-existent Lookup Values";
  97. }
  98. return column.TextToolTip(result);
  99. }
  100. public void Reset()
  101. {
  102. foreach (var item in Items)
  103. {
  104. item.Key = false;
  105. item.Field = "";
  106. item.Constant = "";
  107. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  108. }
  109. Refresh(false, true);
  110. }
  111. public void MatchFields()
  112. {
  113. foreach (var item in Items)
  114. {
  115. var field = ImportFieldGenerator.Fields.FirstOrDefault(x => String.Equals(item.Property.ToUpper(), x.ToUpper()));
  116. if (!String.IsNullOrWhiteSpace(field))
  117. {
  118. item.Field = field;
  119. if (item.Property.Equals(UniqueCode))
  120. item.Key = true;
  121. }
  122. else
  123. {
  124. item.Key = false;
  125. item.Field = "";
  126. item.Constant = "";
  127. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  128. }
  129. }
  130. Refresh(false, true);
  131. }
  132. private BitmapImage? KeyImage(CoreRow? arg)
  133. {
  134. if (arg == null)
  135. return key;
  136. return arg.Get<ImportMapping, bool>(x => x.Key) ? key : null;
  137. }
  138. private bool KeyAction(CoreRow? arg)
  139. {
  140. if (arg == null)
  141. return false;
  142. var item = Items[arg.Index];
  143. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  144. {
  145. if (item.Key)
  146. {
  147. item.Key = false;
  148. return true;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. else
  156. {
  157. item.Key = !item.Key;
  158. return true;
  159. }
  160. }
  161. private BitmapImage? LookupImage(CoreRow? arg)
  162. {
  163. if (arg == null)
  164. return restrict;
  165. var item = Items.FirstOrDefault(x => x.Property.Equals(arg.Get<ImportMapping, string>(c => c.Property)));
  166. if (item == null)
  167. return null;
  168. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  169. return null;
  170. return item.Lookup == ImportLookupType.Create
  171. ? create
  172. : item.Lookup == ImportLookupType.Restrict
  173. ? restrict
  174. : null;
  175. }
  176. private bool LookupAction(CoreRow? arg)
  177. {
  178. if (arg == null)
  179. return false;
  180. var property = arg.Get<ImportMapping, string>(c => c.Property);
  181. var item = Items.FirstOrDefault(x => x.Property.Equals(property));
  182. if (item == null || (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant)))
  183. return false;
  184. if (item.Lookup == ImportLookupType.Restrict)
  185. item.Lookup = ImportLookupType.Create;
  186. else if (item.Lookup == ImportLookupType.Create)
  187. item.Lookup = ImportLookupType.Restrict;
  188. else
  189. return false;
  190. return true;
  191. }
  192. }
  193. }