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 { public class LeavePlanService : ServiceTemplate, ILeavePlanService { #region Private functions and declaration Cache _cache = new Cache(typeof(LeavePlan)); public LeavePlanService() { } private void MapObject(LeavePlan oLeavePlan, DataReader oReader) { base.SetObjectID(oLeavePlan, oReader.GetID("LeavePlanID")); oLeavePlan.EmpID = oReader.GetInt32("EMPLOYEEID").Value; oLeavePlan.LeaveID = ID.FromInteger(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.CoreV35.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(); #region Cache Header oLeavePlan = (LeavePlan)_cache["Get", id]; if (oLeavePlan != null) return oLeavePlan; #endregion 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 } #region Cache Footer _cache.Add(oLeavePlan, "Get", id); #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 Save Leave Plan due to " + e.Message, e); #endregion } } public void Save(ObjectsTemplate 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.FromInteger(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 Save 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 Save Leave Plan due to " + e.Message, e); #endregion } } public void UpdateLeavePlanSubmit(ObjectsTemplate 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 Save Leave Plan due to " + e.Message, e); #endregion } } #region Delete Service public void Delete(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeavePlanDA.Delete(id.Integer, tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new Exception("Failed to Save LeavePlan due to " + e.Message, e); #endregion } } #endregion public ObjectsTemplate Get(int empID) { #region Cache Header ObjectsTemplate oLeavePlans = _cache["Get", empID] as ObjectsTemplate; if (oLeavePlans != null) return oLeavePlans; #endregion 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 } #region Cache Footer _cache.Add(oLeavePlans, "Get", empID); #endregion return oLeavePlans; } public ObjectsTemplate GetAllData() { #region Cache Header ObjectsTemplate oLeavePlans = _cache["Get"] as ObjectsTemplate; if (oLeavePlans != null) return oLeavePlans; #endregion 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 } #region Cache Footer _cache.Add(oLeavePlans, "Get"); #endregion return oLeavePlans; } public ObjectsTemplate GetPlanData(int empID, int leaveYear) { #region Cache Header ObjectsTemplate oLeavePlans = _cache["Get", empID, leaveYear] as ObjectsTemplate; if (oLeavePlans != null) return oLeavePlans; #endregion 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 } #region Cache Footer _cache.Add(oLeavePlans, "Get", empID, leaveYear); #endregion return oLeavePlans; } public ObjectsTemplate GetByMonthRange(int empID, DateTime dFromDate, DateTime dToDate) { #region Cache Header ObjectsTemplate oLeavePlans = _cache["Get", empID, dFromDate, dToDate] as ObjectsTemplate; if (oLeavePlans != null) return oLeavePlans; #endregion 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 } #region Cache Footer _cache.Add(oLeavePlans, "Get", empID, dFromDate, dToDate); #endregion return oLeavePlans; } #endregion } }