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

namespace HRM.DA
{
    public class LMObjectiveService : ServiceTemplate
    {
        #region Object Mapping

        private void MapObject(LMObjective oLMObjective, DataReader oReader)
        {
            SetObjectID(oLMObjective, oReader.GetInt32("LMObjectiveID", 0));
            oLMObjective.ParentID = oReader.GetInt32("ParentID", 0);
            oLMObjective.ObjectiveID = oReader.GetInt32("ObjectiveID", 0);
            this.SetObjectState(oLMObjective, ObjectState.Saved);
        }


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

        #endregion

        #region ILMObjectiveService Members

        #region Get All

        public List<LMObjective> Get()
        {
            List<LMObjective> oLMObjective = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(LMObjectiveDA.Get(tc));
                oLMObjective = this.CreateObjects<LMObjective>(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 oLMObjective;
        }

        #endregion

        #region Get By ID

        public LMObjective Get(int id)
        {
            LMObjective oLMObjective = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(LMObjectiveDA.Get(tc, id));
                oLMObjective = this.CreateObject<LMObjective>(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 oLMObjective;
        }

        #endregion

        #region Insert

        public int Save(LMObjective item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("LMObjective", "LMObjectiveID");
                    base.SetObjectID(item, (id));
                    LMObjectiveDA.Save(tc, item);
                }
                else
                {
                    LMObjectiveDA.Update(tc, item);
                }

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

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

                #endregion
            }
        }

        #endregion

        #region Delete

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                LMObjectiveDA.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
    }
}