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 OccupationService : ServiceTemplate, IOccupationService
    {
        #region constructor

        public OccupationService()
        {
        }

        #endregion

        #region Object

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

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

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

        #endregion

        #region Service implementation

        public Occupation Get(int id)
        {
            Occupation oOccupation = null;

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

        public Occupation Get(TransactionContext tc, int id)
        {
            Occupation oOccupation = null;
            try
            {
                DataReader oreader = new DataReader(OccupationDA.Get(tc, id));
                if (oreader.Read())
                {
                    oOccupation = this.CreateObject<Occupation>(oreader);
                }

                oreader.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return oOccupation;
        }


        public Occupation Get(string sCode)
        {
            Occupation oOccupation = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(OccupationDA.Get(tc, sCode));
                if (oreader.Read())
                {
                    oOccupation = this.CreateObject<Occupation>(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 oOccupation;
        }

        public List<Occupation> Get()
        {
            List<Occupation> occupations = new List<Occupation>();

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

                DataReader dr = new DataReader(OccupationDA.Get(tc));
                occupations = this.CreateObjects<Occupation>(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 occupations;
        }

        public List<Occupation> Get(EnumStatus status)
        {
            List<Occupation> occupations = new List<Occupation>();

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

                DataReader dr = new DataReader(OccupationDA.Get(tc, status));
                occupations = this.CreateObjects<Occupation>(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 occupations;
        }

        public int Save(Occupation oOccupation)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oOccupation.IsNew)
                {
                    int id = tc.GenerateID("OCCUPATION", "OCCUPATIONID");
                    base.SetObjectID(oOccupation, id);
                    if (oOccupation.Code == "")
                        oOccupation.Code = oOccupation.ID.ToString();
                    OccupationDA.Insert(tc, oOccupation);
                }
                else
                {
                    OccupationDA.Update(tc, oOccupation);
                }

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