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 TrainingCostHead Service

    class TrainingCostHeadService : ServiceTemplate
    {
        #region Private functions and declaration

        private void MapObject(TrainingCostHead oTrainingCostHead, DataReader oReader)
        {
            base.SetObjectID(oTrainingCostHead, (oReader.GetInt32("TrainingCostHeadID").Value));
            oTrainingCostHead.Code = oReader.GetString("Code");
            oTrainingCostHead.Name = oReader.GetString("Name");
            //oTrainingCostHead. = (EnumStatus)oReader.GetInt32("Status").Value;
            oTrainingCostHead.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oTrainingCostHead.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue
                ? oReader.GetDateTime("CreatedDate").Value
                : DateTime.Today;
            oTrainingCostHead.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oTrainingCostHead.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oTrainingCostHead, Ease.Core.ObjectState.Saved);
        }

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

        #endregion

        #region Implementation of ITrainingCostHeadService

        #region Get TrainingCostHead

        public TrainingCostHead Get(int TrainingCostHeadID)
        {
            TrainingCostHead oTrainingCostHead = null;

            TransactionContext tc = null;
            try
            {
                #region Retrieving data

                tc = TransactionContext.Begin();

                DataReader oreader = new DataReader(TrainingCostHeadDA.Get(tc, TrainingCostHeadID));
                oreader.Read();
                oTrainingCostHead = CreateObject<TrainingCostHead>(oreader);

                tc.End();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return oTrainingCostHead;
        }

        #endregion

        #region Get TrainingCostHeads

        public List<TrainingCostHead> Get()
        {
            List<TrainingCostHead> oTrainingCostHeads = null;
            ;


            TransactionContext tc = null;
            try
            {
                #region Retrieving data

                tc = TransactionContext.Begin();

                DataReader oreader = new DataReader(TrainingCostHeadDA.Get(tc));
                oTrainingCostHeads = CreateObjects<TrainingCostHead>(oreader);

                tc.End();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return oTrainingCostHeads;
        }

        #endregion

        #region Insert TrainingCostHead

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

                    tc = TransactionContext.Begin(true);

                    #region Checking Duplicate TrainingCostHead Code

                    bool result = false;
                    result = TrainingCostHeadDA.IsExists("TrainingCostHead", "Code", oTrainingCostHead.Code, tc);
                    if (result & oTrainingCostHead.IsNew)
                    {
                        throw new ServiceException("Duplicate code is not allowed.");
                    }

                    #endregion

                    if (oTrainingCostHead.IsNew)
                    {
                        int newID = tc.GenerateID("TrainingCostHead", "TrainingCostHeadID");
                        base.SetObjectID(oTrainingCostHead, (newID));


                        TrainingCostHeadDA.Insert(tc, oTrainingCostHead);
                    }
                    else
                        TrainingCostHeadDA.Update(tc, oTrainingCostHead);


                    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 oTrainingCostHead.ID;
        }

        #endregion

        #region Delete Trainng

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

                    tc = TransactionContext.Begin(true);
                    TrainingCostHeadDA.Delete(tc, TrainingCostHeadID);

                    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

        #endregion
    }

    #endregion
}