EchoTex_Payroll/HRM.DA/Service/Survey/QuestionService.cs
2024-10-14 10:01:49 +06:00

353 lines
12 KiB
C#

using System;
using System.Data;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core;
using System.Collections.Generic;
using Ease.Core.Utility;
using HRM.BO;
using HRM.DA;
using Payroll.BO;
namespace HRM.DA
{
public class QuestionService : ServiceTemplate, IQuestionService
{
public QuestionService()
{
}
private void MapObject(Question oQuestion, DataReader oReader)
{
base.SetObjectID(oQuestion, (oReader.GetInt32("QuestionID").Value));
//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.GetInt32("CreatedBy", 0);
oQuestion.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
;
oQuestion.EstimatedTime = oReader.GetDouble("EstimatedTime").GetValueOrDefault();
oQuestion.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oQuestion, Ease.Core.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(int id)
{
Question oQuestion = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(QuestionDA.Get(tc, id));
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
}
return oQuestion;
}
public List<Question> Get()
{
List<Question> oQuestions = null;
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
}
return oQuestions;
}
public List<Question> GetByCategory(int categoryID)
{
List<Question> oQuestions = null;
List<QuestionCategory> oQuestionCategoryList = new List<QuestionCategory>();
oQuestionCategoryList= new QuestionCategoryService().Get(EnumStatus.Active);
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(QuestionDA.GetByCategory(tc, categoryID));
oQuestions = this.CreateObjects<Question>(dr);
dr.Close();
tc.End();
foreach(var item in oQuestions)
{
item.QuestionAnswers = new QuestionAnswerService().GetByQuestionID(item.ID);
item.NoOfOptions = item.QuestionAnswers.Count;
item.CategoryString = oQuestionCategoryList?.Find(x=>x.ID==item.CategoryID)?.Description;
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oQuestions;
}
public Question GetWithChild(int id)
{
Question oQuestion = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(QuestionDA.Get(tc, id));
if (oreader.Read())
{
oQuestion = this.CreateObject<Question>(oreader);
}
oreader.Close();
tc.End();
if(oQuestion != null)
{
oQuestion.QuestionAnswers = new QuestionAnswerService().GetByQuestionID(oQuestion.ID);
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Question", e);
#endregion
}
return oQuestion;
}
public QuestionAnswerAttachment GetAnswerByAttchmentId(int questionId,int answerId)
{
QuestionAnswerAttachment oFileAttachment = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(QuestionAnswerAttachmentDA.GetAnswerAttachment(tc, questionId, answerId));
while (oreader.Read())
{
oFileAttachment = new QuestionAnswerAttachment();
oFileAttachment.ID = oreader.GetInt32("QUESTIONANSWERATTACHMENTID").Value;
oFileAttachment.QuestionID = oreader.GetInt32("QuestionID", 0);
oFileAttachment.FileAsByteArray = oreader.GetLob("FileData");
oFileAttachment.OriginalFileName = oreader.GetString("OriginalFileName");
oFileAttachment.AnswerID = oreader.GetInt32("AnswerID", 0);
oFileAttachment.AttachmentType = (EnumQuestionAnswerFileType)oreader.GetInt32("AttachmentType").GetValueOrDefault();
}
oreader.Close();
}
catch (Exception ex)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(ex);
throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);
#endregion
}
return oFileAttachment;
}
public bool IsTagged(int id)
{
bool retVal = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
retVal = QuestionDA.IsTagged(tc, id);
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 int 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));
QuestionDA.Insert(tc, oQuestion);
}
else
{
QuestionDA.Update(tc, oQuestion);
}
#region Saving answers
QuestionAnswerDA.DeleteByQuestionId(tc, oQuestion.ID);
foreach (QuestionAnswer ans in oQuestion.QuestionAnswers)
{
int id = tc.GenerateID("QuestionAnswer", "AnswerID");
base.SetObjectID(ans, (id));
ans.QuestionID = oQuestion.ID;
ans.ID = id;
QuestionAnswerDA.Insert(tc, ans);
}
#endregion
tc.End();
foreach (var item in oQuestion.QuestionAnswerAttachments)
{
if (item.ID <= 0 && item.PreviousFileTobase64 != null)
{
string[] items = item.PreviousFileTobase64.Split(new char[] { ',', ' ' }, StringSplitOptions.None);
byte[] newBytes = Convert.FromBase64String(items[1]);
item.FileAsByteArray = newBytes;
}
item.QuestionID = oQuestion.ID;
item.AnswerID = 0;
item.AttachmentType = EnumQuestionAnswerFileType.Question;
QuestionAnswerAttachmentDA.Insert(item, oQuestion.ConnectionString);
}
foreach (QuestionAnswer ans in oQuestion.QuestionAnswers)
{
foreach (var item in ans.QuestionAnswerAttachments)
{
if (item.ID <= 0 && item.PreviousFileTobase64 != null)
{
string[] items = item.PreviousFileTobase64.Split(new char[] { ',', ' ' }, StringSplitOptions.None);
byte[] newBytes = Convert.FromBase64String(items[1]);
item.FileAsByteArray = newBytes;
}
item.QuestionID = oQuestion.ID;
item.AnswerID = ans.ID;
item.AttachmentType = EnumQuestionAnswerFileType.Answer;
QuestionAnswerAttachmentDA.Insert(item, oQuestion.ConnectionString);
}
}
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(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
QuestionAnswerDA.DeleteByQuestionId(tc, id);
QuestionDA.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
}
}