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 Costcenter Service [Serializable] public class CostcenterService : ServiceTemplate, ICostcenterService { #region Private functions and declaration Cache _cache = new Cache(typeof(Costcenter)); #endregion public CostcenterService() { } private void MapObject(Costcenter oCostcenter, DataReader oReader) { base.SetObjectID(oCostcenter, oReader.GetID("CrgID")); oCostcenter.Code = oReader.GetString("code"); oCostcenter.Name = oReader.GetString("description"); oCostcenter.ProfitCenterName = oReader.GetString("ProfitCenterName"); oCostcenter.ParentID = oReader.GetID("parentID"); oCostcenter.ParentsID = oReader.GetString("parentsID"); oCostcenter.Tier = oReader.GetInt32("Tier").Value; oCostcenter.Sequence = oReader.GetInt32("SequenceNo").Value; oCostcenter.Status = (EnumStatus)oReader.GetInt32("Status").Value; oCostcenter.CreatedBy = oReader.GetID("CreatedBy"); oCostcenter.CreatedDate = oReader.GetDateTime("CreationDate").Value; oCostcenter.ModifiedBy = oReader.GetID("ModifiedBy"); oCostcenter.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oCostcenter, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Costcenter oCostcenter = new Costcenter(); MapObject(oCostcenter, oReader); return oCostcenter as T; } protected Costcenter CreateObject(DataReader oReader) { Costcenter oCostcenter = new Costcenter(); MapObject(oCostcenter, oReader); return oCostcenter; } #region Service implementation public Costcenter Get(ID id) { Costcenter oCostcenter = new Costcenter(); #region Cache Header oCostcenter = _cache["Get", id] as Costcenter; if (oCostcenter != null) return oCostcenter; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(CostcenterDA.Get(tc, id)); if (oreader.Read()) { oCostcenter = this.CreateObject(oreader); } oreader.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(oCostcenter, "Get", id); #endregion return oCostcenter; } public Costcenter Get(string code) { Costcenter oCostcenter = new Costcenter(); #region Cache Header oCostcenter = _cache["Get", code] as Costcenter; if (oCostcenter != null) return oCostcenter; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(CostcenterDA.Get(tc, code)); if (oreader.Read()) { oCostcenter = this.CreateObject(oreader); } oreader.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(oCostcenter, "Get", code); #endregion return oCostcenter; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate costcenters = _cache["Get"] as ObjectsTemplate; if (costcenters != null) return costcenters; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.Get(tc)); costcenters = this.CreateObjects(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(costcenters, "Get"); #endregion return costcenters; } public ObjectsTemplate GetParents(EnumStatus status) { #region Cache Header ObjectsTemplate costcenters = _cache["GetParents",status] as ObjectsTemplate; if (costcenters != null) return costcenters; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetParents(tc,status)); costcenters = this.CreateObjects(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(costcenters, "GetParents",status); #endregion return costcenters; } public ObjectsTemplate Get(EnumStatus status, int tier) { #region Cache Header ObjectsTemplate costcenters = _cache["Get", status,tier] as ObjectsTemplate; if (costcenters != null) return costcenters; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.Get(tc, status, tier)); costcenters = this.CreateObjects(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(costcenters, "Get", status,tier); #endregion return costcenters; } public ObjectsTemplate GetChilds(ID parentID) { #region Cache Header ObjectsTemplate costcenters = _cache["GetChilds"] as ObjectsTemplate; if (costcenters != null) return costcenters; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetChilds(tc, parentID)); costcenters = this.CreateObjects(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(costcenters, "GetChilds"); #endregion return costcenters; } public ObjectsTemplate GetByTire(int nTire) { #region Cache Header ObjectsTemplate costcenters = _cache["GetByTire"] as ObjectsTemplate; if (costcenters != null) return costcenters; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetByTire(tc, nTire)); costcenters = this.CreateObjects(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(costcenters, "GetByTire"); #endregion return costcenters; } public ObjectsTemplate GetByTireAndProfitCenter(int nTire, string sProfitCenter) { #region Cache Header ObjectsTemplate costcenters = _cache["GetByTireAndProfitCenter", nTire, sProfitCenter] as ObjectsTemplate; if (costcenters != null) return costcenters; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetByTireAndProfitCenter(tc, nTire, sProfitCenter)); costcenters = this.CreateObjects(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(costcenters, "GetByTireAndProfitCenter", nTire, sProfitCenter); #endregion return costcenters; } public ID Save(Costcenter oCostcenter) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oCostcenter.IsNew) { // base.SetObjectID(oCostcenter, ID.FromInteger(tc.GenerateID("CRG", "CRGID"))); int id = tc.GenerateID("CRG", "CRGID"); base.SetObjectID(oCostcenter, ID.FromInteger(id)); int sequenceId = tc.GenerateID("CRG", "SequenceNO"); oCostcenter.Sequence = sequenceId; CostcenterDA.Insert(tc, oCostcenter); } else { CostcenterDA.Update(tc, oCostcenter); } tc.End(); return oCostcenter.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); CostcenterDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion } #endregion }