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

namespace HRM.DA
{
    #region ShiftService

    [Serializable]
    public class DualShiftEmployeeService : ServiceTemplate
    {
        public DualShiftEmployeeService()
        {
        }

        private void MapObject(DualShiftEmployee oDualShift, DataReader oReader)
        {
            oDualShift.EmployeeID = oReader.GetInt32("EmployeeID").Value;
            oDualShift.EffectDate = oReader.GetDateTime("EFFECTDATE").Value;
            this.SetObjectState(oDualShift, Ease.Core.ObjectState.Saved);
        }

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

        #region ServiceImplementation

        public DualShiftEmployee Get(int id)
        {
            DualShiftEmployee oDualShift = new DualShiftEmployee();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DualShiftEmployeeDA.Get(tc, id));
                if (oreader.Read())
                {
                    oDualShift = this.CreateObject<DualShiftEmployee>(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 oDualShift;
        }

        public List<DualShiftEmployee> Get()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(DualShiftEmployeeDA.Get(tc));
                var oDualShifts = this.CreateObjects<DualShiftEmployee>(dr);
                dr.Close();
                tc.End();
                return oDualShifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<DualShiftEmployee> Get(DateTime date)
        {
            List<DualShiftEmployee> oDualShifts = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(DualShiftEmployeeDA.Get(tc, date));
                oDualShifts = this.CreateObjects<DualShiftEmployee>(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 oDualShifts;
        }

        public int Save(DualShiftEmployee oDualShift)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oDualShift.IsNew)
                {
                    int id = tc.GenerateID("DualShiftEmployee", "DualShiftEmployeeID");
                    base.SetObjectID(oDualShift, (id));
                    DualShiftEmployeeDA.Insert(tc, oDualShift);
                }
                else
                {
                    DualShiftEmployeeDA.Update(tc, oDualShift);
                }

                tc.End();
                return oDualShift.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public void Save(DataTable dTableNightShift)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DualShiftEmployeeDA.Save(tc, dTableNightShift);
                tc.End();
            }
            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);
                DualShiftEmployeeDA.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
    }

    #endregion
}