EchoTex_Payroll/HRM.DA/Service/Survey/SurveyService.cs

495 lines
15 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
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 System;
using System.Data;
using System.Linq;
namespace HRM.DA
{
public class SurveyService : ServiceTemplate, ISurveyService
{
public SurveyService()
{
}
private void MapObject(Survey oSurvey, DataReader oReader)
{
base.SetObjectID(oSurvey, (oReader.GetInt32("SurveyID").Value));
oSurvey.Title = oReader.GetString("Title");
oSurvey.CategoryID = oReader.GetInt32("CategoryID").GetValueOrDefault();
oSurvey.Description = oReader.GetString("Description");
oSurvey.FromDate = oReader.GetDateTime("FromDate").GetValueOrDefault();
oSurvey.ToDate = oReader.GetDateTime("ToDate").GetValueOrDefault();
oSurvey.PublishDate = oReader.GetDateTime("PUBLISHDATE").GetValueOrDefault();
oSurvey.IsForCelyStoped = oReader.GetBoolean("IsForCelyStoped").GetValueOrDefault();
oSurvey.IsPublished = oReader.GetBoolean("IsPublished").GetValueOrDefault();
oSurvey.SurveyType = (EnumSurveyType)oReader.GetInt32("SurveyType").Value;
oSurvey.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oSurvey.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oSurvey.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
oSurvey.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oSurvey.CategoryName = oReader.GetString("SurveyCategory", true,null);
oSurvey.EstimatedTime = oReader.GetDouble("EstimatedTime", true, 0);
oSurvey.ActualTime = oReader.GetDouble("ActualTime", true, 0);
oSurvey.PassMark = oReader.GetInt32("PassMark", true, 0);
this.SetObjectState(oSurvey, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Survey oSurvey = new Survey();
MapObject(oSurvey, oReader);
return oSurvey as T;
}
protected Survey CreateObject(DataReader oReader)
{
Survey oSurvey = new Survey();
MapObject(oSurvey, oReader);
return oSurvey;
}
#region Service implementation
public Survey Get(int id)
{
Survey oSurvey = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SurveyDA.Get(tc, id));
if (oreader.Read())
{
oSurvey = this.CreateObject<Survey>(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 Survey", e);
#endregion
}
return oSurvey;
}
public List<Survey> Get()
{
List<Survey> oSurveys = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.Get(tc));
oSurveys = this.CreateObjects<Survey>(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 oSurveys;
}
public List<Survey> GetWithCategoryID(int categoryId)
{
List<Survey> oSurveys = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.GetByCategory(tc, categoryId));
oSurveys = this.CreateObjects<Survey>(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 oSurveys;
}
public List<Survey> Get(DateTime today)
{
List<Survey> oSurveys = null;
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SurveyDA.Get(tc, today));
oSurveys = CreateObjects<Survey>(oreader);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return oSurveys;
}
//public List<Survey> Get(Employee oEmp)
//{
// List<Survey> oSurveys;
// #region CacheHeader
// oSurveys = (List<Survey>)_cache["Get"];
// if (oSurveys != null)
// return oSurveys;
// #endregion
// TransactionContext tc = null;
// try
// {
// #region Retrieving data
// tc = TransactionContext.Begin();
// DataReader oreader = new DataReader(SurveyDA.Get(tc, oEmp));
// oSurveys = CreateObjects<Survey>(oreader);
// tc.End();
// #endregion
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// throw new ServiceException(e.Message, e);
// #endregion
// }
// #region CacheFooter
// _cache.Add(oSurveys, "Get");
// #endregion
// return oSurveys;
//}
public List<Survey> Get(int categoryId, DateTime fromDT, DateTime toDT)
{
List<Survey> oSurveys = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.GetByCategory(tc, categoryId, fromDT, toDT));
oSurveys = this.CreateObjects<Survey>(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 oSurveys;
}
public List<Survey> Get(DateTime fromDT, DateTime toDT)
{
List<Survey> oSurveys = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.GetByCategory(tc, fromDT, toDT));
oSurveys = this.CreateObjects<Survey>(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 oSurveys;
}
public List<Survey> GetQuestionSetByCategoryID(int categoryID)
{
List<Survey> oSurveys = null;
List<SurveyQuestion> oSurveyQuestions = new List<SurveyQuestion>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.GetQuestionsByCategoryId(tc, categoryID));
oSurveys = this.CreateObjects<Survey>(dr);
dr.Close();
if(oSurveys != null && oSurveys.Count > 0)
{
string ids = String.Join(",", oSurveys.Select(x => x.ID).Distinct());
oSurveyQuestions = new SurveyQuestionService().GetSurveyQuestions(tc, ids);
}
tc.End();
if(oSurveyQuestions != null && oSurveyQuestions.Count > 0)
{
foreach(var temp in oSurveys)
{
var noOfQuestionCount= temp.SurveyQuestions = oSurveyQuestions.Where(x => x.SurveyID == temp.ID).ToList();
temp.NoOfQuestions = noOfQuestionCount.Count();
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oSurveys;
}
public List<Survey> GetIncompleteSurveys(int employeeID, EnumSurveyType surveyType, DateTime fromDT,
DateTime toDT)
{
List<Survey> oSurveys = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.GetIncompleteSurveys(tc, employeeID, surveyType, fromDT, toDT));
oSurveys = this.CreateObjects<Survey>(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 oSurveys;
}
public int Save(Survey oSurvey)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oSurvey.IsNew)
{
int id = tc.GenerateID("Survey", "SurveyID");
base.SetObjectID(oSurvey, (id));
SurveyDA.Insert(tc, oSurvey);
}
else
{
SurveyDA.Update(tc, oSurvey);
}
SurveyQuestionDA.DeleteSurveyQuestion(tc, oSurvey.ID);
SurveyEmployeeDA.DeleteSurveyEmployee(tc, oSurvey.ID);
SurveyOrganizationDA.DeleteSurveyOrganization(tc, oSurvey.ID);
SurveyWeightDefinitionDA.DeleteSurveyWeightDefinition(tc, oSurvey.ID);
//SurveyResultDA.DeleteSurveyResult(tc, oSurvey.ID,oSurvey.SurveyEmployees[0].EmployeeID,oSurvey.SurveyQuestions[0].ID);
foreach (SurveyQuestion sques in oSurvey.SurveyQuestions)
{
sques.SurveyID = oSurvey.ID;
SurveyQuestionDA.Insert(tc, sques);
}
if(oSurvey.SurveyEmployees != null)
{
foreach (SurveyEmployee semp in oSurvey.SurveyEmployees)
{
semp.SurveyID = oSurvey.ID;
SurveyEmployeeDA.Insert(tc, semp);
}
}
if (oSurvey.SurveyOrganizations != null)
{
foreach (SurveyOrganization sorg in oSurvey.SurveyOrganizations)
{
sorg.SurveyID = oSurvey.ID;
SurveyOrganizationDA.Insert(tc, sorg);
}
}
if (oSurvey.SurveyOrganizations != null)
{
foreach (SurveyWeightDefinition swd in oSurvey.SurveyWeightDefinitions)
{
swd.SurveyID = oSurvey.ID;
SurveyWeightDefinitionDA.Insert(tc, swd);
}
}
//foreach (SurveyResult sreslt in oSurvey.SurveyResults)
//{
// sreslt.SurveyID = oSurvey.ID;
// SurveyResultDA.Insert(tc, sreslt);
//}
tc.End();
return oSurvey.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);
SurveyDA.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
}
}
public void Publish(Survey survey)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
SurveyDA.Publish(tc, survey);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Stop(Survey survey)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
SurveyDA.Stop(tc, survey);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
}
}