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

246 lines
8.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.CoreV35;
using Ease.CoreV35.Model;
using Ease.CoreV35.DataAccess;
using Payroll.BO;
using Ease.CoreV35.Caching;
namespace Payroll.Service
{
[Serializable]
public class QuestionService : ServiceTemplate, IQuestionService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Question));
#endregion
public QuestionService() { }
private void MapObject(Question oQuestion, DataReader oReader)
{
base.SetObjectID(oQuestion, oReader.GetID("QuestionID"));
//oQuestion.QuestionID = oReader.GetID("QuestionID");
oQuestion.QuestionNo = oReader.GetString("QuestionNo");
oQuestion.CategoryID = oReader.GetInt32("CategoryID").GetValueOrDefault();
oQuestion.QuestionBody = oReader.GetString("QuestionBody");
oQuestion.Image = oReader.GetString("Image");
oQuestion.IsMultiple = oReader.GetBoolean("IsMultiple").GetValueOrDefault();
oQuestion.IsPublished = oReader.GetBoolean("IsPublished").GetValueOrDefault();
oQuestion.CreationDate = oReader.GetDateTime("CreationDate").Value;
oQuestion.UserID = oReader.GetInt32("UserID").GetValueOrDefault();
oQuestion.QuestionType = (EnumSurveyQuestionType)oReader.GetInt32("QuestionType").Value;
oQuestion.CreatedBy = oReader.GetID("CreatedBy");
oQuestion.ModifiedBy = oReader.GetID("ModifiedBy");
oQuestion.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oQuestion, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Question oQuestion = new Question();
MapObject(oQuestion, oReader);
return oQuestion as T;
}
protected Question CreateObject(DataReader oReader)
{
Question oQuestion = new Question();
MapObject(oQuestion, oReader);
return oQuestion;
}
#region Service implementation
public Question Get(ID id)
{
Question oQuestion = new Question();
#region Cache Header
oQuestion = (Question)_cache["Get", id.Integer];
if (oQuestion != null)
return oQuestion;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(QuestionDA.Get(tc, id.Integer));
if (oreader.Read())
{
oQuestion = this.CreateObject<Question>(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 Question", e);
#endregion
}
#region Cache Footer
_cache.Add(oQuestion, "Get", id.Integer);
#endregion
return oQuestion;
}
public ObjectsTemplate<Question> Get()
{
#region Cache Header
ObjectsTemplate<Question> oQuestions = _cache["Get"] as ObjectsTemplate<Question>;
if (oQuestions != null)
return oQuestions;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(QuestionDA.Get(tc));
oQuestions = this.CreateObjects<Question>(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(oQuestions, "Get");
#endregion
return oQuestions;
}
public ObjectsTemplate<Question> GetByCategory(ID categoryID)
{
#region Cache Header
ObjectsTemplate<Question> oQuestions = _cache["Get", categoryID] as ObjectsTemplate<Question>;
if (oQuestions != null)
return oQuestions;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(QuestionDA.GetByCategory(tc,categoryID.Integer));
oQuestions = this.CreateObjects<Question>(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(oQuestions, "Get", categoryID);
#endregion
return oQuestions;
}
public bool IsTagged(ID id)
{
bool retVal = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
retVal = QuestionDA.IsTagged(tc, id.Integer);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Information", e);
#endregion
}
return retVal;
}
public ID Save(Question oQuestion)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int codeLength = 3;
if (oQuestion.IsNew)
{
int id = tc.GenerateID("Question", "QuestionID");
base.SetObjectID(oQuestion, ID.FromInteger(id));
QuestionDA.Insert(tc, oQuestion);
}
else
{
QuestionDA.Update(tc, oQuestion);
}
#region Saving answers
QuestionAnswerDA.DeleteByQuestionId(tc, oQuestion.ID.Integer);
foreach (QuestionAnswer ans in oQuestion.QuestionAnswers)
{
int id = tc.GenerateID("QuestionAnswer", "AnswerID");
base.SetObjectID(ans, ID.FromInteger(id));
ans.QuestionID = oQuestion.ID.Integer;
QuestionAnswerDA.Insert(tc, ans);
}
#endregion
tc.End();
return oQuestion.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);
QuestionAnswerDA.DeleteByQuestionId(tc, id.Integer);
QuestionDA.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
}
}
#endregion
}
}