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

namespace HRM.DA
{
    #region ShiftTerm Service

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

        private void MapObject(ShiftTerm oShiftTerm, DataReader oReader)
        {
            base.SetObjectID(oShiftTerm, (oReader.GetInt32("ShiftTermID").Value));
            oShiftTerm.ShiftID = oReader.GetInt32("ShiftID", 0);
            oShiftTerm.WeekendTermID = oReader.GetInt32("WeekendTermID", 0);
            oShiftTerm.HolidayTermID = oReader.GetInt32("HolidayTermID", 0);
            oShiftTerm.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oShiftTerm.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oShiftTerm.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oShiftTerm.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oShiftTerm, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public ShiftTerm Get(int id)
        {
            ShiftTerm oShiftTerm = new ShiftTerm();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ShiftTermDA.Get(tc, id));
                if (oreader.Read())
                {
                    oShiftTerm = this.CreateObject<ShiftTerm>(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 oShiftTerm;
        }

        public ShiftTerm GetByShiftID(int shiftID)
        {
            ShiftTerm oShiftTerm = new ShiftTerm();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ShiftTermDA.GetByShiftID(tc, shiftID));
                if (oreader.Read())
                {
                    oShiftTerm = this.CreateObject<ShiftTerm>(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 oShiftTerm;
        }

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

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

                #endregion
            }
        }

        public int Save(ShiftTerm oShiftTerm)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oShiftTerm.IsNew)
                {
                    int id = tc.GenerateID("ShiftTerm", "ShiftTermID");
                    base.SetObjectID(oShiftTerm, (id));
                    ShiftTermDA.Insert(tc, oShiftTerm);
                }
                else
                {
                    ShiftTermDA.Update(tc, oShiftTerm);
                }

                tc.End();
                return oShiftTerm.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);
                ShiftTermDA.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
            }
        }

        public DataSet GetEmpOT(DateTime date, int termID)
        {
            DataSet empOT = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                empOT = ShiftTermDA.GetEmpOT(tc, date, termID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return empOT;
        }

        #endregion
    }

    #endregion
}