EchoTex_Payroll/HRM.BO/Configuration/Configuration.cs
2024-10-14 10:01:49 +06:00

1039 lines
39 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.CoreV35;
//using Ease.CoreV35.Model;
//using Ease.CoreV35.Caching;
//using System.Data.Linq.Mapping;
using System.Xml;
using System.Threading.Tasks;
using System.Data;
using HRM.BO;
using Ease.Core.Model;
namespace Payroll.BO
{
#region Configaration
[Serializable]
public class Configaration : AuditTrailBase
{
//#region Cache Store
//private static Cache _cache = new Cache(typeof(Payroll.BO.Configaration));
//#endregion
#region Constructor
public Configaration()
{
_node = string.Empty;
_value = string.Empty;
_parentID = null;
_type = EnumConfigurationType.None;
}
#endregion
#region Properties
#region Node : string
private string _node;
public string Node
{
get { return _node; }
set
{
// base.OnPropertyChange<string>("Node", _node, value);
_node = value;
}
}
#endregion
#region Value : string
private string _value;
public string Value
{
get { return _value; }
set
{
_value = value;
}
//set
//{
// base.OnPropertyChange<string>("Value", _value, value);
// _value = value;
//}
}
#endregion
#region ParentID : ID
private int? _parentID;
public int? ParentID
{
get { return _parentID; }
set
{
// base.OnPropertyChange<int>("ParentID", _parentID, value);
_parentID = value;
}
}
#endregion
#region Type : EnumConfigurationType
private EnumConfigurationType _type;
public EnumConfigurationType Type
{
get { return _type; }
set
{
// base.OnPropertyChange<short>("Type", (short)_type, (short)value);
_type = value;
}
}
#endregion
#region ConfigurationAttribute
[Serializable]
public class ConfigurationAttribute : ObjectTemplate
{
#region Constructor
public ConfigurationAttribute()
{
_configurationID = null;
_value = "";
_attributeName = "";
}
public ConfigurationAttribute(int configurationID)
{
_configurationID = configurationID;
_value = "";
_attributeName = "";
}
#endregion
#region Property ParentID : ID
private int? _configurationID;
public int? ConfigurationID
{
get { return _configurationID; }
set
{
// base.OnPropertyChange<int>("configurationID", _configurationID, value);
_configurationID = value;
}
}
#endregion
#region AttributeName : string
private string _attributeName;
public string AttributeName
{
get { return _attributeName; }
set
{
// base.OnPropertyChange<string>("AttributeName", _attributeName, value);
_attributeName = value;
}
}
#endregion
#region Value : String
private string _value;
public string Value
{
get { return _value; }
set
{
// base.OnPropertyChange<string>("Value", _value, value);
_value = value;
}
}
#endregion
public new void SetID(int id)
{
base.SetID(id);
}
}
#endregion
#region ConfigurationAttributes
[Serializable]
public class ConfigurationAttributes : List<ConfigurationAttribute>
{
#region Constructor
public ConfigurationAttributes()
{
}
public ConfigurationAttribute GetItemByParentID(int configurationID)
{
return this.Find(delegate(ConfigurationAttribute item) { return item.ConfigurationID == configurationID; });
}
public void Add(int configurationId, string nodeName, string svalue)
{
ConfigurationAttribute att = new ConfigurationAttribute();
att.Value = svalue;
att.AttributeName = nodeName;
// att.SetID(ID.FromInteger(this.Count + 1));
att.SetID(this.Count + 1);
this.Add(att);
}
public string GetAttributeValue(string attributeName)
{
foreach (ConfigurationAttribute oItem in this)
{
if (oItem.AttributeName.ToLower() == attributeName.ToLower())
{
return oItem.Value == null ? "" : oItem.Value;
}
}
return "";
}
#endregion
}
#endregion
#region Property ConfigurationAttributes : ConfigurationAttributes
private ConfigurationAttributes _configurationAttributes;
public ConfigurationAttributes ConAttributes
{
get
{
if (_configurationAttributes == null)
_configurationAttributes = new ConfigurationAttributes();
return _configurationAttributes;
}
}
#endregion
#region Service Factory IConfigarationService : IConfigarationService
internal static IConfigarationService Service
{
get { return Services.Factory.CreateService<IConfigarationService>(typeof(IConfigarationService)); }
}
#endregion
#endregion
#region Functions
//public Configaration Get(ID nID)
//{
// Configaration oConfigaration = null;
// //#region Cache Header
// //oConfigaration = (Configaration)_cache["Get", ID];
// //if (oConfigaration != null)
// // return oConfigaration;
// //#endregion
// oConfigaration = Configaration.Service.Get(ID);
// //#region Cache Footer
// //_cache.Add(oConfigaration, "Get", ID);
// //#endregion
// return oConfigaration;
//}
//public ID Save()
//{
// return Configaration.Service.Save(this);
//}
//public void Delete()
//{
// Configaration.Service.Delete(ID);
//}
public Configaration Get(int id)
{
#region Cache Header
// Configaration configuration = _cache["Get", id] as Configaration;
Configaration configuration = new Configaration();
if (configuration != null)
return configuration;
#endregion
try
{
configuration = Service.Get(id);
}
catch (ServiceException e)
{
throw new Exception(e.Message, e);
}
//#region Cache Footer
//_cache.Add(configuration, "Get", id);
//#endregion
return configuration;
}
public static List<Configaration> Get(EnumConfigurationType configType)
{
//#region Cache Header
//List<Configaration> cofigurations = _cache["Get", configType] as List<Configaration>;
//if (cofigurations != null)
// return cofigurations;
//#endregion
List<Configaration> cofigurations = new List<Configaration>();
try
{
cofigurations = Service.Get(configType);
}
catch (ServiceException e)
{
throw new Exception(e.Message, e);
}
//#region Cache Footer
//_cache.Add(cofigurations, "Get", configType);
//#endregion
return cofigurations;
}
public void Save(List<Configaration> configurations, EnumConfigurationType confictype)
{
try
{
foreach (Configaration conf in configurations)
{
conf.SetAuditTrailProperties();
}
DolowerCase(configurations);
Service.Save(configurations, confictype);
}
catch (ServiceException e)
{
throw new Exception(e.Message, e);
}
}
private void DolowerCase(List<Configaration> configs)
{
foreach (Configaration item in configs)
{
item.Node = item.Node.ToLower();
foreach (ConfigurationAttribute att in item.ConAttributes)
{
att.AttributeName = att.AttributeName.ToLower();
}
}
}
public void Delete(int attributeID)
{
try
{
Service.Delete(attributeID);
//Clear Cache
//CacheInfo.ClearCache(typeof(Country).FullName);
}
catch (ServiceException e)
{
throw new Exception(e.Message, e);
}
}
#endregion
public void SetObjectID(int id)
{
base.SetID(id);
}
public static List<ConfigurationAttribute> GetAttributesByParentID(int p)
{
//#region Cache Header
//ObjectsTemplate<ConfigurationAttribute> cofigurations = _cache["GetAttributesByParentID", p] as ObjectsTemplate<ConfigurationAttribute>;
//if (cofigurations != null)
// return cofigurations;
//#endregion
List<ConfigurationAttribute> cofigurations = new List<ConfigurationAttribute>();
try
{
cofigurations = Service.GetAttributesByParentID(p);
}
catch (ServiceException e)
{
throw new Exception(e.Message, e);
}
//#region Cache Footer
//_cache.Add(cofigurations, "GetAttributesByParentID", p);
//#endregion
return cofigurations;
}
public static List<ConfigurationAttribute> GetAllChildren()
{
return Configaration.Service.GetAllChildren();
}
}
#endregion
#region IConfigaration Service
public interface IConfigarationService
{
Configaration Get(int id);
List<Configaration> Get(EnumConfigurationType confictype);
List<Configaration.ConfigurationAttribute> GetAllChildren();
//ObjectsTemplate<Configaration.ConfigurationAttribute> GetAllChildrens();
List<Configaration.ConfigurationAttribute> GetAllChildrens(string sApplicationType);
List<Configaration> GetAllParent(EnumConfigurationType type);
List<Configaration> GetAllParent();
void Save(List<Configaration> configurations, EnumConfigurationType confictype);
void Delete(int id);
List<Configaration.ConfigurationAttribute> GetAttributesByParentID(int p);
List<Configaration.ConfigurationAttribute> GetAllChildrens();
}
#endregion
public class ConfigurationManager
{
public ConfigurationManager()
{
}
private static List<Configaration> _logicCofiguration;
private static List<Configaration> _uiCofiguration;
private static List<Configaration> _menuCofiguration;
public List<Configaration> LogicConfiguration
{
get
{
return _logicCofiguration;
}
}
public List<Configaration> UICofiguration
{
get
{
return _uiCofiguration;
}
}
public List<Configaration.ConfigurationAttribute> GetAllChildren()
{
return Configaration.Service.GetAllChildren();
}
public List<Configaration.ConfigurationAttribute> GetAllChildrens()
{
return Configaration.Service.GetAllChildrens();
}
public List<Configaration.ConfigurationAttribute> GetAllChildrens(string sApplicationType)
{
return Configaration.Service.GetAllChildrens(sApplicationType);
}
public List<Configaration> GetAllParent(EnumConfigurationType type)
{
return Configaration.Service.GetAllParent(type);
}
public List<Configaration> GetAllParent()
{
return Configaration.Service.GetAllParent();
}
public List<Configaration> MenuCofiguration
{
get
{
return _menuCofiguration;
}
}
public void SetStaticConfiguration(List<Configaration> configs, EnumConfigurationType type)
{
switch (type)
{
case EnumConfigurationType.UI:
_uiCofiguration = configs;
break;
case EnumConfigurationType.Logic:
_logicCofiguration = configs;
break;
case EnumConfigurationType.Menu:
_menuCofiguration = configs;
break;
default:
break;
}
}
public void LoadConfiguration()
{
List<Configaration> oAllParents = this.GetAllParent();
List<Configaration.ConfigurationAttribute> oAllChilds = this.GetAllChildrens();
List<Configaration> logicParents = new List<Configaration>();
List<Configaration> uiParents = new List<Configaration>();
List<Configaration> menuParents = new List<Configaration>();
foreach (Configaration item in oAllParents)
{
if (item.Type == EnumConfigurationType.Logic)
logicParents.Add(item);
}
foreach (Configaration item in oAllParents)
{
if (item.Type == EnumConfigurationType.UI)
uiParents.Add(item);
}
foreach (Configaration item in oAllParents)
{
if (item.Type == EnumConfigurationType.Menu)
menuParents.Add(item);
}
foreach (Configaration item in logicParents)
{
List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
foreach (Configaration.ConfigurationAttribute con in oChilds)
item.ConAttributes.Add(con);
}
foreach (Configaration item in uiParents)
{
List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
foreach (Configaration.ConfigurationAttribute con in oChilds)
item.ConAttributes.Add(con);
}
foreach (Configaration item in menuParents)
{
List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
foreach (Configaration.ConfigurationAttribute con in oChilds)
item.ConAttributes.Add(con);
}
_logicCofiguration = logicParents;
_uiCofiguration = uiParents;
_menuCofiguration = menuParents;
}
public void LoadConfiguration(string mesApplicationType = "web")
{
List<Configaration> oAllParents = this.GetAllParent(EnumConfigurationType.Menu);
List<Configaration.ConfigurationAttribute> oAllChilds = this.GetAllChildrens(mesApplicationType);
List<Configaration> logicParents = new List<Configaration>();
List<Configaration> uiParents = new List<Configaration>();
List<Configaration> menuParents = new List<Configaration>();
foreach (Configaration item in oAllParents)
{
if (item.Type == EnumConfigurationType.Logic)
logicParents.Add(item);
}
foreach (Configaration item in oAllParents)
{
if (item.Type == EnumConfigurationType.UI)
uiParents.Add(item);
}
foreach (Configaration item in oAllParents)
{
if (item.Type == EnumConfigurationType.Menu)
menuParents.Add(item);
}
foreach (Configaration item in logicParents)
{
List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
foreach (Configaration.ConfigurationAttribute con in oChilds)
item.ConAttributes.Add(con);
}
foreach (Configaration item in uiParents)
{
List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
foreach (Configaration.ConfigurationAttribute con in oChilds)
item.ConAttributes.Add(con);
}
foreach (Configaration item in menuParents)
{
List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
foreach (Configaration.ConfigurationAttribute con in oChilds)
item.ConAttributes.Add(con);
}
_logicCofiguration = logicParents;
_uiCofiguration = uiParents;
_menuCofiguration = menuParents;
}
//public async Task LoadConfigurationAsync(string mesApplicationType = "web")
//{
// ObjectsTemplate<Configaration> logicParents = new ObjectsTemplate<Configaration>();
// ObjectsTemplate<Configaration> uiParents = new ObjectsTemplate<Configaration>();
// ObjectsTemplate<Configaration> menuParents = new ObjectsTemplate<Configaration>();
// Task<ObjectsTemplate<Configaration>> oAllParentsAsync = Task.Run(() => this.GetAllParent(EnumConfigurationType.Menu));
// Task<ObjectsTemplate<Configaration.ConfigurationAttribute>> oAllChildsAsync = Task.Run(() => this.GetAllChildrens(mesApplicationType));
// List<Configaration> oAllParents = await oAllParentsAsync;
// foreach (Configaration item in oAllParents)
// {
// if (item.Type == EnumConfigurationType.Logic)
// logicParents.Add(item);
// }
// foreach (Configaration item in oAllParents)
// {
// if (item.Type == EnumConfigurationType.UI)
// uiParents.Add(item);
// }
// foreach (Configaration item in oAllParents)
// {
// if (item.Type == EnumConfigurationType.Menu)
// menuParents.Add(item);
// }
// List<Configaration.ConfigurationAttribute> oAllChilds = await oAllChildsAsync;
// foreach (Configaration item in logicParents)
// {
// List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
// foreach (Configaration.ConfigurationAttribute con in oChilds)
// item.ConAttributes.Add(con);
// }
// foreach (Configaration item in uiParents)
// {
// List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
// foreach (Configaration.ConfigurationAttribute con in oChilds)
// item.ConAttributes.Add(con);
// }
// foreach (Configaration item in menuParents)
// {
// List<Configaration.ConfigurationAttribute> oChilds = oAllChilds.FindAll(delegate(Configaration.ConfigurationAttribute child) { return child.ConfigurationID == item.ID; });
// foreach (Configaration.ConfigurationAttribute con in oChilds)
// item.ConAttributes.Add(con);
// }
// await Task.WhenAll(oAllParentsAsync, oAllChildsAsync);
// _logicCofiguration = logicParents;
// _uiCofiguration = uiParents;
// _menuCofiguration = menuParents;
//}
//public void LoadConfiguration()
//{
// _logicCofiguration = Configaration.Service.Get(EnumConfigurationType.Logic);
// _uiCofiguration = Configaration.Service.Get(EnumConfigurationType.UI);
// _menuCofiguration = Configaration.Service.Get(EnumConfigurationType.Menu);
//}
public static List<Configaration> LogicCofiguration
{
get
{
return _logicCofiguration;
}
}
public static List<Configaration> UIConfiguration
{
get
{
return _uiCofiguration;
}
}
private void GetChilds(List<Configaration> Source, List<Configaration> destination, Configaration parent)
{
IEnumerable<Configaration> Items = from Cnfgs in Source
where Cnfgs.ParentID == parent.ID
select Cnfgs;
if (Items == null) return;
foreach (var item in Items)
{
destination.Add(item);
GetChilds(Source, destination, item);
}
}
public List<Configaration> GetChilds(string NodeName, EnumConfigurationType type)
{
List<Configaration> destinations = new List<Configaration>();
List<Configaration> SourceObject = getSource(type);
Configaration fParent = null;
if (SourceObject != null)
fParent = SourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); });
if (fParent != null) GetChilds(SourceObject, destinations, fParent);
return destinations;
}
private List<Configaration> getSource(EnumConfigurationType type)
{
List<Configaration> SourceObject = null;
switch (type)
{
case EnumConfigurationType.UI:
SourceObject = _uiCofiguration;
break;
case EnumConfigurationType.Logic:
SourceObject = _logicCofiguration;
break;
case EnumConfigurationType.Menu:
SourceObject = _menuCofiguration;
break;
default:
throw new ServiceException("Invalid configuration type");
break;
}
return SourceObject;
}
public List<Configaration> GetChilds(int configid, EnumConfigurationType type)
{
List<Configaration> destinations = new List<Configaration>();
List<Configaration> SourceObject = getSource(type);
Configaration fParent = SourceObject.Find(delegate(Configaration item) { return (item.ID == configid); });
if (fParent != null) GetChilds(SourceObject, destinations, fParent);
return destinations;
}
public List<Configaration> GetChilds(List<Configaration> sourceObject, string NodeName)
{
List<Configaration> destinations = new List<Configaration>();
Configaration fParent = sourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); });
if (fParent != null) GetChilds(sourceObject, destinations, fParent);
return destinations;
}
public Configaration GetItem(List<Configaration> sourceObject, string NodeName)
{
Configaration fParent = sourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); });
return fParent;
}
/// <summary>
/// if parent and child node not found in the configuration, it will return
/// empty string, because to setup default configuration
/// </summary>
/// <param name="parentNodeName"></param>
/// <param name="childNodeName"></param>
/// <param name="type"></param>
/// <returns></returns>
private static string getValue(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
ConfigurationManager manager = new ConfigurationManager();
List<Configaration> parent = manager.GetChilds(parentNodeName, type);
if (parent == null) return "";//throw new ServiceException("parent-Tag-Name not found in the logic configuration");
Configaration child = manager.GetItem(parent, childNodeName);
if (child == null) return "";//throw new ServiceException("child-node-name not found in the logic collection");
return child.Value;
}
/// <summary>
/// If system does not find the value, it will return '0'.
/// if configuration contain other than integer value value, it will return zero ///
/// </summary>
/// <param name="parentNodeName"> parent node name</param>
/// <param name="childNodeName"> child node name of the parent</param>
/// <param name="type"> logic or UI configuration </param>
/// <returns>boolian value</returns>
public static int GetIntValue(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
int intvalue = 0;
try
{
string stringvalue = ConfigurationManager.getValue(parentNodeName, childNodeName, type);
if (int.TryParse(stringvalue, out intvalue))
return intvalue;
else return 0;
//throw new ServiceException("Value in the "
// + "logic-Configuration; Node:Root, Child-Node:RoundOfDegit, Value:" + stringvalue +" is not Integer");
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
}
public static Configaration Get(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
ConfigurationManager manager = new ConfigurationManager();
List<Configaration> parent = manager.GetChilds(parentNodeName, type);
if (parent == null) return null;
Configaration child = manager.GetItem(parent, childNodeName);
if (child == null) return null;
return child;
}
public static int GetAttributeIntValue(string parentNodeName, string childNodeName, string AttributeName, EnumConfigurationType type)
{
int intvalue = 0;
try
{
Configaration oTag = ConfigurationManager.Get(parentNodeName, childNodeName, type);
if (oTag == null) return 0;
string stringvalue = oTag.ConAttributes.GetAttributeValue(AttributeName);
if (stringvalue == "") return 0;
if (int.TryParse(stringvalue, out intvalue))
return intvalue;
else return 0;
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
}
public static string GetAttributeStringValue(string parentNodeName, string childNodeName, string AttributeName, EnumConfigurationType type)
{
try
{
Configaration oTag = ConfigurationManager.Get(parentNodeName, childNodeName, type);
if (oTag == null) return "";
return oTag.ConAttributes.GetAttributeValue(AttributeName);
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
}
public static string GetStringValue(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
try
{
string stringvalue = ConfigurationManager.getValue(parentNodeName, childNodeName, type);
return stringvalue;
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
}
/// <summary>
/// If system does not find the value, it will return false.
/// if configuration contain other than 'YES' value, it will return false
/// </summary>
/// <param name="parentNodeName"> parent node name</param>
/// <param name="childNodeName"> child node name of the parent</param>
/// <param name="type"> logic or UI configuration </param>
/// <returns>boolian value</returns>
public static bool GetBoolValue(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
try
{
string stringvalue = ConfigurationManager.getValue(parentNodeName, childNodeName, type);
if (stringvalue.ToUpper() == "YES" || stringvalue.ToUpper() == "NO")
return ((stringvalue.ToUpper() == "YES") ? true : false);
else return false;
//throw new ServiceException("Value in the "
// + "logic-Configuration; Node:Root, Child-Node:RoundOfDegit, Value:" + stringvalue + " is not Integer");
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
}
/// <summary>
/// If system does not find the value, it will return min value of dateTime(DateTime.minvalue).
/// if configuration value not dateTime format, it will also return min value of dateTime(DateTime.minvalue).
/// </summary>
/// <param name="parentNodeName"> parent node name</param>
/// <param name="childNodeName"> child node name of the parent</param>
/// <param name="type"> logic or UI configuration </param>
/// <returns>DateTime value</returns>
public static DateTime GetDateTimeValue(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
try
{
DateTime dateValue = DateTime.MinValue;
string stringvalue = ConfigurationManager.getValue(parentNodeName, childNodeName, type);
if (DateTime.TryParse(stringvalue, out dateValue))
return dateValue;
else return dateValue;
//throw new ServiceException("Value in the "
// + "logic-Configuration; Node:Root, Child-Node:RoundOfDegit, Value:" + stringvalue + " is not Integer");
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
}
#region Read configuration from XMLFile to Object
public List<Configaration> LoadXMLtoObject(EnumConfigurationType type, string fileName)
{
XmlDocument xmlDoc = new XmlDocument();
//Load the file into the XmlDocument
List<Configaration> configs = new List<Configaration>();
xmlDoc.Load(fileName);
//Find the root nede, and add it togather with its childeren
XmlNode xnod = xmlDoc.DocumentElement;
// Add Parent node in Collection
Configaration parent = AddToColl(configs, xnod, null, type);
ReadFromXML(configs, xnod, parent, type);
return configs;
}
//public ObjectsTemplate<Configaration> LoadXMLtoObject(EnumConfigurationType type)
//{
// XmlDocument xmlDoc = new XmlDocument();
// //Load the file into the XmlDocument
// ObjectsTemplate<Configaration> configs = new ObjectsTemplate<Configaration>();
// switch (type)
// {
// case EnumConfigurationType.Logic:
// xmlDoc.Load("LogicConfiguration.xml");
// break;
// case EnumConfigurationType.UI:
// xmlDoc.Load("UIConfiguration.xml");
// break;
// case EnumConfigurationType.Menu:
// xmlDoc.Load("Menu.xml");
// break;
// }
// //Find the root nede, and add it togather with its childeren
// XmlNode xnod = xmlDoc.DocumentElement;
// // Add Parent node in Collection
// Configaration parent = AddToColl(configs, xnod, null, type);
// ReadFromXML(configs, xnod, parent, type);
// return configs;
//}
private void ReadFromXML(List<Configaration> configs, XmlNode xnod, Configaration parent, EnumConfigurationType type)
{
XmlNode xnodWorking;
//For an element node, retrieve the attributes
if (xnod.NodeType == XmlNodeType.Element)
{
XmlNamedNodeMap mapAttributes = xnod.Attributes;
if (xnod.HasChildNodes)
{
xnodWorking = xnod.FirstChild;
while (xnodWorking != null)
{
if (xnodWorking.NodeType == XmlNodeType.Element)
{
Configaration oparent = AddToColl(configs, xnodWorking, parent.ID, type);
//Add the attributes to the ListBox
foreach (XmlAttribute xnodAttribute in xnodWorking.Attributes)
oparent.ConAttributes.Add(oparent.ID, xnodAttribute.Name, xnodAttribute.Value);
//If there are any child node, call this procedure recursively
ReadFromXML(configs, xnodWorking, oparent, type);
}
xnodWorking = xnodWorking.NextSibling;
}
}
}
}
private Configaration AddToColl(List<Configaration> configs, XmlNode node, int? parentID, EnumConfigurationType type)
{
Payroll.BO.Configaration oConfig = new Payroll.BO.Configaration();
oConfig.Node = node.Name;
//
if (parentID == null)
oConfig.Value = "";
else if (node.ChildNodes.Count != 1 || (node.ChildNodes.Count == 1 && node.ChildNodes[0].ChildNodes.Count != 0))
{
oConfig.Value = "";
}
else
oConfig.Value = node.InnerText;
oConfig.ParentID = parentID;
oConfig.Type = type;
//oConfig.SetObjectID(ID.FromInteger(configs.Count + 1));
oConfig.SetObjectID(configs.Count + 1);
configs.Add(oConfig);
return oConfig;
}
#endregion Read configuration from Object to XMLFile
#region Write configuration from object to XMLFile
public static void WriteXML(string path, EnumConfigurationType type)
{
List<Configaration> configs;
if (type == EnumConfigurationType.Logic)
configs = _logicCofiguration;
else if (type == EnumConfigurationType.UI)
configs = _uiCofiguration;
else configs = _menuCofiguration;
if (configs == null || configs.Count == 0)
throw new ServiceException("Source is empty");
//XmlTextWriter tw = new XmlTextWriter("C:\\testXml.xml", System.Text.Encoding.UTF8);
//tw.Flush();
//tw.Formatting = Formatting.Indented;
//tw.WriteStartDocument();
//tw.WriteStartElement(configs[0].Node);
//ConfigurationManager.WriteXML(tw, configs, configs[0].ID);
//tw.WriteEndElement();
//tw.WriteEndDocument();
XmlDocument xdoc = new XmlDocument();
XmlElement root = xdoc.CreateElement(configs[0].Node);
xdoc.AppendChild(root);
WriteXML(root, xdoc, configs, configs[0].ID);
xdoc.Save(path);
}
//private static void WriteXML(XmlTextWriter tw, ObjectsTemplate<Configaration> configs, ID NodeId)
//{
// List<Configaration> childs = configs.FindAll((delegate(Configaration item) {return item.ParentID == NodeId;}));
// if (childs !=null || childs.Count> 0)
// {
// foreach(Configaration con in childs)
// {
// tw.WriteStartElement(
// if (con.Value == "" || con.Value == String.Empty) tw.WriteStartElement(con.Node);
// else tw.WriteElementString(con.Node, con.Value);
// foreach(Configaration.ConfigurationAttribute att in con.ConAttributes)
// {
// tw.WriteStartAttribute(att.AttributeName, att.Value);
// }
// WriteXML(tw, configs, con.ID);
// if (con.Value == "" || con.Value == String.Empty) tw.WriteEndElement();
// }
// }
//}
private static void WriteXML(XmlElement element, XmlDocument xdoc, List<Configaration> configs, int NodeId)
{
List<Configaration> childs = configs.FindAll((delegate(Configaration item) { return item.ParentID == NodeId; }));
if (childs != null || childs.Count > 0)
{
foreach (Configaration con in childs)
{
XmlElement chilenode = xdoc.CreateElement(con.Node);
chilenode.InnerText = con.Value;
foreach (Configaration.ConfigurationAttribute att in con.ConAttributes)
{
chilenode.SetAttribute(att.AttributeName, att.Value);
}
element.AppendChild(chilenode);
WriteXML(chilenode, xdoc, configs, con.ID);
}
}
}
#endregion Write configuration from object to XMLFile
}
}