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

namespace HRM.DA
{
    public class PasswordHistoryService : ServiceTemplate, IPasswordHistoryService
    {
        private void MapObject(PasswordHistory oHistory, DataReader oReader)
        {
            base.SetObjectID(oHistory, oReader.GetInt32("PasswordHistoryID").Value);
            oHistory.UserID = oReader.GetInt32("UserID").Value;
            oHistory.UserPassword = oReader.GetString("UserPassword");
            oHistory.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            this.SetObjectState(oHistory, Ease.Core.ObjectState.Saved);
        }

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

        public int Save(PasswordHistory oHistory)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                int id = tc.GenerateID("PasswordHistory", "PasswordHistoryID");
                base.SetObjectID(oHistory, id);
                PasswordHistoryDA.Insert(tc, oHistory);

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

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

                #endregion
            }
        }

        public List<PasswordHistory> Get()
        {
            List<PasswordHistory> histories = new List<PasswordHistory>();

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

                DataReader dr = new DataReader(PasswordHistoryDA.Get(tc));
                histories = this.CreateObjects<PasswordHistory>(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 histories;
        }

        public List<PasswordHistory> Get(int UserID)
        {
            List<PasswordHistory> histories = new List<PasswordHistory>();

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

                DataReader dr = new DataReader(PasswordHistoryDA.Get(tc, UserID));
                histories = this.CreateObjects<PasswordHistory>(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 histories;
        }

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                PasswordHistoryDA.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
            }
        }
    }
}