123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using InABox.Clients;
- namespace InABox.Core
- {
- public delegate void DataModelUpdateEvent(string section, DataModel model);
- [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
- public sealed class DataModelTableNameAttribute : Attribute
- {
- public string TableName { get; set; }
- public DataModelTableNameAttribute(string tableName)
- {
- TableName = tableName;
- }
- }
- public interface IDataModelSource
- {
- string SectionName { get; }
- DataModel DataModel(Selection selection);
- event DataModelUpdateEvent? OnUpdateDataModel;
- }
- public interface IDataModelRelationship
- {
- string ChildColumn { get; }
- string ChildTable { get; }
- string ParentColumn { get; }
- string ParentTable { get; }
- bool IsLookup { get; }
- DataRelation AsDataRelation();
- string ParentColumnAsPropertyName();
- string ChildColumnAsPropertyName();
- }
- public interface IDataModelQueryDef : IQueryDef
- {
- string TableName { get; }
- }
- public class DataModelQueryDef<T> : IDataModelQueryDef
- {
- public DataModelQueryDef(Filter<T> filter, Columns<T> columns, SortOrder<T> sortorder, string? alias = null)
- {
- Type = typeof(T);
- Filter = filter;
- Columns = columns;
- SortOrder = sortorder;
- TableName = DataModel.TableName(Type, alias);
- }
- public Type Type { get; }
- public IFilter Filter { get; }
- public IColumns Columns { get; }
- public ISortOrder SortOrder { get; }
- public string TableName { get; }
- }
- public delegate void OnBeforeLoad(CancelEventArgs args);
- public delegate void OnAfterLoad(CancelEventArgs args);
- public interface IDataModelTable
- {
- string Name { get; set; }
- CoreTable Data { get; }
- bool IsDefault { get; set; }
- bool IsRequired { get; set; }
- /// <summary>
- /// The type of entity stored in this table; may be <see langword="null"/> if this table does not represent an entity.
- /// </summary>
- Type? Type { get; }
- IEnumerable<IDataModelTable> ChildTables { get; }
- IDataModelTable ChildTable(string alias);
- IDataModelTable ChildTable<T>(string? alias = null);
- void Load();
- }
- public interface IDataModel
- {
- IEnumerable<DataTable> DefaultTables { get; }
- void AddTable(string alias, CoreTable table, bool isdefault = false);
- void AddTable(Type type, CoreTable table, bool isdefault = false, string? alias = null);
- /// <summary>
- /// Adds a table to the datamodel.
- /// </summary>
- /// <typeparam name="TType">The type of the object represented by the table.</typeparam>
- /// <param name="filter">A filter for the table. If set to <see langword="null"/>, loads all objects of <typeparamref name="TType"/>.</param>
- /// <param name="columns">The columns to load for this table. If set to <see langword="null"/>, loads all the columns of <typeparamref name="TType"/>.</param>
- /// <param name="isdefault">
- /// Is this table default loaded? If set to <see langword="true"/>, this table is added to <see cref="DefaultTables"/>.</param>
- /// <param name="alias">The name of this table. Defaults to <typeparamref name="TType"/> if not set or <see langword="null"/>.</param>
- /// <param name="shouldLoad">
- /// <see langword="true"/> if this table should be loaded - in some cases a table's data should loaded manually.<br/>
- /// Set to <see langword="false"/> if this is the case.
- /// </param>
- void AddTable<TType>(Filter<TType>? filter, Columns<TType>? columns, bool isdefault = false, string? alias = null, bool shouldLoad = true);
- void LinkTable(Type parenttype, string parentcolumn, Type childtype, string childcolumn, string? parentalias = null, string? childalias = null, bool isLookup = false);
- void LinkTable(Type parenttype, string parentcolumn, string childalias, string childcolumn, string? parentalias = null, bool isLookup = false);
- void LinkTable<TParent, TChild>(Expression<Func<TParent, object>> parent, Expression<Func<TChild, object>> child, string? parentalias = null,
- string? childalias = null, bool isLookup = false);
- void AddChildTable<TParent, TChild>(Expression<Func<TParent, object>> parentcolumn, Expression<Func<TChild, object>> childcolumn,
- Filter<TChild>? filter = null, Columns<TChild>? columns = null, bool isdefault = false, string? parentalias = null,
- string? childalias = null);
- void AddLookupTable<TSource, TLookup>(Expression<Func<TSource, object>> sourcecolumn, Expression<Func<TLookup, object>> lookupcolumn,
- Filter<TLookup>? filter = null, Columns<TLookup>? columns = null, bool isdefault = false, string? sourcealias = null,
- string? lookupalias = null);
- /// <summary>
- /// Remove a table from the data model.
- /// </summary>
- /// <typeparam name="TType">The type of the table to be removed.</typeparam>
- /// <param name="alias">The table name, defaulting to the name of <typeparamref name="TType"/>.</param>
- /// <returns><see langword="true"/> if the table was removed, <see langword="false"/> if it was not removed because it didn't exist.</returns>
- bool RemoveTable<TType>(string? alias = null);
- /// <summary>
- /// Gets the filter for a given table, which is used during <see cref="LoadModel(IEnumerable{string}, IDataModelQueryDef[]).
- /// </summary>
- /// <typeparam name="TType">The type of the table to get the filter of.</typeparam>
- /// <param name="alias">The name of the table, defaulting to <typeparamref name="TType"/></param>
- /// <returns>The filter.</returns>
- Filter<TType>? GetFilter<TType>(string? alias = null);
- /// <summary>
- /// Gets the columns for a given table, which are used during <see cref="LoadModel(IEnumerable{string}, IDataModelQueryDef[]).
- /// </summary>
- /// <typeparam name="TType">The type of the table to get the columns of.</typeparam>
- /// <param name="alias">The name of the table, defaulting to <typeparamref name="TType"/></param>
- /// <returns>The columns.</returns>
- Columns<TType>? GetColumns<TType>(string? alias = null);
- /// <summary>
- /// Sets the filter for a given table, which is used during <see cref="LoadModel(IEnumerable{string}, IDataModelQueryDef[]).
- /// </summary>
- /// <typeparam name="TType">The type of the table to set the filter of.</typeparam>
- /// <param name="filter">The new filter.</param>
- /// <param name="alias">The name of the table, defaulting to <typeparamref name="TType"/></param>
- void SetFilter<TType>(Filter<TType>? filter, string? alias = null);
- /// <summary>
- /// Sets the columns for a given table, which are used during <see cref="LoadModel(IEnumerable{string}, IDataModelQueryDef[]).
- /// </summary>
- /// <typeparam name="TType">The type of the table.</typeparam>
- /// <param name="columns">The new columns.</param>
- /// <param name="alias">The name of the table, defaulting to <typeparamref name="TType"/>.</param>
- void SetColumns<TType>(Columns<TType>? columns, string? alias = null);
- void SetIsDefault<TType>(bool isDefault, string? alias = null);
- void SetShouldLoad<TType>(bool shouldLoad, string? alias = null);
- CoreTable GetTable<TType>(string? alias = null);
- void SetTableData(Type type, CoreTable tableData, string? alias = null);
- bool HasTable(Type type, string? alias = null);
- bool HasTable<TType>(string? alias = null);
- void LoadModel(IEnumerable<string>? requiredTables, Dictionary<string, IQueryDef>? requiredQueries = null);
- void LoadModel(IEnumerable<string>? requiredTables, params IDataModelQueryDef[] requiredQueries);
- /// <summary>
- /// Load the model, loading all tables that are set to be default. (See <see cref="SetIsDefault{TType}(bool, string?)"/>).
- /// </summary>
- void LoadModel();
- TType[] ExtractValues<TSource, TType>(Expression<Func<TSource, TType>> column, bool distinct = true, string? alias = null);
- }
- public interface IDataModel<T> : IDataModel
- where T : Entity, IRemotable, IPersistent, new()
- {
- }
- public class TableDoesNotExistException : Exception
- {
- public TableDoesNotExistException(string tableName): base($"No table for {tableName}")
- {
- }
- }
- public class TableDoesNotExistException<TTable> : Exception
- {
- public TableDoesNotExistException(string tableName) : base($"No table for {tableName} of type {typeof(TTable)}")
- {
- }
- }
- public class AmbiguousTableException<TTable> : Exception
- {
- public AmbiguousTableException() : base($"Multiple tables of type {typeof(TTable)}") { }
- public AmbiguousTableException(string alias) : base($"Multiple tables of type {typeof(TTable)} for given alias '{alias}'") { }
- }
- public abstract class DataModel : IDataModel
- {
- private readonly List<IDataModelRelationship> _relationships = new List<IDataModelRelationship>();
- private readonly Dictionary<string, IDataModelTable> _tables = new Dictionary<string, IDataModelTable>();
- public DataModel()
- {
- AddTable<CompanyInformation>(null, null, true);
- AddChildTable<CompanyInformation, Document>(x => x.Logo.ID, x => x.ID, null, null, true, null, "CompanyLogo");
- AddTable(new Filter<User>(x => x.ID).IsEqualTo(ClientFactory.UserGuid), null, true);
- }
- public IEnumerable<KeyValuePair<string, IDataModelTable>> ModelTables => _tables;
- public abstract string Name { get; }
- public IEnumerable<DataRelation> Relations => _relationships.Select(x => x.AsDataRelation()).ToArray();
- public IEnumerable<DataTable> Tables => _tables.Select(x => x.Value.Table.ToDataTable(x.Key));
- public IEnumerable<DataTable> DefaultTables => _tables.Where(x => x.Value.IsDefault).Select(x => x.Value.Table.ToDataTable(x.Key));
- public IEnumerable<string> DefaultTableNames => _tables.Where(x => x.Value.IsDefault).Select(x => x.Key);
- public IEnumerable<string> TableNames => _tables.Select(x => x.Key);
- public TType[] ExtractValues<TSource, TType>(Expression<Func<TSource, TType>> column, bool distinct = true, string? alias = null)
- {
- return GetTable<TSource>(alias).ExtractValues(column, distinct).ToArray();
- }
- public DataSet AsDataSet()
- {
- var result = new DataSet();
- foreach(var (key, table) in _tables)
- {
- var current = table.Table.Columns.ToDictionary(x => x.ColumnName, x => x);
- IColumns? additional = null;
- if(table.Type != null)
- {
- additional = Columns.None(table.Type);
- foreach (var column in CoreUtils.GetColumnNames(table.Type, x => true))
- {
- if (!current.ContainsKey(column))
- {
- additional.Add(column);
- }
- }
- }
- var dataTable = table.Table.ToDataTable(key, additional);
- result.Tables.Add(dataTable);
- }
- //result.Tables.AddRange(_tables.Select(x => x.Value.Table.ToDataTable(x.Key)).ToArray());
- foreach (var relation in _relationships)
- {
- var childTable = result.Tables[relation.ChildTable];
- var parentTable = result.Tables[relation.ParentTable];
- if (childTable is null)
- {
- continue;
- }
- if (parentTable is null)
- {
- result.Tables.Remove(childTable);
- continue;
- }
- var parentColumn = parentTable.Columns[relation.ParentColumn.Replace(".", "_")];
- var childColumn = childTable.Columns[relation.ChildColumn.Replace(".", "_")];
- if (parentColumn is null || childColumn is null)
- {
- result.Tables.Remove(childTable);
- continue;
- }
- if (relation.IsLookup)
- {
- result.Relations.Add(
- string.Format("{0}_{1}", relation.ChildTable, relation.ParentTable),
- childColumn,
- parentColumn,
- false
- );
- }
- else
- {
- result.Relations.Add(
- string.Format("{0}_{1}", relation.ParentTable, relation.ChildTable),
- parentColumn,
- childColumn,
- false
- );
- }
- }
- return result;
- }
- public event OnBeforeLoad? OnBeforeLoad;
- public event OnAfterLoad? OnAfterLoad;
- protected virtual void BeforeLoad(IEnumerable<string> requiredtables)
- {
- }
- protected virtual void CheckRequiredTables(List<string> requiredtables)
- {
- }
- //protected abstract void Load(IEnumerable<string> requiredtables);
- protected virtual void AfterLoad(IEnumerable<string> requiredTables)
- {
- }
- protected void Load(Type type, CoreTable data, IEnumerable<string> requiredtables, string? alias = null)
- {
- CheckTable(type, alias);
- var name = TableName(type, alias);
- var table = _tables[name].Table;
- if(!ReferenceEquals(table, data))
- {
- table.Rows.Clear();
- if (IsRequired(type, requiredtables, alias))
- data.CopyTo(table);
- }
- }
- protected void Load<TType>(CoreTable data, IEnumerable<string> requiredtables, string? alias = null)
- {
- Load(typeof(TType), data, requiredtables, alias);
- }
- protected void Load<TType>(IEnumerable<TType> items, IEnumerable<string> requiredtables, string? alias = null)
- where TType : notnull
- {
- CheckTable<TType>(alias);
- var name = TableName<TType>(alias);
- var table = _tables[name].Table;
- table.Rows.Clear();
- if (IsRequired<TType>(requiredtables))
- foreach (var item in items)
- {
- table.LoadRow(item);
- }
- }
- private void CheckTable<TType>(string? alias = null)
- {
- CheckTable(typeof(TType), alias);
- }
- protected static string TableName<TType>(string? alias = null)
- {
- return TableName(typeof(TType), alias);
- }
- public class DataModelTable
- {
- private bool shouldLoad;
- public DataModelTable(Type? type, CoreTable table, bool isDefault, IFilter? filter, IColumns? columns, bool shouldLoad = true)
- {
- Type = type;
- Table = table;
- IsDefault = isDefault;
- Filter = filter;
- Columns = columns;
- ShouldLoad = shouldLoad;
- }
- public Type? Type { get; }
- public CoreTable Table { get; set; }
- public bool IsDefault { get; set; }
- public IFilter? Filter { get; set; }
- public IColumns? Columns { get; set; }
- public bool ShouldLoad
- {
- get => shouldLoad && Type != null;
- set
- {
- shouldLoad = value;
- }
- }
- }
- private interface IDataModelLoadTable : IDataModelTable
- {
- public IFilter? GetFilter();
- public IQueryDef GetQueryDef();
- public void LoadData(CoreTable data);
- }
- public class DataModelLoadTable<T> : IDataModelTable, IDataModelLoadTable
- where T : Entity, IRemotable, IPersistent, new()
- {
- public string Name { get; set; }
- public Type? Type => typeof(T);
- public DataModel Model { get; set; }
- public CoreTable Data { get; private set; }
- /// <summary>
- /// Set to <see langword="true"/> if this table should be included in lists of default required tables. Thus, this is not used for the actual loading of data; use <see cref="IsRequired"/> for that.
- /// </summary>
- public bool IsDefault { get; set; }
- public bool IsRequired { get; set; }
- public Filter<T> Filter { get; set; }
- public Columns<T> Columns { get; set; }
- public IEnumerable<IDataModelTable> ChildTables =>
- Model._relationships.Where(x => x.ParentTable == Name).Select(x => Model._tables[x.ChildTable]);
- public DataModelLoadTable(DataModel model, string name, Filter<T> filter, Columns<T> columns)
- {
- Model = model;
- Name = name;
- Filter = filter;
- Columns = columns;
- }
- public IDataModelTable ChildTable(string alias)
- {
- var name = TableName(alias);
- var relation = Model._relationships.Where(x => x.ParentTable == Name && x.ChildTable == alias).FirstOrDefault();
- if(relation != null && Model._tables.TryGetValue(name, out var table))
- {
- return table;
- }
- throw new TableDoesNotExistException(name);
- }
- public IDataModelTable ChildTable<TChild>(string? alias = null)
- {
- IDataModelTable table;
- if (alias != null)
- {
- table = ChildTable(alias);
- if(table.Type != typeof(TChild))
- {
- throw new TableDoesNotExistException<TChild>(TableName<TChild>(alias));
- }
- return table;
- }
- var relations = ChildTables.Where(x => x.Type == typeof(TChild)).ToList();
- if(relations.Count == 1)
- {
- return relations[0];
- }
- var childName = TableName<TChild>(alias);
- var single = relations.Where(x => x.Name == childName).SingleOrDefault()
- ?? throw new AmbiguousTableException<TChild>();
- return single;
- }
- public void Load()
- {
- Data = Client.Query(GetQueryDef());
- }
- public Filter<T>? GetFilter()
- {
- var relation = Model._relationships.Where(x => x.ChildTable == Name).FirstOrDefault();
- if(relation != null && Model._tables[relation.ParentTable] is IDataModelLoadTable loadTable && loadTable.Type is Type parentType)
- {
- var subFilter = new Filter<T>(relation.ChildColumnAsPropertyName());
- subFilter.Operator = Operator.InQuery;
- subFilter.Value = SubQuery.Create(parentType,
- loadTable.GetFilter(),
- Column.Create(parentType, relation.ParentColumnAsPropertyName()));
- return Filters<T>.Combine(Filter, subFilter);
- }
- return Filter;
- }
- IFilter? IDataModelLoadTable.GetFilter() => GetFilter();
- public QueryDef<T> GetQueryDef()
- {
- return new QueryDef<T>(GetFilter(), Columns, LookupFactory.DefineSort<T>());
- }
- IQueryDef IDataModelLoadTable.GetQueryDef() => GetQueryDef();
- public void LoadData(CoreTable data)
- {
- Data = data;
- }
- }
- #region New Load Methods
- public virtual void LoadModel(IEnumerable<string>? requiredTables)
- {
- var requiredTablesList = requiredTables != null ? requiredTables.ToList() : new List<string>();
- CheckRequiredTables(requiredTablesList);
- var args = new CancelEventArgs();
- OnBeforeLoad?.Invoke(args);
- if (!args.Cancel) BeforeLoad(requiredTablesList);
- var queries = new Dictionary<string, IQueryDef>();
- foreach (var (key, table) in _tables)
- {
- if(requiredTables is null || requiredTablesList.Contains(key))
- {
- if(table is IDataModelLoadTable loadTable)
- {
- queries[key] = loadTable.GetQueryDef();
- }
- else
- {
- table.Load();
- }
- }
- }
- var results = Client.QueryMultiple(queries);
- foreach (var (key, data) in results)
- if (_tables.TryGetValue(key, out var table) && table is IDataModelLoadTable loadTable)
- loadTable.LoadData(data);
- else
- Logger.Send(LogType.Error, "", $"QueryMultiple returned table with key {key}, which is not in the data model!");
- args = new CancelEventArgs();
- OnAfterLoad?.Invoke(args);
- if (!args.Cancel) AfterLoad(requiredTablesList);
- }
- public void LoadModel()
- {
- LoadModel(DefaultTableNames);
- }
- #endregion
- #region Non-Generic Stuff
- public bool IsChildTable(string tableName)
- {
- return _relationships.Any(x => x.ChildTable == tableName);
- }
- public static string TableName(string alias) => alias;
- public static string TableName(Type? type, string? alias = null)
- {
- return string.IsNullOrWhiteSpace(alias) ? (type ?? throw new Exception("No type or alias given!")).EntityName().Split('.').Last() : alias;
- }
- private void CheckTable(Type? type, string? alias = null)
- {
- var name = TableName(type, alias);
- if (!_tables.ContainsKey(name))
- throw new TableDoesNotExistException(name);
- }
- public void AddTable(Type? type, CoreTable table, bool isdefault = false, string? alias = null)
- {
- var name = TableName(type, alias);
- if (!_tables.ContainsKey(name))
- _tables[name] = new DataModelTable(type, table, isdefault, null, null, false);
- else
- throw new Exception(string.Format("[{0}] already exists in this data model!", name));
- }
- public void SetTableData(Type type, CoreTable tableData, string? alias = null)
- {
- var name = TableName(type, alias);
- if (_tables.TryGetValue(name, out var table))
- {
- table.Table = tableData;
- table.ShouldLoad = false;
- }
- else
- {
- throw new Exception(string.Format("[{0}] does not exist in this data model!", name));
- }
- }
- public void LinkTable(Type parenttype, string parentcolumn, string childalias, string childcolumn, string? parentalias = null, bool isLookup = false) =>
- LinkTable(parenttype, parentcolumn, null, childcolumn, parentalias, childalias, isLookup);
- public void LinkTable(Type parenttype, string parentcolumn, Type? childtype, string childcolumn, string? parentalias = null,
- string? childalias = null, bool isLookup = false)
- {
- CheckTable(parenttype, parentalias);
- CheckTable(childtype, childalias);
- var relationship = new DataModelRelationship(
- TableName(parenttype, parentalias),
- parentcolumn,
- TableName(childtype, childalias),
- childcolumn,
- isLookup
- );
- if (!_relationships.Any(x => x.ParentTable.Equals(relationship.ParentTable) && x.ChildTable.Equals(relationship.ChildTable)))
- _relationships.Add(relationship);
- }
- public bool HasTable(Type type, string? alias = null)
- {
- var name = TableName(type, alias);
- return _tables.ContainsKey(name);
- }
- public bool HasTable<T>(string? alias = null) => HasTable(typeof(T), alias);
- #endregion
- #region Adding & Linking Tables
- public void AddTable(string alias, CoreTable table, bool isdefault = false) => AddTable(null, table, isdefault, alias);
- public void AddTable<TType>(Filter<TType>? filter, Columns<TType>? columns, bool isdefault = false, string? alias = null, bool shouldLoad = true)
- {
- var name = TableName<TType>(alias);
- if (!_tables.ContainsKey(name))
- {
- var table = new CoreTable();
- if(columns != null)
- {
- table.LoadColumns(columns);
- }
- else
- {
- table.LoadColumns(typeof(TType));
- }
- _tables[name] = new DataModelTable(typeof(TType), table, isdefault, filter, columns, shouldLoad);
- }
- }
- public void AddChildTable<TParent, TChild>(Expression<Func<TParent, object>> parentcolumn, Expression<Func<TChild, object>> childcolumn,
- Filter<TChild>? filter = null, Columns<TChild>? columns = null, bool isdefault = false, string? parentalias = null, string? childalias = null)
- {
- CheckTable<TParent>(parentalias);
- AddTable(filter, columns, isdefault, childalias);
- LinkTable(parentcolumn, childcolumn, parentalias, childalias, false);
- }
- public void AddLookupTable<TSource, TLookup>(Expression<Func<TSource, object>> sourcecolumn, Expression<Func<TLookup, object>> lookupcolumn,
- Filter<TLookup>? filter = null, Columns<TLookup>? columns = null, bool isdefault = false,
- string? sourcealias = null, string? lookupalias = null)
- {
- CheckTable<TSource>(sourcealias);
- AddTable(filter, columns, isdefault, lookupalias);
- LinkTable(sourcecolumn, lookupcolumn, sourcealias, lookupalias, true);
- }
- public CoreTable GetTable<TType>(string? alias = null)
- {
- CheckTable<TType>(alias);
- var name = TableName<TType>(alias);
- return _tables[name].Table;
- }
- public DataModelTable GetDataModelTable(string name)
- {
- return _tables[name];
- }
- public DataModelTable GetDataModelTable<TType>(string? alias = null)
- {
- CheckTable<TType>(alias);
- var name = TableName<TType>(alias);
- return _tables[name];
- }
- protected bool IsRequired<TType>(IEnumerable<string> requiredtables, string? alias = null)
- {
- var name = TableName<TType>(alias);
- return requiredtables == null || requiredtables.Contains(name);
- }
- protected bool IsRequired(Type type, IEnumerable<string> requiredtables, string? alias = null)
- {
- var name = TableName(type, alias);
- return requiredtables == null || requiredtables.Contains(name);
- }
- public void LinkTable<TParent, TChild>(Expression<Func<TParent, object>> parent, Expression<Func<TChild, object>> child,
- string? parentalias = null, string? childalias = null, bool isLookup = false)
- {
- CheckTable<TParent>(parentalias);
- CheckTable<TChild>(childalias);
- var relationship = new DataModelRelationship<TParent, TChild>(parentalias, parent, childalias, child, isLookup);
- if (!_relationships.Any(x => x.ParentTable.Equals(relationship.ParentTable) && x.ChildTable.Equals(relationship.ChildTable)))
- _relationships.Add(relationship);
- //SetupIDs<TParent>(relationship.ParentColumn);
- }
- #endregion
- #region Getting/Setting Table Data
- public Filter<TType>? GetFilter<TType>(string? alias = null)
- {
- var table = GetDataModelTable<TType>(alias);
- return table.Filter as Filter<TType>;
- }
- public Columns<TType>? GetColumns<TType>(string? alias = null)
- {
- var table = GetDataModelTable<TType>(alias);
- return table.Columns as Columns<TType>;
- }
- public IColumns? GetColumns(string alias)
- {
- var table = GetDataModelTable(alias);
- return table.Columns;
- }
- [Obsolete("Use SetColumns instead")]
- public void SetTableColumns<TType>(Columns<TType> columns, string? alias = null) => SetColumns(columns, alias);
- public void SetFilter<TType>(Filter<TType>? filter, string? alias = null)
- {
- var table = GetDataModelTable<TType>(alias);
- table.Filter = filter;
- }
- public void SetColumns<TType>(Columns<TType>? columns, string? alias = null)
- {
- var table = GetDataModelTable<TType>(alias);
- table.Columns = columns;
- }
- public void SetIsDefault<TType>(bool isDefault, string? alias = null)
- {
- var table = GetDataModelTable<TType>(alias);
- table.IsDefault = isDefault;
- }
- public void SetShouldLoad<TType>(bool shouldLoad, string? alias = null)
- {
- var table = GetDataModelTable<TType>(alias);
- table.ShouldLoad = shouldLoad;
- }
- #endregion
- #region Removing Tables
- private bool RemoveTable(Type type, string? alias = null)
- {
- var name = TableName(type, alias);
- return _tables.Remove(name);
- }
- public bool RemoveTable<TType>(string? alias = null) => RemoveTable(typeof(TType), alias);
- #endregion
- }
- public abstract class DataModel<T> : DataModel, IDataModel<T>
- where T : Entity, IRemotable, IPersistent, new()
- {
- public DataModel(Filter<T>? filter, Columns<T>? columns = null, SortOrder<T>? sort = null)
- {
- Filter = filter;
- Columns = columns;
- Sort = sort;
- AddTable(filter, columns, true);
- AddChildTable<T, AuditTrail>(x => x.ID, x => x.EntityID);
- }
- public Filter<T>? Filter { get; set; }
- public Columns<T>? Columns { get; set; }
- public SortOrder<T>? Sort { get; set; }
- }
- public class DataModelRelationship : IDataModelRelationship
- {
- public DataModelRelationship(string parenttable, string parentcolumn, string childtable, string childcolumn, bool isLookup = false)
- {
- ParentTable = parenttable;
- ParentColumn = parentcolumn;
- ChildTable = childtable;
- ChildColumn = childcolumn;
- IsLookup = isLookup;
- }
- public string ChildColumn { get; }
- public string ChildTable { get; }
- public string ParentColumn { get; }
- public string ParentTable { get; }
- public bool IsLookup { get; }
- public DataRelation AsDataRelation()
- {
- string parentTable, parentColumn, childTable, childColumn;
- // Reverse the relationships if it is a lookup
- if (IsLookup)
- {
- parentTable = ChildTable;
- parentColumn = ChildColumn;
- childTable = ParentTable;
- childColumn = ParentColumn;
- }
- else
- {
- parentTable = ParentTable;
- parentColumn = ParentColumn;
- childTable = ChildTable;
- childColumn = ChildColumn;
- }
- var result = new DataRelation(
- string.Format("{0}_{1}_{2}_{3}", parentTable, parentColumn, childTable, childColumn),
- parentTable,
- childTable,
- new[] { parentColumn },
- new[] { childColumn },
- false
- );
- result.RelationName = string.Format("{0}_{1}_{2}_{3}", parentTable, parentColumn, childTable, childColumn);
- return result;
- }
- public string ParentColumnAsPropertyName()
- {
- return ParentColumn;
- }
- public string ChildColumnAsPropertyName()
- {
- return ChildColumn;
- }
- }
- public class DataModelRelationship<TParent, TChild> : IDataModelRelationship
- {
- public DataModelRelationship(string? parentalias, Expression<Func<TParent, object>> parent, string? childalias,
- Expression<Func<TChild, object>> child, bool isLookup)
- {
- ParentTable = string.IsNullOrWhiteSpace(parentalias) ? typeof(TParent).EntityName().Split('.').Last() : parentalias;
- Parent = parent;
- ChildTable = string.IsNullOrWhiteSpace(childalias) ? typeof(TChild).EntityName().Split('.').Last() : childalias;
- Child = child;
- IsLookup = isLookup;
- }
- public Expression<Func<TChild, object>> Child { get; }
- public Expression<Func<TParent, object>> Parent { get; }
- public string ChildTable { get; }
- public string ChildColumn => CoreUtils.GetFullPropertyName(Child, ".").Replace('.', '_');
- public string ParentTable { get; }
- public string ParentColumn => CoreUtils.GetFullPropertyName(Parent, ".").Replace('.', '_');
- public bool IsLookup { get; }
- public string ParentColumnAsPropertyName()
- {
- return CoreUtils.GetFullPropertyName(Parent, ".");
- }
- public string ChildColumnAsPropertyName()
- {
- return CoreUtils.GetFullPropertyName(Child, ".");
- }
- public DataRelation AsDataRelation()
- {
- string parentTable, parentColumn, childTable, childColumn;
- // Reverse the relationships if it is a lookup
- if (IsLookup)
- {
- parentTable = ChildTable;
- parentColumn = ChildColumn;
- childTable = ParentTable;
- childColumn = ParentColumn;
- }
- else
- {
- parentTable = ParentTable;
- parentColumn = ParentColumn;
- childTable = ChildTable;
- childColumn = ChildColumn;
- }
- var result = new DataRelation(
- string.Format("{0}_{1}_{2}_{3}", parentTable, parentColumn, childTable, childColumn),
- parentTable,
- childTable,
- new[] { parentColumn },
- new[] { childColumn },
- false
- );
- result.RelationName = string.Format("{0}_{1}_{2}_{3}", parentTable, parentColumn, childTable, childColumn);
- return result;
- }
- }
- }
|