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
}
///
/// Néhány alaptulajdonság van felvéve, aztan majd lehet bővíteni.
///
public class Configuration : ILogConfig
{
///
/// Configuration constuctor
///
/// the main "config" node
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
///
/// Reads configuration string.
///
/// Server config
/// XPath location of node
/// Configuration value
/// Invalid configuration
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));
}
///
/// Reads configuration string.
///
/// Server config
/// XPath location of node
/// The default value if missing from configuration
/// Configuration value
/// Invalid configuration
protected static string ReadString(XmlNode confignode, string xpath, string defaultValue)
{
XmlNode current;
return (current = confignode.SelectSingleNode(xpath)) != null ? current.InnerText : defaultValue;
}
///
/// Reads configuration list.
///
/// Server config
/// XPath location of node
/// Name of list element node
/// Configuration list
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;
}
///
/// Reads configuration number.
///
/// Server config
/// XPath location of node
/// The default value if missing from configuration, null value implies required node
/// Configuration value
///
/// Invalid configuration or is null and node is missing.
///
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));
}
///
/// Reads configuration switch.
///
/// Server config
/// XPath location of node
/// The default value if missing from configuration, null value implies required node
/// Configuration value
///
/// Invalid configuration or is null and node is missing.
///
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));
}
///
/// Reads configuration enumeration.
///
/// Server config
/// XPath location of node
/// The default value if missing from configuration, null value implies required node
/// Configuration value
///
/// Invalid configuration or is null and node is missing.
///
protected static TEnum ReadEnum(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(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(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
///
/// A kiszolgáló neve.
///
public string ServerName { get; private set; }
string dbconnection = null;
///
/// Adatbáziskapcsolat-leíró karakterlánc.
///
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; }
///
/// A kiszolgáló naplózási szintje.
///
public LogLevel ServerLogLevel { get; private set; }
///
/// A kiszolgáló alapértelmezett régiókódja.
///
public int LCID { get; private set; }
///
/// Munkamenet elévülési ideje, percben.
///
public int SessionTimeout
{
get { return _sessionTimeout; }
set { _sessionTimeout = value; }
}
///
/// Rendszer típus
///
public SystemType SystemType
{
get { return _systemType; }
set { _systemType = value; }
}
///
/// A konfiguráció, mint XML dokumentum.
///
public XmlNode ConfigDocument { get; private set; }
///
/// Az egy kérésben maximálisan megengedett felhozott rekordok száma.
///
public int MaximalRecordCountPerRequest { get; set; }
///
/// Naplózás konzolra.
///
public bool LogToConsole { get; private set; }
#endregion
#region ILogConfig
///
/// A kiszolgáló naplózási szintje.
///
LogLevel ILogConfig.LogLevel { get { return ServerLogLevel; } }
string ILogConfig.ConnectionString { get { return _logDbConnection; } }
#endregion
}
}