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

461 lines
15 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 SurveyService : ServiceTemplate, ISurveyService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Survey));
#endregion
public SurveyService() { }
private void MapObject(Survey oSurvey, DataReader oReader)
{
base.SetObjectID(oSurvey, oReader.GetID("SurveyID"));
oSurvey.Title = oReader.GetString("Title");
oSurvey.CategoryID = oReader.GetInt32("CategoryID").GetValueOrDefault();
oSurvey.Description = oReader.GetString("Description");
oSurvey.FromDate = oReader.GetDateTime("FromDate").Value;
oSurvey.ToDate = oReader.GetDateTime("ToDate").Value;
oSurvey.PublishDate = oReader.GetDateTime("PublishDate");
oSurvey.IsForCelyStoped = oReader.GetBoolean("IsForCelyStoped").GetValueOrDefault();
oSurvey.IsPublished = oReader.GetBoolean("IsPublished").GetValueOrDefault();
oSurvey.SurveyType = (EnumSurveyType)oReader.GetInt32("SurveyType").Value;
oSurvey.CreatedBy = oReader.GetID("CreatedBy");
oSurvey.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oSurvey.ModifiedBy = oReader.GetID("ModifiedBy");
oSurvey.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oSurvey, Ease.CoreV35.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(ID id)
{
Survey oSurvey = new Survey();
#region Cache Header
oSurvey = (Survey)_cache["Get", id.Integer];
if (oSurvey != null)
return oSurvey;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SurveyDA.Get(tc, id.Integer));
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
}
#region Cache Footer
_cache.Add(oSurvey, "Get", id.Integer);
#endregion
return oSurvey;
}
public ObjectsTemplate<Survey> Get()
{
#region Cache Header
ObjectsTemplate<Survey> oSurveys = _cache["Get"] as ObjectsTemplate<Survey>;
if (oSurveys != null)
return oSurveys;
#endregion
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
}
#region Cache Footer
_cache.Add(oSurveys, "Get");
#endregion
return oSurveys;
}
public ObjectsTemplate<Survey> Get(int categoryId)
{
#region Cache Header
ObjectsTemplate<Survey> oSurveys = _cache["Get", categoryId] as ObjectsTemplate<Survey>;
if (oSurveys != null)
return oSurveys;
#endregion
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
}
#region Cache Footer
_cache.Add(oSurveys, "Get", categoryId);
#endregion
return oSurveys;
}
public ObjectsTemplate<Survey> Get(DateTime today)
{
ObjectsTemplate<Survey> oSurveys;
#region CacheHeader
oSurveys = (ObjectsTemplate<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, 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
}
#region CacheFooter
_cache.Add(oSurveys, "Get");
#endregion
return oSurveys;
}
//public ObjectsTemplate<Survey> Get(Employee oEmp)
//{
// ObjectsTemplate<Survey> oSurveys;
// #region CacheHeader
// oSurveys = (ObjectsTemplate<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 ObjectsTemplate<Survey> Get(int categoryId, DateTime fromDT, DateTime toDT)
{
#region Cache Header
ObjectsTemplate<Survey> oSurveys = _cache["Get",categoryId,fromDT,toDT] as ObjectsTemplate<Survey>;
if (oSurveys != null)
return oSurveys;
#endregion
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
}
#region Cache Footer
_cache.Add(oSurveys, "Get", categoryId, fromDT, toDT);
#endregion
return oSurveys;
}
public ObjectsTemplate<Survey> Get(DateTime fromDT, DateTime toDT)
{
#region Cache Header
ObjectsTemplate<Survey> oSurveys = _cache["Get", fromDT, toDT] as ObjectsTemplate<Survey>;
if (oSurveys != null)
return oSurveys;
#endregion
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
}
#region Cache Footer
_cache.Add(oSurveys, "Get", fromDT, toDT);
#endregion
return oSurveys;
}
public ObjectsTemplate<Survey> GetIncompleteSurveys(ID employeeID, EnumSurveyType surveyType, DateTime fromDT, DateTime toDT)
{
#region Cache Header
ObjectsTemplate<Survey> oSurveys = _cache["GetIncompleteSurveys", employeeID.Integer, surveyType, fromDT, toDT] as ObjectsTemplate<Survey>;
if (oSurveys != null)
return oSurveys;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SurveyDA.GetIncompleteSurveys(tc, employeeID.Integer, 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
}
#region Cache Footer
_cache.Add(oSurveys, "GetIncompleteSurveys", employeeID.Integer, surveyType, fromDT, toDT);
#endregion
return oSurveys;
}
public ID Save(Survey oSurvey)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oSurvey.IsNew)
{
int id = tc.GenerateID("Survey", "SurveyID");
base.SetObjectID(oSurvey, ID.FromInteger(id));
SurveyDA.Insert(tc, oSurvey);
}
else
{
SurveyDA.Update(tc, oSurvey);
}
SurveyQuestionDA.DeleteSurveyQuestion(tc, oSurvey.ID.Integer);
SurveyEmployeeDA.DeleteSurveyEmployee(tc, oSurvey.ID.Integer);
SurveyOrganizationDA.DeleteSurveyOrganization(tc, oSurvey.ID.Integer);
SurveyWeightDefinitionDA.DeleteSurveyWeightDefinition(tc, oSurvey.ID.Integer);
//SurveyResultDA.DeleteSurveyResult(tc, oSurvey.ID.Integer,oSurvey.SurveyEmployees[0].EmployeeID,oSurvey.SurveyQuestions[0].ID.Integer);
foreach (SurveyQuestion sques in oSurvey.SurveyQuestions)
{
sques.SurveyID = oSurvey.ID;
SurveyQuestionDA.Insert(tc, sques);
}
foreach (SurveyEmployee semp in oSurvey.SurveyEmployees)
{
semp.SurveyID = oSurvey.ID;
SurveyEmployeeDA.Insert(tc, semp);
}
foreach (SurveyOrganization sorg in oSurvey.SurveyOrganizations)
{
sorg.SurveyID = oSurvey.ID;
SurveyOrganizationDA.Insert(tc, sorg);
}
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(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
SurveyDA.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
}
}
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
}
}