using System; using System.Data; using System.Linq; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.DataAccess; using System.Collections.Generic; using Payroll.BO; using Ease.CoreV35.Caching; namespace Payroll.Service { #region Configaration Service [Serializable] public class ConfigarationService : ServiceTemplate, IConfigarationService { private static Cache _cache = new Cache(typeof(ConfigarationService)); private void MapChildObject(Configaration.ConfigurationAttribute ca, DataReader dr) { ca.ConfigurationID = dr.GetID("ParentID"); ca.AttributeName = dr.GetString("AttributeName"); ca.Value = dr.GetString("Value"); base.SetObjectID(ca, dr.GetID("AttributeID")); base.SetObjectState(ca, ObjectState.Saved); } private Configaration.ConfigurationAttribute CreateChildObject(DataReader dr) { Configaration.ConfigurationAttribute ca = new Configaration.ConfigurationAttribute(); MapChildObject(ca, dr); return ca; } private void FillChildren(Configaration parent, DataReader dr) { while (dr.Read()) { Configaration.ConfigurationAttribute item = this.CreateChildObject(dr); parent.ConAttributes.Add(item); } } private void MapObject(Configaration configuration, DataReader dr) { base.SetObjectID(configuration, dr.GetID("ConfigurationId")); configuration.Node = dr.GetString("Node"); configuration.Value = dr.GetString("Value"); configuration.Type = (EnumConfigurationType)dr.GetInt16("Type").Value; configuration.CreatedBy = dr.GetID("CreatedBy"); configuration.CreatedDate = dr.GetDateTime("CreatedDate").Value; configuration.ModifiedBy = dr.GetID("ModifiedBy"); configuration.ModifiedDate = dr.GetDateTime("ModifiedDate"); configuration.ParentID = dr.GetID("ParentID"); base.SetObjectState(configuration, ObjectState.Saved); } protected override T CreateObject(DataReader dr) { Configaration configuration = new Configaration(); MapObject(configuration, dr); return configuration as T; } public void Save(ObjectsTemplate configurations, EnumConfigurationType type) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); //Delete All Configuration and its Attributes ConfigarationDA.DeleteAll(tc, type); int Id = tc.GenerateID("Configuration", "ConfigurationID"); Id = Id + 2000; foreach (Configaration config in configurations) { this.UpdateParent(config.ID, ID.FromInteger(Id), configurations); config.SetObjectID(ID.FromInteger(Id)); Id = Id + 1; } foreach (Configaration config in configurations) { //Save Configuration config.Type = type; ConfigarationDA.Insert(tc, config); //Save Children foreach (var item in config.ConAttributes) { item.ConfigurationID = config.ID; int id = tc.GenerateID("ConfigurationAttribute", "AttributeID"); item.SetID(ID.FromInteger(id)); ConfigarationDA.Insert(tc, item); } } 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(ID oldId, ID currentID, ObjectsTemplate configurations) { foreach (Configaration config in configurations) { if (config.ParentID == oldId) { config.ParentID = currentID; } } } public void Delete(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); ConfigarationDA.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 Configaration Get(ID id) { #region Cache Header Configaration configuration = _cache["Get", id] as Configaration; if (configuration != null) return configuration; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.Get(tc, id)); configuration = this.loadItem(dr); dr.Close(); if (configuration != null && configuration.ID != null && configuration.ID.Integer != 0) { dr = new DataReader(ConfigarationDA.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 } #region Cache Footer _cache.Add(configuration, "Get", id); #endregion return configuration; } Configaration loadItem(DataReader dr) { Configaration configuration = null; try { if (dr.Read()) configuration = this.CreateObject(dr); } catch (Exception e) { #region Handle Exception throw new ServiceException(e.Message, e); #endregion } return configuration; } public ObjectsTemplate GetAllParent() { #region Cache Header ObjectsTemplate configurations = _cache["GetAllParent"] as ObjectsTemplate; if (configurations != null) return configurations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.GetAllParent(tc)); configurations = this.CreateObjects(dr); dr.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } #region Cache Footer _cache.Add(configurations, "GetAllParent"); #endregion return configurations; } public ObjectsTemplate GetAllParent(EnumConfigurationType type) { #region Cache Header ObjectsTemplate configurations = _cache["GetAllParent"] as ObjectsTemplate; if (configurations != null) return configurations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.GetAllParent(tc, type)); configurations = this.CreateObjects(dr); dr.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } #region Cache Footer _cache.Add(configurations, "GetAllParent"); #endregion return configurations; } public ObjectsTemplate GetAllChildrens() { Configaration.ConfigurationAttributes items = new Configaration.ConfigurationAttributes(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.GetAllChildrens(tc)); while (dr.Read()) { Configaration.ConfigurationAttribute ca = new Configaration.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 ObjectsTemplate Get(EnumConfigurationType configType) { #region Cache Header ObjectsTemplate configurations = _cache["Get", configType] as ObjectsTemplate; if (configurations != null) return configurations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.Get(tc, configType)); configurations = this.CreateObjects(dr); dr.Close(); foreach (Configaration item in configurations) { dr = new DataReader(ConfigarationDA.GetChildren(tc, item.ID)); FillChildren(item, 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 } #region Cache Footer _cache.Add(configurations, "Get", configType); #endregion return configurations; } public ObjectsTemplate GetAttributesByParentID(int nParentID) { Configaration.ConfigurationAttributes items = new Configaration.ConfigurationAttributes(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.GetChildrenByNode(tc, nParentID)); while (dr.Read()) { Configaration.ConfigurationAttribute ca = new Configaration.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 ObjectsTemplate GetAttributesByValue(string sMenuKey) { Configaration.ConfigurationAttributes items = new Configaration.ConfigurationAttributes(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.GetChildrenByValue(tc, sMenuKey)); while (dr.Read()) { Configaration.ConfigurationAttribute ca = new Configaration.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 ObjectsTemplate GetAllChildren() { Configaration.ConfigurationAttributes items = new Configaration.ConfigurationAttributes(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ConfigarationDA.GetAllChildren(tc)); while (dr.Read()) { Configaration.ConfigurationAttribute ca = new Configaration.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; } } #endregion }