using System;
using System.Collections.Generic;
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 ShiftTermDetail Service

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

        private void MapObject(ShiftTermDetail oShiftTermDetail, DataReader oReader)
        {
            base.SetObjectID(oShiftTermDetail, (oReader.GetInt32("ShiftTermDetailID").Value));
            oShiftTermDetail.ShiftTermID = oReader.GetInt32("ShiftTermID", 0);
            oShiftTermDetail.Hour = oReader.GetDouble("OTHour").Value;
            oShiftTermDetail.SequenceNo = oReader.GetInt32("SequenceNo").Value;
            oShiftTermDetail.TermID = oReader.GetInt32("TermID", 0);
            this.SetObjectState(oShiftTermDetail, Ease.Core.ObjectState.Saved);
        }

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

        protected ShiftTermDetail CreateObject(DataReader oReader)
        {
            ShiftTermDetail oShiftTermDetail = new ShiftTermDetail();
            MapObject(oShiftTermDetail, oReader);
            return oShiftTermDetail;
        }

        #region Service implementation

        public ShiftTermDetail Get(int id)
        {
            ShiftTermDetail oShiftTermDetail = new ShiftTermDetail();

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

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

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

                #endregion
            }
        }

        public List<ShiftTermDetail> GetByShiftTermID(int nShiftTermID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftTermDetailDA.GetByShiftTermID(tc, nShiftTermID));
                var shiftTermDetails = this.CreateObjects<ShiftTermDetail>(dr);
                dr.Close();
                tc.End();
                return shiftTermDetails;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public int Save(ShiftTermDetail oShiftTermDetail)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oShiftTermDetail.IsNew)
                {
                    int id = tc.GenerateID("ShiftTermDetail", "ShiftTermDetailID");
                    base.SetObjectID(oShiftTermDetail, (id));
                    ShiftTermDetailDA.Insert(tc, oShiftTermDetail);
                }
                else
                {
                    ShiftTermDetailDA.Update(tc, oShiftTermDetail);
                }

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