From d13e6fcdadbddef4422008a53497cd81f99b3fb0 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Wed, 11 Feb 2026 08:59:10 -0500 Subject: [PATCH] improvements to ISynchronizable --- .../Infrastructure/Jobs/SynchronizationJob.cs | 44 ++++++------- .../HtmlText/Manager/HtmlTextManager.cs | 61 +++++++++++-------- .../HtmlText/Repository/HtmlTextRepository.cs | 14 +---- Oqtane.Server/Modules/ISynchronizable.cs | 5 +- 4 files changed, 59 insertions(+), 65 deletions(-) diff --git a/Oqtane.Server/Infrastructure/Jobs/SynchronizationJob.cs b/Oqtane.Server/Infrastructure/Jobs/SynchronizationJob.cs index 3d079633..32ac379c 100644 --- a/Oqtane.Server/Infrastructure/Jobs/SynchronizationJob.cs +++ b/Oqtane.Server/Infrastructure/Jobs/SynchronizationJob.cs @@ -16,9 +16,8 @@ namespace Oqtane.Infrastructure // JobType = "Oqtane.Infrastructure.SynchronizationJob, Oqtane.Server" // synchronization only supports sites in the same tenant (database) - // module title is used as a key to identify module instances on a page (ie. using "-" as a module title is problematic ie. content as configuration) - // relies on Module.ModifiedOn to be set if the module content changes (for efficiency) - // modules must implement ISynchronizable interface (new interface as IPortable was generally only implemented in an additive manner) + // module title is used as a key to identify module instances on a page + // modules must implement ISynchronizable interface // define settings that should not be synchronized (should be extensible in the future) List excludedSettings = new List() { @@ -580,8 +579,6 @@ namespace Oqtane.Infrastructure secondaryPageModule.Module.AllPages = false; secondaryPageModule.Module.IsDeleted = false; - var updateContent = false; - if (pageModule == null) { // check if module exists @@ -592,7 +589,6 @@ namespace Oqtane.Infrastructure if (siteGroupMember.SiteGroup.Type == SiteGroupTypes.Synchronization) { module = moduleRepository.AddModule(secondaryPageModule.Module); - updateContent = true; } log += Log(siteGroupMember, $"Module Added: {secondaryPageModule.Title} - {CreateLink(siteGroupMember.AliasName + "/" + secondaryPage.Path)}"); } @@ -617,7 +613,6 @@ namespace Oqtane.Infrastructure { moduleRepository.UpdateModule(secondaryPageModule.Module); } - updateContent = true; log += Log(siteGroupMember, $"Module Updated: {secondaryPageModule.Title} - {CreateLink(siteGroupMember.AliasName + "/" + secondaryPage.Path)}"); } if (primaryPageModule.ModifiedOn > siteGroupMember.SynchronizedOn) @@ -629,31 +624,30 @@ namespace Oqtane.Infrastructure log += Log(siteGroupMember, $"Module Instance Updated: {secondaryPageModule.Title} - {CreateLink(siteGroupMember.AliasName + "/" + secondaryPage.Path)}"); } } + } - // module content - if (updateContent && primaryPageModule.Module.ModuleDefinition.ServerManagerType != "") + // module content + if (primaryPageModule.Module.ModuleDefinition.ServerManagerType != "") + { + Type moduleType = Type.GetType(primaryPageModule.Module.ModuleDefinition.ServerManagerType); + if (moduleType != null && moduleType.GetInterface(nameof(ISynchronizable)) != null) { - Type moduleType = Type.GetType(primaryPageModule.Module.ModuleDefinition.ServerManagerType); - if (moduleType != null && moduleType.GetInterface(nameof(ISynchronizable)) != null) + try { - try + var moduleObject = ActivatorUtilities.CreateInstance(provider, moduleType); + var moduleContent = ((ISynchronizable)moduleObject).ExtractModule(primaryPageModule.Module, siteGroupMember.SynchronizedOn.Value); + if (!string.IsNullOrEmpty(moduleContent)) { - var moduleObject = ActivatorUtilities.CreateInstance(provider, moduleType); - var primaryModuleContent = ((ISynchronizable)moduleObject).ExtractModule(primaryPageModule.Module); - var secondaryModuleContent = ((ISynchronizable)moduleObject).ExtractModule(secondaryPageModule.Module); - if (primaryModuleContent != secondaryModuleContent) + if (siteGroupMember.SiteGroup.Type == SiteGroupTypes.Synchronization) { - if (siteGroupMember.SiteGroup.Type == SiteGroupTypes.Synchronization) - { - ((ISynchronizable)moduleObject).LoadModule(secondaryPageModule.Module, primaryModuleContent, primaryPageModule.Module.ModuleDefinition.Version); - } - log += Log(siteGroupMember, $"Module Content Updated: {secondaryPageModule.Title} - {CreateLink(siteGroupMember.AliasName + "/" + secondaryPage.Path)}"); + ((ISynchronizable)moduleObject).LoadModule(secondaryPageModule.Module, moduleContent); } + log += Log(siteGroupMember, $"Module Content Updated: {secondaryPageModule.Title} - {CreateLink(siteGroupMember.AliasName + "/" + secondaryPage.Path)}"); } - catch - { - // error exporting/importing - } + } + catch + { + // error exporting/importing } } } diff --git a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs index da556510..93647fe7 100644 --- a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs +++ b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs @@ -59,39 +59,50 @@ namespace Oqtane.Modules.HtmlText.Manager // IPortable implementation public string ExportModule(Module module) - { - return GetModuleContent(module); - } - - public void ImportModule(Module module, string content, string version) - { - SaveModuleContent(module, content, version); - } - - // ISynchronizable implementation - public string ExtractModule(Module module) - { - return GetModuleContent(module); - } - - public void LoadModule(Module module, string content, string version) - { - SaveModuleContent(module, content, version); - } - - private string GetModuleContent(Module module) { string content = ""; - var htmltexts = _htmlText.GetHtmlTexts(module.ModuleId); - if (htmltexts != null && htmltexts.Any()) + var htmltext = GetModuleContent(module.ModuleId); + if (htmltext != null) { - var htmltext = htmltexts.OrderByDescending(item => item.CreatedOn).First(); content = WebUtility.HtmlEncode(htmltext.Content); } return content; } - private void SaveModuleContent(Module module, string content, string version) + public void ImportModule(Module module, string content, string version) + { + SaveModuleContent(module, content); + } + + // ISynchronizable implementation + public string ExtractModule(Module module, DateTime lastSynchronizedOn) + { + string content = ""; + var htmltext = GetModuleContent(module.ModuleId); + if (htmltext != null && htmltext.CreatedOn > lastSynchronizedOn) + { + content = WebUtility.HtmlEncode(htmltext.Content); + } + return content; + } + + public void LoadModule(Module module, string content) + { + SaveModuleContent(module, content); + } + + private Models.HtmlText GetModuleContent(int moduleId) + { + // get the most recent htmltext record for the module + var htmltexts = _htmlText.GetHtmlTexts(moduleId); + if (htmltexts != null && htmltexts.Any()) + { + return htmltexts.OrderByDescending(item => item.CreatedOn).First(); + } + return null; + } + + private void SaveModuleContent(Module module, string content) { content = WebUtility.HtmlDecode(content); var htmlText = new Models.HtmlText(); diff --git a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs index 4c320455..53688d4c 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs @@ -2,7 +2,6 @@ using System.Linq; using Oqtane.Documentation; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; -using Oqtane.Repository; namespace Oqtane.Modules.HtmlText.Repository { @@ -19,12 +18,10 @@ namespace Oqtane.Modules.HtmlText.Repository public class HtmlTextRepository : IHtmlTextRepository, ITransientService { private readonly IDbContextFactory _factory; - private readonly IModuleRepository _moduleRepository; - public HtmlTextRepository(IDbContextFactory factory, IModuleRepository moduleRepository) + public HtmlTextRepository(IDbContextFactory factory) { _factory = factory; - _moduleRepository = moduleRepository; } public IEnumerable GetHtmlTexts(int moduleId) @@ -44,11 +41,6 @@ namespace Oqtane.Modules.HtmlText.Repository using var db = _factory.CreateDbContext(); db.HtmlText.Add(htmlText); db.SaveChanges(); - - // update module ModifiedOn date - var module = _moduleRepository.GetModule(htmlText.ModuleId); - _moduleRepository.UpdateModule(module); - return htmlText; } @@ -60,10 +52,6 @@ namespace Oqtane.Modules.HtmlText.Repository { db.HtmlText.Remove(htmlText); db.SaveChanges(); - - // update module ModifiedOn date - var module = _moduleRepository.GetModule(htmlText.ModuleId); - _moduleRepository.UpdateModule(module); } } } diff --git a/Oqtane.Server/Modules/ISynchronizable.cs b/Oqtane.Server/Modules/ISynchronizable.cs index 7dce3284..456d992f 100644 --- a/Oqtane.Server/Modules/ISynchronizable.cs +++ b/Oqtane.Server/Modules/ISynchronizable.cs @@ -1,3 +1,4 @@ +using System; using Oqtane.Models; namespace Oqtane.Modules @@ -6,8 +7,8 @@ namespace Oqtane.Modules { // You Must Set The "ServerManagerType" In Your IModule Interface - string ExtractModule(Module module); + string ExtractModule(Module module, DateTime lastSynchronizedOn); - void LoadModule(Module module, string content, string version); + void LoadModule(Module module, string content); } }