CEL_Payroll/Payroll.Service/Loan/Service/LoanService.cs

220 lines
6.5 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 Loan Service
[Serializable]
public class LoanService : ServiceTemplate, ILoanService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Loan));
#endregion
public LoanService() { }
private void MapObject(Loan oLoan, DataReader oReader)
{
base.SetObjectID(oLoan, oReader.GetID("LoanID"));
oLoan.Name = oReader.GetString("description");
oLoan.IsConfirmationRequired = oReader.GetBoolean("isConfirmationRequired").HasValue ? oReader.GetBoolean("isConfirmationRequired").Value : false;
oLoan.InterestRate = oReader.GetDouble("interestRate").Value;
oLoan.Sequence = oReader.GetInt32("SequenceNo").Value;
oLoan.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oLoan.CreatedBy = oReader.GetID("CreatedBy");
oLoan.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oLoan.ModifiedBy = oReader.GetID("ModifiedBy");
oLoan.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oLoan, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Loan oLoan = new Loan();
MapObject(oLoan, oReader);
return oLoan as T;
}
protected Loan CreateObject(DataReader oReader)
{
Loan oLoan = new Loan();
MapObject(oLoan, oReader);
return oLoan;
}
#region Service implementation
public Loan Get(ID id)
{
Loan oLoan = new Loan();
#region Cache Header
oLoan = _cache["Get", id] as Loan;
if (oLoan != null)
return oLoan;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LoanDA.Get(tc, id));
if (oreader.Read())
{
oLoan = this.CreateObject<Loan>(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(oLoan, "Get", id);
#endregion
return oLoan;
}
public Loan Get(string loanNo)
{
Loan oLoan = new Loan();
#region Cache Header
oLoan = _cache["Get", loanNo] as Loan;
if (oLoan != null)
return oLoan;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LoanDA.Get(tc, loanNo));
if (oreader.Read())
{
oLoan = this.CreateObject<Loan>(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(oLoan, "Get", loanNo);
#endregion
return oLoan;
}
public ObjectsTemplate<Loan> Get(EnumStatus status)
{
#region Cache Header
ObjectsTemplate<Loan> loans = _cache["Get",status] as ObjectsTemplate<Loan>;
if (loans != null)
return loans;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LoanDA.Get(tc,status));
loans = this.CreateObjects<Loan>(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(loans, "Get",status);
#endregion
return loans;
}
public ID Save(Loan oLoan)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oLoan.IsNew)
{
int id = tc.GenerateID("LOANBASIC", "LoanID");
base.SetObjectID(oLoan, ID.FromInteger(id));
int seqNo = tc.GenerateID("LOANBASIC", "SequenceNO");
oLoan.Sequence = seqNo;
LoanDA.Insert(tc, oLoan);
}
else
{
LoanDA.Update(tc, oLoan);
}
tc.End();
return oLoan.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);
LoanDA.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
}