using System; using System.Collections.Generic; using System.Linq; using InABox.Clients; namespace InABox.Core { public abstract class DataLookupEditor : BaseEditor, ILookupEditor { public DataLookupEditor(Type type, params string[] othercolumns) { Type = type; Visible = Visible.Hidden; LookupWidth = int.MaxValue; var name = type.Name; OtherColumns = new Dictionary(); foreach (var othercolumn in othercolumns) { var map = othercolumn.Split('='); if (map.Length == 2) OtherColumns[map.First()] = map.Last(); else if (map.Length == 1) OtherColumns[map.First()] = map.First(); } } public Dictionary OtherColumns { get; } public Type Type { get; } public int LookupWidth { get; set; } public EditorButton[]? Buttons { get; set; } public virtual CoreTable Values(string columnname, object[]? items) { var client = ClientFactory.CreateClient(Type); IFilter? filter; if (items == null || !items.Any()) filter = LookupFactory.DefineFilter(Type); else filter = LookupFactory.DefineFilter(Type, items.First().GetType(), items); var columns = LookupFactory.DefineColumns(Type); foreach (var key in OtherColumns.Keys) columns.Add(key); var sort = LookupFactory.DefineSort(Type); var result = client.Query(filter, columns, sort); result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) }); foreach (var row in result.Rows) { var values = row.ToDictionary(new[] { "Display" }); row["Display"] = LookupFactory.FormatLookup(Type, values, new string[] { }); //String.Join(" / ", values.Where(x => (x != null) && !String.IsNullOrWhiteSpace(x.ToString()))); } return result; } public void Clear() { } protected BaseEditor CloneDataLookupEditor() { var result = (Activator.CreateInstance(GetType(), Type) as DataLookupEditor)!; foreach (var key in OtherColumns.Keys) result.OtherColumns[key] = OtherColumns[key]; result.LookupWidth = LookupWidth; return result; } } }