From 1c5e538b7dc8a75b8a666b1b8c30816c65637460 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 19 Sep 2025 18:59:09 -0400 Subject: [PATCH] support for and debugging custom Mochafile tenant adjustments --- mocha-common | 2 +- .../src/app/Mocha.Oms.Server/Program.cs | 192 +++++++++++++++++- .../Commands/AttributesCommand.cs | 4 + .../Commands/InstanceCommand.cs | 5 + .../src/lib/Mocha.Core/KnownInstanceGuids.cs | 1 + mocha-dotnet/src/lib/Mocha.Core/Oms.cs | 21 +- .../OmsImplementations/MemoryOms.cs | 13 +- .../src/lib/Mocha.Core/UI/JsonRenderer.cs | 4 + .../src/lib/Mocha.Core/UI/Widgets/Text.cs | 4 +- 9 files changed, 226 insertions(+), 20 deletions(-) diff --git a/mocha-common b/mocha-common index 4c5a4c3..75cdd15 160000 --- a/mocha-common +++ b/mocha-common @@ -1 +1 @@ -Subproject commit 4c5a4c336ea30705dc6992039383de9b4bbffba5 +Subproject commit 75cdd15b4c62cab8f4279303c4e522387c14e0b3 diff --git a/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs b/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs index f96d7d2..e92eb90 100644 --- a/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs +++ b/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs @@ -11,6 +11,7 @@ using Mocha.Core.UI.Server; using Mocha.Core.Logging; using Mocha.Core.Logging.Loggers; using Mocha.Core.Storage; +using System.Text; /// /// Provides the entry point for a simple application which starts a Web server @@ -132,7 +133,7 @@ public class Program : WebApplication for (int i = 0; i < requiredLibraries.Length; i++) { Log.WriteLine("loading prerequisite library '{0}'... ", requiredLibraries[i]); - if (!TryLoadLibrary(Oms, path, "net.alcetech.Mocha.System.mcl")) + if (!TryLoadLibrary(Oms, path, requiredLibraries[i])) { Log.WriteLine("[^b^1FAILED^0] loading prerequisite library '{0}'", requiredLibraries[i]); return; @@ -206,6 +207,15 @@ public class Program : WebApplication private bool LoadMochafile(string mochafilename) { + if (!System.IO.File.Exists(mochafilename)) + { + Log.WriteLine("[ ^1ERROR ^0] Mochafile not found: '{0}'", mochafilename); + return false; + } + + string curdir = System.Environment.CurrentDirectory; + System.Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(mochafilename); + Log.WriteLine("oms-dotnet: loading Mochafile at '{0}'", mochafilename); JsonObject? json = JsonNode.Parse(System.IO.File.ReadAllText(mochafilename)) as JsonObject; @@ -215,6 +225,13 @@ public class Program : WebApplication return false; } + List appLibraryRefs = new List(); + if (json.ContainsKey("application") && json["application"] is JsonObject objApp) + { + Log.WriteLine("loading application library references..."); + LoadLibraryRefs(objApp, appLibraryRefs); + } + if (json.ContainsKey("tenants")) { if (json["tenants"] is JsonArray aryTenants) @@ -232,7 +249,133 @@ public class Program : WebApplication } for (int i = 0; i < count; i++) { - CreateTenant2(name); + TenantHandle th = CreateTenant2(name); + Oms.SelectTenant(th); + + if (appLibraryRefs.Count > 0) + { + foreach (string l in appLibraryRefs) + { + LibraryHandle lh = Oms.LoadLibrary(l); + if (lh != LibraryHandle.Empty) + { + Oms.AddLibraryReference(lh); + } + } + } + + Log.WriteLine("loading tenant library references..."); + List tenantLibraryRefs = new List(); + LoadLibraryRefs(jo, tenantLibraryRefs); + if (tenantLibraryRefs.Count > 0) + { + foreach (string l in tenantLibraryRefs) + { + LibraryHandle lh = Oms.LoadLibrary(l); + if (lh != LibraryHandle.Empty) + { + Oms.AddLibraryReference(lh); + } + } + } + + Oms.SelectTenant(th); + if (jo.ContainsKey("attributeValues") && jo["attributeValues"] is JsonArray aryAttrs) + { + foreach (JsonNode? attval in aryAttrs) + { + if (attval is JsonObject objatt) + { + InstanceHandle ihSrc = InstanceHandle.Empty; + InstanceHandle ihAtt = InstanceHandle.Empty; + if (!objatt.ContainsKey("value")) + continue; + + JsonNode nvalue = objatt["value"]; + string srckey = "", attkey = ""; + if (objatt.ContainsKey("sourceInstance")) + { + srckey = objatt["sourceInstance"]?.GetValue() ?? ""; + if (!Oms.TryParseInstanceRef(srckey, out ihSrc)) + { + Log.WriteLine("assignAttribute failed - invalid source instance ref '{0}'", srckey); + continue; + } + } + if (objatt.ContainsKey("attributeInstance")) + { + attkey = objatt["attributeInstance"]?.GetValue() ?? ""; + if (!Oms.TryParseInstanceRef(attkey, out ihAtt)) + { + Log.WriteLine("assignAttribute failed - invalid attribute instance ref '{0}'", attkey); + continue; + } + } + + if (nvalue.GetValueKind() == System.Text.Json.JsonValueKind.String) + { + string val = nvalue.GetValue(); + Log.WriteLine("assignAttribute {0} - {1} : '{2}'", srckey, attkey, val); + Oms.SetAttributeValue(ihSrc, ihAtt, val); + } + else if (nvalue.GetValueKind() == System.Text.Json.JsonValueKind.Object) + { + string? val = ParseValueEx(nvalue as JsonObject); + if (val != null) + { + Log.WriteLine("assignAttribute {0} - {1} : '{2}'", srckey, attkey, val); + Oms.SetAttributeValue(ihSrc, ihAtt, val); + } + } + } + } + } + + if (jo.ContainsKey("themeOverrides") && jo["themeOverrides"] is JsonArray aryThemes) + { + StringBuilder sbStyleSheet = new StringBuilder(); + foreach (JsonNode? thm in aryThemes) + { + if (thm is JsonObject objthm) + { + if (!objthm.ContainsKey("type")) + continue; + if (!objthm.ContainsKey("filename")) + continue; + + string type = objthm["type"].GetValue(); + string filename = objthm["filename"].GetValue(); + + if (type.Equals("stylesheet")) + { + string content = ""; + try + { + content = System.IO.File.ReadAllText(filename); + } + catch (FileNotFoundException ex) + { + Log.WriteLine("[ ^1ERROR^0 ] file not found: '{0}'", System.IO.Path.Join(new string[] { System.Environment.CurrentDirectory, filename })); + continue; + } + sbStyleSheet.AppendLine(content); + } + } + } + + if (sbStyleSheet.Length > 0) + { + InstanceHandle a_Name = Oms.GetInstance(KnownAttributeGuids.Text.Name); + InstanceHandle a_ContentType = Oms.GetInstance(KnownAttributeGuids.Text.ContentType); + InstanceHandle a_Value = Oms.GetInstance(KnownAttributeGuids.Text.Value); + + InstanceHandle c_StyleSheet = Oms.GetInstance(KnownInstanceGuids.Classes.StyleSheet); + InstanceHandle i_StyleSheet = Oms.CreateInstanceOf(c_StyleSheet); + Oms.SetAttributeValue(i_StyleSheet, a_Name, "Mochafile Overrides"); + Oms.SetAttributeValue(i_StyleSheet, a_ContentType, "text/css"); + Oms.SetAttributeValue(i_StyleSheet, a_Value, sbStyleSheet.ToString()); + } + } } } } @@ -240,10 +383,50 @@ public class Program : WebApplication } Log.WriteLine("oms-dotnet: Mochafile processing complete"); + System.Environment.CurrentDirectory = curdir; return true; } - private void CreateTenant2(string name) + private string? ParseValueEx(JsonObject o) + { + if (o.ContainsKey("type")) + { + switch (o["type"].GetValue()) + { + case "base64": + { + byte[] value = new byte[0]; + if (o.ContainsKey("value")) + { + value = System.Text.Encoding.UTF8.GetBytes(o["value"].GetValue()); + } + else if (o.ContainsKey("filename")) + { + value = System.IO.File.ReadAllBytes(o["filename"].GetValue()); + } + return System.Convert.ToBase64String(value); + } + } + } + return null; + } + + private void LoadLibraryRefs(JsonObject toplevel, ICollection appLibraryRefs) + { + if (toplevel.ContainsKey("libraryReferences") && toplevel["libraryReferences"] is JsonArray aryRefs) + { + foreach (JsonObject o in aryRefs) + { + if (o.ContainsKey("path")) + { + Log.WriteLine("---- {0}", o["path"].GetValue()); + appLibraryRefs.Add(o["path"].GetValue()); + } + } + } + } + + private TenantHandle CreateTenant2(string name) { int ct = 0; TenantHandle th = Oms.GetTenantByName(name); @@ -272,8 +455,9 @@ public class Program : WebApplication Oms.SelectTenant(th); Oms.AddLibraryReference(Oms.GetLibrary("net.alcetech.Mocha.System")); Oms.AddLibraryReference(Oms.GetLibrary("net.alcetech.Mocha.Web")); - + Log.WriteLine("oms-dotnet: create tenant ok, handle is '{0}'", th); + return th; } private bool TryLoadLibrary(Oms oms, string path, string fileName) diff --git a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/AttributesCommand.cs b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/AttributesCommand.cs index 3427ed6..946c46b 100644 --- a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/AttributesCommand.cs +++ b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/AttributesCommand.cs @@ -18,6 +18,7 @@ using System.Text.Json.Nodes; using MBS.Web; using Mocha.Core; +using Mocha.Core.Logging; using Mocha.Core.UI; namespace Mocha.Core.UI.Server.Commands; @@ -28,6 +29,7 @@ public class AttributesCommand : InstanceCommand protected override void ProcessInternal(WebServerProcessRequestEventArgs e, Core.Oms oms, Core.OmsContext ctx, StreamWriter sw) { + Console.WriteLine("current tenant is {0}", oms.GetTenantName(oms.CurrentTenant)); if (e.Context.Request.PathParts.Length > 7) { // /tenants/{tenantName}/instances/{iid}/attrbutes/{attkey}/current @@ -53,6 +55,8 @@ public class AttributesCommand : InstanceCommand } value = oms.GetAttributeValue(ProcessingInstance, att, String.Empty, dt); + // Log.WriteLine("oms: get attribute value {0} . {1} / {2} : '{3}'", e.Context.Request.PathParts[2], e.Context.Request.PathParts[4], e.Context.Request.PathParts[6], value); + e.Context.Response.ResponseCode = 200; e.Context.Response.ResponseText = "OK"; e.Context.Response.ContentType = "text/plain"; diff --git a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs index 5f3d6e9..ba9e1e8 100644 --- a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs +++ b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs @@ -18,6 +18,7 @@ using System.Text.Json.Nodes; using MBS.Web; using Mocha.Core; +using Mocha.Core.Logging; namespace Mocha.Core.UI.Server.Commands; @@ -29,12 +30,16 @@ public abstract class InstanceCommand : TenantedCommand protected override bool BeforeProcessInternal(WebServerProcessRequestEventArgs e, Core.Oms oms, Core.OmsContext ctx, StreamWriter sw) { if (e.Context.Request.PathParts.Length < 5) + { + Log.WriteLine("oms-dotnet ERROR: invalid request for /tenants/???/instances/??? (path parts length {0})", e.Context.Request.PathParts.Length); return true; + } string tenantName = e.Context.Request.PathParts[2]; TenantHandle th = oms.GetTenantByName(tenantName); if (th != TenantHandle.Empty) { + Log.WriteLine("oms-dotnet: select tenant '{0}'", tenantName); oms.SelectTenant(th); } else diff --git a/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs b/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs index 566ab1f..f65604b 100755 --- a/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs @@ -145,6 +145,7 @@ namespace Mocha.Core public static Guid Style { get; } = new Guid("{A48C843A-B24B-4BC3-BE6F-E2D069229B0A}"); public static Guid StyleRule { get; } = new Guid("{C269A1F3-E014-4230-B78D-38EAF6EA8A81}"); public static Guid StyleClass { get; } = new Guid("{a725f089-7763-4887-af37-da52358c378c}"); + public static Guid StyleSheet { get; } = new Guid("{a727f9fa-0688-4203-9acf-7306b88c44fe}"); public static Guid Page { get; } = new Guid("{D9626359-48E3-4840-A089-CD8DA6731690}"); public static Guid ContainerPageComponent { get; } = new Guid("{6AD6BD1C-7D1C-4AC9-9642-FEBC61E9D6FF}"); diff --git a/mocha-dotnet/src/lib/Mocha.Core/Oms.cs b/mocha-dotnet/src/lib/Mocha.Core/Oms.cs index 511ec54..b65ec34 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/Oms.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/Oms.cs @@ -597,11 +597,16 @@ public abstract class Oms } InstanceHandle ir = CreateInstance(guid); - + + /* + // this is done by SetParentClass, below int ct = CountInstances(ir_class.GetHandle()); InstanceKey pclass_key = GetInstanceKey(ir_class.GetHandle()); - InstanceKey new_index = new InstanceKey(pclass_key.InstanceIndex, ct); + InstanceKey new_index = new InstanceKey(pclass_key.InstanceIndex, ct + 1); + + Log.WriteLine("oms: creating instance of class {0} with instance key {1}", pclass_key, new_index); SetInstanceKey(ir, new_index); + */ DateTime dt = DateTime.Now; if (!_InhibitStorage) @@ -669,7 +674,7 @@ public abstract class Oms // AssignRelationship(ir_parent, GetInstance(KnownRelationshipGuids.Class__has__Instance), ir); int ct = CountInstances(ir_parent); - SetInstanceKey(ir, new InstanceKey(GetInstanceKey(ir_parent).InstanceIndex, ct - 1)); + SetInstanceKey(ir, new InstanceKey(GetInstanceKey(ir_parent).InstanceIndex, ct /* - 1 */)); } public int CountInstances(InstanceHandle ir_parent) @@ -836,6 +841,8 @@ public abstract class Oms _derivedData[sh][attribute] = value; return; } + + Log.WriteLine("oms: setting attribute value {0} . {1} = '{2}'", sh, attribute, value); SetAttributeValueInternal(sh, attribute, value, dt); if (!_InhibitStorage) @@ -1784,12 +1791,12 @@ public abstract class Oms { if (_libraries.ContainsKey(name)) { - Log.WriteLine("[^b^2SUCCESS^0] LoadLibrary - using handle '{0}' for '{1}'", _libraries[name], name); + Log.WriteLine("[^b^2SUCCESS^0] GetLibrary - using handle '{0}' for '{1}'", _libraries[name], name); return _libraries[name]; } else { - Log.WriteLine("[^b^1FAILURE^0] LoadLibrary - not found library '{0}'", name); + Log.WriteLine("[^b^1FAILURE^0] GetLibrary - not found library '{0}'", name); Log.WriteLine("Available libraries are: "); foreach (KeyValuePair kvp in _libraries) { @@ -1893,7 +1900,9 @@ public abstract class Oms if (!System.IO.File.Exists(filename) && !Directory.Exists(filename)) { - throw new FileNotFoundException(null, filename); + Log.WriteLine("[^b^1FAILURE^0] LoadLibrary - file not found '{0}'", filename); + return LibraryHandle.Empty; + //throw new FileNotFoundException(null, filename); } // get a list of all library loaders we have implemented diff --git a/mocha-dotnet/src/lib/Mocha.Core/OmsImplementations/MemoryOms.cs b/mocha-dotnet/src/lib/Mocha.Core/OmsImplementations/MemoryOms.cs index 6f2ffa7..a979baf 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/OmsImplementations/MemoryOms.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/OmsImplementations/MemoryOms.cs @@ -272,17 +272,14 @@ public class MemoryOms : Oms { _Attributes[source] = new Dictionary>(); } - - List list; if (!_Attributes[source].ContainsKey(attribute)) { - list = new List(); - _Attributes[source][attribute] = list; - } - else - { - list = _Attributes[source][attribute]; + _Attributes[source][attribute] = new List(); } + + List list = _Attributes[source][attribute]; + + // Log.WriteLine("memory-oms: SAV {0} . {1} '{2}'", GetInstanceKey(source), GetInstanceKey(attribute), value); list.Add(new AttributeValue(effectiveDate, value)); } diff --git a/mocha-dotnet/src/lib/Mocha.Core/UI/JsonRenderer.cs b/mocha-dotnet/src/lib/Mocha.Core/UI/JsonRenderer.cs index 8fc0b37..df1bba8 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/UI/JsonRenderer.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/UI/JsonRenderer.cs @@ -16,6 +16,8 @@ // along with Mocha.NET. If not, see . using System.Text.Json.Nodes; +using Microsoft.VisualBasic; +using Mocha.Core.Logging; namespace Mocha.Core.UI; @@ -71,6 +73,8 @@ public class JsonRenderer : Renderer public static JsonArray GetAttributesAsJson(Core.Oms oms, InstanceHandle h, InstanceHandle hAtt = default(InstanceHandle)) { JsonArray a = new JsonArray(); + Log.WriteLine("GAAJ: CT = {0}", oms.GetTenantName(oms.CurrentTenant)); + IEnumerable atts = oms.GetAttributes(h); foreach (MochaAttribute att in atts) { diff --git a/mocha-dotnet/src/lib/Mocha.Core/UI/Widgets/Text.cs b/mocha-dotnet/src/lib/Mocha.Core/UI/Widgets/Text.cs index 4954600..074770b 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/UI/Widgets/Text.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/UI/Widgets/Text.cs @@ -17,6 +17,7 @@ using System.Text.Json.Nodes; +using Mocha.Core.Logging; namespace Mocha.Core.UI; @@ -45,11 +46,12 @@ public class Text : Widget { value = val.ToString(); } - + if (value == null) { // no value specified for the target instance, so get default value of the Text Attribute value = OMS.GetAttributeValue(ParentInstance, OMS.GetInstance(KnownAttributeGuids.Text.Value)); + Log.WriteLine("TextAttribute from parent instance {0}, value == '{1}' OMS = '{2}'", OMS.GetInstanceKey(ParentInstance), value, OMS.GetTenantName(OMS.CurrentTenant)); }