233 lines
9.3 KiB
C#
233 lines
9.3 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Email;
|
|
using Sarand.Net.Standard.Base.IO;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Sarand.Net.Standard.Dbas.Base;
|
|
using Saxonite.Net.Core.Web.Admin.Consoles;
|
|
using Saxonite.Net.Core.Web.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.Helpers;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Saxonite.Net.Standard.Base.Consoles;
|
|
using Saxonite.Net.Standard.Base.Helpers;
|
|
using Saxonite.Net.Standard.Base.Mgr;
|
|
using Saxonite.Net.Standard.Dbas.App;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Admin.App
|
|
{
|
|
//=======================================================================
|
|
public class SaxApplication_Admin : SaxApplication_Web
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private bool generateDatabaseDefinitions = false;
|
|
private bool populateRegions = false;
|
|
private Dictionary<String, SaxSessionConfig> sessionConfigs = new Dictionary<string, SaxSessionConfig>();
|
|
private Dictionary<String, SaxEmailConfig> emailConfigs = new Dictionary<string, SaxEmailConfig>();
|
|
// Session contexts.
|
|
private class SessionContextData
|
|
{
|
|
public SessionContextData(SaxSessionContext_Web sessionContext)
|
|
{
|
|
SessionContext = sessionContext;
|
|
}
|
|
public SaxSessionContext_Web SessionContext { get; }
|
|
public ISacsDatabase CoreDatabase { get; set; }
|
|
}
|
|
private readonly Dictionary<int, SessionContextData> sessionContexts = new Dictionary<int, SessionContextData>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaxApplication_Admin(IConfiguration configuration)
|
|
{
|
|
InitialiseTemporaryLoggingFolder();
|
|
try
|
|
{
|
|
AppInstance = this;
|
|
SaxGlobals.App = this;
|
|
SaxGlobals.System = (System_Web = new SaxSystem_Web());
|
|
Configuration = configuration;
|
|
ConsoleHelper_Web = new SaxConsoleHelper_WebAdmin(this);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Error(GetType().Name + ".CTR " + ExceptionUtil.GetErrorStateMessage(exc));
|
|
throw exc;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public static SaxApplication_Admin AppInstance { get; private set; }
|
|
public override ISacsDatabase GetCoreDatabase(SaxSessionContext sessionContext)
|
|
{
|
|
return sessionContexts.GetValue(sessionContext.Index, null)?.CoreDatabase;
|
|
}
|
|
public override String SessionCookieName { get { return Sax.Config.Admin.Session.Cookie.Name; } }
|
|
// Configuration.
|
|
public override SaxSessionConfig SessionConfig { get { return sessionConfigs.Values.ToList().getConfigForApplication(); } }
|
|
public override SaxEmailConfig EmailConfig { get { return emailConfigs.Values.ToList().getConfigForSession(SessionConfig); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Initialisation.
|
|
/// <summary>Initialise environment - from Startup.</summary>
|
|
internal void InitWebHostEnvironment(IWebHostEnvironment env)
|
|
{
|
|
InitEnvironment(env);
|
|
}
|
|
/// <summary>Initialise environment - from internal (above) or unit tests.</summary>
|
|
protected void InitEnvironment(IWebHostEnvironment env)
|
|
{
|
|
String fn = GetType().Name + ".InitEnvironment";
|
|
Development = env.IsDevelopment();
|
|
environmentName = env.EnvironmentName;
|
|
SaLogger.LogFilenamePrefix = "Sax-Web-Adm";
|
|
SaLogger.IncludeHostInFileMessage = false;
|
|
SaLogger.IncludeUserInFileMessage = false;
|
|
|
|
try
|
|
{
|
|
// Load server configuration.
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(env.ContentRootPath)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false)
|
|
.AddJsonFile($"appsettings.machine.{SaxSystem.ComputerName}.json", optional: false, reloadOnChange: false)
|
|
.AddEnvironmentVariables();
|
|
SaxConfiguration = builder.Build();
|
|
|
|
// Application configuration.
|
|
sessionConfigs = SaxSessionConfigHelper.ExtractSessionConfigs(SaxConfiguration.GetSection("SessionConfigs"));
|
|
emailConfigs = SaxEmailConfigHelper.ExtractEmailConfigs(SaxConfiguration.GetSection("EmailConfigs"));
|
|
LoggingRootFolder = SessionConfig.Logging.RootFolder;
|
|
|
|
// Application instance.
|
|
if (SessionConfig.Instance.EqualsCI("Dev")) instanceType = Sax.InstanceType.Dev;
|
|
else if (SessionConfig.Instance.ContainsCI("Q")) instanceType = Sax.InstanceType.QC;
|
|
else if (SessionConfig.Instance.ContainsCI("UAT")) instanceType = Sax.InstanceType.UAT;
|
|
else if (SessionConfig.Instance.EqualsCI("PreProd")) instanceType = Sax.InstanceType.UAT;
|
|
|
|
// Initialise hard-coded logging path.
|
|
if (StringUtil.IsEmpty(LoggingRootFolder))
|
|
{
|
|
if (SaxSystem.ComputerName.EqualsCI("WIN-BPCLM1QUOMT"))
|
|
LoggingRootFolder = @"D:\Saxonite\Logs\" + (Development ? "Dev" : "Test");
|
|
}
|
|
if (DirectoryUtil.DirectoryExists(LoggingRootFolder))
|
|
SaLogger.LogFolder = LoggingRootFolder;
|
|
LogApplicationStart(fn);
|
|
|
|
// Configure database connection.
|
|
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig();
|
|
config.location = SessionConfig.CoreDatabase.Location;
|
|
config.user = SessionConfig.CoreDatabase.User;
|
|
config.pword = SessionConfig.CoreDatabase.Password;
|
|
SacsDatabase coreDatabase = SacsDatabaseConsole.OpenOdbcDatabase(config);
|
|
if (coreDatabase.HasErrorState())
|
|
SaLogger.Warn(fn + " Database initialisation error: " + coreDatabase.ErrorStateMessage);
|
|
|
|
// Session context - only one context available/necessary (for now).
|
|
SessionContextData sessionContextData = new SessionContextData(new SaxSessionContext_Web(100, Sax.Application.Admin));
|
|
sessionContextData.CoreDatabase = coreDatabase;
|
|
sessionContexts.Add(sessionContextData.SessionContext.Index, sessionContextData);
|
|
SessionContext = sessionContextData.SessionContext;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Error(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
|
|
// Occasional startup processing.
|
|
if (generateDatabaseDefinitions)
|
|
GenerateDatabaseDefinition(SessionContext);
|
|
if (populateRegions)
|
|
populateRegionsFromDataFile(SessionContext);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
private void GenerateDatabaseDefinition(SaxSessionContext sessionContext)
|
|
{
|
|
if (!generateDatabaseDefinitions || !SaxSystem.DebugMode || SessionContext == null) return;
|
|
if (!StringUtil.EmptyOnNull(EnvironmentName).EqualsCI("Development")) return;
|
|
ISacsDatabase database = GetCoreDatabase(sessionContext);
|
|
if (database == null || StringUtil.IsEmpty(database.ConnectionString)) return;
|
|
|
|
// Generate database definitions.
|
|
SaxDbDfnGenerator.Generate(database.ConnectionString);
|
|
//SaUpdateLogDbDfnGenerator.Generate(database.ConnectionString);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
private void populateRegionsFromDataFile(SaxSessionContext sessionContext)
|
|
{
|
|
using (SaxInternals internals = new SaxInternals(sessionContext))
|
|
{
|
|
SaxReferenceMgr referenceMgr = internals.ReferenceMgr;
|
|
String regionsData = SaxResourceHelper.GetTextResource(SaxResourceHelper.TextResource.Data_CountriesWithRegions_Txt);
|
|
String[] regionDataValues = regionsData.Split("\n");
|
|
SysId parentRegionId = null;
|
|
for (int idx = 0; idx < regionDataValues.Length; idx++)
|
|
{
|
|
String regionData = regionDataValues[idx].ReplaceAll("\r", "");
|
|
if (StringUtil.IsEmpty(regionData)) continue;
|
|
if (regionData.Length > 3 && regionData[2] == ':')
|
|
{
|
|
String iso2Code = regionData.Substring(0, 2);
|
|
String name = regionData.Substring(3);
|
|
SaxRegion region = getRegionByName(referenceMgr, name);
|
|
if (region == null)
|
|
{
|
|
region = referenceMgr.CreateRegion();
|
|
region.RegionType = Sax.RegionType.Country;
|
|
region.CountryIso2Code = iso2Code;
|
|
region.Name = name;
|
|
region.RegionIdParent = parentRegionId;
|
|
if (!referenceMgr.InsertRegion(region, new DbUpdateInfo(new SysId(), "", "")))
|
|
{
|
|
SaLogger.Error("Failed to insert region - " + referenceMgr.ErrorStateMessage);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Sub continent.
|
|
SaxRegion region = getRegionByName(referenceMgr, regionData);
|
|
if (region == null)
|
|
{
|
|
region = referenceMgr.CreateRegion();
|
|
region.RegionType = Sax.RegionType.SubContinent;
|
|
region.Name = regionData;
|
|
if (!referenceMgr.InsertRegion(region, new DbUpdateInfo(new SysId(), "", "")))
|
|
{
|
|
SaLogger.Error("Failed to insert region - " + referenceMgr.ErrorStateMessage);
|
|
return;
|
|
}
|
|
}
|
|
parentRegionId = region.Id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private SaxRegion getRegionByName(SaxReferenceMgr referenceMgr, String name)
|
|
{
|
|
SaxRegion.Params prmRegion = new SaxRegion.Params();
|
|
prmRegion.NameFull = name;
|
|
return referenceMgr.GetRegion(prmRegion);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|