using System; using Ease.Core.Model; using Ease.Core.DataAccess; using HRM.BO; using System.Collections.Generic; using Ease.Core.Utility; namespace HRM.DA { public class DisciplineService : ServiceTemplate, IDisciplineService { #region constructor public DisciplineService() { } #endregion #region Object private void MapObject(Discipline oDiscipline, DataReader oReader) { base.SetObjectID(oDiscipline, oReader.GetInt32("DISCIPLINEID").Value); oDiscipline.Code = oReader.GetString("CODE"); oDiscipline.Description = oReader.GetString("DESCRIPTION"); //oDiscipline.EducationLevelID = ID.FromInteger(oReader.GetInt32("EDUCATIONLEVELID").Value).IsUnassigned ? ID.FromInteger(1) : ID.FromInteger(oReader.GetInt32("EDUCATIONLEVELID").Value); oDiscipline.EducationLevelID = oReader.GetInt32("EDUCATIONLEVELID",true, 0); oDiscipline.Sequence = oReader.GetInt32("SequenceNo").Value; oDiscipline.Status = (EnumStatus)oReader.GetInt32("Status").Value; oDiscipline.CreatedBy = oReader.GetInt32("CreatedBy", 0); oDiscipline.CreatedDate = oReader.GetDateTime("CreationDate").Value; oDiscipline.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oDiscipline.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oDiscipline, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Discipline oDiscipline = new Discipline(); MapObject(oDiscipline, oReader); return oDiscipline as T; } private void MapDisLevelObject(Discipline.DisciplineLevel oDisLevel, DataReader oReader) { base.SetObjectID(oDisLevel, oReader.GetInt32("DISCIPLINELEVELID").Value); oDisLevel.DiscilineID = oReader.GetInt32("DISCIPLINEID", 0); oDisLevel.LevelID = oReader.GetInt32("LEVELID", 0); this.SetObjectState(oDisLevel, Ease.Core.ObjectState.Saved); } protected List CreateDisLevelObjects(DataReader oReader) { List oDisciplineLevels = new List(); while (oReader.Read()) { Discipline.DisciplineLevel oDisLevel = new Discipline.DisciplineLevel(); MapDisLevelObject(oDisLevel, oReader); oDisciplineLevels.Add(oDisLevel); } return oDisciplineLevels; } public Discipline GetRelatedData(TransactionContext tc, Discipline oDiscipline) { oDiscipline.DisciplineLevels = GetDisplineByDID(tc, oDiscipline.ID); return oDiscipline; } #endregion #region Service implementation public Discipline Get(int id) { Discipline oDiscipline = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(DisciplineDA.Get(tc, id)); if (oreader.Read()) { oDiscipline = this.CreateObject(oreader); } oreader.Close(); if (oDiscipline != null) oDiscipline = GetRelatedData(tc, oDiscipline); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oDiscipline; } public Discipline Get(string sCode) { Discipline oDiscipline = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(DisciplineDA.Get(tc, sCode)); if (oreader.Read()) { oDiscipline = 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 } return oDiscipline; } public List Get() { List disciplines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.Get(tc)); disciplines = 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 disciplines; } public List Get(EnumStatus status, int payrolltypeid) { List disciplines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.Get(tc, status)); disciplines = 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 disciplines; } public List Get(EnumStatus status) { List disciplines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.Get(tc, status)); disciplines = 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 disciplines; } public List GetByEducationLevel(int levelID) { List disciplines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.GetByEducationLevel(tc, levelID)); disciplines = 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 disciplines; } public List GetDisplineByDID(int DisID) { List disciplines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.GetDisplineByDID(tc, DisID)); disciplines = this.CreateDisLevelObjects(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 disciplines; } public List GetDisplineByDID(TransactionContext tc, int DisID) { List disciplines = new List(); try { DataReader dr = new DataReader(DisciplineDA.GetDisplineByDID(tc, DisID)); disciplines = this.CreateDisLevelObjects(dr); dr.Close(); } catch (Exception e) { #region Handle Exception ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return disciplines; } public List GetDisByEducationLevel(int LevelID) { List disciplines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.GetDisByEducationLevel(tc, LevelID)); disciplines = this.CreateDisLevelObjects(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 disciplines; } public string GetNextCode() { TransactionContext tc = null; string _code = ""; try { tc = TransactionContext.Begin(); _code = GlobalFunctionService.GetMaxCode(tc, "DISCIPLINE", "Code"); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return _code; } public int Save(Discipline oDiscipline) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oDiscipline.IsNew) { int id = tc.GenerateID("DISCIPLINE", "DISCIPLINEID"); base.SetObjectID(oDiscipline, id); oDiscipline.Code = GlobalFunctionService.GetMaxCode(tc, "DISCIPLINE", "Code"); DisciplineDA.Insert(tc, oDiscipline); } else { DisciplineDA.Update(tc, oDiscipline); DisciplineDA.DeleteByDisID(tc, oDiscipline.ID); } //foreach (Discipline.DisciplineLevel disciplineLevel in oDiscipline.DisciplineLevels) //{ // int id = tc.GenerateID("DisciplineLevel", "DisciplineLevelID"); // disciplineLevel.DiscilineID = oDiscipline.ID; // base.SetObjectID(disciplineLevel, ID.FromInteger(id)); // DisciplineDA.Insert(tc, disciplineLevel); //} tc.End(); return oDiscipline.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 id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DisciplineDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } internal List GetByID(TransactionContext tc, int id) { List oEducationLevels = new List(); try { DataReader dr = new DataReader(DisciplineDA.GetByID(tc, id)); oEducationLevels = 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 oEducationLevels; } public List GetByIDs(string ids) { List oDisciplineList = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DisciplineDA.GetByIDs(tc, ids)); oDisciplineList = 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 oDisciplineList; } #endregion } }