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 LeavePlanService : ServiceTemplate, ILeavePlanService { #region Private functions and declaration public LeavePlanService() { } private void MapObject(LeavePlan oLeavePlan, DataReader oReader) { base.SetObjectID(oLeavePlan, oReader.GetInt32("LeavePlanID").Value); oLeavePlan.EmpID = oReader.GetInt32("EmployeeID").Value; oLeavePlan.LeaveID = oReader.GetString("LeaveID") == null ? 0 : oReader.GetInt32("LeaveID").Value; oLeavePlan.LeaveYear = oReader.GetInt32("LeaveYear").Value; oLeavePlan.PlanFromDate = oReader.GetDateTime("PlanFromDate").Value; oLeavePlan.PlanToDate = oReader.GetDateTime("PlanToDate").Value; oLeavePlan.EntryDate = oReader.GetDateTime("EntryDate").Value; oLeavePlan.IsApproved = oReader.GetBoolean(("IsApproved")).Value; oLeavePlan.IsSubmitted = oReader.GetBoolean("IsSubmitted").Value; this.SetObjectState(oLeavePlan, Ease.Core.ObjectState.Saved); //this.SetObjectState(oLeave, ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { LeavePlan oLeavePlan = new LeavePlan(); MapObject(oLeavePlan, oReader); return oLeavePlan as T; } private LeavePlan CreateObject(DataReader oReader) { LeavePlan oLeavePlan = new LeavePlan(); MapObject(oLeavePlan, oReader); return oLeavePlan; } #endregion #region Service implementation public LeavePlan GetID(int id) { LeavePlan oLeavePlan = new LeavePlan(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeavePlanDA.GetID(tc, id)); if (oreader.Read()) { oLeavePlan = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave", e); #endregion } return oLeavePlan; } public bool IsEmpIDExist(int empID, int leaveYear) { TransactionContext tc = null; bool result = false; try { tc = TransactionContext.Begin(); result = LeavePlanDA.IsEmpIDExist(empID, leaveYear, tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Duplicate code is not allowed."); #endregion } return result; } public bool IsEmpApproved(int empID, int leaveYear, DateTime FormDate, DateTime ToDate, bool isApprove) { TransactionContext tc = null; bool result = false; try { tc = TransactionContext.Begin(); result = LeavePlanDA.IsEmpApproved(empID, leaveYear, FormDate, ToDate, isApprove, tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Can't Collect Employee!!!"); #endregion } return result; } public void UpdateApprove(int empID, int leaveYear, DateTime FormDate, DateTime ToDate, bool isApproved) { LeavePlan leavePlan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeavePlanDA.UpdateApprove(tc, empID, leaveYear, FormDate, ToDate, isApproved); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new Exception("Failed to Insert Leave Plan due to " + e.Message, e); #endregion } } public void Save(List oLeavePlans) { LeavePlan leavePlan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (LeavePlan oLeavePlan in oLeavePlans) { if (oLeavePlan.IsNew) { int id = tc.GenerateID("LeavePlan", "LeavePlanID"); base.SetObjectID(oLeavePlan, id); LeavePlanDA.Insert(tc, oLeavePlan); } else { LeavePlanDA.Update(tc, oLeavePlan); } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new Exception("Failed to Insert Leave Plan due to " + e.Message, e); #endregion } } public void UpDateLeavePlan(LeavePlan oLeaveItem, int PlanID) { LeavePlan leavePlan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeavePlanDA.UpDateLeavePlan(tc, oLeaveItem, PlanID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new Exception("Failed to Insert Leave Plan due to " + e.Message, e); #endregion } } public void UpdateLeavePlanSubmit(List oLeaveItems) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (LeavePlan oItem in oLeaveItems) { oItem.IsSubmitted = true; LeavePlanDA.UpdateLeavePlanSubmit(tc, oItem); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new Exception("Failed to Insert Leave Plan due to " + e.Message, e); #endregion } } #region Delete Service public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeavePlanDA.Delete(id, tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new Exception("Failed to Insert LeavePlan due to " + e.Message, e); #endregion } } #endregion public List Get(int empID) { List oLeavePlans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeavePlanDA.Get(tc, empID)); oLeavePlans = 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 oLeavePlans; } public List GetAllData() { List oLeavePlans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeavePlanDA.GetAllData(tc)); oLeavePlans = 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 oLeavePlans; } public List GetPlanData(int empID, int leaveYear) { List oLeavePlans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeavePlanDA.GetPlanData(tc, empID, leaveYear)); oLeavePlans = 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 oLeavePlans; } public List GetByMonthRange(int empID, DateTime dFromDate, DateTime dToDate) { List oLeavePlans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeavePlanDA.GetByMonthRange(tc, empID, dFromDate, dToDate)); oLeavePlans = 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 oLeavePlans; } #endregion } }