using System;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using System.Collections.Generic;
using HRM.BO;
using System.Data;
using Ease.Core;
using System.Linq;

namespace HRM.DA
{
    #region Training Service

    class TrainingScheduleAttnService : ServiceTemplate
    {
        #region Private functions and declaration

        private void MapObject(TrainingScheduleAttn oTrainingScheduleAttn, DataReader oReader)
        {
            base.SetObjectID(oTrainingScheduleAttn, (oReader.GetInt32("TSAttnID").Value));
            oTrainingScheduleAttn.TrainingScheduleID = oReader.GetInt32("TrainingScheduleID", 0);
            oTrainingScheduleAttn.TSDateID = oReader.GetInt32("TSDateID", 0);
            oTrainingScheduleAttn.EmployeeID = oReader.GetInt32("EmployeeID", 0);

            this.SetObjectState(oTrainingScheduleAttn, Ease.Core.ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            TrainingScheduleAttn oTrainingScheduleAttng = new TrainingScheduleAttn();
            MapObject(oTrainingScheduleAttng, oReader);
            return oTrainingScheduleAttng as T;
        }

        #endregion

        #region Implementation of ITrainingScheduleAttnService

        #region Get TrainingScheduleAttn

        public TrainingScheduleAttn Get(int trainingScheduleAttnID)
        {
            TrainingScheduleAttn otrainingScheduleAttn = null;

            TransactionContext tc = null;
            try
            {
                #region Retrieving data

                tc = TransactionContext.Begin();

                DataReader oreader = new DataReader(TrainingScheduleAttnDA.Get(tc, trainingScheduleAttnID));
                oreader.Read();
                otrainingScheduleAttn = CreateObject<TrainingScheduleAttn>(oreader);

                tc.End();

                #endregion
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return otrainingScheduleAttn;
        }

        #endregion

        #region Get List<TrainingScheduleAttn>

        public List<TrainingScheduleAttn> Get()
        {
            List<TrainingScheduleAttn> trainingScheduleAttns = null;

            TransactionContext tc = null;
            try
            {
                #region Retrieving data

                tc = TransactionContext.Begin();

                DataReader oreader = new DataReader(TrainingScheduleAttnDA.Get(tc));
                trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);

                tc.End();

                #endregion
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return trainingScheduleAttns;
        }

        public List<TrainingScheduleAttn> GetByTrainingScheduleID(int trainingScheduleID)
        {
            List<TrainingScheduleAttn> trainingScheduleAttns = null;

            TransactionContext tc = null;
            try
            {
                #region Retrieving data

                tc = TransactionContext.Begin();

                DataReader oreader =
                    new DataReader(TrainingScheduleAttnDA.GetByTrainingScheduleID(tc, trainingScheduleID));
                trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);

                tc.End();

                #endregion
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return trainingScheduleAttns;
        }

        public List<TrainingScheduleAttn> GetByTrainingScheduleDateID(int trainingScheduleDateID)
        {
            List<TrainingScheduleAttn> trainingScheduleAttns = null;

            #endregion

            TransactionContext tc = null;
            try
            {
                #region Retrieving data

                tc = TransactionContext.Begin();

                DataReader oreader =
                    new DataReader(TrainingScheduleAttnDA.GetByTrainingScheduleDateID(tc, trainingScheduleDateID));
                trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);

                tc.End();

                #endregion
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return trainingScheduleAttns;
        }

        #endregion

        #region Insert TrainingScheduleAttn

        public int Save(TrainingScheduleAttn oTrainingScheduleAttn)
        {
            try
            {
                TransactionContext tc = null;
                try
                {
                    #region Saving data

                    tc = TransactionContext.Begin(true);


                    if (oTrainingScheduleAttn.IsNew)
                    {
                        int newID = tc.GenerateID("TrainingScheduleAttn", "TSAttnID");
                        base.SetObjectID(oTrainingScheduleAttn, (newID));


                        TrainingScheduleAttnDA.Insert(tc, oTrainingScheduleAttn);
                    }
                    else
                        TrainingScheduleAttnDA.Update(tc, oTrainingScheduleAttn);


                    tc.End();

                    #endregion
                }
                catch (Exception e)
                {
                    #region Handle Exception

                    if (tc != null)
                        tc.HandleError();
                    throw new ServiceException(e.Message, e);

                    #endregion
                }
            }
            catch (Exception e)
            {
                //throw new ServiceException(e.Message, e);
                throw new Exception(e.Message, e);
            }

            return oTrainingScheduleAttn.ID;
        }

        public int Save(TransactionContext tc, TrainingScheduleAttn oTrainingScheduleAttn)
        {
            try
            {
                try
                {
                    #region Saving data

                    if (oTrainingScheduleAttn.IsNew)
                    {
                        int newID = tc.GenerateID("TrainingScheduleAttn", "TSAttnID");
                        base.SetObjectID(oTrainingScheduleAttn, (newID));
                        TrainingScheduleAttnDA.Insert(tc, oTrainingScheduleAttn);
                    }
                    else
                        TrainingScheduleAttnDA.Update(tc, oTrainingScheduleAttn);

                    #endregion
                }
                catch (Exception e)
                {
                    #region Handle Exception

                    if (tc != null)
                        tc.HandleError();
                    throw new ServiceException(e.Message, e);

                    #endregion
                }
            }
            catch (Exception e)
            {
                //throw new ServiceException(e.Message, e);
                throw new Exception(e.Message, e);
            }

            return oTrainingScheduleAttn.ID;
        }

        #endregion

        #region Delete

        public void Delete(int id)
        {
            try
            {
                TransactionContext tc = null;
                try
                {
                    #region Deleting data

                    tc = TransactionContext.Begin(true);
                    TrainingScheduleAttnDA.Delete(tc, id);

                    tc.End();

                    #endregion
                }
                catch (Exception e)
                {
                    #region Handle Exception

                    if (tc != null)
                        tc.HandleError();

                    throw new ServiceException(e.Message, e);

                    #endregion
                }
            }
            catch (Exception e)
            {
                throw new ServiceException(e.Message, e);
            }
        }

        #endregion

        public bool DoesTrainingScheduleAttnExists(int trainingScheduleDateID)
        {
            try
            {
                TransactionContext tc = null;
                tc = TransactionContext.Begin();
                bool result = false;
                result = TrainingScheduleAttnDA.DoesTrainingScheduleAttnExists(tc, trainingScheduleDateID);
                tc.End();
                return result;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        #endregion
    }
}