EchoTex_Payroll/HRM.DA/Service/Common/SystemConfigurationService.cs

1222 lines
42 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using System;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using System.Collections.Generic;
using HRM.BO;
using System.Data;
using System.Xml;
using System.Linq;
using System.IO;
namespace HRM.DA
{
#region Configaration Service
public class SystemConfigarationService : ServiceTemplate, ISystemConfigaration
{
int _rolePermissinID = 0;
private void MapChildObject(SystemConfigaration.ConfigurationAttribute ca, DataReader dr)
{
ca.ConfigurationID = dr.GetInt32("ParentID").Value;
ca.AttributeName = dr.GetString("AttributeName");
ca.Value = dr.GetString("Value");
base.SetObjectID(ca, dr.GetInt32("AttributeID").Value);
base.SetObjectState(ca, Ease.Core.ObjectState.Saved);
}
private SystemConfigaration.ConfigurationAttribute CreateChildObject(DataReader dr)
{
SystemConfigaration.ConfigurationAttribute ca = new SystemConfigaration.ConfigurationAttribute();
MapChildObject(ca, dr);
return ca;
}
private void FillChildren(SystemConfigaration parent, DataReader dr)
{
while (dr.Read())
{
SystemConfigaration.ConfigurationAttribute item = this.CreateChildObject(dr);
parent.ConAttributes.Add(item);
}
}
private List<SystemConfigaration.ConfigurationAttribute> CreateAttribures(DataReader dr)
{
List<SystemConfigaration.ConfigurationAttribute> oatt =
new List<SystemConfigaration.ConfigurationAttribute>();
while (dr.Read())
{
SystemConfigaration.ConfigurationAttribute item = this.CreateChildObject(dr);
oatt.Add(item);
}
return oatt;
}
private void MapObject(SystemConfigaration configuration, DataReader dr)
{
base.SetObjectID(configuration, dr.GetInt32("ConfigurationId").Value);
configuration.Node = dr.GetString("Node");
configuration.Value = dr.GetString("Value");
configuration.Type = (EnumConfigurationType)dr.GetInt16("Type").Value;
configuration.CreatedBy = dr.GetInt32("CreatedBy").Value;
configuration.CreatedDate = dr.GetDateTime("CreatedDate").Value;
configuration.ModifiedBy = dr.GetInt32("ModifiedBy");
configuration.ModifiedDate = dr.GetDateTime("ModifiedDate");
configuration.ParentID = dr.GetInt32("ParentID", 0);
base.SetObjectState(configuration, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader dr)
{
SystemConfigaration configuration = new SystemConfigaration();
MapObject(configuration, dr);
return configuration as T;
}
public void Save(List<SystemConfigaration> configurations, EnumConfigurationType type)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
//Delete All Configuration and its Attributes
SystemConfigarationDA.DeleteAll(tc, type);
int Id = tc.GenerateID("Configuration", "ConfigurationID");
Id = Id + 2000;
int configattID = tc.GenerateID("ConfigurationAttribute", "AttributeID");
foreach (SystemConfigaration config in configurations)
{
this.UpdateParent(config.ID, Id, configurations);
config.SetObjectID(Id);
Id = Id + 1;
}
foreach (SystemConfigaration config in configurations)
{
//Insert Configuration
config.Type = type;
SystemConfigarationDA.Insert(tc, config);
//Insert Children
foreach (var item in config.ConAttributes)
{
item.ConfigurationID = config.ID;
configattID = configattID + 1;
item.ID = configattID;
SystemConfigarationDA.Insert(tc, item);
}
}
if (type == EnumConfigurationType.Menu)
{
SystemConfigarationDA.UpdateRolePermission(tc);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
private void UpdateParent(int oldId, int currentID, List<SystemConfigaration> configurations)
{
foreach (SystemConfigaration config in configurations)
{
if (config.ParentID == oldId)
{
config.ParentID = currentID;
}
}
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
SystemConfigarationDA.Delete(tc, id);
tc.End();
//CacheInfo.ClearCache(typeof(CountryService).FullName);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public SystemConfigaration Get(int id)
{
SystemConfigaration configuration = null;
;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.Get(tc, id));
configuration = this.loadItem(dr);
dr.Close();
if (configuration != null && configuration.ID != null && configuration.ID != 0)
{
dr = new DataReader(SystemConfigarationDA.GetChildren(tc, id));
FillChildren(configuration, dr);
dr.Close();
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return configuration;
}
SystemConfigaration loadItem(DataReader dr)
{
SystemConfigaration configuration = null;
try
{
if (dr.Read())
configuration = this.CreateObject<SystemConfigaration>(dr);
}
catch (Exception e)
{
#region Handle Exception
throw new ServiceException(e.Message, e);
#endregion
}
return configuration;
}
public List<SystemConfigaration> GetAllParent()
{
List<SystemConfigaration> configurations = new List<SystemConfigaration>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.GetAllParent(tc));
configurations = this.CreateObjects<SystemConfigaration>(dr);
dr.Close();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return configurations;
}
public List<SystemConfigaration> GetAllParent(EnumConfigurationType type)
{
#region Cache Header
List<SystemConfigaration> configurations = new List<SystemConfigaration>();
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.GetAllParent(tc, type));
configurations = this.CreateObjects<SystemConfigaration>(dr);
dr.Close();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return configurations;
}
public List<SystemConfigaration.ConfigurationAttribute> GetAllChildrens()
{
SystemConfigaration.ConfigurationAttributes items = new SystemConfigaration.ConfigurationAttributes();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.GetAllChildrens(tc));
while (dr.Read())
{
SystemConfigaration.ConfigurationAttribute ca = new SystemConfigaration.ConfigurationAttribute();
MapChildObject(ca, dr);
items.Add(ca);
}
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<SystemConfigaration> Get(EnumConfigurationType configType)
{
//List<SystemConfigaration> configurations = new List<SystemConfigaration>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.Get(tc, configType));
var configurations = this.CreateObjects<SystemConfigaration>(dr);
dr.Close();
dr = new DataReader(SystemConfigarationDA.GetChildrenbyType(tc, configType));
var atrr = this.CreateAttribures(dr);
dr.Close();
configurations.ForEach(x =>
{
var atr = atrr.FindAll(a => a.ConfigurationID == x.ID);
if (atr != null)
{
x.ConAttributes.AddRange(atr);
}
});
//foreach (SystemConfigaration item in configurations)
//{
// dr = new DataReader(SystemConfigarationDA.GetChildren(tc, item.ID));
// FillChildren(item, dr);
// dr.Close();
//}
tc.End();
return configurations;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<SystemConfigaration.ConfigurationAttribute> GetAttributesByParentID(int nParentID)
{
SystemConfigaration.ConfigurationAttributes items = new SystemConfigaration.ConfigurationAttributes();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.GetChildrenByNode(tc, nParentID));
while (dr.Read())
{
SystemConfigaration.ConfigurationAttribute ca = new SystemConfigaration.ConfigurationAttribute();
MapChildObject(ca, dr);
items.Add(ca);
}
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<SystemConfigaration.ConfigurationAttribute> GetAttributesByValue(string sMenuKey)
{
SystemConfigaration.ConfigurationAttributes items = new SystemConfigaration.ConfigurationAttributes();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.GetChildrenByValue(tc, sMenuKey));
while (dr.Read())
{
SystemConfigaration.ConfigurationAttribute ca = new SystemConfigaration.ConfigurationAttribute();
MapChildObject(ca, dr);
items.Add(ca);
}
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<SystemConfigaration.ConfigurationAttribute> GetAllChildren()
{
SystemConfigaration.ConfigurationAttributes items = new SystemConfigaration.ConfigurationAttributes();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.GetAllChildren(tc));
while (dr.Read())
{
SystemConfigaration.ConfigurationAttribute ca = new SystemConfigaration.ConfigurationAttribute();
MapChildObject(ca, dr);
items.Add(ca);
}
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<SystemConfigaration> GetAllChildrenByParentNode(EnumConfigurationType type, string parentNode)
{
List<SystemConfigaration> oConfigList = new List<SystemConfigaration>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SystemConfigarationDA.GetAllChildrenByParentNode(tc, type, parentNode));
oConfigList = this.CreateObjects<SystemConfigaration>(oreader);
oreader.Close();
if (oConfigList.Count > 0)
{
string parentid = "";
oConfigList.ForEach(x =>
{
parentid = parentid + x.ID.ToString() + ", ";
});
parentid = parentid.Substring(0, parentid.Length - 2);
oreader = new DataReader(SystemConfigarationDA.GetallChildren(tc, parentid));
var olist = CreateAttribures(oreader);
// FillChildren(ca, oreader);
oConfigList.ForEach(x =>
{
var childs = olist.FindAll(a => a.ConfigurationID == x.ID);
if(childs!=null)
x.ConAttributes.AddRange(childs);
});
oreader.Close();
}
foreach (var ca in oConfigList)
{
}
tc.End();
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
return oConfigList;
}
public object GetconfigValue(EnumConfigurationType type, string firstParent, string secondParent)
{
object returnvalue = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
returnvalue =SystemConfigarationDA.getconfigValue(tc, type, firstParent,secondParent);
tc.End();
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
return returnvalue;
}
public int GetconfigIntValue(EnumConfigurationType type, string firstParent, string secondParent)
{
object returnvalue = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
returnvalue = SystemConfigarationDA.getconfigValue(tc, type, firstParent, secondParent);
tc.End();
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
return Convert.ToInt32( returnvalue);
}
public double GetconfigDoubleValue(EnumConfigurationType type, string firstParent, string secondParent)
{
object returnvalue = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
returnvalue = SystemConfigarationDA.getconfigValue(tc, type, firstParent, secondParent);
tc.End();
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
return Convert.ToDouble(returnvalue);
}
public string GetconfigStringValue(EnumConfigurationType type, string firstParent, string secondParent)
{
object returnvalue = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
returnvalue = SystemConfigarationDA.getconfigValue(tc, type, firstParent, secondParent);
tc.End();
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
if (returnvalue == null)
returnvalue = "";
return Convert.ToString(returnvalue);
}
public bool GetconfigBooleanValue(EnumConfigurationType type, string firstParent, string secondParent)
{
bool returnvalue = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
object rvalue = SystemConfigarationDA.getconfigValue(tc, type, firstParent, secondParent);
if (rvalue !=null && ((string)rvalue).ToUpper() =="YES")
{
returnvalue = true;
}
tc.End();
}
catch (Exception ex)
{
throw new ServiceException(ex.InnerException.Message);
}
return returnvalue;
}
public List<SystemConfigaration> GetChilds(List<SystemConfigaration> sourceObject, string NodeName)
{
List<SystemConfigaration> destinations = new List<SystemConfigaration>();
SystemConfigaration fParent = sourceObject.Find(delegate (SystemConfigaration item) { return (item.Node == NodeName); });
if (fParent != null) GetChilds(sourceObject, destinations, fParent);
return destinations;
}
private void GetChilds(List<SystemConfigaration> Source, List<SystemConfigaration> destination, SystemConfigaration parent)
{
IEnumerable<SystemConfigaration> 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);
}
}
#region Menu Creation
private bool IsParent(SystemConfigaration con)
{
if (con.ConAttributes.Count > 4 && con.ConAttributes[3].Value.Length == 0)
{
return true;
}
return false;
}
private bool IsChield(SystemConfigaration c, SystemConfigaration con)
{
if (c.ConAttributes.Count > 4 && c.ConAttributes[3].Value.ToString().ToLower()
.Equals(con.ConAttributes[0].Value.ToString().ToLower()))
{
{
return true;
}
}
return false;
}
public List<SystemConfigaration> GetMenu(EnumConfigurationType configType)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SystemConfigarationDA.Get(tc, configType));
var configurations = this.CreateObjects<SystemConfigaration>(dr);
dr.Close();
dr = new DataReader(SystemConfigarationDA.GetChildren(tc));
var configChildrens = new List<SystemConfigaration.ConfigurationAttribute>();
while (dr.Read())
{
configChildrens.Add(CreateChildObject(dr));
}
dr.Close();
foreach (SystemConfigaration item in configurations)
{
item.ConAttributes.AddRange(configChildrens.FindAll(x => x.ConfigurationID == item.ID));
}
tc.End();
return configurations;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
private void Recure(List<SystemConfigaration> mainSource, TreeNode node, SystemConfigaration con)
{
List<SystemConfigaration> chields = mainSource.FindAll(delegate (SystemConfigaration c)
{
return IsChield(c, con);
});
foreach (SystemConfigaration chield in chields)
{
TreeNode cnode = new TreeNode();
cnode.Text = chield.ConAttributes[1].Value.ToString();
cnode.Tag = chield.ConAttributes[0].Value.ToString();
Recure(mainSource, cnode, chield);
node.Nodes.Add(cnode);
}
}
public List<TreeNode> LoadMenu()
{
List<SystemConfigaration> MenuCofiguration = this.GetMenu(EnumConfigurationType.Menu);
List<SystemConfigaration> parents = MenuCofiguration.FindAll(delegate (SystemConfigaration con)
{
return IsParent(con);
});
List<TreeNode> tvMenues = new List<TreeNode>();
foreach (SystemConfigaration con in parents)
{
TreeNode node = new TreeNode();
node.Text = con.ConAttributes[1].Value.ToString();
node.Tag = con.ConAttributes[0].Value.ToString();
Recure(MenuCofiguration, node, con);
tvMenues.Add(node);
}
return tvMenues;
}
public List<Role.RolePermission> getMenuesFromConfig()
{
List<SystemConfigaration> MenuCofiguration = this.GetMenu(EnumConfigurationType.Menu);
List<SystemConfigaration> parents = MenuCofiguration.FindAll(delegate (SystemConfigaration con)
{
return IsParent(con);
});
List<Role.RolePermission> tvMenues = new List<Role.RolePermission>();
_rolePermissinID = 0;
foreach (SystemConfigaration con in parents)
{
_rolePermissinID = _rolePermissinID + 1;
if (con.ConAttributes.Count < 5)
{
continue;
}
Role.RolePermission node = new Role.RolePermission();
node.Name = con.ConAttributes[0].Value.ToString();
node.Name = con.ConAttributes[1].Value.ToString();
node.RoleType = Role.RolePermission.GetRoletype(con.ConAttributes[5].Value.ToString());
node.PermissionCode = con.ConAttributes[0].Value.ToString();
node.ID = tvMenues.Count + 1;
tvMenues.Add(node);
RecureRole(MenuCofiguration, tvMenues, node, 2);
}
return tvMenues;
}
private bool IsChield(SystemConfigaration c, Role.RolePermission parent)
{
if (c.ConAttributes.Count > 4 &&
c.ConAttributes[3].Value.ToString().ToLower().Equals(parent.PermissionCode))
{
{
return true;
}
}
return false;
}
private void RecureRole(List<SystemConfigaration> mainSource, List<Role.RolePermission> allpermissions,
Role.RolePermission parent, int nextid)
{
List<SystemConfigaration> chields = mainSource.FindAll(delegate (SystemConfigaration c)
{
return IsChield(c, parent);
});
foreach (SystemConfigaration chield in chields)
{
if (chield.ConAttributes.Count < 5)
{
continue;
}
_rolePermissinID = _rolePermissinID + 1;
Role.RolePermission node = new Role.RolePermission();
node.Name = chield.ConAttributes[1].Value.ToString();
node.PermissionCode = chield.ConAttributes[0].Value.ToString();
node.Link = chield.ConAttributes[2].Value.ToString();
node.parentID = parent.ID;
node.RoleType = parent.RoleType;
nextid = allpermissions.Count + 1;
node.ID = allpermissions.Count + 1;
allpermissions.Add(node);
RecureRole(mainSource, allpermissions, node, nextid);
}
}
public List<SystemConfigaration> LoadXMLtoObject(EnumConfigurationType type, System.IO.Stream fileName)
{
ConfigurationManager omanager = new ConfigurationManager();
return omanager.LoadXMLtoObject(type, fileName);
}
public string GetXMLFile(EnumConfigurationType type)
{
return new ConfigurationManager().GetXML(type);
}
#endregion
}
#endregion
public class ConfigurationManager
{
public ConfigurationManager()
{
_logicCofiguration = new List<SystemConfigaration>();
_uiCofiguration = new List<SystemConfigaration>();
_menuCofiguration = new List<SystemConfigaration>();
}
private List<SystemConfigaration> _logicCofiguration { get; set; }
private List<SystemConfigaration> _uiCofiguration { get; set; }
private List<SystemConfigaration> _menuCofiguration { get; set; }
//public List<SystemConfigaration> LogicConfiguration
//{
// get
// {
// return _logicCofiguration;
// }
//}
//public List<SystemConfigaration> UICofiguration
//{
// get
// {
// return _uiCofiguration;
// }
//}
//public List<SystemConfigaration> MenuCofiguration
//{
// get
// {
// return _menuCofiguration;
// }
//}
public void SetStaticConfiguration(List<SystemConfigaration> 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;
}
}
private List<SystemConfigaration> getSource(EnumConfigurationType type)
{
List<SystemConfigaration> 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");
}
return SourceObject;
}
public SystemConfigaration GetItem(List<SystemConfigaration> sourceObject, string NodeName)
{
SystemConfigaration fParent = sourceObject.Find(delegate (SystemConfigaration item)
{
return (item.Node == NodeName);
});
return fParent;
}
#region Read configuration from XMLFile to Object
public List<SystemConfigaration> LoadXMLtoObject(EnumConfigurationType type, string fileName)
{
XmlDocument xmlDoc = new XmlDocument();
//Load the file into the XmlDocument
List<SystemConfigaration> configs = new List<SystemConfigaration>();
xmlDoc.Load(fileName);
//Find the root nede, and add it togather with its childeren
XmlNode xnod = xmlDoc.DocumentElement;
// Add Parent node in Collection
SystemConfigaration parent = AddToColl(configs, xnod, 0, type);
ReadFromXML(configs, xnod, parent, type);
return configs;
}
public List<SystemConfigaration> LoadXMLtoObject(EnumConfigurationType type, System.IO.Stream fileName)
{
XmlDocument xmlDoc = new XmlDocument();
//Load the file into the XmlDocument
List<SystemConfigaration> configs = new List<SystemConfigaration>();
xmlDoc.Load(fileName);
//Find the root nede, and add it togather with its childeren
XmlNode xnod = xmlDoc.DocumentElement;
// Add Parent node in Collection
SystemConfigaration parent = AddToColl(configs, xnod, 0, type);
ReadFromXML(configs, xnod, parent, type);
return configs;
}
private void ReadFromXML(List<SystemConfigaration> configs, XmlNode xnod, SystemConfigaration 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)
{
SystemConfigaration 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 SystemConfigaration AddToColl(List<SystemConfigaration> configs, XmlNode node, int parentID,
EnumConfigurationType type)
{
SystemConfigaration oConfig = new SystemConfigaration();
oConfig.Node = node.Name;
//
if (parentID <= 0)
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(configs.Count + 1);
configs.Add(oConfig);
return oConfig;
}
#endregion Read configuration from Object to XMLFile
#region Write configuration from object to XMLFile
public void WriteXML(string path, EnumConfigurationType type)
{
List<SystemConfigaration> configs = null;
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);
}
public string GetXML( EnumConfigurationType type)
{
List<SystemConfigaration> configs = new SystemConfigarationService().Get(type);
if (configs == null || configs.Count == 0)
throw new ServiceException("Source is empty");
XmlDocument xdoc = new XmlDocument();
XmlElement root = xdoc.CreateElement(configs[0].Node);
xdoc.AppendChild(root);
WriteXML(root, xdoc, configs, configs[0].ID);
// xdoc.Save(path);
string sxml = xdoc.InnerXml;
return sxml;
}
private static void WriteXML(XmlElement element, XmlDocument xdoc, List<SystemConfigaration> configs,
int nodeId)
{
List<SystemConfigaration> childs = configs.Where(x => x.ParentID == nodeId).ToList();
if (childs != null || childs.Count > 0)
{
foreach (SystemConfigaration con in childs)
{
XmlElement chilenode = xdoc.CreateElement(con.Node);
chilenode.InnerText = con.Value;
foreach (SystemConfigaration.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
private void GetChilds(List<SystemConfigaration> Source, List<SystemConfigaration> destination,
SystemConfigaration parent)
{
IEnumerable<SystemConfigaration> 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 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);
}
}
public List<SystemConfigaration> GetChilds(string NodeName, EnumConfigurationType type)
{
List<SystemConfigaration> destinations = new List<SystemConfigaration>();
List<SystemConfigaration> SourceObject = getSource(type);
SystemConfigaration fParent = null;
if (SourceObject != null)
fParent = SourceObject.Find(delegate (SystemConfigaration item) { return (item.Node == NodeName); });
if (fParent != null) GetChilds(SourceObject, destinations, fParent);
return destinations;
}
private static string getValue(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
ConfigurationManager manager = new ConfigurationManager();
List<SystemConfigaration> parent = manager.GetChilds(parentNodeName, type);
if (parent == null)
return ""; //throw new ServiceException("parent-Tag-Name not found in the logic configuration");
SystemConfigaration child = manager.GetItem(parent, childNodeName);
if (child == null)
return ""; //throw new ServiceException("child-node-name not found in the logic collection");
return child.Value;
}
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");
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);
}
}
public static SystemConfigaration Get(string parentNodeName, string childNodeName, EnumConfigurationType type)
{
ConfigurationManager manager = new ConfigurationManager();
List<SystemConfigaration> parent = manager.GetChilds(parentNodeName, type);
if (parent == null) return null;
SystemConfigaration 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
{
SystemConfigaration 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 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);
}
}
}
}