// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace Spludlow
{
public class ConfigMake
{
public static string Create(string hostId)
{
return Create(hostId, false);
}
public static string Create(string hostId, bool offlineHost)
{
Spludlow.Schema dataSet = new Spludlow.Schema();
Spludlow.Data.IDAL dal2 = Spludlow.Data.DAL.Create("@Spludlow");
foreach (string tableName in new string[] { "Organizations", "Networks", "Hosts" })
dal2.Fill(dataSet.Tables[tableName], "SELECT * FROM " + tableName);
string programData = null;
string[][] serverAddresses = null;
if (offlineHost == false)
{
programData = (string)Spludlow.Call.Now(hostId, "Spludlow", "Spludlow.Config", "ProgramData");
serverAddresses = Spludlow.Config.WCFServerAddresses(hostId);
}
return Create(dataSet, hostId, programData, serverAddresses);
}
public static string Create(Schema dataSet, string hostId, string programDataDirectory, string[][] wcfServerAddresses)
{
Spludlow.Schema.HostsRow thisHost = dataSet.Hosts.FindByHostId(hostId);
if (thisHost == null)
throw new ApplicationException("Can not find Hosts Row:\t" + hostId);
Spludlow.Schema.NetworksRow thisNetwork = dataSet.Networks.FindByNetworkId(thisHost.NetworkId);
if (thisNetwork == null)
throw new ApplicationException("Can not find Networks Row:\t" + thisHost.NetworkId);
Spludlow.Schema.OrganizationsRow thisOrganization = dataSet.Organizations.FindByOrganizationId(thisNetwork.OrganizationId);
if (thisOrganization == null)
throw new ApplicationException("Can not find Organizations Row:\t" + thisNetwork.OrganizationId);
StringBuilder text = new StringBuilder();
text.AppendLine("");
text.AppendLine();
text.AppendLine("");
text.AppendLine();
text.AppendLine("");
text.AppendLine();
AppendConfiguration(thisOrganization, text);
AppendConfiguration(thisNetwork, text);
AppendConfiguration(thisHost, text);
text.AppendLine();
AppendLine("", text);
text.AppendLine();
AppendColumns(thisOrganization, new string[] { "OrganizationId", "MasterHostId", "LogHostId", "MailHostAddress", "MailToAddress", "MailFromAddress" }, text);
AppendColumns(thisNetwork, new string[] { "NetworkId", "RouterHostId", "SmtpServer", "QueueNamespace" }, text);
AppendColumns(thisHost, new string[] { "HostId", "AdminWebAddress", "IgnoreSecurityKey", "SystemUpdateMode" }, text);
string developSources = "";
foreach (Spludlow.Schema.HostsRow row in dataSet.Hosts.Select("DevelopSource = True"))
{
if (developSources != "")
developSources += ", ";
developSources += row.HostId;
}
AppendLine("DevelopSources", developSources, text);
text.AppendLine();
if (programDataDirectory != null)
AppendLine("ProgramData", programDataDirectory, text);
else
text.AppendLine("");
text.AppendLine();
Dictionary> organizationNetworks = new Dictionary>();
Dictionary> networkHosts = new Dictionary>();
List networkRouters = new List();
string hosts = "";
foreach (Spludlow.Schema.OrganizationsRow organization in dataSet.Organizations.Rows)
{
organizationNetworks.Add(organization.OrganizationId, new List());
foreach (Spludlow.Schema.NetworksRow network in dataSet.Networks.Select("OrganizationId = '" + organization.OrganizationId + "'"))
{
organizationNetworks[organization.OrganizationId].Add(network.NetworkId);
networkHosts.Add(network.NetworkId, new List());
networkRouters.Add(new string[] { network.NetworkId, network.RouterHostId });
foreach (Spludlow.Schema.HostsRow host in dataSet.Hosts.Select("NetworkId = '" + network.NetworkId + "'"))
{
networkHosts[network.NetworkId].Add(host.HostId);
AppendLine("Host." + host.HostId, host.HostId, text);
string address = host.LocalWebAddress;
if (host.NetworkId != thisHost.NetworkId)
{
Spludlow.Schema.HostsRow routerHost = dataSet.Hosts.FindByHostId(network.RouterHostId);
if (routerHost == null)
throw new ApplicationException("Can not find Router Host for network:\t" + host.NetworkId + "\t" + hostId);
address = "@" + routerHost.PublicWebAddress;
}
AppendLine("Address." + host.HostId, address, text);
text.AppendLine();
if (hosts != "")
hosts += ", ";
hosts += host.HostId;
}
}
}
AppendLine("Hosts", hosts, text);
text.AppendLine();
AppendMembership("Organizations", "OrganizationNetworks", organizationNetworks, text);
AppendMembership("Networks", "NetworkHosts", networkHosts, text);
foreach (string[] networkRouter in networkRouters)
AppendLine("NetworkRouter." + networkRouter[0], networkRouter[1], text);
text.AppendLine();
if (wcfServerAddresses != null)
{
foreach (string[] wcfPair in wcfServerAddresses)
{
string key = wcfPair[0];
string remoteAddress = wcfPair[1];
AppendLine("Remotings." + key, remoteAddress, text);
}
}
else
{
text.AppendLine("");
}
text.AppendLine();
text.AppendLine("");
return text.ToString();
}
private static void AppendMembership(string parentsConfigName, string parentChildConfigName, Dictionary> memberships, StringBuilder text)
{
string parents = "";
foreach (string parentId in memberships.Keys)
{
string data = "";
foreach (string childId in memberships[parentId])
{
if (data != "")
data += ", ";
data += childId;
}
AppendLine(parentChildConfigName + "." + parentId, data, text);
if (parents != "")
parents += ", ";
parents += parentId;
}
text.AppendLine();
AppendLine(parentsConfigName, parents, text);
text.AppendLine();
}
private static void AppendConfiguration(DataRow row, StringBuilder text)
{
string configuration = (string)row["Configuration"];
if (configuration == "")
return;
text.AppendLine();
AppendLine("", text);
text.AppendLine();
foreach (string line in Spludlow.Text.SplitLines(configuration))
AppendLine(line, text);
text.AppendLine();
}
private static void AppendColumns(DataRow row, string[] columnNames, StringBuilder text)
{
foreach (string columnName in columnNames)
{
string key = columnName;
if (key.EndsWith("HostId") == true && key.Length > 6)
key = "Host." + key.Substring(0, key.Length - 6);
string typeName = row.Table.Columns[columnName].DataType.Name;
string data;
switch (typeName)
{
case "String":
data = (string)row[columnName];
break;
case "Boolean":
data = ((bool)row[columnName]).ToString();
break;
default:
throw new ApplicationException("Unsuported Type: " + typeName);
}
AppendLine(key, data, text);
}
text.AppendLine();
}
private static void AppendLine(string lineText, StringBuilder text)
{
text.Append("\t");
text.AppendLine(lineText);
}
private static void AppendLine(string key, string data, StringBuilder text)
{
text.Append("\t");
text.Append("");
text.AppendLine();
}
private static void AppendRemark(string remark, StringBuilder text)
{
text.Append("\t");
text.Append("");
text.AppendLine();
}
public static void SetFlagAllHosts(string flagName, bool value)
{
Spludlow.Data.IDAL dal = Spludlow.Data.DAL.Create("@Spludlow");
StringBuilder commandText = new StringBuilder();
commandText.Append("UPDATE Hosts SET @ColumnName = @Value");
commandText.Replace("@ColumnName", flagName);
string textValue = "0";
if (value == true)
textValue = "1";
commandText.Replace("@Value", textValue);
dal.ExecuteNonQuery(commandText.ToString());
}
}
}