DynamicImportMappingGrid.cs 6.8 KB

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