DynamicImportMappingGrid.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = Properties.Resources.tick.AsBitmapImage();
  13. private readonly BitmapImage key = Properties.Resources.key.AsBitmapImage();
  14. private readonly BitmapImage restrict = Properties.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 DynamicActionColumn(KeyImage, KeyAction) { Position = DynamicActionColumnPosition.Start, ToolTip = KeyToolTip });
  22. HiddenColumns.Add(x => x.Key);
  23. ActionColumns.Add(new DynamicActionColumn(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. protected 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. var key = row.Get<ImportMapping, bool>(x => x.Key);
  63. result = key
  64. ? "This field forms part (or all) of the unique key for imported records"
  65. : "";
  66. return column.TextToolTip(result);
  67. }
  68. private FrameworkElement LookupToolTip(DynamicActionColumn column, CoreRow row)
  69. {
  70. var result = "";
  71. if (row == null)
  72. result = "This column determines whether or not lookup values are automatically created as required, or cause the import to fail.";
  73. var lookup = row.Get<ImportMapping, ImportLookupType>(x => x.Lookup);
  74. result = lookup == ImportLookupType.None
  75. ? ""
  76. : lookup == ImportLookupType.Create
  77. ? "Create Lookup Values as required"
  78. : "Restrict Importing for invalid / non-existent Lookup Values";
  79. return column.TextToolTip(result);
  80. }
  81. public void Reset()
  82. {
  83. foreach (var item in Items)
  84. {
  85. item.Key = false;
  86. item.Field = "";
  87. item.Constant = "";
  88. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  89. }
  90. Refresh(false, true);
  91. }
  92. public void MatchFields()
  93. {
  94. foreach (var item in Items)
  95. if (ImportFieldGenerator.Fields.Contains(item.Property))
  96. {
  97. item.Field = item.Property;
  98. if (item.Property.Equals(UniqueCode))
  99. item.Key = true;
  100. }
  101. else
  102. {
  103. item.Key = false;
  104. item.Field = "";
  105. item.Constant = "";
  106. item.Lookup = item.Lookup == ImportLookupType.Create ? ImportLookupType.Restrict : item.Lookup;
  107. }
  108. Refresh(false, true);
  109. }
  110. private BitmapImage KeyImage(CoreRow arg)
  111. {
  112. if (arg == null)
  113. return key;
  114. return arg.Get<ImportMapping, bool>(x => x.Key) ? key : null;
  115. }
  116. private bool KeyAction(CoreRow arg)
  117. {
  118. if (arg == null)
  119. return false;
  120. var item = Items[arg.Index];
  121. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  122. item.Key = false;
  123. else
  124. item.Key = !item.Key;
  125. return true;
  126. }
  127. private BitmapImage LookupImage(CoreRow arg)
  128. {
  129. if (arg == null)
  130. return restrict;
  131. var item = Items.FirstOrDefault(x => x.Property.Equals(arg.Get<ImportMapping, string>(c => c.Property)));
  132. if (item == null)
  133. return null;
  134. if (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant))
  135. return null;
  136. return item.Lookup == ImportLookupType.Create
  137. ? create
  138. : item.Lookup == ImportLookupType.Restrict
  139. ? restrict
  140. : null;
  141. }
  142. private bool LookupAction(CoreRow arg)
  143. {
  144. if (arg == null)
  145. return false;
  146. var property = arg.Get<ImportMapping, string>(c => c.Property);
  147. var item = Items.FirstOrDefault(x => x.Property.Equals(property));
  148. if (item == null || (string.IsNullOrWhiteSpace(item.Field) && string.IsNullOrWhiteSpace(item.Constant)))
  149. return false;
  150. if (item.Lookup == ImportLookupType.Restrict)
  151. item.Lookup = ImportLookupType.Create;
  152. else if (item.Lookup == ImportLookupType.Create)
  153. item.Lookup = ImportLookupType.Restrict;
  154. else
  155. return false;
  156. return true;
  157. }
  158. }
  159. }