DynamicImportMappingGrid.cs 7.4 KB

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