115 lines
3.0 KiB
C#
115 lines
3.0 KiB
C#
///
|
|
/// Copyright (c) 2013-2017 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
using Config.Entities;
|
|
|
|
namespace TBF.BenchControl
|
|
{
|
|
public class TestParamsBase : Generic.ITestParams
|
|
{
|
|
protected ComponentTest testParamsEntity;
|
|
|
|
public virtual XmlSerializer GetSerializer() { return new XmlSerializer(GetType()); }
|
|
|
|
public string ComponentName { get { return componentName; } }
|
|
protected string componentName;
|
|
|
|
public Test Test { get { return test; } }
|
|
protected Test test;
|
|
|
|
/// <summary>
|
|
/// Parameterless constructor required for serialization
|
|
/// </summary>
|
|
public TestParamsBase()
|
|
{
|
|
InitializeAll();
|
|
}
|
|
|
|
/// <summary> To be overridden </summary>
|
|
public virtual void InitializeAll()
|
|
{
|
|
}
|
|
|
|
/// <summary> To be overridden </summary>
|
|
public virtual ICollection<string> ParamValues(int ix)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/// <summary> To be overridden </summary>
|
|
public virtual void UpdateFromDbEntity(ComponentTest dbEntity)
|
|
{
|
|
}
|
|
|
|
public void FromDbEntity(string cmpntName, Test test)
|
|
{
|
|
ComponentTest componentTest = test.GetTestParamsEntity(cmpntName);
|
|
if (componentTest != null)
|
|
{
|
|
UpdateFromDbEntity(componentTest);
|
|
return;
|
|
}
|
|
|
|
/// Create a new entity and add it to the list test.MoreParams
|
|
InitializeAll();
|
|
this.testParamsEntity = this.ToDbEntity(cmpntName, test);
|
|
this.testParamsEntity.SetParamsProvider(this as IParamsProvider);
|
|
this.test = test;
|
|
this.componentName = cmpntName;
|
|
test.MoreParams.Add(testParamsEntity);
|
|
}
|
|
|
|
public ComponentTest ToDbEntity(string cmpntName, Test test)
|
|
{
|
|
using (var writer = new StringWriter())
|
|
{
|
|
GetSerializer().Serialize(writer, this); /// Serialize this class with parameters
|
|
return new ComponentTest(cmpntName, writer.ToString(), test); /// Return new ComponentTest entity
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates DB entities for test parameters
|
|
/// </summary>
|
|
/// <returns>true when successful</returns>
|
|
public bool UpdateEmbeddedDbEntity()
|
|
{
|
|
if (testParamsEntity == null) return false;
|
|
|
|
using (var writer = new StringWriter())
|
|
{
|
|
/// Serialize this class and update the entity
|
|
GetSerializer().Serialize(writer, this);
|
|
testParamsEntity.Parameters = writer.ToString();
|
|
testParamsEntity.CmpntName = componentName;
|
|
testParamsEntity.Test = test;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public virtual string ParamName(int i) { return string.Empty; }
|
|
|
|
public virtual int ParamsCount() { return 0; }
|
|
|
|
public virtual string ToString(int i) { return string.Empty; }
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < ParamsCount(); i++)
|
|
{
|
|
if (i != 0) sb.Append("; ");
|
|
sb.Append(ParamName(i));
|
|
sb.Append("=");
|
|
sb.Append(ToString(i));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|