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

namespace HRM.DA
{
    #region Class NatureOfTrainingService

    public class NatureOfTrainingService : ServiceTemplate, INatureOfTrainingService
    {
        #region Object

        private void MapObject(NatureOfTraining oNatureOfTraining, DataReader oReader)
        {
            base.SetObjectID(oNatureOfTraining, oReader.GetInt32("NATUREOFTRAININGID").Value);
            oNatureOfTraining.Code = oReader.GetString("CODE");
            oNatureOfTraining.Name = oReader.GetString("NAME");
            oNatureOfTraining.Sequence = oReader.GetInt32("SequenceNo").Value;
            oNatureOfTraining.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oNatureOfTraining.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oNatureOfTraining.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oNatureOfTraining.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oNatureOfTraining.ModifiedDate = oReader.GetDateTime("ModifiedDate");

            this.SetObjectState(oNatureOfTraining, Ease.Core.ObjectState.Saved);
        }

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

        #endregion

        #region Service implementation

        public NatureOfTraining Get(int id)
        {
            NatureOfTraining oNatureOfTraining = null;

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

        public NatureOfTraining Get(string sCode)
        {
            NatureOfTraining oNatureOfTraining = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(NatureOfTrainingDA.Get(tc, sCode));
                if (oreader.Read())
                {
                    oNatureOfTraining = this.CreateObject<NatureOfTraining>(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 oNatureOfTraining;
        }

        public List<NatureOfTraining> Get()
        {
            List<NatureOfTraining> natureOfTrainings = new List<NatureOfTraining>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(NatureOfTrainingDA.Get(tc));
                natureOfTrainings = this.CreateObjects<NatureOfTraining>(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 natureOfTrainings;
        }

        public List<NatureOfTraining> Get(EnumStatus status)
        {
            List<NatureOfTraining> natureOfTrainings = new List<NatureOfTraining>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(NatureOfTrainingDA.Get(tc, status));
                natureOfTrainings = this.CreateObjects<NatureOfTraining>(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 natureOfTrainings;
        }

        public int Save(NatureOfTraining oNatureOfTraining)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oNatureOfTraining.IsNew)
                {
                    int id = tc.GenerateID("NATUREOFTRAINING", "NATUREOFTRAININGID");
                    base.SetObjectID(oNatureOfTraining, id);
                    if (oNatureOfTraining.Code == "")
                        oNatureOfTraining.Code = oNatureOfTraining.ID.ToString();
                    oNatureOfTraining.Status = EnumStatus.Active;
                    NatureOfTrainingDA.Insert(tc, oNatureOfTraining);
                }
                else
                {
                    NatureOfTrainingDA.Update(tc, oNatureOfTraining);
                }

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