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

244 lines
6.9 KiB
C#

///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Common;
using Config.Entities;
using TBF.Rig.Generic;
using TBF.Resources;
using TBF.Rig.Output.DataStorage.UniDataStorageWriter.UI;
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;
/// <summary>
/// Writer-friendly alias for QueryTemplate.
/// This property is not serialized to keep backward compatibility with existing XML.
/// </summary>
[XmlIgnore]
public string WriteTemplate
{
get { return QueryTemplate; }
set { QueryTemplate = value; }
}
/// <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;
}
private readonly string[] paramNames = new string[]
{
"Data Storage 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.RemoteDatabase,
StorageTypes.RemoteJson,
StorageTypes.RemoteCsv,
StorageTypes.LocalDatabase,
StorageTypes.LocalJson,
StorageTypes.LocalCsv,
};
case 1:
case 2:
default:
return null;
}
}
public string ToString(int i)
{
return string.Format(
"Name={0}, DataStorageType={1}",
Name,
DataStorageType);
}
public CfgUpdateFlags UpdateParam(int i, string strValue)
{
switch (i)
{
case 0:
DataStorageType = strValue;
return CfgUpdateFlags.RestartRqrd;
case 1:
DataSource = strValue;
return CfgUpdateFlags.RestartRqrd;
case 2:
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 (string.IsNullOrWhiteSpace(strValue))
{
message = "Data source must not be empty.";
return false;
}
return true;
case 2:
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.DataSource = this.DataSource;
prms.QueryTemplate = this.QueryTemplate;
}
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
}
/// <summary>
/// Converts internal storage type identifier to user-friendly name.
/// </summary>
public static string StorageTypeToDisplayName(string storageType)
{
switch ((storageType ?? string.Empty).Trim())
{
case StorageTypes.RestApi:
return "REST API";
case StorageTypes.RemoteDatabase:
return "Remote database";
case StorageTypes.RemoteJson:
return "Remote JSON";
case StorageTypes.RemoteCsv:
return "Remote CSV";
case StorageTypes.LocalDatabase:
return "Local database";
case StorageTypes.LocalJson:
return "Local JSON";
case StorageTypes.LocalCsv:
return "Local CSV";
default:
return string.IsNullOrWhiteSpace(storageType) ? "<empty>" : storageType;
}
}
}
}