CEL_Payroll/Payroll.Service/Users/Service/UserRoleService.cs

526 lines
16 KiB
C#
Raw Permalink Normal View History

2024-09-17 14:30:13 +06:00
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 UserRole Service
[Serializable]
public class UserRoleService : ServiceTemplate, IUserRoleService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(UserRole));
#endregion
public UserRoleService() { }
private void MapObject(UserRole oUserRole, DataReader oReader)
{
base.SetObjectID(oUserRole, oReader.GetID("UserRoleID"));
oUserRole.RoleID = oReader.GetInt32("RoleID").Value;
oUserRole.UserID = oReader.GetID("UserID");
oUserRole.EventDate = oReader.GetDateTime("EventDate").Value;
oUserRole.CreatedBy = oReader.GetID("CreatedBy");
oUserRole.AuthorizedBy = oReader.GetID("AuthorizedBy");
oUserRole.AuthorizedDate = oReader.GetDateTime("AuthorizedDate");
oUserRole.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oUserRole.ComputerName = oReader.GetString("ComputerName");
oUserRole.ApprovedComputerName = oReader.GetString("ApprovedComputerName");
this.SetObjectState(oUserRole, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
UserRole oUserRole = new UserRole();
MapObject(oUserRole, oReader);
return oUserRole as T;
}
protected UserRole CreateObject(DataReader oReader)
{
UserRole oUserRole = new UserRole();
MapObject(oUserRole, oReader);
return oUserRole;
}
#region Service implementation
public UserRole Get(ID id)
{
UserRole oUserRole = new UserRole();
#region Cache Header
oUserRole = _cache["Get", id] as UserRole;
if (oUserRole != null)
return oUserRole;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserRoleDA.Get(tc, id));
if (oreader.Read())
{
oUserRole = this.CreateObject<UserRole>(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(oUserRole, "Get", id);
#endregion
return oUserRole;
}
public ObjectsTemplate<UserRole> Get()
{
#region Cache Header
ObjectsTemplate<UserRole> userRoles = _cache["Get"] as ObjectsTemplate<UserRole>;
if (userRoles != null)
return userRoles;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserRoleDA.Get(tc));
userRoles = this.CreateObjects<UserRole>(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(userRoles, "Get");
#endregion
return userRoles;
}
public ObjectsTemplate<UserRole> GetByUserID(ID UserID)
{
#region Cache Header
ObjectsTemplate<UserRole> userRoles = _cache["Get", UserID] as ObjectsTemplate<UserRole>;
if (userRoles != null)
return userRoles;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserRoleDA.GetByUserID(tc, UserID));
userRoles = this.CreateObjects<UserRole>(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(userRoles, "Get", UserID);
#endregion
return userRoles;
}
public ID Save(UserRole oUserRole)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oUserRole.IsNew)
{
int id = tc.GenerateID("UserRole", "UserRoleID");
base.SetObjectID(oUserRole, ID.FromInteger(id));
UserRoleDA.Insert(tc, oUserRole);
}
else
{
UserRoleDA.Update(tc, oUserRole);
}
tc.End();
return oUserRole.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void SaveSingleUserRole(UserRole oUserRole)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int id = tc.GenerateID("UserRole", "UserRoleID");
oUserRole.SetObjectID(id);
UserRoleDA.Insert(tc, oUserRole);
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 DeleteByUserID2(ID nID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
this.DeleteByUserID(tc, nID);
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<UserRole> oUserRoles)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
//this.DeleteByUserID(tc,oUserRoles[0].UserID);
foreach (UserRole userRole in oUserRoles)
{
this.Save(tc, userRole);
}
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<UserRole> oUserRoles)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
//this.DeleteByUserID(tc,oUserRoles[0].UserID);
foreach (UserRole userRole in oUserRoles)
{
this.Save2(tc, userRole);
}
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<UserRole> oUserRoles)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
//this.DeleteByUserID(tc,oUserRoles[0].UserID);
foreach (UserRole userRole in oUserRoles)
{
this.Save3(tc, userRole);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
internal ID Save(TransactionContext tc, UserRole oUserRole)
{
try
{
if (oUserRole.IsNew)
{
int id = tc.GenerateID("UserRole", "UserRoleID");
base.SetObjectID(oUserRole, ID.FromInteger(id));
UserRoleDA.Insert(tc, oUserRole);
}
else
{
UserRoleDA.Update(tc, oUserRole);
}
return oUserRole.ID;
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
internal ID Save2(TransactionContext tc, UserRole oUserRole)
{
try
{
//if (oUserRole.IsNew)
//{
int id = tc.GenerateID("UserRole", "UserRoleID");
base.SetObjectID(oUserRole, ID.FromInteger(id));
UserRoleDA.Insert(tc, oUserRole);
//}
//else
//{
// UserRoleDA.Update(tc, oUserRole);
//}
return oUserRole.ID;
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
internal ID Save3(TransactionContext tc, UserRole oUserRole)
{
try
{
//if (oUserRole.IsNew)
//{
//int id = tc.GenerateID("UserRole", "UserRoleID");
//base.SetObjectID(oUserRole, ID.FromInteger(id));
//UserRoleDA.Insert(tc, oUserRole);
//}
//else
//{
UserRoleDA.Update(tc, oUserRole);
//}
return oUserRole.ID;
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Delete(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.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
}
}
public void Delete(TransactionContext tc, ID id)
{
try
{
UserRoleDA.Delete(tc, id);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void DeleteByUserID(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.DeleteByUserID(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
}
}
public void DeleteByUserID(int nUserID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.DeleteByUserID(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
}
}
public DataSet GetRoleAudit(int type, DateTime fromDate, DateTime ToDate)
{
DataSet role = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
role = UserRoleDA.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 void DeleteByUserID(TransactionContext tc, ID id)
{
try
{
UserRoleDA.DeleteByUserID(tc, id);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Update(ID nID, EnumStatus status)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.Update(tc, nID, status);
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
}