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 RelationService : ServiceTemplate, IRelationService
    {
        #region constructor

        public RelationService()
        {
        }

        #endregion

        #region Object

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

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

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

        #endregion

        #region Service implementation

        public Relation Get(int id)
        {
            Relation oRelation = null;

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

        public Relation Get(string sCode)
        {
            Relation oRelation = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(RelationDA.Get(tc, sCode));
                if (oreader.Read())
                {
                    oRelation = this.CreateObject<Relation>(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 oRelation;
        }

        public List<Relation> Get()
        {
            List<Relation> relations = new List<Relation>();

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

                DataReader dr = new DataReader(RelationDA.Get(tc));
                relations = this.CreateObjects<Relation>(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 relations;
        }

        public List<Relation> Get(EnumStatus status)
        {
            List<Relation> relations = new List<Relation>();

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

                DataReader dr = new DataReader(RelationDA.Get(tc, status));
                relations = this.CreateObjects<Relation>(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 relations;
        }

        public int Save(Relation oRelation)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oRelation.IsNew)
                {
                    int id = tc.GenerateID("PERSONRELATION", "RELATIONID");
                    base.SetObjectID(oRelation, id);
                    if (oRelation.Code == "")
                        oRelation.Code = oRelation.ID.ToString();
                    RelationDA.Insert(tc, oRelation);
                }
                else
                {
                    RelationDA.Update(tc, oRelation);
                }

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