using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using System.Data; using HRM.BO; namespace HRM.DA { public class RolePermissionService : ServiceTemplate { public RolePermissionService() { } private void MapObject(Role.RolePermission oRolePermission, DataReader oReader) { base.SetObjectID(oRolePermission, oReader.GetInt32("RolePermissionID", 0)); oRolePermission.RoleID = oReader.GetInt32("roleID", 0); oRolePermission.CreatedBy = oReader.GetInt32("CreatedBy", 0); oRolePermission.CreatedDate = oReader.GetDateTime("CreatedDate").Value; oRolePermission.PermissionCode = oReader.GetString("PermissionCode"); oRolePermission.Name = oReader.GetString("Name"); oRolePermission.Link = oReader.GetString("Link"); oRolePermission.Status = (EnumAuthStatus)oReader.GetInt32("AuthStatus"); oRolePermission.ApproveBy = oReader.GetInt32("ApproveBy").Value; oRolePermission.ApproveDate = oReader.GetDateTime("ApproveDate").Value; oRolePermission.ModifiedBy = oReader.GetInt32("ModifiedBy").Value; oRolePermission.ModifiedDate = oReader.GetDateTime("ModifiedDate").Value; this.SetObjectState(oRolePermission, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Role.RolePermission oRolePermission = new Role.RolePermission(); MapObject(oRolePermission, oReader); return oRolePermission as T; } #region Service implementation public Role.RolePermission Get(int id) { Role.RolePermission oRolePermission = new Role.RolePermission(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(RolePermissionDA.Get(tc, id)); if (oreader.Read()) { oRolePermission = 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 oRolePermission; } public List Get() { List rolePermissions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(RolePermissionDA.Get(tc)); rolePermissions = 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 rolePermissions; } //public RolePermission Get(string sKey, int RoleID) //{ // RolePermission oRolePermission = null; // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // DataReader oreader = new DataReader(RolePermissionDA.Get(tc, sKey, RoleID)); // if (oreader.Read()) // { // oRolePermission = 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 oRolePermission; //} public List GetByRoleID(int roleID) { List rolePermissions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(RolePermissionDA.Get(tc, roleID)); rolePermissions = 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 rolePermissions; } public DataSet GetRolePerAudit(int type, DateTime fromDate, DateTime ToDate) { DataSet rolePer = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); rolePer = RolePermissionDA.GetRolePerAudit(tc, type, fromDate, ToDate); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return rolePer; } public List GetAll(int roleID) { List rolePermissions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(RolePermissionDA.GetAll(tc, roleID)); rolePermissions = 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 rolePermissions; } public List GetAllItem(int roleID) { List rolePermissions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(RolePermissionDA.GetAllItem(tc, roleID)); rolePermissions = 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 rolePermissions; } public List Get(int roleID, EnumAuthStatus eStatus) { List rolePermissions = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(RolePermissionDA.Get(tc, roleID, eStatus)); rolePermissions = 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 rolePermissions; } public int Save(Role.RolePermission oRolePermission) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oRolePermission.IsNew) { int id = tc.GenerateID("RolePermission", "RolePermissionID"); base.SetObjectID(oRolePermission, id); RolePermissionDA.Insert(tc, oRolePermission); } else { RolePermissionDA.Update(tc, oRolePermission); } tc.End(); return oRolePermission.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void DeleteByRoleID(int nRoleID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); RolePermissionDA.DeleteByRoleID(tc, nRoleID); 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 Save(List _RolePermissions) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); //RolePermissionDA.DeleteByRoleID(tc, _RolePermissions[0].RoleID); foreach (Role.RolePermission permission in _RolePermissions) { //if (permission.IsNew) //{ int id = tc.GenerateID("RolePermission", "RolePermissionID"); base.SetObjectID(permission, id); RolePermissionDA.Insert(tc, permission); //} //tc.ExecuteNonQuery("Update UserRole set status=%n,createdby=%n where roleid=%n",(int)EnumStatus.Inactive,Payroll.BO.User.CurrentUser.ID.Integer, _RolePermissions[0].RoleID.Integer); } 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 Update(List _RolePermissions) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (Role.RolePermission permission in _RolePermissions) { RolePermissionDA.Update(tc, permission); } 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 Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); RolePermissionDA.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 } }