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

namespace HRM.DA
{
	public class MOBILEAppVersionService : ServiceTemplate, IMOBILEAppVersionService
    {
        public MOBILEAppVersionService() { }

        private void MapObject(MOBILEAppVersion oMOBILEAppVersion, DataReader oReader)
        {
            base.SetObjectID(oMOBILEAppVersion, oReader.GetInt32("MOBILEAppVersionID").Value);
            oMOBILEAppVersion.VersionNumber = oReader.GetString("VersionNumber");
            oMOBILEAppVersion.DownloadMessage = oReader.GetString("DownloadMessage");
            oMOBILEAppVersion.DownloadLink = oReader.GetString("DownloadLink");
            oMOBILEAppVersion.IsCurrentVersion = oReader.GetBoolean("IsCurrentVersion").Value;
            oMOBILEAppVersion.AllowOldVersion = oReader.GetBoolean("AllowOld").Value;
            this.SetObjectState(oMOBILEAppVersion, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public MOBILEAppVersion GetLatestVersion()
        {
            MOBILEAppVersion oAppVersion = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(MOBILEAppVersionDA.GetLatestVersion(tc));
                if (oreader.Read())
                {
                    oAppVersion = this.CreateObject<MOBILEAppVersion>(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 oAppVersion;
        }

        #endregion
    }
}