using System; using System.Data; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core; using System.Collections.Generic; using Ease.Core.Utility; using HRM.BO; namespace HRM.DA { class BudgetComponentService : ServiceTemplate, IBudgetComponentService { #region Private functions and declaration #endregion #region Object Mapping #region Parent Object Mapping private void MapObject(BudgetComponent oBudgetComponent, DataReader oReader) { SetObjectID(oBudgetComponent, (oReader.GetInt32("BudgetComponentID").Value)); oBudgetComponent.BasedOn = (EnumBasedOnPercent)oReader.GetInt32("BasedOn").Value; oBudgetComponent.BudgetID = oReader.GetInt32("BudgetID", 0); oBudgetComponent.ComponentType = (EnumBudgetComponent)oReader.GetInt32("ComponentType").Value; oBudgetComponent.FlatAmount = oReader.GetDouble("FlatAmount").Value; oBudgetComponent.Name = oReader.GetString("Name"); oBudgetComponent.OriginID = oReader.GetInt32("OriginID", 0); oBudgetComponent.ParameterID = oReader.GetInt32("ParameterID", 0); oBudgetComponent.Percentage = oReader.GetDouble("Percentage").Value; oBudgetComponent.Periodicity = oReader.GetDouble("Periodicity").Value; oBudgetComponent.Taxable = oReader.GetBoolean("Taxable").Value; oBudgetComponent.PayrollTypeID = oReader.GetInt32("PayrollTypeID", 0); oBudgetComponent.IndivisuallyDefined = oReader.GetBoolean("IndivisuallyDefined").Value; SetObjectState(oBudgetComponent, ObjectState.Saved); oBudgetComponent.OBudgetComponentIndivisuals = new List(); oBudgetComponent.OBudgetComponentGrades = new List(); oBudgetComponent.OBudgetComponentIndivisuals = GetBudgetComponentIndivisuals(oBudgetComponent.ID); oBudgetComponent.OBudgetComponentGrades = GetBudgetComponentGrades(oBudgetComponent.ID); } protected override T CreateObject(DataReader dr) { BudgetComponent oBudgetComponent = new BudgetComponent(); MapObject(oBudgetComponent, dr); return oBudgetComponent as T; } #endregion #region Child Object Mapping #region BudgetComponentIndivisual Mapping private void MapBudgetComponentIndivisual(BudgetComponentIndivisual oBudgetComponentIndivisual, DataReader oReader) { SetObjectID(oBudgetComponentIndivisual, (oReader.GetInt32("BudgetComponentIndivisualID").Value)); oBudgetComponentIndivisual.Amount = oReader.GetDouble("Amount").Value; oBudgetComponentIndivisual.BudgetComponentID = oReader.GetInt32("BudgetComponentID", 0); oBudgetComponentIndivisual.DetailType = (EnumADEmpType)oReader.GetInt32("DetailType").Value; oBudgetComponentIndivisual.EmployeeID = oReader.GetInt32("EmployeeID", 0); oBudgetComponentIndivisual.FromDate = oReader.GetDateTime("FromDate").GetValueOrDefault(); oBudgetComponentIndivisual.ToDate = oReader.GetDateTime("ToDate").GetValueOrDefault(); oBudgetComponentIndivisual.ValueType = (EnumValueType)oReader.GetInt32("ValueType").Value; this.SetObjectState(oBudgetComponentIndivisual, ObjectState.Saved); } private BudgetComponentIndivisual CreateBudgetComponentIndivisualObject(DataReader oReader) { BudgetComponentIndivisual oBudgetComponentIndivisual = new BudgetComponentIndivisual(); MapBudgetComponentIndivisual(oBudgetComponentIndivisual, oReader); return oBudgetComponentIndivisual; } protected List CreateBudgetComponentIndivisualObjects(DataReader oReader) { List oBudgetComponentIndivisuals = new List(); while (oReader.Read()) { BudgetComponentIndivisual item = CreateBudgetComponentIndivisualObject(oReader); oBudgetComponentIndivisuals.Add(item); } return oBudgetComponentIndivisuals; } #endregion #region BudgetComponentGrade Mapping private void MapBudgetComponentGrade(BudgetComponentGrade oBudgetComponentGrade, DataReader oReader) { SetObjectID(oBudgetComponentGrade, (oReader.GetInt32("BudgetComponentGradeID").Value)); oBudgetComponentGrade.BudgetComponentID = oReader.GetInt32("BudgetComponentID", 0); oBudgetComponentGrade.GradeID = oReader.GetInt32("GradeID", 0); this.SetObjectState(oBudgetComponentGrade, ObjectState.Saved); } private BudgetComponentGrade CreateBudgetComponentGradeObject(DataReader oReader) { BudgetComponentGrade oBudgetComponentGrade = new BudgetComponentGrade(); MapBudgetComponentGrade(oBudgetComponentGrade, oReader); return oBudgetComponentGrade; } protected List CreateBudgetComponentGradeObjects(DataReader oReader) { List oBudgetComponentGrades = new List(); while (oReader.Read()) { BudgetComponentGrade item = CreateBudgetComponentGradeObject(oReader); oBudgetComponentGrades.Add(item); } return oBudgetComponentGrades; } #endregion #endregion #endregion #region Service Implementation #region Parent's Service Implementation public BudgetComponent Get(int nID) { BudgetComponent oBudgetComponent = new BudgetComponent(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(BudgetComponentDA.Get(tc, nID)); if (oreader.Read()) { oBudgetComponent = 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 } return oBudgetComponent; } public List Get() { List oBudgetComponents = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(BudgetComponentDA.Get(tc)); // if (oreader.Read()) //{ oBudgetComponents = this.CreateObjects(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 } return oBudgetComponents; } public List GetWithBudgetID(int BudgetID) { List oBudgetComponents = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(BudgetComponentDA.Get(tc, BudgetID)); // if (oreader.Read()) //{ oBudgetComponents = this.CreateObjects(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 } return oBudgetComponents; } public int Save(BudgetComponent item) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (item.IsNew) { int id = tc.GenerateID("BudgetComponent", "BudgetComponentID"); SetObjectID(item, (id)); BudgetComponentDA.Insert(tc, item); } else { BudgetComponentDA.Update(tc, item); #region delete childrern BudgetComponentDA.Delete(tc, "BudgetComponentIndivisual", "BudgetComponentID", item.ID); BudgetComponentDA.Delete(tc, "BudgetComponentGrade", "BudgetComponentID", item.ID); #endregion } #region Insert Children #region BudgetComponentIndivisual foreach (BudgetComponentIndivisual oBudgetComponentIndivisual in item.OBudgetComponentIndivisuals) { oBudgetComponentIndivisual.BudgetComponentID = item.ID; this.SetObjectID(oBudgetComponentIndivisual, (tc.GenerateID("BudgetComponentIndivisual", "BudgetComponentIndivisualID"))); BudgetComponentDA.Insert(tc, oBudgetComponentIndivisual); } #endregion #region BudgetComponentGrade foreach (BudgetComponentGrade oBudgetComponentGrade in item.OBudgetComponentGrades) { oBudgetComponentGrade.BudgetComponentID = item.ID; this.SetObjectID(oBudgetComponentGrade, (tc.GenerateID("BudgetComponentGrade", "BudgetComponentGradeID"))); BudgetComponentDA.Insert(tc, oBudgetComponentGrade); } #endregion #endregion tc.End(); return item.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public int Save(BudgetComponent item, TransactionContext tc) { try { int id = tc.GenerateID("BudgetComponent", "BudgetComponentID"); SetObjectID(item, (id)); BudgetComponentDA.Insert(tc, item); #region Insert Children #region BudgetComponentIndivisual foreach (BudgetComponentIndivisual oBudgetComponentIndivisual in item.OBudgetComponentIndivisuals) { oBudgetComponentIndivisual.BudgetComponentID = item.ID; this.SetObjectID(oBudgetComponentIndivisual, (tc.GenerateID("BudgetComponentIndivisual", "BudgetComponentIndivisualID"))); BudgetComponentDA.Insert(tc, oBudgetComponentIndivisual); } #endregion #region BudgetComponentGrade foreach (BudgetComponentGrade oBudgetComponentGrade in item.OBudgetComponentGrades) { oBudgetComponentGrade.BudgetComponentID = item.ID; this.SetObjectID(oBudgetComponentGrade, (tc.GenerateID("BudgetComponentGrade", "BudgetComponentGradeID"))); BudgetComponentDA.Insert(tc, oBudgetComponentGrade); } #endregion #endregion return item.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(int nID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); #region delete childrern BudgetComponentDA.Delete(tc, "BudgetComponentIndivisual", "BudgetComponentID", nID); BudgetComponentDA.Delete(tc, "BudgetComponentGrade", "BudgetComponentID", nID); #endregion BudgetComponentDA.Delete(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 } } #endregion #region Child's Service Implementation public List GetBudgetComponentIndivisuals(int id) { List oBudgetComponentIndivisuals = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(BudgetComponentDA.GetBudgetComponentIndivisuals(tc, id)); // if (oreader.Read()) //{ oBudgetComponentIndivisuals = this.CreateBudgetComponentIndivisualObjects(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 } return oBudgetComponentIndivisuals; } public List GetBudgetComponentGrades(int id) { List oBudgetComponentGrades = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(BudgetComponentDA.GetBudgetComponentGrades(tc, id)); // if (oreader.Read()) //{ oBudgetComponentGrades = this.CreateBudgetComponentGradeObjects(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 } return oBudgetComponentGrades; } #endregion #endregion } }