using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using HRM.BO; namespace HRM.DA { public class LeaveExceptionService : ServiceTemplate, ILeaveExceptionService { #region Private functions and declaration public LeaveExceptionService() { } private void MapObject(LeaveException oLeaveException, DataReader oReader) { //base.SetObjectID(oLeaveException, oReader.GetInt32("LeaveExceptionID").Value); oLeaveException.EmployeeID = oReader.GetInt32("EmployeeID").Value; oLeaveException.StartDate = oReader.GetDateTime("StartDate").Value; oLeaveException.EndDate = oReader.GetDateTime("EndDate").Value; oLeaveException.LeaveID = oReader.GetInt16("LeaveID").Value; oLeaveException.OpeningBalance = oReader.GetDouble("OpeningBalance").Value; oLeaveException.MaxDays = oReader.GetDouble("MaxDays").Value; oLeaveException.CFDays = oReader.GetDouble("CFDays").Value; this.SetObjectState(oLeaveException, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { LeaveException oLeave = new LeaveException(); MapObject(oLeave, oReader); return oLeave as T; } private LeaveException CreateObject(DataReader oReader) { LeaveException oLeave = new LeaveException(); MapObject(oLeave, oReader); return oLeave; } #endregion public List Get() { List employees = new List(); employees = new EmployeeService().GetAllEmps(); List leaves = new LeaveService().GetAll(); List oLeaveExceptions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveExceptionDA.Get(tc)); oLeaveExceptions = this.CreateObjects(dr); dr.Close(); tc.End(); foreach (LeaveException item in oLeaveExceptions) { Employee employee = employees.Find(x => x.ID == item.EmployeeID); item.EmployeeNo = employee != null ? employee.EmployeeNo : string.Empty; item.EmployeeName = employee != null ? employee.Name : string.Empty; Leave leave = leaves.Find(x => x.ID == item.LeaveID); item.LeaveName = leave != null ? leave.Description : string.Empty; } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oLeaveExceptions; } public List Get(DateTime dFrom, DateTime dTo) { List oLeaves = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveExceptionDA.Get2(tc, dFrom, dTo)); oLeaves = 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 } return oLeaves; } public List GetByEmpID(int nLeaveID, int nEmpID) { List employees = new List(); employees = new EmployeeService().GetAllEmps(); List leaves = new LeaveService().GetAll(); List oLeaveExceptions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveExceptionDA.GetByEmpID(tc, nLeaveID, nEmpID)); oLeaveExceptions = this.CreateObjects(dr); dr.Close(); tc.End(); foreach (LeaveException item in oLeaveExceptions) { Employee employee = employees.Find(x => x.ID == item.EmployeeID); item.EmployeeNo = employee != null ? employee.EmployeeNo : string.Empty; item.EmployeeName = employee != null ? employee.Name : string.Empty; Leave leave = leaves.Find(x => x.ID == item.LeaveID); item.LeaveName = leave != null ? leave.Description : string.Empty; } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oLeaveExceptions; } public int Save(LeaveException oLeave) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeaveExceptionDA.Delete(tc, oLeave.EmployeeID); LeaveExceptionDA.Insert(tc, oLeave); tc.End(); return oLeave.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); //throw new ServiceException("Failed to Get Leave", e); throw new Exception("Failed to Insert Leave. Because " + e.Message, e); #endregion } } public void DeleteItem(int nID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeaveExceptionDA.DeleteItem(tc, nID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void DeleteByEmpID(int empID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeaveExceptionDA.Delete(tc, empID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } } }