HRM-10/HRM.DA/Service/Users/RoleService.cs
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

833 lines
30 KiB
C#

using HRM.BO;
using Ease.Core;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace HRM.DA
{
#region Role Service
public class RoleService : ServiceTemplate, IRoleService
{
public RoleService() { }
private void MapObject(Role oRole, DataReader oReader)
{
base.SetObjectID(oRole,oReader.GetInt32("RoleID").Value);
oRole.Code = oReader.GetString("Code");
oRole.Name = oReader.GetString("name");
oRole.Description = oReader.GetString("description");
oRole.Sequence = oReader.GetInt32("Sequence").Value;
// oRole.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oRole.CreatedBy = oReader.GetInt32("CreatedBy").Value;
oRole.ModifiedBy = oReader.GetInt32("AUTHORIZEDBY");
oRole.ModifiedDate = oReader.GetDateTime("AUTHORIZEDDATE");
oRole.ApproveBy = oReader.GetInt32("ApproveBy");
oRole.ApproveDate = oReader.GetDateTime("ApproveDate");
oRole.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oRole.RoleType = (EnumRoleType) oReader.GetInt32("RoleType");
oRole.RoleStatus = (EnumAuthStatus)(oReader.GetInt32("Status"));
this.SetObjectState(oRole, Ease.Core.ObjectState.Saved);
}
#region Child Object Mapping
private void MapObject(Role.RolePermission oRolePermission, DataReader oReader)
{
base.SetObjectID(oRolePermission, oReader.GetInt32("RolePermissionID").Value);
oRolePermission.RoleID = oReader.GetInt32("roleID").Value;
oRolePermission.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oRolePermission.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
//oRolePermission.RoleType =(EnumRoleType) oReader.GetInt32("RoleType");
oRolePermission.PermissionCode = oReader.GetString("PermissionCode");
oRolePermission.Name = oReader.GetString("Name");
oRolePermission.Link = oReader.GetString("Link");
//oRolePermission.Status =(EnumAuthStatus) oReader.GetInt32("Status").Value;
int? statusValue = oReader.GetInt32("Status");
oRolePermission.Status = statusValue.HasValue ? (EnumAuthStatus)statusValue.Value : EnumAuthStatus.None;
oRolePermission.parentID = oReader.GetInt32("ParentID");
oRolePermission.ApproveBy = oReader.GetInt32("ApprovedBy");
oRolePermission.ApproveDate = oReader.GetDateTime("ApprovedDate");
oRolePermission.isBookmarked = oReader.GetBoolean("bookmarked", true, false);
oRolePermission.Icon = oReader.GetString("Icon", true, null);
this.SetObjectState(oRolePermission, Ease.Core.ObjectState.Saved);
}
private void MapObject(Role.RoleActionPermission oRolePermission, DataReader oReader)
{
base.SetObjectID(oRolePermission, oReader.GetInt32("RoleActionPermissionID", 0));
oRolePermission.RoleID = oReader.GetInt32("RoleID", 0);
oRolePermission.RolePermissionID = oReader.GetInt32("RolePermissionID", 0);
oRolePermission.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oRolePermission.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oRolePermission.ActionCode = oReader.GetString("PermissionActionCode");
oRolePermission.Name = oReader.GetString("Name");
oRolePermission.Status = (EnumAuthStatus)oReader.GetInt32("Status");
oRolePermission.ApproveBy = oReader.GetInt32("ApprovedBy");
oRolePermission.ApproveDate = oReader.GetDateTime("ApprovedDate");
this.SetObjectState(oRolePermission, Ease.Core.ObjectState.Saved);
}
//private void MapObject(Role.RoleDesignation oRolePermission, DataReader oReader)
//{
// base.SetObjectID(oRolePermission, oReader.GetInt32("RoleDesignationID", 0));
// oRolePermission.RoleID = oReader.GetInt32("RoleID", 0);
// oRolePermission.DesignationID = oReader.GetInt32("DesignationID", 0);
// oRolePermission.CreatedBy = oReader.GetInt32("CreatedBy", 0);
// oRolePermission.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
// oRolePermission.RoleDesignationStatus = (EnumAuthStatus)oReader.GetInt32("RoleDesignationStatus");
// oRolePermission.ModifiedBy = oReader.GetInt32("ModifiedBy");
// oRolePermission.ModifiedDate = oReader.GetDateTime("ModifiedDate");
// this.SetObjectState(oRolePermission, Ease.Core.ObjectState.Saved);
//}
#endregion
#region CreateObject
protected override T CreateObject<T>(DataReader oReader)
{
Role oRole = new Role();
MapObject(oRole, oReader);
return oRole as T;
}
protected Role.RolePermission CreateRolePermissionObject(DataReader oReader)
{
Role.RolePermission oRole = new Role.RolePermission();
MapObject(oRole, oReader);
return oRole;
}
protected Role.RoleActionPermission CreateRoleActionPermissionObject(DataReader oReader)
{
Role.RoleActionPermission oRole = new Role.RoleActionPermission();
MapObject(oRole, oReader);
return oRole;
}
//protected Role.RoleDesignation CreateRoleDesignationObject(DataReader oReader)
//{
// Role.RoleDesignation oRole = new Role.RoleDesignation();
// MapObject(oRole, oReader);
// return oRole;
//}
#endregion
#region Related Data
//protected void GetRoleRelatedData(TransactionContext tc, Role oRole)
//{
// oRole.RolePermissions = GetRolePermission(oRole.ID,true);
// //oRole._RoleDesignation = GetRoleDesignation(oRole.ID);
//}
//protected void GetRolePermissionRelatedData(TransactionContext tc, Role.RolePermission oRole)
//{
// oRole._RoleActionPermission = GetRoleActionPermissionByRolePermissionID(oRole.ID);
//}
#endregion
#region Service implementation
#region RoleService
public List<Role> GetAllRole()
{
List<Role> roles = new List<Role>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(RoleDA.GetAllRole(tc));
roles = this.CreateObjects<Role>(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 roles;
}
public Role Get(int id)
{
Role oRole = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(RoleDA.Get(tc, id));
if (oreader.Read())
{
oRole = this.CreateObject<Role>(oreader);
}
oreader.Close();
if(oRole !=null)
{
oreader = new DataReader(RoleDA.GetRolePermissionByRoleID(tc, id));
while (oreader.Read())
{
Role.RolePermission ot = this.CreateRolePermissionObject(oreader);
//ot.setState(ObjectState.Saved);
oRole.RolePermissions.Add(ot);
}
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 oRole;
}
public DataSet GetRoleAudit(int type, DateTime fromDate, DateTime ToDate)
{
DataSet role = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
role = RoleDA.GetRoleAudit(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 role;
}
public DataSet GetRolePermissionRoot(int nRoleID)
{
DataSet role = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
role = RoleDA.GetRoleRootPermissionName(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
}
return role;
}
public List<Role> Get(EnumAuthStatus status)
{
List<Role> roles = new List<Role>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(RoleDA.Get(tc, status));
roles = this.CreateObjects<Role>(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 roles;
}
public List<Role> GetRole()
{
List<Role> roles = new List<Role>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(RoleDA.GetRole(tc));
roles = this.CreateObjects<Role>(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 roles;
}
public int Save(Role oRole)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oRole.IsNew)
{
int id = tc.GenerateID("Role", "RoleID");
int seqNo = tc.GenerateID("Role", "Sequence");
oRole.Sequence = seqNo;
base.SetObjectID(oRole, id);
RoleDA.Insert(tc, oRole);
}
else
{
RoleDA.Update(tc, oRole);
#region Delete Children
RoleDA.Delete(tc, "RolePermission", "RoleID", oRole.ID);
// RoleDA.Delete(tc, "Role.RoleActionPermission", "RoleID", oRole.ID);
// RoleDA.Delete(tc, "RoleDesignation", "RoleID", oRole.ID);
#endregion
}
if (oRole.RolePermissions != null & oRole.RolePermissions.Count > 0)
{
int maxID = oRole.RolePermissions.Max(x => x.ID);
int tempid = oRole.RolePermissions[0].ID;
int newid = RoleDA.GetNewID(tc, "RolePermission", "RolePermissionID");
newid = newid + 1;
if (newid <= maxID)
newid = maxID + 1;
for (int i=0; i< oRole.RolePermissions.Count; i ++)
{
tempid = oRole.RolePermissions[i].ID;
oRole.RolePermissions[i].ID = newid;
oRole.RolePermissions.ForEach(x => {
if (x.parentID == tempid)
x.parentID = newid;
});
newid = newid + 1;
}
foreach (Role.RolePermission rolePermission in oRole.RolePermissions)
{
rolePermission.RoleID = oRole.ID;
rolePermission.CreatedBy = oRole.CreatedBy;
rolePermission.CreatedDate = oRole.CreatedDate;
//if (oRole.RoleStatus == EnumAuthStatus.Approved )
//{
// if (rolePermission.Status == EnumAuthStatus.DeletedNotYetApprove)
// continue;
// else rolePermission.Status = EnumAuthStatus.None;
//}
if (oRole.RoleStatus == EnumAuthStatus.Approved)
{
if (rolePermission.Status == EnumAuthStatus.DeletedNotYetApprove)
continue;
else if (rolePermission.Status == EnumAuthStatus.EditedNotYetApprove || rolePermission.Status == EnumAuthStatus.NewNotYetApprove || rolePermission.Status == EnumAuthStatus.Approved || rolePermission.Status == EnumAuthStatus.None)
rolePermission.Status = EnumAuthStatus.Approved;
else
rolePermission.Status = EnumAuthStatus.None;
}
else
{
if (rolePermission.Status == EnumAuthStatus.Approved || rolePermission.Status == EnumAuthStatus.None)
rolePermission.Status = EnumAuthStatus.Approved;
}
RoleDA.Insert(tc, rolePermission);
foreach (Role.RoleActionPermission RoleActionPermission in rolePermission._RoleActionPermission)
{
RoleActionPermission.RoleID = oRole.ID;
RoleActionPermission.RolePermissionID = rolePermission.ID;
this.SetObjectID(RoleActionPermission, RoleDA.GetNewID(tc, "Role.RoleActionPermission", "RoleActionPermissionID"));
RoleDA.Insert(tc, RoleActionPermission);
}
}
}
tc.End();
return oRole.ID;
}
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);
#region Delete Children
RoleDA.DeleteRoleActionPermission(tc, id);
RoleDA.DeleteRolePermission(tc, id);
RoleDA.DeleteRoleDesignation(tc, id);
#endregion
RoleDA.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
#region Child Service Implementation
#region RolePermision
public List<Role.RolePermission> GetRolePermission(int id)
{
List<Role.RolePermission> oRolePermission = new List<Role.RolePermission>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(RoleDA.GetRolePermissionByRoleID(tc, id));
while (oreader.Read())
{
Role.RolePermission ot = this.CreateRolePermissionObject(oreader);
//ot.setState(ObjectState.Saved);
oRolePermission.Add(ot);
}
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<WebMenuHead> GetRolePermissionbyUserID(int Userid)
{
List<WebMenuHead> menus = new List<WebMenuHead>();
UserService ouservice = new UserService();
User ouser = ouservice.Get(Userid);
if (ouser == null)
{
throw new Exception("Invalid User ID");
}
if (ouser.SISU == true)
{
WebMenu omenu = new WebMenu();
return omenu.AdminMenus;
}
TransactionContext tc = null;
try
{
DataReader oreader;
bool isLineManager = false;
tc = TransactionContext.Begin();
#region OLD
//if (ouser.UserType == EnumUserType.Employee)
//{
// isLineManager = new EmployeeService().CheckLineManager(tc, ouser.EmployeeID != null ? ouser.EmployeeID.Value : 0);
// if (isLineManager)
// {
// oreader = new DataReader(RoleDA.GetRolePermissionByUserIDForLM(tc, Userid));
// }
// else
// {
// oreader = new DataReader(RoleDA.GetRolePermissionByUserID(tc, Userid));
// }
//}
//else
//{
// oreader = new DataReader(RoleDA.GetRolePermissionByUserID(tc, Userid));
//}
#endregion
oreader = new DataReader(RoleDA.GetRolePermissionByUserID(tc, ouser.ID, ouser.UserType == EnumUserType.Employee ? ouser.EmployeeID ?? 0 : 0));
List<Role.RolePermission> perissins = new List<Role.RolePermission>();
while (oreader.Read())
{
Role.RolePermission ot = this.CreateRolePermissionObject(oreader);
perissins.Add(ot);
}
oreader.Close();
tc.End();
if (perissins.Count > 0)
{
var parents = perissins.FindAll(x => x.parentID == null);
if (parents != null)
{
WebMenuHead hhead = new WebMenuHead("H01", "");
menus.Add(hhead);
hhead.items = new List<WebMenuGroup>();
hhead.items.Add(new WebMenuGroup("H01.H01", "Dashboard", "/"));
foreach (Role.RolePermission item in parents)
{
WebMenuHead oheadItem = menus.FirstOrDefault(x => x.menuCode == item.PermissionCode);
if (oheadItem == null)
{
// WebMenuHead's own constructor already sets a default icon, so a
// missing/blank item.Icon (no menuIcon in Menu.xml yet) falls back to it.
oheadItem = new WebMenuHead(item.PermissionCode, item.Name);
oheadItem.icon = !string.IsNullOrWhiteSpace(item.Icon) ? item.Icon : oheadItem.icon;
menus.Add(oheadItem);
}
var firstChilds = perissins.FindAll(x => x.parentID == item.ID);
if (firstChilds == null) oheadItem.icon = "pi pi-fw pi-th-large";
foreach (Role.RolePermission fchild in firstChilds)
{
if (oheadItem.items == null)
oheadItem.items = new List<WebMenuGroup>();
WebMenuGroup onewGroup = oheadItem.items.FirstOrDefault(x => x.menuCode == fchild.PermissionCode);
if (onewGroup == null)
{
onewGroup = new WebMenuGroup(fchild.PermissionCode, fchild.Name, fchild.Link);
onewGroup.icon = !string.IsNullOrWhiteSpace(fchild.Icon) ? fchild.Icon : onewGroup.icon;
oheadItem.items.Add(onewGroup);
}
var secondChilds = perissins.FindAll(x => x.parentID == fchild.ID);
if (secondChilds==null) onewGroup.icon = "pi pi-fw pi-th-large";
foreach (Role.RolePermission sechild in secondChilds)
{
if (onewGroup.items == null)
onewGroup.items = new List<WebMenuItem>();
WebMenuItem thirdchild = onewGroup.items.FirstOrDefault(x => x.menuCode == sechild.PermissionCode);
if (thirdchild == null)
{
thirdchild = new WebMenuItem(sechild.PermissionCode, sechild.Name, sechild.Link);
thirdchild.icon = !string.IsNullOrWhiteSpace(sechild.Icon) ? sechild.Icon : thirdchild.icon;
onewGroup.items.Add(thirdchild);
}
var thirdChilds = perissins.FindAll(x => x.parentID == sechild.ID);
if (thirdChilds == null) thirdchild.icon = "pi pi-fw pi-th-large";
foreach (Role.RolePermission fourthChild in thirdChilds)
{
if (thirdchild.items == null)
thirdchild.items = new List<WebMenuItemDetail>();
WebMenuItemDetail menufourthItem = thirdchild.items.FirstOrDefault(x => x.menuCode == fourthChild.PermissionCode);
if (menufourthItem == null)
{
menufourthItem = new WebMenuItemDetail(fourthChild.PermissionCode, fourthChild.Name, fourthChild.Link);
menufourthItem.icon = !string.IsNullOrWhiteSpace(fourthChild.Icon) ? fourthChild.Icon : menufourthItem.icon;
thirdchild.items.Add(menufourthItem);
}
}
}
}
}
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
SortMenuTree(menus);
ApplyMenuStyles(menus);
return menus;
}
// Icons come entirely from RolePermission.Icon (sourced from the uploaded Menu.xml's
// menuIcon attribute, see SystemConfigarationDA.UpdateRolePermission) and were already
// applied when each node was created above; a node with no DB icon simply keeps the
// generic default its own WebMenuHead/WebMenuGroup/WebMenuItem(Detail) constructor set
// (one default for a parent node, a different one for a child node) - no per-menu-code
// hardcoding here. This method only still handles ordering/dividers via WebMenuStyle.Arrange.
void ApplyMenuStyles(List<WebMenuHead> menus)
{
foreach (var head in menus)
{
if (head.items == null) continue;
WebMenuStyle.Arrange(head.menuCode, head.items);
foreach (var group in head.items)
{
if (group.items == null) continue;
WebMenuStyle.Arrange(group.menuCode, group.items);
}
}
}
void SortMenuTree(List<WebMenuHead> menus)
{
if (menus == null) return;
menus.Sort((a, b) => CompareMenuCode(a.menuCode, b.menuCode));
foreach (var head in menus)
{
if (head.items != null)
SortMenuGroupList(head.items);
}
}
void SortMenuGroupList(List<WebMenuGroup> groups)
{
if (groups == null) return;
groups.Sort((a, b) => CompareMenuCode(a.menuCode, b.menuCode));
foreach (var group in groups)
{
if (group.items != null)
SortMenuItemList(group.items);
}
}
void SortMenuItemList(List<WebMenuItem> items)
{
if (items == null) return;
items.Sort((a, b) => CompareMenuCode(a.menuCode, b.menuCode));
foreach (var item in items)
{
if (item.items != null)
SortMenuItemDetailList(item.items);
}
}
void SortMenuItemDetailList(List<WebMenuItemDetail> details)
{
if (details == null) return;
details.Sort((a, b) => CompareMenuCode(a.menuCode, b.menuCode));
}
// Numeric hierarchy compare
int CompareMenuCode(string a, string b)
{
var aParts = a?.Split('.').Select(s => int.TryParse(s, out int v) ? v : 0).ToArray() ?? new int[0];
var bParts = b?.Split('.').Select(s => int.TryParse(s, out int v) ? v : 0).ToArray() ?? new int[0];
int len = Math.Max(aParts.Length, bParts.Length);
for (int i = 0; i < len; i++)
{
int av = i < aParts.Length ? aParts[i] : 0;
int bv = i < bParts.Length ? bParts[i] : 0;
if (av != bv) return av.CompareTo(bv);
}
return 0;
}
//public List<Role.RolePermission> GetRolePermission(int id, bool IsRelatedDataNeeded)
//{
// List<Role.RolePermission> oRolePermission = new List<Role.RolePermission>();
// TransactionContext tc = null;
// try
// {
// tc = TransactionContext.Begin();
// DataReader oreader = new DataReader(RoleDA.GetRolePermissionByRoleID(tc, id));
// while (oreader.Read())
// {
// Role.RolePermission ot = this.CreateRolePermissionObject(oreader);
// if(IsRelatedDataNeeded)
// GetRolePermissionRelatedData(tc, ot);
// //ot.setState(ObjectState.Saved);
// oRolePermission.Add(ot);
// }
// 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;
//}
#endregion
#region RoleActionPermision
public List<Role.RoleActionPermission> GetRoleActionPermissionByRoleID(int id)
{
List<Role.RoleActionPermission> oRolePermission = new List<Role.RoleActionPermission>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(RoleDA.GetRoleActionPermissionByRoleID(tc, id));
while (oreader.Read())
{
Role.RoleActionPermission ot = this.CreateRoleActionPermissionObject(oreader);
oRolePermission.Add(ot);
}
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<Role.RoleActionPermission> GetRoleActionPermissionByRolePermissionID(int id)
{
List<Role.RoleActionPermission> oRolePermission = new List<Role.RoleActionPermission>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(RoleDA.GetRoleActionPermissionByRolePermissionID(tc, id));
while (oreader.Read())
{
Role.RoleActionPermission ot = this.CreateRoleActionPermissionObject(oreader);
oRolePermission.Add(ot);
}
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;
}
#endregion
#endregion
#endregion
}
#endregion
}