DataLookupEditor.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /// <summary>
  27. /// The type of the parent entity; this is set by <see cref="DatabaseSchema"/>.
  28. /// </summary>
  29. public Type ParentType { get; set; }
  30. public Type Type { get; }
  31. public int LookupWidth { get; set; }
  32. public EditorButton[]? Buttons { get; set; }
  33. public virtual CoreTable Values(string columnname, BaseObject[]? items)
  34. {
  35. var client = ClientFactory.CreateClient(Type);
  36. var filter = LookupFactory.DefineLookupFilter(ParentType, Type, columnname, items ?? (Array.CreateInstance(ParentType, 0) as BaseObject[])!);
  37. var columns = LookupFactory.DefineLookupColumns(ParentType, Type, columnname);
  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. row["Display"] = LookupFactory.FormatLookup(ParentType, Type, row, columnname);
  46. }
  47. return result;
  48. }
  49. public void Clear()
  50. {
  51. }
  52. protected BaseEditor CloneDataLookupEditor()
  53. {
  54. var result = (Activator.CreateInstance(GetType(), Type) as DataLookupEditor)!;
  55. foreach (var key in OtherColumns.Keys)
  56. result.OtherColumns[key] = OtherColumns[key];
  57. result.LookupWidth = LookupWidth;
  58. result.ParentType = ParentType;
  59. return result;
  60. }
  61. }
  62. }