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; 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 { base.OnPropertyChange("Value", _value, value); _value = value; } } #endregion #region ParentID : ID private ID _parentID; public ID 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(ID configurationID) { _configurationID = configurationID; _value = ""; _attributeName = ""; } #endregion #region Property ParentID : ID private ID _configurationID; public ID 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(ID id) { base.SetID(id); } } #endregion #region ConfigurationAttributes [Serializable] public class ConfigurationAttributes : ObjectsTemplate { #region Constructor public ConfigurationAttributes() { } public ConfigurationAttribute GetItemByParentID(ID configurationID) { return this.Find(delegate(ConfigurationAttribute item) { return item.ConfigurationID == configurationID; }); } public void Add(ID configurationId, string nodeName, string svalue) { ConfigurationAttribute att = new ConfigurationAttribute(); att.Value = svalue; att.AttributeName = nodeName; att.SetID(ID.FromInteger(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; } } 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(ID id) { #region Cache Header Configaration configuration = _cache["Get", id] as 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 ObjectsTemplate Get(EnumConfigurationType configType) { #region Cache Header ObjectsTemplate cofigurations = _cache["Get", configType] as ObjectsTemplate; if (cofigurations != null) return cofigurations; #endregion 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(ObjectsTemplate 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(ObjectsTemplate 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(ID attributeID) { try { Service.Delete(attributeID); //Clear Cache //CacheInfo.ClearCache(typeof(Country).FullName); } catch (ServiceException e) { throw new Exception(e.Message, e); } } public static ObjectsTemplate GetAttributesByParentID(int nParentID) { ObjectsTemplate cofigurations = new ObjectsTemplate(); try { cofigurations = Service.GetAttributesByParentID(nParentID); } catch (ServiceException e) { throw new Exception(e.Message, e); } return cofigurations; } public ObjectsTemplate GetAllChildren() { return Configaration.Service.GetAllChildren(); } #endregion public void SetObjectID(ID id) { base.SetID(id); } } #endregion #region IConfigaration Service public interface IConfigarationService { Configaration Get(ID id); ObjectsTemplate Get(EnumConfigurationType confictype); void Save(ObjectsTemplate configurations, EnumConfigurationType confictype); void Delete(ID id); ObjectsTemplate GetAllChildrens(); ObjectsTemplate GetAllChildren(); ObjectsTemplate GetAllParent(EnumConfigurationType type); ObjectsTemplate GetAllParent(); ObjectsTemplate GetAttributesByParentID(int nParentID); } #endregion [Serializable] public class ConfigurationManager { public ConfigurationManager() { } private static ObjectsTemplate _logicCofiguration; private static ObjectsTemplate _uiCofiguration; private static ObjectsTemplate _menuCofiguration; public ObjectsTemplate LogicConfiguration { get { return _logicCofiguration; } } public ObjectsTemplate UICofiguration { get { return _uiCofiguration; } } public ObjectsTemplate MenuCofiguration { get { return _menuCofiguration; } } public void SetStaticConfiguration(ObjectsTemplate 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() //{ // _logicCofiguration = Configaration.Service.Get(EnumConfigurationType.Logic); // _uiCofiguration = Configaration.Service.Get(EnumConfigurationType.UI); // _menuCofiguration = Configaration.Service.Get(EnumConfigurationType.Menu); //} public void LoadConfiguration() { List oAllParents = this.GetAllParent(); List oAllChilds = this.GetAllChildrens(); ObjectsTemplate logicParents = new ObjectsTemplate(); ObjectsTemplate uiParents = new ObjectsTemplate(); ObjectsTemplate menuParents = new ObjectsTemplate(); 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 static ObjectsTemplate LogicCofiguration { get { return _logicCofiguration; } } public ObjectsTemplate GetAllParent(EnumConfigurationType type) { return Configaration.Service.GetAllParent(type); } public ObjectsTemplate GetAllParent() { return Configaration.Service.GetAllParent(); } public ObjectsTemplate GetAllChildrens() { return Configaration.Service.GetAllChildrens(); } public static ObjectsTemplate UIConfiguration { get { return _uiCofiguration; } } private void GetChilds(ObjectsTemplate Source, ObjectsTemplate 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 ObjectsTemplate GetChilds(string NodeName , EnumConfigurationType type) { ObjectsTemplate destinations = new ObjectsTemplate(); ObjectsTemplate SourceObject = getSource(type); Configaration fParent = SourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); }); if (fParent != null) GetChilds(SourceObject, destinations, fParent); return destinations; } private ObjectsTemplate getSource(EnumConfigurationType type) { ObjectsTemplate 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 ObjectsTemplate GetChilds(ID configid, EnumConfigurationType type) { ObjectsTemplate destinations = new ObjectsTemplate(); ObjectsTemplate SourceObject = getSource(type); Configaration fParent = SourceObject.Find(delegate(Configaration item) { return (item.ID == configid); }); if (fParent != null) GetChilds(SourceObject, destinations, fParent); return destinations; } public ObjectsTemplate GetChilds(ObjectsTemplate sourceObject, string NodeName) { ObjectsTemplate destinations = new ObjectsTemplate(); Configaration fParent = sourceObject.Find(delegate(Configaration item) { return (item.Node == NodeName); }); if (fParent != null) GetChilds(sourceObject, destinations, fParent); return destinations; } public Configaration GetItem(ObjectsTemplate 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(); ObjectsTemplate 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(); ObjectsTemplate 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 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 ObjectsTemplate LoadXMLtoObject(EnumConfigurationType type, string fileName) { XmlDocument xmlDoc = new XmlDocument(); //Load the file into the XmlDocument ObjectsTemplate configs = new ObjectsTemplate(); 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(ObjectsTemplate 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(ObjectsTemplate configs, XmlNode node, ID 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)); 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) { ObjectsTemplate 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, 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) { 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 } }