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

204 lines
7.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Payroll.BO;
using System.Data;
using Ease.CoreV35.Model;
using System.Data.SqlClient;
using Ease.CoreV35.DataAccess;
using Ease.CoreV35.DataAccess.SQL;
using Ease.CoreV35.Caching;
namespace Payroll.Service
{
[Serializable]
public class SurveyAnswerCountService:ServiceTemplate,ISurveyAnswerCountService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(SurveyAnswerCount));
#endregion
public SurveyAnswerCountService() { }
private void MapObject(SurveyAnswerCount oSurveyAnswerCount, DataReader oReader)
{
//base.SetObjectID(oSurveyAnswerCount, oReader.GetID("SurveyAnswerCountID"));
oSurveyAnswerCount.SurveyID = oReader.GetID("SurveyID");
oSurveyAnswerCount.QuestionID = oReader.GetInt32("QuestionID").GetValueOrDefault();
oSurveyAnswerCount.AnswerID = oReader.GetInt32("AnswerID").GetValueOrDefault();
oSurveyAnswerCount.HitCount = oReader.GetInt32("HitCount").GetValueOrDefault();
this.SetObjectState(oSurveyAnswerCount, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
SurveyAnswerCount oSurveyAnswerCount = new SurveyAnswerCount();
MapObject(oSurveyAnswerCount, oReader);
return oSurveyAnswerCount as T;
}
protected SurveyAnswerCount CreateObject(DataReader oReader)
{
SurveyAnswerCount oSurveyAnswerCount = new SurveyAnswerCount();
MapObject(oSurveyAnswerCount, oReader);
return oSurveyAnswerCount;
}
#region Service implementation
public ObjectsTemplate<SurveyAnswerCount> GetSurveyAnswerCount()
{
#region Cache Header
ObjectsTemplate<SurveyAnswerCount> oSurveyAnswerCounts = _cache["GetSurveyAnswerCount"] as ObjectsTemplate<SurveyAnswerCount>;
if (oSurveyAnswerCounts != null)
return oSurveyAnswerCounts;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyAnswerCountDA.GetSurveyAnswerCount(tc));
oSurveyAnswerCounts = this.CreateObjects<SurveyAnswerCount>(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(oSurveyAnswerCounts, "GetSurveyAnswerCount");
#endregion
return oSurveyAnswerCounts;
}
public ObjectsTemplate<SurveyAnswerCount> GetSurveyAnswerCount(ID surveyId)
{
#region Cache Header
ObjectsTemplate<SurveyAnswerCount> oSurveyAnswerCounts = _cache["GetSurveyAnswerCount",surveyId] as ObjectsTemplate<SurveyAnswerCount>;
if (oSurveyAnswerCounts != null)
return oSurveyAnswerCounts;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyAnswerCountDA.GetSurveyAnswerCount(tc, surveyId.Integer));
oSurveyAnswerCounts = this.CreateObjects<SurveyAnswerCount>(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(oSurveyAnswerCounts, "GetSurveyAnswerCount", surveyId);
#endregion
return oSurveyAnswerCounts;
}
public SurveyAnswerCount GetSurveyAnswerCount(ID surveyId, int nQuestionId, int nAnswerId)
{
SurveyAnswerCount oSurveyAnswerCount = new SurveyAnswerCount();
#region Cache Header
oSurveyAnswerCount = (SurveyAnswerCount)_cache["GetSurveyAnswerCount", surveyId.Integer, nQuestionId, nAnswerId];
if (oSurveyAnswerCount != null)
return oSurveyAnswerCount;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SurveyAnswerCountDA.GetSurveyAnswerCount(tc, surveyId.Integer, nQuestionId, nAnswerId));
if (oreader.Read())
{
oSurveyAnswerCount = this.CreateObject<SurveyAnswerCount>(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 SurveyAnswerCount", e);
#endregion
}
#region Cache Footer
_cache.Add(oSurveyAnswerCount, "GetSurveyAnswerCount", surveyId.Integer, nQuestionId, nAnswerId);
#endregion
return oSurveyAnswerCount;
}
public ID Save(SurveyAnswerCount oSurveyAnswerCount)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oSurveyAnswerCount.IsNew)
{
//int id = tc.GenerateID("SurveyAnswerCount", "SurveyAnswerCountID");
//base.SetObjectID(oSurveyAnswerCount, ID.FromInteger(id));
SurveyAnswerCountDA.Insert(tc, oSurveyAnswerCount);
}
else
{
SurveyAnswerCountDA.Update(tc, oSurveyAnswerCount);
}
tc.End();
return oSurveyAnswerCount.SurveyID;
}
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);
SurveyAnswerCountDA.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
}
}