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

269 lines
7.7 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 class District Service
[Serializable]
public class DistrictService : ServiceTemplate, IDistrictService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(District));
#endregion
#region constructor
public DistrictService() { }
#endregion
#region object
private void MapObject(District oDistrict, DataReader oReader)
{
base.SetObjectID(oDistrict, oReader.GetID("DISTRICTID"));
oDistrict.Code = oReader.GetString("CODE");
oDistrict.Name = oReader.GetString("NAME");
oDistrict.DivisionID = oReader.GetID("DIVISIONID");
oDistrict.Sequence = oReader.GetInt32("SequenceNo").Value;
oDistrict.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oDistrict.CreatedBy = oReader.GetID("CreatedBy");
oDistrict.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oDistrict.ModifiedBy = oReader.GetID("ModifiedBy");
oDistrict.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oDistrict, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
District oDistrict = new District();
MapObject(oDistrict, oReader);
return oDistrict as T;
}
protected District CreateObject(DataReader oReader)
{
District oDistrict = new District();
MapObject(oDistrict, oReader);
return oDistrict;
}
#endregion
#region Service implementation
public District Get(ID id)
{
District oDistrict = new District();
#region Cache Header
oDistrict = _cache["Get", id] as District;
if (oDistrict != null)
return oDistrict;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(DistrictDA.Get(tc, id));
if (oreader.Read())
{
oDistrict = this.CreateObject<District>(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(oDistrict, "Get", id);
#endregion
return oDistrict;
}
public District Get(string sCode)
{
District oDistrict = new District();
#region Cache Header
oDistrict = _cache["Get", sCode] as District;
if (oDistrict != null)
return oDistrict;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(DistrictDA.Get(tc, sCode));
if (oreader.Read())
{
oDistrict = this.CreateObject<District>(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(oDistrict, "Get", sCode);
#endregion
return oDistrict;
}
public ObjectsTemplate<District> Get()
{
#region Cache Header
ObjectsTemplate<District> districts = _cache["Get"] as ObjectsTemplate<District>;
if (districts != null)
return districts;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(DistrictDA.Get(tc));
districts = this.CreateObjects<District>(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(districts, "Get");
#endregion
return districts;
}
public ObjectsTemplate<District> Get(EnumStatus status)
{
#region Cache Header
ObjectsTemplate<District> districts = _cache["Get"] as ObjectsTemplate<District>;
if (districts != null)
return districts;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(DistrictDA.Get(tc, status));
districts = this.CreateObjects<District>(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(districts, "Get", status);
#endregion
return districts;
}
public ID Save(District oDistrict)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oDistrict.IsNew)
{
int id = tc.GenerateID("DISTRICT", "DISTRICTID");
base.SetObjectID(oDistrict, ID.FromInteger(id));
DistrictDA.Insert(tc, oDistrict);
}
else
{
DistrictDA.Update(tc, oDistrict);
}
tc.End();
return oDistrict.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);
DistrictDA.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
}