tbf/TBF/Rig/Input/DataStorage/UniDataStorageReader/ReaderCfg.cs

170 lines
5.1 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;
namespace TBF.Rig.Input.DataStorage.UniDataStorageReader
{
///
/// Class and file name is preserved for backward compatibility
///
public class ReaderCfg : ComponentCfgBase, Generic.IComponentCfg, IParamsProvider
{
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(ReaderCfg) })[0];
public override XmlSerializer GetSerializer() { return Serializer; }
public IComponentCfgCtrl GetControl(IList<Config.Entities.Component> cmpntEntities) { return new ReaderCfgCtrl(); }
public string DataStorageType;
public string DataSource;
public string QueryTemplate;
/// Private parameterless constructor invoked by all other (public) constructors
ReaderCfg()
{
InitializeAll();
}
public ReaderCfg(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;
}
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;
}
}
void CopyContentTo(ReaderCfg prms)
{
prms.DataStorageType = this.DataStorageType;
prms.DataSource = this.DataSource;
prms.QueryTemplate = this.QueryTemplate;
}
public IParamsProvider Clone()
{
ReaderCfg pars = new ReaderCfg();
CopyContentTo(pars);
return pars;
}
public bool UpdateEmbeddedDbEntity()
{
return true; /// =OK, do nothing
}
public static class StorageTypes
{
public const string RestApi = "REST_API";
public const string RemoteDatabase = "REMOTE_DATABASE";
public const string RemoteJson = "REMOTE_JSON";
public const string RemoteCsv = "REMOTE_CSV";
public const string LocalDatabase = "LOCAL_DATABASE";
public const string LocalJson = "LOCAL_JSON";
public const string LocalCsv = "LOCAL_CSV";
}
}
}