DynamicImportMappingGrid.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if (ImportFieldGenerator.Fields.Contains(item.Property))
  102. {
  103. item.Field = item.Property;
  104. if (item.Property.Equals(UniqueCode))
  105. item.Key = true;
  106. }
  107. else
  108. {
  109. item.Key = false;
  110. item.Field = "";
  111. item.Constant = "";
  112. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  113. }
  114. Refresh(false, true);
  115. }
  116. private BitmapImage? KeyImage(CoreRow? arg)
  117. {
  118. if (arg == null)
  119. return key;
  120. return arg.Get<ImportMapping, bool>(x => x.Key) ? key : null;
  121. }
  122. private bool KeyAction(CoreRow? arg)
  123. {
  124. if (arg == null)
  125. return false;
  126. var item = Items[arg.Index];
  127. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  128. item.Key = false;
  129. else
  130. item.Key = !item.Key;
  131. return true;
  132. }
  133. private BitmapImage? LookupImage(CoreRow? arg)
  134. {
  135. if (arg == null)
  136. return restrict;
  137. var item = Items.FirstOrDefault(x => x.Property.Equals(arg.Get<ImportMapping, string>(c => c.Property)));
  138. if (item == null)
  139. return null;
  140. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  141. return null;
  142. return item.Lookup == ImportLookupType.Create
  143. ? create
  144. : item.Lookup == ImportLookupType.Restrict
  145. ? restrict
  146. : null;
  147. }
  148. private bool LookupAction(CoreRow? arg)
  149. {
  150. if (arg == null)
  151. return false;
  152. var property = arg.Get<ImportMapping, string>(c => c.Property);
  153. var item = Items.FirstOrDefault(x => x.Property.Equals(property));
  154. if (item == null || (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant)))
  155. return false;
  156. if (item.Lookup == ImportLookupType.Restrict)
  157. item.Lookup = ImportLookupType.Create;
  158. else if (item.Lookup == ImportLookupType.Create)
  159. item.Lookup = ImportLookupType.Restrict;
  160. else
  161. return false;
  162. return true;
  163. }
  164. }
  165. }