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 UserLogInInfoService : ServiceTemplate, IUserLogInInfoService { public UserLogInInfoService() { } private void MapObject(UserLogInInfo oUserLogInInfo, DataReader oReader) { base.SetObjectID(oUserLogInInfo, oReader.GetInt32("SlID").Value); oUserLogInInfo.UserID = oReader.GetInt32("UserID", 0); oUserLogInInfo.LoginTime = oReader.GetDateTime("LoginTime").Value; oUserLogInInfo.PCNumber = oReader.GetString("PCNumber"); oUserLogInInfo.IPAddress = oReader.GetString("IPAddress"); oUserLogInInfo.DeviceId = oReader.GetString("DeviceId"); if (oReader.GetDateTime("LogoutTime") != null) oUserLogInInfo.LogoutTime = Convert.ToDateTime(oReader.GetString("LogoutTime")); oUserLogInInfo.LogoutType = oReader.GetString("LogoutType") == null ? EnumLogoutType.NotInitiate : (EnumLogoutType)oReader.GetInt32("LogoutType").Value; } protected override T CreateObject(DataReader oReader) { UserLogInInfo oUserLogInInfo = new UserLogInInfo(); MapObject(oUserLogInInfo, oReader); return oUserLogInInfo as T; } #region Service implementation public UserLogInInfo Get(int id) { UserLogInInfo oUserLogInInfo = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(UserLogInInfoDA.Get(tc, id)); if (oreader.Read()) { oUserLogInInfo = 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 oUserLogInInfo; } public List Get() { List UserLogInInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UserLogInInfoDA.Get(tc)); UserLogInInfos = 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 UserLogInInfos; } public List GetLoggedUsers() { List UserLogInInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UserLogInInfoDA.GetLoggedUsers(tc)); UserLogInInfos = 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 UserLogInInfos; } public List Get(string sLoginID) { List UserLogInInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UserLogInInfoDA.Get(tc, sLoginID)); UserLogInInfos = 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 UserLogInInfos; } public List GetLastLogin(string sLoginID, bool isWebLogin) { List UserLogInInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = null; if (!isWebLogin) dr = new DataReader(UserLogInInfoDA.GetLastLogin(tc, sLoginID)); else dr = new DataReader(UserLogInInfoDA.GetLastWebLogin(tc, sLoginID)); UserLogInInfos = 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 UserLogInInfos; } public void UpdateLogoutTime(int nLoginID) { TransactionContext tc = null; tc = TransactionContext.Begin(); UserLogInInfoDA.UpdateLogoutTime(tc, nLoginID); tc.End(); } public List Get(DateTime dtFrom, DateTime dtTo) { List UserLogInInfos = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UserLogInInfoDA.Get(tc, dtFrom, dtTo)); UserLogInInfos = 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 UserLogInInfos; } public List GetAuditTrail(DateTime dtFrom, DateTime dtTo) { List items = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UserLogInInfoDA.GetAuditTrail(tc, dtFrom, dtTo)); while (dr.Read()) { UserLogInInfo 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 long NoofLogin(int nUserID) { long nCount = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(); nCount = UserLogInInfoDA.NoofLogin(tc, nUserID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return nCount; } public int Save(UserLogInInfo oUserLogInInfo) { TransactionContext tc = null; try { // Before the transaction: the computer-name lookup can take a moment. if (oUserLogInInfo.IsNew) FillClientInfo(oUserLogInInfo); tc = TransactionContext.Begin(true); if (oUserLogInInfo.IsNew) { int id = tc.GenerateID("UserLoginInfo", "SlID"); base.SetObjectID(oUserLogInInfo, id); UserLogInInfoDA.Insert(tc, oUserLogInInfo); } else UserLogInInfoDA.Update(tc, oUserLogInInfo); tc.End(); return oUserLogInInfo.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public int Save(TransactionContext tc, UserLogInInfo oUserLogInInfo) { try { if (oUserLogInInfo.IsNew) { FillClientInfo(oUserLogInInfo); UserLogInInfoDA.Insert(tc, oUserLogInInfo); } else UserLogInInfoDA.Update(tc, oUserLogInInfo); return oUserLogInInfo.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } /// /// Fills IPAddress and DeviceId from the current HTTP request, keeping whatever the /// caller already set (e.g. the mobile login knows the DeviceID from its request body). /// Call it on the request thread BEFORE handing the login to a background thread /// (UserService.SaveLoginfo): once the request is over there is nothing left to read. /// public static void CaptureRequestInfo(UserLogInInfo oUserLogInInfo) { if (string.IsNullOrWhiteSpace(oUserLogInInfo.IPAddress)) oUserLogInInfo.IPAddress = ClientRequestInfo.GetIPAddress(); if (string.IsNullOrWhiteSpace(oUserLogInInfo.DeviceId)) oUserLogInInfo.DeviceId = ClientRequestInfo.GetDeviceId(); } /// /// IP / device id from the request (when still reachable), then the computer name: /// reverse DNS of that IP, or the IP itself when the network has no name for it. /// private static void FillClientInfo(UserLogInInfo oUserLogInInfo) { CaptureRequestInfo(oUserLogInInfo); if (string.IsNullOrWhiteSpace(oUserLogInInfo.PCNumber)) oUserLogInInfo.PCNumber = ClientRequestInfo.ResolveComputerName(oUserLogInInfo.IPAddress); } public void Delete(int id) { //Not Needed } #endregion } }