CEL_Payroll/Payroll.Service/Basic/Service/FunctionService.cs

215 lines
6.6 KiB
C#
Raw 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 Function Service
[Serializable]
public class FunctionService : ServiceTemplate, IFunctionService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Function));
#endregion
public FunctionService() { }
private void MapObject(Function oFunction, DataReader oReader)
{
base.SetObjectID(oFunction, oReader.GetID("FunctionID"));
oFunction.Code = oReader.GetString("code");
oFunction.Name = oReader.GetString("description");
oFunction.Sequence = oReader.GetInt32("SequenceNo").Value;
oFunction.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oFunction.CreatedBy = oReader.GetID("CreatedBy");
oFunction.CreatedDate = oReader.GetDateTime("CreatedDate").Value ;
oFunction.ModifiedBy = oReader.GetID("ModifiedBy");
oFunction.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oFunction, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Function oFunction = new Function();
MapObject(oFunction, oReader);
return oFunction as T;
}
protected Function CreateObject(DataReader oReader)
{
Function oFunction = new Function();
MapObject(oFunction, oReader);
return oFunction;
}
#region Service implementation
public Function Get(ID id)
{
Function oFunction = new Function();
#region Cache Header
oFunction = _cache["Get", id] as Function;
if (oFunction != null)
return oFunction;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(FunctionDA.Get(tc, id));
if (oreader.Read())
{
oFunction = this.CreateObject<Function>(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(oFunction, "Get", id);
#endregion
return oFunction;
}
public Function Get(string code)
{
Function oFunction = new Function();
#region Cache Header
oFunction = _cache["Get", code] as Function;
if (oFunction != null)
return oFunction;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(FunctionDA.Get(tc, code));
if (oreader.Read())
{
oFunction = this.CreateObject<Function>(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(oFunction, "Get", code);
#endregion
return oFunction;
}
public ObjectsTemplate<Function> Get(EnumStatus status)
{
#region Cache Header
ObjectsTemplate<Function> Functions = _cache["Get", status] as ObjectsTemplate<Function>;
if (Functions != null)
return Functions;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(FunctionDA.Get(tc, status));
Functions = this.CreateObjects<Function>(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(Functions, "Get", status);
#endregion
return Functions;
}
public ID Save(Function oFunction)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oFunction.IsNew)
{
int id = tc.GenerateID("[Function]", "FunctionID");
base.SetObjectID(oFunction , ID.FromInteger(id));
int seqNo = tc.GenerateID("[Function]", "SequenceNo");
oFunction.Sequence = seqNo;
FunctionDA.Insert(tc, oFunction);
}
else
{
FunctionDA.Update(tc, oFunction);
}
tc.End();
return oFunction.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(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
FunctionDA.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
}