tbf/TBF/Rig/Output/DataStorage/UniDataStorageWriter/WriterCfg.cs

286 lines
8.8 KiB
C#

using Common;
using Config.Entities;
///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using TBF.Resources;
using TBF.Rig.Generic;
using TBF.Rig.Output.DataStorage.UniDataStorageWriter.Interfaces;
using TBF.Rig.Output.DataStorage.UniDataStorageWriter.UI;
using static TBF.Rig.Output.DataStorage.UniDataStorageWriter.Types;
namespace TBF.Rig.Output.DataStorage.UniDataStorageWriter
{
///
/// Class and file name is preserved for backward compatibility
///
public class WriterCfg : ComponentCfgBase, Generic.IComponentCfg, IParamsProvider
{
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(WriterCfg) })[0];
public override XmlSerializer GetSerializer() { return Serializer; }
public IComponentCfgCtrl GetControl(IList<Config.Entities.Component> cmpntEntities) { return new WriterCfgCtrl(); }
/// <summary>
/// Internal storage type identifier.
/// </summary>
public string DataStorageType;
/// <summary>
/// Data source definition, e.g. file path, connection string, URL, etc.
/// </summary>
public string DataSource;
/// <summary>
/// Original field name preserved for backward compatibility.
/// For writer semantics this represents the write template.
/// </summary>
public string QueryTemplate;
public string TechnologyType;
public WriteMode WriteMode;
public List<string> WriteTemplates;
/// <summary>
/// Private parameterless constructor invoked by all other constructors.
/// </summary>
WriterCfg()
{
InitializeAll();
}
public WriterCfg(string name, IComponentFactory factory)
: this()
{
this.Name = name;
this.Factory = factory;
}
public string ComponentName
{
get { return Name; }
}
public void InitializeAll()
{
DataStorageType = string.Empty;
DataSource = string.Empty;
QueryTemplate = string.Empty;
TechnologyType = string.Empty;
WriteMode = WriteMode.Insert;
WriteTemplates = new List<string>();
}
private readonly string[] paramNames = new string[]
{
"Data Storage type",
"Technology type",
"Data source",
"Query template",
};
public string ParamName(int i) { return paramNames[i]; }
public int ParamsCount() { return paramNames.Length; }
public ICollection<string> ParamValues(int i)
{
switch (i)
{
case 0:
return new string[]
{
StorageTypes.RestApi,
StorageTypes.LocalDatabase,
StorageTypes.RemoteDatabase,
StorageTypes.LocalFile,
StorageTypes.RemoteFile,
};
case 1:
switch ((DataStorageType ?? string.Empty).Trim())
{
case StorageTypes.LocalDatabase:
case StorageTypes.RemoteDatabase:
return new string[]
{
TechnologyTypes.MicrosoftSql,
TechnologyTypes.MySqlMariaDb,
TechnologyTypes.SQLite,
};
case StorageTypes.LocalFile:
case StorageTypes.RemoteFile:
return new string[]
{
TechnologyTypes.Csv,
TechnologyTypes.Xls,
TechnologyTypes.Json,
};
default:
return null;
}
case 2:
case 3:
default:
return null;
}
}
public string ToString(int i)
{
return string.Format(
"Name={0}, DataStorageType={1}, TechnologyType={2}",
Name,
DataStorageType,
TechnologyType);
}
public CfgUpdateFlags UpdateParam(int i, string strValue)
{
switch (i)
{
case 0: DataStorageType = strValue; return CfgUpdateFlags.RestartRqrd;
case 1: TechnologyType = strValue; return CfgUpdateFlags.RestartRqrd;
case 2: DataSource = strValue; return CfgUpdateFlags.RestartRqrd;
case 3: QueryTemplate = strValue; return CfgUpdateFlags.RestartRqrd;
default: return CfgUpdateFlags.None;
}
}
public bool ValidateParam(int i, string strValue, out string message)
{
message = string.Empty;
strValue = strValue ?? string.Empty;
switch (i)
{
case 0:
if (string.IsNullOrWhiteSpace(strValue))
{
message = "Data storage type must be selected.";
return false;
}
return true;
case 1:
if ((DataStorageType == StorageTypes.LocalDatabase || DataStorageType == StorageTypes.RemoteDatabase ||
DataStorageType == StorageTypes.LocalFile || DataStorageType == StorageTypes.RemoteFile) &&
string.IsNullOrWhiteSpace(strValue))
{
message = "Technology type must be selected.";
return false;
}
return true;
case 2:
if (string.IsNullOrWhiteSpace(strValue))
{
message = "Data source must not be empty.";
return false;
}
return true;
case 3:
if (string.IsNullOrWhiteSpace(strValue))
{
message = "Query template must not be empty.";
return false;
}
return true;
default:
message = "Invalid parameter index.";
return false;
}
}
private void CopyContentTo(WriterCfg prms)
{
prms.DataStorageType = this.DataStorageType;
prms.TechnologyType = this.TechnologyType;
prms.DataSource = this.DataSource;
prms.QueryTemplate = this.QueryTemplate;
prms.WriteMode = this.WriteMode;
prms.WriteTemplates = new List<string>();
if (this.WriteTemplates != null)
{
foreach (string item in this.WriteTemplates)
{
prms.WriteTemplates.Add(item);
}
}
}
public IParamsProvider Clone()
{
WriterCfg pars = new WriterCfg();
CopyContentTo(pars);
return pars;
}
/// <summary>
/// Strongly typed helper for internal use.
/// </summary>
public WriterCfg ShallowCopy()
{
WriterCfg copy = new WriterCfg(this.Name, this.Factory);
CopyContentTo(copy);
return copy;
}
public bool UpdateEmbeddedDbEntity()
{
return true; /// =OK, do nothing
}
private static bool TryParseTemplateItem(string item, out WriteMode mode, out string template)
{
mode = WriteMode.Insert;
template = string.Empty;
if (string.IsNullOrWhiteSpace(item))
return false;
int separatorIndex = item.IndexOf('|');
if (separatorIndex <= 0)
return false;
string modeText = item.Substring(0, separatorIndex).Trim();
template = item.Substring(separatorIndex + 1).Trim();
if (!Enum.TryParse(modeText, true, out mode))
return false;
return true;
}
public string GetTemplate(WriteMode mode)
{
if (WriteTemplates == null)
return string.Empty;
foreach (string item in WriteTemplates)
{
WriteMode m;
string t;
if (TryParseTemplateItem(item, out m, out t) && m == mode)
{
return t;
}
}
return QueryTemplate ?? string.Empty; // fallback
}
}
}