using System; using System.Data; using System.Linq; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.DataAccess; using System.Collections.Generic; using Payroll.BO; using Ease.CoreV35.Caching; namespace Payroll.Service { #region RolePermission Service [Serializable] public class RolePermissionService : ServiceTemplate, IRolePermissionService { #region Private functions and declaration Cache _cache = new Cache(typeof(RolePermission)); #endregion public RolePermissionService() { } private void MapObject(RolePermission oRolePermission, DataReader oReader) { base.SetObjectID(oRolePermission, oReader.GetID("RolePermissionID")); oRolePermission.MenuKey = oReader.GetString("menuKey"); oRolePermission.RoleID = oReader.GetID("roleID"); oRolePermission.CreatedBy = oReader.GetID("CreatedBy"); oRolePermission.CreatedDate = oReader.GetDateTime("CreatedDate").Value; oRolePermission.Add = oReader.GetBoolean("AddButton").Value; oRolePermission.Edit = oReader.GetBoolean("EditButton").Value; oRolePermission.DeleteButton = oReader.GetBoolean("DeleteButton").Value; oRolePermission.Authorize = oReader.GetBoolean("AuthorizeButton").Value; oRolePermission.Lock = oReader.GetBoolean("LockButton").Value; oRolePermission.Process = oReader.GetBoolean("ProcessButton").Value; oRolePermission.Undo = oReader.GetBoolean("UndoButton").Value; oRolePermission.Approve = oReader.GetBoolean("ApproveButton").Value; oRolePermission.Active = oReader.GetBoolean("ActiveButton").Value; oRolePermission.Up = oReader.GetBoolean("UpButton").Value; oRolePermission.Down = oReader.GetBoolean("DownButton").Value; oRolePermission.SaveButton = oReader.GetBoolean("SaveButton").Value; oRolePermission.IsApproved = (EnumStatus)oReader.GetInt32("IsApproved").Value; oRolePermission.MenuPermissionStatus = (EnumMenuPermissionStatus)oReader.GetInt32("MenuPermissionStatus").Value; oRolePermission.ComputerName = oReader.GetString("ComputerName"); oRolePermission.ApprovedComputerName = oReader.GetString("ApprovedComputerName"); this.SetObjectState(oRolePermission, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { RolePermission oRolePermission = new RolePermission(); MapObject(oRolePermission, oReader); return oRolePermission as T; } protected RolePermission CreateObject(DataReader oReader) { RolePermission oRolePermission = new RolePermission(); MapObject(oRolePermission, oReader); return oRolePermission; } #region Service implementation public RolePermission Get(ID id) { RolePermission oRolePermission = new RolePermission(); #region Cache Header oRolePermission = _cache["Get", id] as RolePermission; if (oRolePermission != null) return oRolePermission; #endregion 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 } #region Cache Footer _cache.Add(oRolePermission, "Get", id); #endregion return oRolePermission; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate rolePermissions = _cache["Get"] as ObjectsTemplate; if (rolePermissions != null) return rolePermissions; #endregion 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 } #region Cache Footer _cache.Add(rolePermissions, "Get"); #endregion return rolePermissions; } public RolePermission Get(string sKey, ID nRoleID) { RolePermission oRolePermission = new RolePermission(); #region Cache Header oRolePermission = _cache["Get", sKey] as RolePermission; if (oRolePermission != null) return oRolePermission; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(RolePermissionDA.Get(tc, sKey, nRoleID.Integer)); 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 } #region Cache Footer _cache.Add(oRolePermission, "Get", sKey, nRoleID.Integer); #endregion return oRolePermission; } public ObjectsTemplate Get(int roleID) { #region Cache Header ObjectsTemplate rolePermissions = _cache["Get", roleID] as ObjectsTemplate; if (rolePermissions != null) return rolePermissions; #endregion 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 } #region Cache Footer _cache.Add(rolePermissions, "Get", roleID); #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 ObjectsTemplate GetAll(int roleID) { #region Cache Header ObjectsTemplate rolePermissions = _cache["Get", roleID] as ObjectsTemplate; if (rolePermissions != null) return rolePermissions; #endregion 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 } #region Cache Footer _cache.Add(rolePermissions, "Get", roleID); #endregion return rolePermissions; } public ObjectsTemplate GetAllItem(int roleID) { #region Cache Header ObjectsTemplate rolePermissions = _cache["Get", roleID] as ObjectsTemplate; if (rolePermissions != null) return rolePermissions; #endregion 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 } #region Cache Footer _cache.Add(rolePermissions, "Get", roleID); #endregion return rolePermissions; } public ObjectsTemplate Get(int roleID, EnumStatus eStatus) { #region Cache Header ObjectsTemplate rolePermissions = _cache["Get", roleID] as ObjectsTemplate; if (rolePermissions != null) return rolePermissions; #endregion 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 } #region Cache Footer _cache.Add(rolePermissions, "Get", roleID); #endregion return rolePermissions; } public ID Save(RolePermission oRolePermission) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oRolePermission.IsNew) { int id = tc.GenerateID("RolePermission", "RolePermissionID"); base.SetObjectID(oRolePermission, ID.FromInteger(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(ID 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(ObjectsTemplate _RolePermissions) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); //RolePermissionDA.DeleteByRoleID(tc, _RolePermissions[0].RoleID); foreach (RolePermission permission in _RolePermissions) { //if (permission.IsNew) //{ int id = tc.GenerateID("RolePermission", "RolePermissionID"); base.SetObjectID(permission, ID.FromInteger(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 Save(List _RolePermissions) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); //RolePermissionDA.DeleteByRoleID(tc, _RolePermissions[0].RoleID); foreach (RolePermission permission in _RolePermissions) { //if (permission.IsNew) //{ int id = tc.GenerateID("RolePermission", "RolePermissionID"); base.SetObjectID(permission, ID.FromInteger(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 (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(ID 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 } #endregion }