114 lines
3.2 KiB
C#
114 lines
3.2 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 ProcedureParamsBase : Generic.IProcedureParams
|
|
{
|
|
protected ComponentProcedure procedureParamsEntity;
|
|
|
|
public virtual XmlSerializer GetSerializer() { return new XmlSerializer(GetType()); }
|
|
|
|
public string ComponentName { get { return componentName; } }
|
|
protected string componentName;
|
|
|
|
public Procedure Procedure { get { return procedure; } }
|
|
protected Procedure procedure;
|
|
|
|
/// <summary>
|
|
/// Parameterless constructor required for serialization
|
|
/// </summary>
|
|
public ProcedureParamsBase()
|
|
{
|
|
InitializeAll();
|
|
}
|
|
|
|
/// <summary> To be overridden </summary>
|
|
public virtual void InitializeAll()
|
|
{
|
|
}
|
|
|
|
/// <summary> To be overridden </summary>
|
|
public virtual IList<string> ParamValues(int ix)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/// <summary> To be overridden </summary>
|
|
public virtual void UpdateFromDbEntity(ComponentProcedure dbEntity)
|
|
{
|
|
}
|
|
|
|
public void FromDbEntity(string cmpntName, Procedure procedure)
|
|
{
|
|
ComponentProcedure componentProc = procedure.GetProcedureParamsEntity(cmpntName);
|
|
if (componentProc != null)
|
|
{
|
|
UpdateFromDbEntity(componentProc);
|
|
return;
|
|
}
|
|
|
|
/// Create a new entity and add it to the list test.MoreParams
|
|
this.procedureParamsEntity = this.ToDbEntity(cmpntName, procedure);
|
|
this.procedureParamsEntity.SetParamsProvider(this as IParamsProvider);
|
|
this.procedure = procedure;
|
|
this.componentName = cmpntName;
|
|
procedure.MoreParams.Add(procedureParamsEntity);
|
|
}
|
|
|
|
public ComponentProcedure ToDbEntity(string cmpntName, Procedure procedure)
|
|
{
|
|
using (var writer = new StringWriter())
|
|
{
|
|
GetSerializer().Serialize(writer, this); /// Serialize this class with parameters
|
|
return new ComponentProcedure(cmpntName, writer.ToString(), procedure); /// Return new ComponentProcedure entity
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates DB entities for procedure parameters
|
|
/// </summary>
|
|
/// <returns>true when successful</returns>
|
|
public bool UpdateEmbeddedDbEntity()
|
|
{
|
|
if (procedureParamsEntity == null) return false;
|
|
|
|
using (var writer = new StringWriter())
|
|
{
|
|
/// Serialize this class and update the entity
|
|
GetSerializer().Serialize(writer, this);
|
|
procedureParamsEntity.Parameters = writer.ToString();
|
|
procedureParamsEntity.CmpntName = componentName;
|
|
procedureParamsEntity.Procedure = procedure;
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|