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 EmployeePosting Service [Serializable] public class EmployeePostingService : ServiceTemplate, IEmployeePostingService { #region Private functions and declaration Cache _cache = new Cache(typeof(EmployeePosting)); #endregion public EmployeePostingService() { } private void MapObject(EmployeePosting oEmployeePosting, DataReader oReader) { base.SetObjectID(oEmployeePosting, oReader.GetID("HREmpPostingID")); oEmployeePosting.EmployeeID = oReader.GetID("employeeID"); oEmployeePosting.EffectDate = oReader.GetDateTime("postingDate").Value; oEmployeePosting.DepartmentID = oReader.GetID("departmentID"); oEmployeePosting.LocationID = oReader.GetID("locationID"); oEmployeePosting.DesignationID = oReader.GetID("designationID"); oEmployeePosting.CreatedBy = oReader.GetID("CreatedBy"); oEmployeePosting.CreatedDate = oReader.GetDateTime("CreationDate").Value; oEmployeePosting.ModifiedBy = oReader.GetID("ModifiedBy"); oEmployeePosting.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oEmployeePosting, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { EmployeePosting oEmployeePosting = new EmployeePosting(); MapObject(oEmployeePosting, oReader); return oEmployeePosting as T; } protected EmployeePosting CreateObject(DataReader oReader) { EmployeePosting oEmployeePosting = new EmployeePosting(); MapObject(oEmployeePosting, oReader); return oEmployeePosting; } #region Service implementation public EmployeePosting Get(ID id) { EmployeePosting oEmployeePosting = new EmployeePosting(); #region Cache Header oEmployeePosting = _cache["Get", id] as EmployeePosting; if (oEmployeePosting != null) return oEmployeePosting; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(EmployeePostingDA.Get(tc, id)); if (oreader.Read()) { oEmployeePosting = 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(oEmployeePosting, "Get", id); #endregion return oEmployeePosting; } public EmployeePosting Get(ID nEmpID, DateTime dEffectDate, int payrollTypeID) { EmployeePosting oEmployeePosting = new EmployeePosting(); #region Cache Header oEmployeePosting = _cache["Get", nEmpID,dEffectDate] as EmployeePosting; if (oEmployeePosting != null) return oEmployeePosting; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(EmployeePostingDA.Get(tc, nEmpID, dEffectDate, payrollTypeID)); if (oreader.Read()) { oEmployeePosting = 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(oEmployeePosting, "Get", nEmpID,dEffectDate); #endregion return oEmployeePosting; } public ObjectsTemplate GetByEmpID(ID nID) { #region Cache Header ObjectsTemplate employeePostings = _cache["Get", nID] as ObjectsTemplate; if (employeePostings != null) return employeePostings; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeePostingDA.GetEmpid(tc, nID)); employeePostings = 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(employeePostings, "Get", nID); #endregion return employeePostings; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate employeePostings = _cache["Get"] as ObjectsTemplate; if (employeePostings != null) return employeePostings; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeePostingDA.Get(tc)); employeePostings = 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(employeePostings, "Get"); #endregion return employeePostings; } public ID Save(EmployeePosting oEmployeePosting) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oEmployeePosting.IsNew) { int id = tc.GenerateID("employeeposting", "hremppostingid"); base.SetObjectID(oEmployeePosting, ID.FromInteger(id)); EmployeePostingDA.DeleteByPostingDate(tc, oEmployeePosting.EffectDate,oEmployeePosting.EmployeeID); EmployeePostingDA.Insert(tc, oEmployeePosting); EmployeeDA.UpdateEmpPosting(tc, oEmployeePosting); } else { EmployeePostingDA.Update(tc, oEmployeePosting); EmployeeDA.UpdateEmpPosting(tc, oEmployeePosting); } tc.End(); return oEmployeePosting.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 { EmployeePostingService osvr = new EmployeePostingService(); EmployeePosting oposing = osvr.Get(id); tc = TransactionContext.Begin(true); EmployeePostingDA.Delete(tc, id); ObjectsTemplate employeePostings = this.GetByEmpID(tc, oposing.EmployeeID); if (employeePostings == null || employeePostings.Count == 0) { EmployeePosting oEmployeePosting = new EmployeePosting(); oEmployeePosting.DepartmentID =null; oEmployeePosting.DesignationID =null; oEmployeePosting.LocationID = null; oEmployeePosting.EmployeeID = oposing.EmployeeID; EmployeeDA.UpdateEmpPosting(tc, oEmployeePosting); } else { EmployeeDA.UpdateEmpPosting(tc, employeePostings[0]); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public ObjectsTemplate GetByEmpID(TransactionContext tc, ID nID) { ObjectsTemplate employeePostings = null; try { DataReader dr = new DataReader(EmployeePostingDA.GetEmpid(tc, nID)); employeePostings = 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 } return employeePostings; } public void DeleteAll() { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); EmployeePostingDA.DeleteAll(tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DataSet GetEmpCurrentPosting(DateTime EffectDate, int payrollTypeID) { DataSet oEmpPosting = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); oEmpPosting = EmployeePostingDA.GetEmpCurrentPosting(tc, EffectDate, payrollTypeID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oEmpPosting; } public DataSet GetEmpPrvPosting(DateTime EffectDate, int payrollTypeID) { DataSet oEmpPosting = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); oEmpPosting = EmployeePostingDA.GetEmpPrvPosting(tc, EffectDate, payrollTypeID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oEmpPosting; } #endregion } #endregion }