using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using HRM.BO; namespace HRM.DA { public class BadLoginAttemptInfoService : ServiceTemplate, IBadLoginAttemptInfoService { public BadLoginAttemptInfoService() { } private void MapObject(BadLoginAttemptInfo oBadLoginAttemptInfo, DataReader oReader) { base.SetObjectID(oBadLoginAttemptInfo, oReader.GetInt32("SlID").Value); oBadLoginAttemptInfo.LoginID = oReader.GetString("LoginID"); oBadLoginAttemptInfo.AttemptTime = oReader.GetDateTime("AttemptTime").Value; oBadLoginAttemptInfo.PCNumber = oReader.GetString("PCNumber"); oBadLoginAttemptInfo.IPAddress = oReader.GetString("IPAddress"); oBadLoginAttemptInfo.DeviceId = oReader.GetString("DeviceId"); } protected override T CreateObject(DataReader oReader) { BadLoginAttemptInfo oBadLoginAttemptInfo = new BadLoginAttemptInfo(); MapObject(oBadLoginAttemptInfo, oReader); return oBadLoginAttemptInfo as T; } protected BadLoginAttemptInfo CreateObject(DataReader oReader) { BadLoginAttemptInfo oBadLoginAttemptInfo = new BadLoginAttemptInfo(); MapObject(oBadLoginAttemptInfo, oReader); return oBadLoginAttemptInfo; } #region Service implementation public BadLoginAttemptInfo Get(int id) { BadLoginAttemptInfo oBadLoginAttemptInfo = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(BadLoginAttemptInfoDA.Get(tc, id)); if (oreader.Read()) { oBadLoginAttemptInfo = this.CreateObject(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 oBadLoginAttemptInfo; } public List Get() { List BadLoginAttemptInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(BadLoginAttemptInfoDA.Get(tc)); BadLoginAttemptInfos = this.CreateObjects(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 BadLoginAttemptInfos; } public List Get(DateTime dtFrom, DateTime dtTo) { List BadLoginAttemptInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(BadLoginAttemptInfoDA.Get(tc, dtFrom, dtTo)); BadLoginAttemptInfos = this.CreateObjects(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 BadLoginAttemptInfos; } public List GetAuditTrail(DateTime dtFrom, DateTime dtTo) { List items = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(BadLoginAttemptInfoDA.GetAuditTrail(tc, dtFrom, dtTo)); while (dr.Read()) { BadLoginAttemptInfo item = this.CreateObject(dr); item.LoginID = dr.GetString("LoginID"); item.UserName = dr.GetString("UserName"); items.Add(item); } 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 items; } public void Save(BadLoginAttemptInfo oBadLoginAttemptInfo) { TransactionContext tc = null; try { // IP / device id come from the request (only reachable here when Save runs on // the request thread; SaveInThread captures them before it leaves it). The // computer name is a reverse-DNS lookup of that IP, done here because it can // take a moment and SaveInThread runs this off the request thread. CaptureRequestInfo(oBadLoginAttemptInfo); if (string.IsNullOrWhiteSpace(oBadLoginAttemptInfo.PCNumber)) oBadLoginAttemptInfo.PCNumber = ClientRequestInfo.ResolveComputerName(oBadLoginAttemptInfo.IPAddress); tc = TransactionContext.Begin(true); BadLoginAttemptInfoDA.Insert(tc, oBadLoginAttemptInfo); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void SaveInThread(BadLoginAttemptInfo oBadLoginAttemptInfo) { // Read the request NOW: by the time the thread runs, the request may be over. CaptureRequestInfo(oBadLoginAttemptInfo); System.Threading.Thread saveThread = new System.Threading.Thread(() => Save(oBadLoginAttemptInfo)) { IsBackground = true }; saveThread.Start(); } /// /// Fills IPAddress and DeviceId from the current request, keeping whatever the caller /// already set (e.g. the mobile login knows the DeviceID from its request body). /// private static void CaptureRequestInfo(BadLoginAttemptInfo oBadLoginAttemptInfo) { if (string.IsNullOrWhiteSpace(oBadLoginAttemptInfo.IPAddress)) oBadLoginAttemptInfo.IPAddress = ClientRequestInfo.GetIPAddress(); if (string.IsNullOrWhiteSpace(oBadLoginAttemptInfo.DeviceId)) oBadLoginAttemptInfo.DeviceId = ClientRequestInfo.GetDeviceId(); } public void Delete(int id) { //Not Needed } #endregion } }