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

namespace HRM.DA
{
    public class OtherTalentService : ServiceTemplate, IOtherTalentService
    {
        #region constructor

        public OtherTalentService()
        {
        }

        #endregion

        #region object

        private void MapObject(OtherTalent oOtherTalent, DataReader oReader)
        {
            base.SetObjectID(oOtherTalent, oReader.GetInt32("OTHERTALENTID").Value);
            oOtherTalent.Code = oReader.GetString("CODE");
            oOtherTalent.Description = oReader.GetString("DESCRIPTION");
            oOtherTalent.Sequence = oReader.GetInt32("SequenceNo").Value;
            oOtherTalent.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oOtherTalent.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oOtherTalent.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oOtherTalent.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oOtherTalent.ModifiedDate = oReader.GetDateTime("ModifiedDate");

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

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

        #endregion

        #region service implementation

        public OtherTalent Get(int id)
        {
            OtherTalent oOtherTalent = null;

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

        public OtherTalent Get(string sCode)
        {
            OtherTalent oOtherTalent = null;


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

        public List<OtherTalent> Get()
        {
            List<OtherTalent> otherTalents = new List<OtherTalent>();

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

                DataReader dr = new DataReader(OtherTalentDA.Get(tc));
                otherTalents = this.CreateObjects<OtherTalent>(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 otherTalents;
        }

        public List<OtherTalent> Get(EnumStatus status)
        {
            List<OtherTalent> otherTalents = new List<OtherTalent>();

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

                DataReader dr = new DataReader(OtherTalentDA.Get(tc, status));
                otherTalents = this.CreateObjects<OtherTalent>(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 otherTalents;
        }

        public int Save(OtherTalent oOtherTalent)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oOtherTalent.IsNew)
                {
                    int id = tc.GenerateID("OTHERTALENT", "OTHERTALENTID");
                    base.SetObjectID(oOtherTalent, id);
                    OtherTalentDA.Insert(tc, oOtherTalent);
                }
                else
                {
                    OtherTalentDA.Update(tc, oOtherTalent);
                }

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