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 PMSUpdateHistoryService : ServiceTemplate
    {
        #region Object Mapping

        private void MapObject(PMSUpdateHistory oObjective, DataReader oReader)
        {
            SetObjectID(oObjective, oReader.GetInt32("PMSUpdateHistoryID", 0));
            oObjective.ObjectiveSetID = oReader.GetInt32("ObjectiveSetID", 0);
            oObjective.PMPYearID = oReader.GetInt32("PMPYearID", 0);
            oObjective.ObjectID = oReader.GetInt32("ObjectID", 0);
            oObjective.ChangeType = (EnumObjectiveChangeType)oReader.GetInt32("ChangeType").GetValueOrDefault();
            oObjective.ActorType = (EnumActor)oReader.GetInt32("ActorType").GetValueOrDefault();
            oObjective.PmpStatus = (EnumPMPStatus)oReader.GetInt32("PmpStatus").GetValueOrDefault();
            oObjective.Sequence = oReader.GetInt32("Sequence").GetValueOrDefault();
            oObjective.Description = oReader.GetString("Description");

            this.SetObjectState(oObjective, ObjectState.Saved);
        }

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

        #endregion

        #region IObjectiveService Members

        #region Get All

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

        #endregion

        #region Get By ID

        public PMSUpdateHistory Get(int id)
        {
            PMSUpdateHistory oObjective = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(PMSUpdateHistoryDA.Get(tc, id));

                oObjective = this.CreateObject<PMSUpdateHistory>(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 oObjective;
        }

        #endregion

        #region Get By Employee ID

        public List<PMSUpdateHistory> GetByPMSYearID(int PMPID)
        {
            List<PMSUpdateHistory> oObjectives = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(PMSUpdateHistoryDA.GetByPMSYearID(tc, PMPID));
                //if (oreader.Read())
                //{
                //    oObjectives = this.CreateObjects<Objective>(oreader);
                //}
                oObjectives = this.CreateObjects<PMSUpdateHistory>(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 oObjectives;
        }

        public List<PMSUpdateHistory> GetByObjectiveSetID(int ObjSetid)
        {
            List<PMSUpdateHistory> oObjectives = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(PMSUpdateHistoryDA.GetByObjectiveSetID(tc, ObjSetid));
                //if (oreader.Read())
                //{
                //    oObjectives = this.CreateObjects<Objective>(oreader);
                //}
                oObjectives = this.CreateObjects<PMSUpdateHistory>(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 oObjectives;
        }


        #region Insert

        public int Save(PMSUpdateHistory item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);


                if (item.IsNew)
                {
                    //List<ObjectiveSet> objSet= ObjectiveSetService.GetByEmployeeID(item.EmployeeID, item.PMPYearID);
                    int id = tc.GenerateID("Objective", "ObjectiveID");
                    base.SetObjectID(item, (id));
                    PMSUpdateHistoryDA.Insert(tc, item);
                }
                else
                {
                    PMSUpdateHistoryDA.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
            }
        }

        public void Save(List<PMSUpdateHistory> items)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                int id = tc.GenerateID("Objective", "ObjectiveID");

                foreach (PMSUpdateHistory item in items)
                {
                    if (item.IsNew)
                    {
                        base.SetObjectID(item, (id));
                        PMSUpdateHistoryDA.Insert(tc, item);
                        id++;
                    }
                    else
                    {
                        PMSUpdateHistoryDA.Update(tc, item);
                    }


                    //return item.ID; 
                }

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

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

                #endregion
            }
        }


        public void Update(List<PMSUpdateHistory> items)
        {
            string sError = string.Empty;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                foreach (PMSUpdateHistory item in items)
                {
                    PMSUpdateHistoryDA.Update(tc, item);
                }


                tc.End();
            }
            catch (Exception e)
            {
                throw new Exception(sError);
            }
        }

        #endregion

        #region Delete

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

        #endregion
    }
}