EchoTex_Payroll/HRM.DA/Service/Users/RoleService.cs

720 lines
26 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
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);
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
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(RoleDA.GetRolePermissionByUserID(tc, Userid));
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", "Dash-Board", "/"));
foreach (Role.RolePermission item in parents)
{
WebMenuHead oheadItem = menus.FirstOrDefault(x => x.menuCode == item.PermissionCode);
if (oheadItem == null)
{
oheadItem = new WebMenuHead(item.PermissionCode, item.Name);
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);
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);
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);
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
}
return menus;
}
//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
}