using System; using System.Data; using System.Linq; using System.Collections.Generic; using HRM.BO; using HRM.DA; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core.Utility; using Ease.Core; using NPOI.SS.Formula.Functions; namespace HRM.DA { public class LessonsService : ServiceTemplate, ILessonsService { public LessonsService() { } private void MapObject(Lessons oLessons, DataReader oReader) { base.SetObjectID(oLessons, oReader.GetInt32("LessonID").Value); oLessons.CourseID = oReader.GetInt32("CourseID").Value; oLessons.LessonTitle = oReader.GetString("LessonTitle"); oLessons.LessonNo = oReader.GetString("LessonNo"); oLessons.Description = oReader.GetString("Description"); oLessons.Duration = oReader.GetString("Duration"); oLessons.Status = (EnumStatus)oReader.GetInt32("Status").Value; oLessons.DifficultyLevel = (EnumDifficultyLevel)oReader.GetInt32("DifficultyLevel").Value; oLessons.IsQuizAvailable = oReader.GetBoolean("IsQuizAvailable", false); oLessons.CreatedBy = oReader.GetInt32("CreatedBy", 0); oLessons.CreatedDate = oReader.GetDateTime("CreationDate").Value; oLessons.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oLessons.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oLessons, ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Lessons oLessons = new Lessons(); MapObject(oLessons, oReader); return oLessons as T; } protected Lessons CreateObject(DataReader oReader) { Lessons oLessons = new Lessons(); MapObject(oLessons, oReader); return oLessons; } #region Save public int Save(Lessons oLessons) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oLessons.IsNew) { int ID = tc.GenerateID("LMS_Lessons", "LessonID"); base.SetObjectID(oLessons, ID); LessonsDA.Insert(tc, oLessons); } else { LessonsDA.Update(tc, oLessons); } tc.End(); return oLessons.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(TransactionContext tc, Lessons oLessons) { try { if (oLessons.IsNew) { int ID = tc.GenerateID("LMS_Lessons", "LessonID"); base.SetObjectID(oLessons, ID); LessonsDA.Insert(tc, oLessons); } else { LessonsDA.Update(tc, oLessons); } #region For LessonContent foreach (LessonContent oContent in oLessons.LessonContents) { if (oContent.IsNew == true) { oContent.LessonID = oLessons.ID; oContent.CreatedBy = oLessons.CreatedBy; oContent.CreatedDate = oLessons.CreatedDate; } else { oContent.ModifiedBy = oLessons.ModifiedBy; oContent.ModifiedDate = oLessons.ModifiedDate; } new LessonContentService().Save(tc, oContent); } #endregion return oLessons.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion #region Delete public void Delete(int ID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LessonsDA.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 } } #endregion #region Update Lesson Status public void UpdateLessonStatus(Lessons oLessons) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oLessons.IsNew) { LessonsDA.UpdateLessonStatus(tc, oLessons); } 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 GET public Lessons Get(int id) { Lessons oLessons = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LessonsDA.Get(tc, id)); if (oreader.Read()) { oLessons = 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 oLessons; } public List Get(EnumStatus status) { List oLessons = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LessonsDA.Get(tc, status)); oLessons = 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 oLessons; } public List Get(EnumStatus status, int courseID) { List oLessons = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LessonsDA.Get(tc, status, courseID)); oLessons = 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 oLessons; } public List GetAll() { List oLessons = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LessonsDA.GetAll(tc)); oLessons = 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 oLessons; } public List GetByCourseID(int courseID) { List oLessons = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LessonsDA.GetByCourseID(tc, courseID)); oLessons = 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 oLessons; } public List GetByCourseID(TransactionContext tc, int courseID) { List oLessons = new List(); try { DataReader dr = new DataReader(LessonsDA.GetByCourseID(tc, courseID)); oLessons = this.CreateObjects(dr); dr.Close(); if (oLessons.Count > 0) { foreach(Lessons olesson in oLessons) { olesson.LessonContents = new LessonContentService().GetByLessonID(tc, olesson.ID); } } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oLessons; } #endregion } }