DFLookupControl.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using ComboItemType = System.Tuple<string, System.Collections.Generic.Dictionary<string, object?>>;
  9. namespace InABox.DynamicGrid
  10. {
  11. public class DFLookupControl : DynamicFormFieldControl<DFLayoutLookupField, DFLayoutLookupFieldProperties, Guid>
  12. {
  13. private DFLayoutLookupValue _value = null!;
  14. private ComboBox Combo = null!; // late-initialising
  15. protected override FrameworkElement Create()
  16. {
  17. _value = new DFLayoutLookupValue();
  18. Combo = new ComboBox();
  19. if (CoreUtils.TryGetEntity(Field.Properties.LookupType, out var type))
  20. {
  21. var client = ClientFactory.CreateClient(type);
  22. //Task.Run(() =>
  23. //{
  24. var columns = LookupFactory.DefineColumns(type);
  25. foreach(var property in Field.Properties.AdditionalPropertiesList)
  26. {
  27. columns.Add(property);
  28. }
  29. IFilter? filter;
  30. if (!Field.Properties.Filter.IsNullOrWhiteSpace())
  31. {
  32. filter = Serialization.Deserialize(typeof(Filter<>).MakeGenericType(type), Field.Properties.Filter) as IFilter;
  33. filter?.ConvertCustomValues((_, value) =>
  34. {
  35. var property = System.Text.Encoding.UTF8.GetString(value.Data);
  36. return FormDesignGrid.DataModel?.GetEntityValue(property);
  37. });
  38. }
  39. else
  40. {
  41. filter = LookupFactory.DefineFilter(type);
  42. }
  43. var table = client.Query(
  44. filter,
  45. columns,
  46. LookupFactory.DefineSort(type)
  47. );
  48. var lookups = new Dictionary<Guid, ComboItemType>()
  49. {
  50. { Guid.Empty, new("(None)", table.NewRow().ToDictionary(new[] { "ID" })) }
  51. };
  52. foreach (var row in table.Rows)
  53. {
  54. var id = row.Get<Guid>("ID");
  55. var values = row.ToDictionary(new[] { "ID" });
  56. var display = LookupFactory.FormatLookup(type, values, new string[] { });
  57. lookups[id] = new(display, values);
  58. }
  59. //Dispatcher.Invoke(() =>
  60. //{
  61. Combo.DisplayMemberPath = "Value.Item1";
  62. Combo.SelectedValuePath = "Key";
  63. Combo.ItemsSource = lookups;
  64. Combo.VerticalContentAlignment = VerticalAlignment.Center;
  65. Combo.SelectionChanged += (sender, e) => ChangeField();
  66. //});
  67. //});
  68. }
  69. return Combo;
  70. }
  71. public override void Deserialize(string serialized)
  72. {
  73. _value.Load(serialized);
  74. }
  75. public override string Serialize()
  76. {
  77. return _value.ToString();
  78. }
  79. public override object? GetEntityValue()
  80. {
  81. return (Guid?)Combo.SelectedValue ?? Guid.Empty;
  82. }
  83. public override void SetEntityValue(object? value)
  84. {
  85. Combo.SelectedValue = value;
  86. }
  87. public override object? GetData(string dataField)
  88. {
  89. return ((KeyValuePair<Guid, ComboItemType>?)Combo.SelectedItem)?.Value.Item2.GetValueOrDefault(dataField);
  90. }
  91. public override Dictionary<string, object?>? GetAdditionalValues()
  92. {
  93. if (Field.Properties.AdditionalPropertiesList.Count == 0)
  94. return null;
  95. var values = new Dictionary<string, object?>();
  96. foreach(var field in Field.Properties.AdditionalPropertiesList)
  97. {
  98. values[field] = GetData(field);
  99. }
  100. return values;
  101. }
  102. public override Guid GetValue() => ((KeyValuePair<Guid, ComboItemType>?)Combo.SelectedItem)?.Key ?? Guid.Empty;
  103. public override void SetValue(Guid value)
  104. {
  105. var lookups = Combo.ItemsSource as Dictionary<Guid, ComboItemType>;
  106. if (lookups is null)
  107. return;
  108. var lookup = lookups!.FirstOrDefault(x => x.Key == value);
  109. Combo.SelectedItem = lookup;
  110. }
  111. protected override bool IsEmpty() => Combo.SelectedValue == null || (Guid)Combo.SelectedValue == Guid.Empty;
  112. }
  113. }