using System.Globalization; using System.Xml.Linq; using Microsoft.Data.SqlClient; namespace XmlTool.Importers { public sealed class ServerTransferProfileSummary { public int ServerTransferCount { get; init; } public int EnabledServerTransferCount { get; init; } public int LimitCount { get; init; } public int DuplicateServerIndexCount { get; init; } public int CurrentAccountServerCount { get; init; } public string ServerIndexes { get; init; } = string.Empty; } public static class ServerTransferProfileInspector { public static ServerTransferProfileSummary ValidateXml(string serverTransferXmlPath) { List records = LoadRecords(serverTransferXmlPath); return BuildSummary(records, currentAccountServerCount: 0); } public static ServerTransferProfileSummary AssessDatabase(string connectionString, string serverTransferXmlPath) { List records = LoadRecords(serverTransferXmlPath); using SqlConnection connection = new(connectionString); connection.Open(); return BuildSummary(records, ExecuteScalarInt(connection, "SELECT COUNT(*) FROM [Account].[Server];")); } private static ServerTransferProfileSummary BuildSummary(IReadOnlyList records, int currentAccountServerCount) { return new ServerTransferProfileSummary { ServerTransferCount = records.Count, EnabledServerTransferCount = records.Count(x => x.Enable), LimitCount = records.Sum(x => x.LimitCount), DuplicateServerIndexCount = CountDuplicates(records.Select(x => x.ServerIndex)), CurrentAccountServerCount = currentAccountServerCount, ServerIndexes = records.Count == 0 ? "(none)" : string.Join(", ", records.Select(x => x.ServerIndex).OrderBy(x => x)) }; } private static List LoadRecords(string serverTransferXmlPath) { if (!File.Exists(serverTransferXmlPath)) { throw new FileNotFoundException("ServerTransfer.xml not found.", serverTransferXmlPath); } XDocument document = XDocument.Load(serverTransferXmlPath); XElement root = document.Root ?? throw new InvalidOperationException("ServerTransfer.xml is missing a root node."); if (root.Name.LocalName != "ServerTransferTable") { throw new InvalidOperationException("ServerTransfer.xml root node must be ."); } List records = new(); int rowIndex = 0; foreach (XElement transferElement in root.Elements("ServerTransfer")) { rowIndex++; string rowContext = $"ServerTransfer row {rowIndex}"; int serverIndex = ReadRequiredInt(transferElement, "ServerIndex", rowContext); bool enable = ReadRequiredBool(transferElement, "Enable", rowContext); _ = ReadRequiredString(transferElement, "ServerName", rowContext); XElement limitsElement = transferElement.Element("Limits") ?? throw new InvalidOperationException($"{rowContext} is missing required node."); int limitCount = 0; foreach (XElement limitElement in limitsElement.Elements("Limit")) { limitCount++; string limitContext = $"{rowContext} Limit row {limitCount}"; _ = ReadRequiredInt(limitElement, "LimitType", limitContext); _ = ReadRequiredUlong(limitElement, "LimitValue", limitContext); } records.Add(new ServerTransferRecord(serverIndex, enable, limitCount)); } return records; } private static int ReadRequiredInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!int.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) { throw new InvalidOperationException($"{recordContext} has invalid integer value in '{elementName}': '{element.Value}'."); } return value; } private static ulong ReadRequiredUlong(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!ulong.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong value)) { throw new InvalidOperationException($"{recordContext} has invalid unsigned long value in '{elementName}': '{element.Value}'."); } return value; } private static bool ReadRequiredBool(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!bool.TryParse(element.Value, out bool value)) { throw new InvalidOperationException($"{recordContext} has invalid bool value in '{elementName}': '{element.Value}'."); } return value; } private static string ReadRequiredString(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } return element.Value; } private static int ExecuteScalarInt(SqlConnection connection, string sql) { using SqlCommand command = new(sql, connection); object? value = command.ExecuteScalar(); return Convert.ToInt32(value, CultureInfo.InvariantCulture); } private static int CountDuplicates(IEnumerable values) { return values.GroupBy(x => x).Where(x => x.Count() > 1).Sum(x => x.Count() - 1); } private sealed record ServerTransferRecord(int ServerIndex, bool Enable, int LimitCount); } }