1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InABox.Core
- {
- public interface IExpressionModelGenerator
- {
- public List<string> GetVariables(object?[] items);
- }
- public delegate DataModel GetVariables();
- public class ExpressionEditor : BaseEditor
- {
- /// <summary>
- /// The <see cref="IExpressionModel"/> to use for this expression; if set to null, the variables must be manually set for the editor.
- /// </summary>
- public Type? ModelType { get; set; }
- private IExpressionModelGenerator? _modelGenerator;
- public Type? ModelGenerator { get; }
- public event GetVariables OnGetVariables;
- [DoNotSerialize]
- [JsonIgnore]
- public List<string>? VariableNames { get; set; }
- public ExpressionEditor(Type? modelType, Type? modelGenerator = null)
- {
- ModelType = modelType;
- if (modelGenerator != null)
- {
- if (!typeof(IExpressionModelGenerator).IsAssignableFrom(modelGenerator))
- {
- throw new Exception($"Model generator must be a {nameof(IExpressionModelGenerator)}");
- }
- ModelGenerator = modelGenerator;
- }
- }
- protected override BaseEditor DoClone()
- {
- return new ExpressionEditor(ModelType, ModelGenerator)
- {
- VariableNames = VariableNames
- };
- }
- public List<string> GetVariables(object?[] items)
- {
- if (VariableNames != null)
- return VariableNames;
- if (ModelGenerator != null)
- {
- _modelGenerator ??= (Activator.CreateInstance(ModelGenerator) as IExpressionModelGenerator)!;
- return _modelGenerator.GetVariables(items);
- }
- if (ModelType != null)
- return CoreExpression.GetModelVariables(ModelType);
- var model = OnGetVariables?.Invoke();
- if (model != null)
- return GetVariablesFromDataModel(model);
- return new List<string>();
- }
- private List<string> GetVariablesFromDataModel(DataModel model)
- {
- List<string> list = new List<string>();
- foreach (var table in model.DefaultTables)
- {
- foreach (var col in table.Columns)
- {
- if (col.ToString() != "ID")
- list.Add(table.ToString() + "." + col.ToString());
- }
- }
- list.Sort();
- return list;
- }
- }
- }
|