CEL_Payroll/Payroll.Service/Leave/Service/LeaveService.cs
2024-09-17 14:30:13 +06:00

374 lines
11 KiB
C#

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 Leave Service
[Serializable]
public class LeaveService : ServiceTemplate, ILeaveService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Leave));
public LeaveService() { }
private void MapObject(Leave oLeave, DataReader oReader)
{
base.SetObjectID(oLeave, oReader.GetID("LeaveID"));
oLeave.Code = oReader.GetString("Code");
oLeave.Description = oReader.GetString("Description");
oLeave.RemarksNeeded = oReader.GetBoolean("RemarksNeeded").Value;
oLeave.ApplicableFor = (EnumGender)oReader.GetInt32("ApplicableFor").Value;
oLeave.IsBalanceCalculationNeeded = oReader.GetBoolean("IsBalanceCalRequired").Value;
oLeave.AutoLeaveReason = oReader.GetBoolean("AutoLeaveReason").Value;
oLeave.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oLeave.IsEarnedLeave = oReader.GetBoolean("IsEarnedLeave").Value;
oLeave.Sequence = oReader.GetInt32("SequenceNo").Value;
oLeave.CreatedBy = oReader.GetID("CreatedBy");
oLeave.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oLeave.ModifiedBy = oReader.GetID("ModifiedBy");
oLeave.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oLeave.IsHalfDayLeave = oReader.GetBoolean("IsHalfDayLeave").Value;
oLeave.IsCompensatoryLeave = oReader.GetBoolean("IsCompensatoryLeave").Value;
this.SetObjectState(oLeave, Ease.CoreV35.ObjectState.Saved);
//this.SetObjectState(oLeave, ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Leave oLeave = new Leave();
MapObject(oLeave, oReader);
return oLeave as T;
}
private Leave CreateObject(DataReader oReader)
{
Leave oLeave = new Leave();
MapObject(oLeave, oReader);
return oLeave;
}
#endregion
#region Service implementation
public Leave Get(ID id)
{
Leave oLeave = new Leave();
#region Cache Header
oLeave = (Leave)_cache["Get", id.Integer];
if (oLeave != null)
return oLeave;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveDA.Get(tc, id.Integer));
if (oreader.Read())
{
oLeave = this.CreateObject<Leave>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave", e);
#endregion
}
#region Cache Footer
_cache.Add(oLeave, "Get", id.Integer);
#endregion
return oLeave;
}
public Leave GetIDByName(string sLeave)
{
Leave oLeave = new Leave();
#region Cache Header
oLeave = (Leave)_cache["Get", sLeave];
if (oLeave != null)
return oLeave;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveDA.GetIDByName(tc, sLeave));
if (oreader.Read())
{
oLeave = this.CreateObject<Leave>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave", e);
#endregion
}
#region Cache Footer
_cache.Add(oLeave, "Get", sLeave);
#endregion
return oLeave;
}
public Leave GetIDByCode(string sLeave)
{
Leave oLeave = new Leave();
#region Cache Header
oLeave = (Leave)_cache["Get", sLeave];
if (oLeave != null)
return oLeave;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveDA.GetIDByCode(tc, sLeave));
if (oreader.Read())
{
oLeave = this.CreateObject<Leave>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave", e);
#endregion
}
#region Cache Footer
_cache.Add(oLeave, "Get", sLeave);
#endregion
return oLeave;
}
public ObjectsTemplate<Leave> Get()
{
#region Cache Header
ObjectsTemplate<Leave> oLeaves = _cache["Get"] as ObjectsTemplate<Leave>;
if (oLeaves != null)
return oLeaves;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveDA.Get(tc));
oLeaves = this.CreateObjects<Leave>(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(oLeaves, "Get");
#endregion
return oLeaves;
}
public ObjectsTemplate<Leave> Get(string InParameterSQL)
{
#region Cache Header
ObjectsTemplate<Leave> oLeaves = _cache["Get"] as ObjectsTemplate<Leave>;
if (oLeaves != null)
return oLeaves;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveDA.Get(tc, InParameterSQL));
oLeaves = this.CreateObjects<Leave>(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(oLeaves, "Get");
#endregion
return oLeaves;
}
public ObjectsTemplate<Leave> Get(EnumStatus status)
{
#region Cache Header
ObjectsTemplate<Leave> leaves = _cache["Get", status] as ObjectsTemplate<Leave>;
if (leaves != null)
return leaves;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveDA.Get(tc, status));
leaves = this.CreateObjects<Leave>(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(leaves, "Get", status);
#endregion
return leaves;
}
public ID Save(Leave oLeave)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oLeave.IsNew)
{
int id = tc.GenerateID("Leave", "LeaveID");
base.SetObjectID(oLeave, ID.FromInteger(id));
int seqNo = tc.GenerateID("Leave", "SequenceNO");
oLeave.Sequence = seqNo;
LeaveDA.Insert(tc, oLeave);
}
else
{
LeaveDA.Update(tc, oLeave);
}
tc.End();
return oLeave.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
//throw new ServiceException("Failed to Get Leave", e);
throw new Exception("Failed to Save Leave. Because " + e.Message, e);
#endregion
}
}
public void Delete(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveDA.Delete(tc, id.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 ObjectsTemplate<Leave> GetLeaves()
{
#region Cache Header
ObjectsTemplate<Leave> leaves = _cache["Get"] as ObjectsTemplate<Leave>;
if (leaves != null)
return leaves;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveDA.GetLeaves(tc));
leaves = this.CreateObjects<Leave>(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(leaves, "Get");
#endregion
return leaves;
}
#endregion
}
#endregion
}