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

namespace HRM.DA
{
    public class HRJoiningQuestionaryService : ServiceTemplate, IHRJoiningQuestionaryService
    {
        #region Object Mapping

        private void MapObject(HRJoiningQuestionary oHrJoiningQuestionary, DataReader oReader)
        {
            SetObjectID(oHrJoiningQuestionary, oReader.GetInt32("QuestionaryID").Value);
            oHrJoiningQuestionary.QuestionIndex = (int)oReader.GetInt32("QuestionIndex");
            oHrJoiningQuestionary.QuestionNo = oReader.GetString("QuestionNo");
            oHrJoiningQuestionary.Description = oReader.GetString("Description");
            oHrJoiningQuestionary.IsPrimary = (EnumIsPrimary)oReader.GetInt32("IsPrimary");
            oHrJoiningQuestionary.AnsDatatype = (EnumAnswerType)oReader.GetInt32("AnsDatatype");
            SetObjectState(oHrJoiningQuestionary, ObjectState.Saved);
        }

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

        #endregion

        #region Service Implementation

        public HRJoiningQuestionary Get(int nID)
        {
            HRJoiningQuestionary oHRJoiningQuestionary = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(HRJoiningQuestionaryDA.Get(tc, nID));
                if (oreader.Read())
                {
                    oHRJoiningQuestionary = CreateObject<HRJoiningQuestionary>(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 oHRJoiningQuestionary;
        }


        public List<HRJoiningQuestionary> Get(EnumStatus status)
        {
            List<HRJoiningQuestionary> oHRJoiningQuestionaries = new List<HRJoiningQuestionary>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(HRJoiningQuestionaryDA.Get(tc, status));
                // if (oreader.Read())
                //{
                oHRJoiningQuestionaries = this.CreateObjects<HRJoiningQuestionary>(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 oHRJoiningQuestionaries;
        }

        public List<HRJoiningQuestionary> GetByQuestionNo(string sQuesNo)
        {
            List<HRJoiningQuestionary> oHRJoiningQuestionaries = new List<HRJoiningQuestionary>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(HRJoiningQuestionaryDA.GetByQuestionNo(tc, sQuesNo));
                oHRJoiningQuestionaries = this.CreateObjects<HRJoiningQuestionary>(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 oHRJoiningQuestionaries;
        }


        public int Save(HRJoiningQuestionary item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("HRJoiningQuestionary", "QuestionaryID");
                    SetObjectID(item, id);
                    int quesIndex = tc.GenerateID("HRJoiningQuestionary", "QuestionIndex");
                    item.QuestionIndex = quesIndex;
                    HRJoiningQuestionaryDA.Insert(tc, item);
                }
                else
                {
                    HRJoiningQuestionaryDA.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 Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                HRJoiningQuestionaryDA.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
    }
}