DynamicImportMappingGrid.cs 7.5 KB

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