Browse Source

Added ability to add type-less tables to a datamodel

Kenric Nugteren 2 years ago
parent
commit
0641abec75

+ 2 - 3
prs.desktop/Dashboards/HumanResources/EmployeeQualificationDashboard.xaml.cs

@@ -139,8 +139,7 @@ namespace PRSDesktop
             Properties.Qualifications = Qualifications;
             Properties.Employees = Employees;
 
-            CoreTable = new();
-            CoreTable.TableName = "Qualifications";
+            CoreTable = new("Qualifications");
             ColumnHeaders = new();
 
             var employeeFilter = EmployeeFilter;
@@ -296,7 +295,7 @@ namespace PRSDesktop
 
         private void DoExport()
         {
-            var newTable = new CoreTable() { TableName = CoreTable.TableName };
+            var newTable = new CoreTable(CoreTable.TableName);
             newTable.LoadColumns(CoreTable.Columns);
             foreach (var row in CoreTable.Rows)
             {

+ 9 - 7
prs.desktop/Grids/DataModelTemplateGrid.cs

@@ -7,6 +7,7 @@ using InABox.Clients;
 using InABox.Core;
 using InABox.DynamicGrid;
 using InABox.WPF;
+using org.w3c.dom.html;
 using static InABox.Core.DataModelTemplate;
 using ScriptEditorWindow = InABox.DynamicGrid.ScriptEditorWindow;
 
@@ -57,19 +58,20 @@ namespace PRSDesktop
             var edt = new ScriptEditorWindow(toEdit as string, SyntaxLanguage.HTML);
 
             var snippets = new Dictionary<string, string[]>();
-            var tables = Model.AsDictionary;
-            foreach (var key in tables.Keys)
+            foreach (var (name, table) in Model.ModelTables)
             {
-                var name = key.Name;
                 var variable = name.Substring(0, 1).ToLower();
 
                 var cursnippets = new List<string>();
 
                 cursnippets.Add("{{ for " + variable + " in " + name + " }}\n\n{{ end }}");
-                var props = DatabaseSchema.Properties(key)
-                    .Where(x => x.Editor != null && x.Editor.GetType() != typeof(NullEditor) && x.Editor.Visible != Visible.Hidden);
-                foreach (var prop in props)
-                    cursnippets.Add("{{ " + variable + "." + prop.Name + " }}");
+                if(table.Type is not null)
+                {
+                    var props = DatabaseSchema.Properties(table.Type)
+                        .Where(x => x.Editor != null && x.Editor.GetType() != typeof(NullEditor) && x.Editor.Visible != Visible.Hidden);
+                    foreach (var prop in props)
+                        cursnippets.Add("{{ " + variable + "." + prop.Name + " }}");
+                }
                 snippets[name] = cursnippets.ToArray();
             }
             edt.Snippets = snippets;

+ 7 - 5
prs.desktop/Utils/DataModelUtils.cs

@@ -24,13 +24,15 @@ namespace PRSDesktop
             var so = new ScriptObject();
             ScriptObjectExtensions.Import(so, "string.to_base64", new Func<byte[], string>(b => Convert.ToBase64String(b)));
 
-            var tables = model.AsDictionary;
-            foreach (var key in tables.Keys)
+            foreach (var (key, table) in model.ModelTables)
             {
                 var objects = new List<object>();
-                foreach (var tablerow in tables[key].Rows)
-                    objects.Add(tablerow.ToObject(key));
-                so.Add(key.Name, objects);
+                if (table.Type is not null)
+                {
+                    foreach (var tablerow in table.Table.Rows)
+                        objects.Add(tablerow.ToObject(table.Type));
+                }
+                so.Add(key, objects);
             }
 
             templatecontext.PushGlobal(so);

+ 5 - 5
prs.desktop/Utils/EmailUtils.cs

@@ -200,17 +200,17 @@ namespace PRSDesktop
         private static string DetermineName(DataModel model)
         {
             string title = model.Name;
-            if (model.AsDictionary.ContainsKey(typeof(Requisition)))
+            if (model.HasTable<Requisition>())
             {
-                CoreTable table = model.AsDictionary[typeof(Requisition)];
+                CoreTable table = model.GetTable<Requisition>();
                 title = title + " - " + table.Rows.FirstOrDefault().Get<Requisition, string>(x => x.Title);
             }
-            else if (model.AsDictionary.ContainsKey(typeof(PurchaseOrder)))
+            else if (model.HasTable<PurchaseOrder>())
             {
                 title = "Purchase Order ";
-                CoreTable table = model.AsDictionary[typeof(PurchaseOrder)];
+                CoreTable table = model.GetTable<PurchaseOrder>();
                 if (table.Rows.Count == 1)
-                    title = title + table.Rows.FirstOrDefault().Get<PurchaseOrder, string>(x => x.PONumber);
+                    title += table.Rows.FirstOrDefault().Get<PurchaseOrder, string>(x => x.PONumber);
                 else if (table.Rows.Count > 1)
                 {
                     foreach (CoreRow row in table.Rows)

+ 15 - 0
prs.server/Engines/WebEngine/WebListener.cs

@@ -84,6 +84,10 @@ namespace PRSServer
         {
             _dataModel.AddTable(filter, columns, isdefault, alias, shouldLoad);
         }
+        public void AddTable(string alias, CoreTable table, bool isdefault = false)
+        {
+            _dataModel.AddTable(alias, table, isdefault);
+        }
 
         public void LinkTable(Type parenttype, string parentcolumn, Type childtype, string childcolumn, string? parentalias = null,
             string? childalias = null, bool isLookup = false)
@@ -97,6 +101,11 @@ namespace PRSServer
             _dataModel.LinkTable(parent, child, parentalias, childalias);
         }
 
+        public void LinkTable(Type parenttype, string parentcolumn, string childalias, string childcolumn, string? parentalias = null, bool isLookup = false)
+        {
+            _dataModel.LinkTable(parenttype, parentcolumn, childalias, childcolumn, parentalias, isLookup);
+        }
+
         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)
@@ -126,6 +135,11 @@ namespace PRSServer
             return _dataModel.HasTable(type, alias);
         }
 
+        public bool HasTable<TType>(string? alias = null)
+        {
+            return _dataModel.HasTable<TType>(alias);
+        }
+
         public void LoadModel(IEnumerable<string> requiredTables, Dictionary<string, IQueryDef>? requiredQueries = null)
         {
             _dataModel.LoadModel(requiredTables, requiredQueries);
@@ -156,6 +170,7 @@ namespace PRSServer
         public void SetIsDefault<TType>(bool isDefault, string? alias = null) => _dataModel.SetIsDefault<TType>(isDefault, alias);
 
         public void SetShouldLoad<TType>(bool shouldLoad, string? alias = null) => _dataModel.SetShouldLoad<TType>(shouldLoad, alias);
+
     }
 
     public class RazorReferenceResolver : IReferenceResolver