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;
using HRM.DA;


namespace HRM.DA
{
    public class SurveyCategoryService : ServiceTemplate, ISurveyCategoryService
    {
        public SurveyCategoryService()
        {
        }

        private void MapObject(SurveyCategory oSurveyCategory, DataReader oReader)
        {
            base.SetObjectID(oSurveyCategory, (oReader.GetInt32("CategoryID").Value));
            oSurveyCategory.Code = oReader.GetString("Code");
            oSurveyCategory.Description = oReader.GetString("Description");
            oSurveyCategory.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oSurveyCategory.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oSurveyCategory.CreatedDate = oReader.GetDateTime("CreationDate").HasValue
                ? oReader.GetDateTime("CreationDate").Value
                : DateTime.MinValue;
            oSurveyCategory.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oSurveyCategory.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;
            this.SetObjectState(oSurveyCategory, Ease.Core.ObjectState.Saved);
        }

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

        protected SurveyCategory CreateObject(DataReader oReader)
        {
            SurveyCategory oSurveyCategory = new SurveyCategory();
            MapObject(oSurveyCategory, oReader);
            return oSurveyCategory;
        }

        #region Service implementation

        public string GetNextCode()
        {
            TransactionContext tc = null;
            string _code = "";
            try
            {
                tc = TransactionContext.Begin();
                //####   _code = GlobalFunctionService.GetMaxCode(tc, "SurveyCategory", "codeautogenerate", "SurveyCategory", "Code");
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return _code;
        }

        public SurveyCategory Get(int id)
        {
            SurveyCategory oSurveyCategory = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(SurveyCategoryDA.Get(tc, id));
                if (oreader.Read())
                {
                    oSurveyCategory = this.CreateObject<SurveyCategory>(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 oSurveyCategory;
        }

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

        public List<SurveyCategory> Get(EnumStatus status)
        {
            List<SurveyCategory> SurveyCategorys = null;

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

                DataReader dr = new DataReader(SurveyCategoryDA.Get(tc, status));
                SurveyCategorys = this.CreateObjects<SurveyCategory>(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 SurveyCategorys;
        }

        public int Save(SurveyCategory oSurveyCategory)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oSurveyCategory.IsNew)
                {
                    int id = tc.GenerateID("SurveyCategory", "CategoryID");
                    //int seqID = tc.GenerateID("SurveyCategory", "SequenceNO");
                    base.SetObjectID(oSurveyCategory, (id));
                    // oSurveyCategory.Sequence = seqID;
                    SurveyCategoryDA.Insert(tc, oSurveyCategory);
                }
                else
                {
                    SurveyCategoryDA.Update(tc, oSurveyCategory);
                }

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