DataLookupEditor.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Clients;
  5. namespace InABox.Core
  6. {
  7. public abstract class DataLookupEditor : BaseEditor, ILookupEditor
  8. {
  9. public DataLookupEditor(Type type, params string[] othercolumns)
  10. {
  11. Type = type;
  12. Visible = Visible.Hidden;
  13. LookupWidth = int.MaxValue;
  14. var name = type.Name;
  15. OtherColumns = new Dictionary<string, string>();
  16. foreach (var othercolumn in othercolumns)
  17. {
  18. var map = othercolumn.Split('=');
  19. if (map.Length == 2)
  20. OtherColumns[map.First()] = map.Last();
  21. else if (map.Length == 1)
  22. OtherColumns[map.First()] = map.First();
  23. }
  24. }
  25. public Dictionary<string, string> OtherColumns { get; }
  26. public Type Type { get; }
  27. public int LookupWidth { get; set; }
  28. public EditorButton[]? Buttons { get; set; }
  29. public virtual CoreTable Values(string columnname, object[]? items)
  30. {
  31. var client = ClientFactory.CreateClient(Type);
  32. IFilter? filter;
  33. if (items == null || !items.Any())
  34. filter = LookupFactory.DefineFilter(Type);
  35. else
  36. filter = LookupFactory.DefineFilter(Type, items.First().GetType(), items);
  37. var columns = LookupFactory.DefineColumns(Type);
  38. foreach (var key in OtherColumns.Keys)
  39. columns.Add(key);
  40. var sort = LookupFactory.DefineSort(Type);
  41. var result = client.Query(filter, columns, sort);
  42. result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) });
  43. foreach (var row in result.Rows)
  44. {
  45. var values = row.ToDictionary(new[] { "Display" });
  46. row["Display"] =
  47. LookupFactory.FormatLookup(Type, values,
  48. new string[] { }); //String.Join(" / ", values.Where(x => (x != null) && !String.IsNullOrWhiteSpace(x.ToString())));
  49. }
  50. return result;
  51. }
  52. public void Clear()
  53. {
  54. }
  55. protected BaseEditor CloneDataLookupEditor()
  56. {
  57. var result = (Activator.CreateInstance(GetType(), Type) as DataLookupEditor)!;
  58. foreach (var key in OtherColumns.Keys)
  59. result.OtherColumns[key] = OtherColumns[key];
  60. result.LookupWidth = LookupWidth;
  61. return result;
  62. }
  63. }
  64. }