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

namespace HRM.DA
{
    public class GrossDefinationService : ServiceTemplate, IGrossDefinationService
    {
        #region Private and Protected functions

        private void MapObject(GrossDefination oGrossDefination, DataReader oReader)
        {
            base.SetObjectID(oGrossDefination, oReader.GetInt32("GrossDefinationID").Value);
            oGrossDefination.SalaryComponentType = (EnumSalaryComponent)oReader.GetInt32("SalaryComponentType").Value;
            oGrossDefination.ComponentID = oReader.GetInt32("ComponentID", 0);
            oGrossDefination.Quantity = oReader.GetInt32("Quantity").Value;
            oGrossDefination.Description = oReader.GetString("Description");
            oGrossDefination.BenefitDefinationType =
                (EnumBenefitDefinationType)oReader.GetInt32("BenefitDefinationType").Value;
            oGrossDefination.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oGrossDefination.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oGrossDefination.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oGrossDefination.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oGrossDefination, Ease.Core.ObjectState.Saved);
        }

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

        #endregion Private and Protected functions

        #region IGrossDefinationService Members

        #region Get methods

        public GrossDefination Get(int id)
        {
            GrossDefination oGrossDefination = null;

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

        public List<GrossDefination> Get()
        {
            List<GrossDefination> oGrossDefinationColl = new List<GrossDefination>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(GrossDefinationDA.Get(tc));
                while (oreader.Read())
                {
                    GrossDefination oGrossDefination = new GrossDefination();
                    oGrossDefination = this.CreateObject<GrossDefination>(oreader);
                    oGrossDefinationColl.Add(oGrossDefination);
                }

                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 oGrossDefinationColl;
        }

        public List<GrossDefination> Get(EnumBenefitDefinationType type)
        {
            List<GrossDefination> oGrossDefinationColl = new List<GrossDefination>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(GrossDefinationDA.Get(tc, type));
                while (oreader.Read())
                {
                    GrossDefination oGrossDefination = new GrossDefination();
                    oGrossDefination = this.CreateObject<GrossDefination>(oreader);
                    oGrossDefinationColl.Add(oGrossDefination);
                }

                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 oGrossDefinationColl;
        }

        #endregion Get methods


        #region Insert methods

        public void Save(GrossDefination grossDefination)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (grossDefination.IsNew)
                {
                    int id = tc.GenerateID("GrossDefination", "GrossDefinationID");
                    base.SetObjectID(grossDefination, id);

                    grossDefination.CreatedBy = grossDefination.CreatedBy;
                    //grossDefination.CreatedDate = GlobalFunctionDA.GetOperationDate(tc);
                    grossDefination.CreatedDate = DateTime.Now;
                    GrossDefinationDA.Save(tc, grossDefination);
                }
                else
                {
                    grossDefination.ModifiedBy = grossDefination.ModifiedBy;
                    //grossDefination.ModifiedDate = GlobalFunctionDA.GetOperationDate(tc);
                    grossDefination.ModifiedDate = DateTime.Now;
                    GrossDefinationDA.Update(tc, grossDefination);
                }

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

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

                #endregion
            }
        }

        #endregion Insert methods]

        #region Delete methods

        public void Delete(int grossDefinationID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                GrossDefinationDA.Delete(tc, grossDefinationID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        #endregion Delete methods

        #endregion IGrossDefinationService Members
    }
}