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("Node", _node, value); _node = value; } } #endregion #region Value : string private string _value; public string Value { get { return _value; } set { _value = value; } //set //{ // base.OnPropertyChange("Value", _value, value); // _value = value; //} } #endregion #region ParentID : ID private int? _parentID; public int? ParentID { get { return _parentID; } set { // base.OnPropertyChange("ParentID", _parentID, value); _parentID = value; } } #endregion #region Type : EnumConfigurationType private EnumConfigurationType _type; public EnumConfigurationType Type { get { return _type; } set { // base.OnPropertyChange("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("configurationID", _configurationID, value); _configurationID = value; } } #endregion #region AttributeName : string private string _attributeName; public string AttributeName { get { return _attributeName; } set { // base.OnPropertyChange("AttributeName", _attributeName, value); _attributeName = value; } } #endregion #region Value : String private string _value; public string Value { get { return _value; } set { // base.OnPropertyChange("Value", _value, value); _value = value; } } #endregion public new void SetID(int id) { base.SetID(id); } } #endregion #region ConfigurationAttributes [Serializable] public class ConfigurationAttributes : List { #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(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 Get(EnumConfigurationType configType) { //#region Cache Header //List cofigurations = _cache["Get", configType] as List; //if (cofigurations != null) // return cofigurations; //#endregion List cofigurations = new List(); 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 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 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 GetAttributesByParentID(int p) { //#region Cache Header //ObjectsTemplate cofigurations = _cache["GetAttributesByParentID", p] as ObjectsTemplate; //if (cofigurations != null) // return cofigurations; //#endregion List cofigurations = new List(); 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 GetAllChildren() { return Configaration.Service.GetAllChildren(); } } #endregion #region IConfigaration Service public interface IConfigarationService { Configaration Get(int id); List Get(EnumConfigurationType confictype); List GetAllChildren(); //ObjectsTemplate GetAllChildrens(); List GetAllChildrens(string sApplicationType); List GetAllParent(EnumConfigurationType type); List GetAllParent(); void Save(List configurations, EnumConfigurationType confictype); void Delete(int id); List GetAttributesByParentID(int p); List GetAllChildrens(); } #endregion public class ConfigurationManager { public ConfigurationManager() { } private static List _logicCofiguration; private static List _uiCofiguration; private static List _menuCofiguration; public List LogicConfiguration { get { return _logicCofiguration; } } public List UICofiguration { get { return _uiCofiguration; } } public List GetAllChildren() { return Configaration.Service.GetAllChildren(); } public List GetAllChildrens() { return Configaration.Service.GetAllChildrens(); } public List GetAllChildrens(string sApplicationType) { return Configaration.Service.GetAllChildrens(sApplicationType); } public List GetAllParent(EnumConfigurationType type) { return Configaration.Service.GetAllParent(type); } public List GetAllParent() { return Configaration.Service.GetAllParent(); } public List MenuCofiguration { get { return _menuCofiguration; } } public void SetStaticConfiguration(List 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 oAllParents = this.GetAllParent(); List oAllChilds = this.GetAllChildrens(); List logicParents = new List(); List uiParents = new List(); List menuParents = new List(); 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 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 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 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 oAllParents = this.GetAllParent(EnumConfigurationType.Menu); List oAllChilds = this.GetAllChildrens(mesApplicationType); List logicParents = new List(); List uiParents = new List(); List menuParents = new List(); 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 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 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 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 logicParents = new ObjectsTemplate(); // ObjectsTemplate uiParents = new ObjectsTemplate(); // ObjectsTemplate menuParents = new ObjectsTemplate(); // Task> oAllParentsAsync = Task.Run(() => this.GetAllParent(EnumConfigurationType.Menu)); // Task> oAllChildsAsync = Task.Run(() => this.GetAllChildrens(mesApplicationType)); // List 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 oAllChilds = await oAllChildsAsync; // foreach (Configaration item in logicParents) // { // List 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 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 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 LogicCofiguration { get { return _logicCofiguration; } } public static List UIConfiguration { get { return _uiCofiguration; } } private void GetChilds(List Source, List destination, Configaration parent) { IEnumerable 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 GetChilds(string NodeName, EnumConfigurationType type) { List destinations = new List(); List 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 getSource(EnumConfigurationType type) { List 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 GetChilds(int configid, EnumConfigurationType type) { List destinations = new List(); List 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 GetChilds(List sourceObject, string NodeName) { List destinations = new List(); Configaration fParent = sourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); }); if (fParent != null) GetChilds(sourceObject, destinations, fParent); return destinations; } public Configaration GetItem(List sourceObject, string NodeName) { Configaration fParent = sourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); }); return fParent; } /// /// if parent and child node not found in the configuration, it will return /// empty string, because to setup default configuration /// /// /// /// /// private static string getValue(string parentNodeName, string childNodeName, EnumConfigurationType type) { ConfigurationManager manager = new ConfigurationManager(); List 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; } /// /// If system does not find the value, it will return '0'. /// if configuration contain other than integer value value, it will return zero /// /// /// parent node name /// child node name of the parent /// logic or UI configuration /// boolian value 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 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); } } /// /// If system does not find the value, it will return false. /// if configuration contain other than 'YES' value, it will return false /// /// parent node name /// child node name of the parent /// logic or UI configuration /// boolian 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") ? 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); } } /// /// 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). /// /// parent node name /// child node name of the parent /// logic or UI configuration /// DateTime value 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 LoadXMLtoObject(EnumConfigurationType type, string fileName) { XmlDocument xmlDoc = new XmlDocument(); //Load the file into the XmlDocument List configs = new List(); 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 LoadXMLtoObject(EnumConfigurationType type) //{ // XmlDocument xmlDoc = new XmlDocument(); // //Load the file into the XmlDocument // ObjectsTemplate configs = new ObjectsTemplate(); // 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 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 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 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 configs, ID NodeId) //{ // List 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 configs, int NodeId) { List 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 } }