init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
356
Framework/Util/Configuration.cs
Normal file
356
Framework/Util/Configuration.cs
Normal file
|
@ -0,0 +1,356 @@
|
|||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using Kreta.Framework.Logging;
|
||||
using Kreta.Resources;
|
||||
|
||||
namespace Kreta.Framework
|
||||
{
|
||||
public enum SystemType
|
||||
{
|
||||
[Display(Name = nameof(EnumsResource.Ismeretlen), ResourceType = typeof(EnumsResource))]
|
||||
Ismeretlen = -1,
|
||||
|
||||
[Display(Name = nameof(EnumsResource.Teszt), ResourceType = typeof(EnumsResource))]
|
||||
Teszt = 0,
|
||||
|
||||
[Display(Name = nameof(EnumsResource.KlebelsbergKozpont), ResourceType = typeof(EnumsResource))]
|
||||
KK = 1,
|
||||
|
||||
[Display(Name = nameof(EnumsResource.NemzetiSzakkepzesiEsFelnottkepzesiHivatal), ResourceType = typeof(EnumsResource))]
|
||||
NSZFH = 2,
|
||||
|
||||
[Display(Name = nameof(EnumsResource.HermanOttoIntezet), ResourceType = typeof(EnumsResource))]
|
||||
HOI = 3,
|
||||
|
||||
[Display(Name = nameof(EnumsResource.Azure), ResourceType = typeof(EnumsResource))]
|
||||
AZURE = 4,
|
||||
|
||||
[Display(Name = nameof(EnumsResource.Azure), ResourceType = typeof(EnumsResource))]
|
||||
NSZFH_EMA = 5
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Néhány alaptulajdonság van felvéve, aztan majd lehet bővíteni.
|
||||
/// </summary>
|
||||
public class Configuration : ILogConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration constuctor
|
||||
/// </summary>
|
||||
/// <param name="configurationNode">the main "config" node</param>
|
||||
public Configuration(XmlNode configurationNode)
|
||||
{
|
||||
var cfg = configurationNode;
|
||||
try
|
||||
{
|
||||
ConfigDocument = cfg;
|
||||
// ReSharper disable once DoNotCallOverridableMethodsInConstructor
|
||||
ReadConfig(cfg);
|
||||
}
|
||||
catch (InvalidConfigurationException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new InvalidConfigurationException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
#region Beolvasás
|
||||
|
||||
/// <summary>
|
||||
/// Reads configuration string.
|
||||
/// </summary>
|
||||
/// <param name="confignode">Server config</param>
|
||||
/// <param name="xpath">XPath location of node</param>
|
||||
/// <returns>Configuration value</returns>
|
||||
/// <exception cref="InvalidConfigurationException">Invalid configuration</exception>
|
||||
protected static string ReadString(XmlNode confignode, string xpath)
|
||||
{
|
||||
XmlNode current;
|
||||
if ((current = confignode.SelectSingleNode(xpath)) != null)
|
||||
{
|
||||
return current.InnerText;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Missing configuration entry: {0}.", xpath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads configuration string.
|
||||
/// </summary>
|
||||
/// <param name="confignode">Server config</param>
|
||||
/// <param name="xpath">XPath location of node</param>
|
||||
/// <param name="defaultValue">The default value if missing from configuration</param>
|
||||
/// <returns>Configuration value</returns>
|
||||
/// <exception cref="InvalidConfigurationException">Invalid configuration</exception>
|
||||
protected static string ReadString(XmlNode confignode, string xpath, string defaultValue)
|
||||
{
|
||||
XmlNode current;
|
||||
return (current = confignode.SelectSingleNode(xpath)) != null ? current.InnerText : defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads configuration list.
|
||||
/// </summary>
|
||||
/// <param name="confignode">Server config</param>
|
||||
/// <param name="xpath">XPath location of node</param>
|
||||
/// <param name="listElementNode">Name of list element node</param>
|
||||
/// <returns>Configuration list</returns>
|
||||
protected static string[] ReadStrings(XmlNode confignode, string xpath, string listElementNode)
|
||||
{
|
||||
XmlNode current;
|
||||
if ((current = confignode.SelectSingleNode(xpath)) != null)
|
||||
{
|
||||
return (
|
||||
from XmlNode node in current.ChildNodes
|
||||
where node.Name.Equals(listElementNode, StringComparison.OrdinalIgnoreCase)
|
||||
select node.InnerText)
|
||||
.ToArray();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads configuration number.
|
||||
/// </summary>
|
||||
/// <param name="confignode">Server config</param>
|
||||
/// <param name="xpath">XPath location of node</param>
|
||||
/// <param name="defaultValue">The default value if missing from configuration, <c>null</c> value implies required node</param>
|
||||
/// <returns>Configuration value</returns>
|
||||
/// <exception cref="InvalidConfigurationException">
|
||||
/// Invalid configuration or <paramref name="defaultValue"/> is <c>null</c> and node is missing.
|
||||
/// </exception>
|
||||
protected static int ReadNumber(XmlNode confignode, string xpath, int? defaultValue = null)
|
||||
{
|
||||
XmlNode current;
|
||||
if ((current = confignode.SelectSingleNode(xpath)) != null)
|
||||
{
|
||||
if (int.TryParse(current.InnerText, out int innerNumber))
|
||||
{
|
||||
return innerNumber;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Invalid configuration entry: {0}.", xpath));
|
||||
}
|
||||
if (defaultValue != null)
|
||||
{
|
||||
return (int)defaultValue;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Missing configuration entry: {0}.", xpath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads configuration switch.
|
||||
/// </summary>
|
||||
/// <param name="confignode">Server config</param>
|
||||
/// <param name="xpath">XPath location of node</param>
|
||||
/// <param name="defaultValue">The default value if missing from configuration, <c>null</c> value implies required node</param>
|
||||
/// <returns>Configuration value</returns>
|
||||
/// <exception cref="InvalidConfigurationException">
|
||||
/// Invalid configuration or <paramref name="defaultValue"/> is <c>null</c> and node is missing.
|
||||
/// </exception>
|
||||
protected static bool ReadBool(XmlNode confignode, string xpath, bool? defaultValue = null)
|
||||
{
|
||||
XmlNode current;
|
||||
if ((current = confignode.SelectSingleNode(xpath)) != null)
|
||||
{
|
||||
if (bool.TryParse(current.InnerText, out var innerBool))
|
||||
{
|
||||
return innerBool;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Invalid configuration entry: {0}.", xpath));
|
||||
}
|
||||
if (defaultValue != null)
|
||||
{
|
||||
return (bool)defaultValue;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Missing configuration entry: {0}.", xpath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads configuration enumeration.
|
||||
/// </summary>
|
||||
/// <param name="confignode">Server config</param>
|
||||
/// <param name="xpath">XPath location of node</param>
|
||||
/// <param name="defaultValue">The default value if missing from configuration, <c>null</c> value implies required node</param>
|
||||
/// <returns>Configuration value</returns>
|
||||
/// <exception cref="InvalidConfigurationException">
|
||||
/// Invalid configuration or <paramref name="defaultValue"/> is <c>null</c> and node is missing.
|
||||
/// </exception>
|
||||
protected static TEnum ReadEnum<TEnum>(XmlNode confignode, string xpath, TEnum? defaultValue = null) where TEnum : struct
|
||||
{
|
||||
XmlNode current;
|
||||
if ((current = confignode.SelectSingleNode(xpath)) != null)
|
||||
{
|
||||
if (Enum.TryParse(current.InnerText, true, out TEnum innerEnum))
|
||||
{
|
||||
return innerEnum;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Invalid configuration entry: {0}.", xpath));
|
||||
}
|
||||
if (defaultValue != null)
|
||||
{
|
||||
return (TEnum)defaultValue;
|
||||
}
|
||||
throw new InvalidConfigurationException(string.Format("Missing configuration entry: {0}.", xpath));
|
||||
}
|
||||
|
||||
protected virtual void ReadConfig(XmlNode confignode)
|
||||
{
|
||||
string xpath = "";
|
||||
try
|
||||
{
|
||||
// Kiszolgáló egyedi neve.
|
||||
xpath = @"/config/server/name";
|
||||
ServerName = ReadString(confignode, xpath);
|
||||
|
||||
// Az adatbázis-kapcsolatot leíró karakterlánc.
|
||||
xpath = @"/config/server/dbconnection";
|
||||
DBConnection = ReadString(confignode, xpath);
|
||||
|
||||
xpath = @"/config/server/globalapidbconnection";
|
||||
GlobalDbConnection = ReadString(confignode, xpath);
|
||||
|
||||
// A naplózás szintje.
|
||||
xpath = @"/config/server/loglevel";
|
||||
ServerLogLevel = ReadEnum<LogLevel>(confignode, xpath);
|
||||
#if !DEBUG
|
||||
// Release szerver legfeljebb INFO részletességű lehet.
|
||||
if (ServerLogLevel < LogLevel.INFO)
|
||||
{
|
||||
ServerLogLevel = LogLevel.INFO;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Munkamenet elévülési ideje.
|
||||
xpath = @"/config/server/sessiontimeout";
|
||||
SessionTimeout = ReadNumber(confignode, xpath);
|
||||
|
||||
// Alapértelmezett nyelv.
|
||||
xpath = @"/config/server/lcid";
|
||||
LCID = ReadNumber(confignode, xpath, 1038);
|
||||
|
||||
xpath = @"/config/server/logdbconnection";
|
||||
_logDbConnection = ReadString(confignode, xpath, DBConnection);
|
||||
|
||||
// Konzolra való naplózás engedélyezése.
|
||||
xpath = @"/config/server/logtoconsole";
|
||||
LogToConsole = ReadBool(confignode, xpath, false);
|
||||
|
||||
// A maximálisan lehozható rekordok száma.
|
||||
xpath = @"/config/server/maxrecordsperrequest";
|
||||
MaximalRecordCountPerRequest = ReadNumber(confignode, xpath, 200);
|
||||
|
||||
xpath = @"/config/server/systemtype";
|
||||
SystemType = ReadEnum<SystemType>(confignode, xpath, SystemType.Ismeretlen);
|
||||
}
|
||||
catch (InvalidConfigurationException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new InvalidConfigurationException(string.Format("Invalid configuration entry: {0}.", xpath), exception);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Mezok
|
||||
|
||||
string _logDbConnection;
|
||||
|
||||
volatile int _sessionTimeout = 10;
|
||||
|
||||
SystemType _systemType = SystemType.Ismeretlen;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Tulajdonságok
|
||||
|
||||
/// <summary>
|
||||
/// A kiszolgáló neve.
|
||||
/// </summary>
|
||||
public string ServerName { get; private set; }
|
||||
|
||||
string dbconnection = null;
|
||||
/// <summary>
|
||||
/// Adatbáziskapcsolat-leíró karakterlánc.
|
||||
/// </summary>
|
||||
public string DBConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
return dbconnection;
|
||||
}
|
||||
set
|
||||
{
|
||||
dbconnection = value;
|
||||
|
||||
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(dbconnection);
|
||||
DatabaseName = builder.InitialCatalog;
|
||||
}
|
||||
}
|
||||
|
||||
public string GlobalDbConnection { get; set; }
|
||||
|
||||
public string DatabaseName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A kiszolgáló naplózási szintje.
|
||||
/// </summary>
|
||||
public LogLevel ServerLogLevel { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// A kiszolgáló alapértelmezett régiókódja.
|
||||
/// </summary>
|
||||
public int LCID { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Munkamenet elévülési ideje, percben.
|
||||
/// </summary>
|
||||
public int SessionTimeout
|
||||
{
|
||||
get { return _sessionTimeout; }
|
||||
set { _sessionTimeout = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rendszer típus
|
||||
/// </summary>
|
||||
public SystemType SystemType
|
||||
{
|
||||
get { return _systemType; }
|
||||
set { _systemType = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A konfiguráció, mint XML dokumentum.
|
||||
/// </summary>
|
||||
public XmlNode ConfigDocument { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Az egy kérésben maximálisan megengedett felhozott rekordok száma.
|
||||
/// </summary>
|
||||
public int MaximalRecordCountPerRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Naplózás konzolra.
|
||||
/// </summary>
|
||||
public bool LogToConsole { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region ILogConfig
|
||||
|
||||
/// <summary>
|
||||
/// A kiszolgáló naplózási szintje.
|
||||
/// </summary>
|
||||
LogLevel ILogConfig.LogLevel { get { return ServerLogLevel; } }
|
||||
|
||||
string ILogConfig.ConnectionString { get { return _logDbConnection; } }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue