using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ease.CoreV35.Caching; using Ease.CoreV35.DataAccess; using Ease.CoreV35.Model; using Payroll.BO; namespace Payroll.Service { public class NotificationService : ServiceTemplate, INotificationService { #region Cache Cache _cache = new Cache(typeof(Notification)); #endregion #region Map Objects #region Map Parent private void MapObject(Notification oNotification, DataReader oReader) { base.SetObjectID(oNotification, oReader.GetID("NotificationID")); oNotification.Subject = oReader.GetString("Subject"); oNotification.Body = oReader.GetString("Body"); oNotification.TagOutputID = oReader.GetID("TagOutputID").IsUnassigned ? ID.FromInteger(0) : oReader.GetID("TagOutputID"); oNotification.ObjectID = oReader.GetID("ObjectID").IsUnassigned ? ID.FromInteger(0) : oReader.GetID("ObjectID"); oNotification.OptionalObjectID = oReader.GetID("OptionalObjectID").IsUnassigned ? ID.FromInteger(0) : oReader.GetID("OptionalObjectID"); oNotification.Attachment = oReader.GetString("Attachment"); oNotification.Footer = oReader.GetString("Footer"); oNotification.Type = (EnumNotificationType)oReader.GetInt32("Type"); //oNotification.Medium = (EnumNotificationMedium) oReader.GetInt32("Medium"); oNotification.IsMailNf = oReader.GetBoolean("IsMailNf").Value; oNotification.IsSMSNf = oReader.GetBoolean("IsSMSNf").Value; oNotification.IsWebNf = oReader.GetBoolean("IsWebNf").Value; oNotification.Status = (EnumNotificationStatus) oReader.GetInt32("Status"); this.SetObjectState(oNotification, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Notification oNotification = new Notification(); MapObject(oNotification, oReader); return oNotification as T; } #endregion #region Map NotificationParticipants private void MapParticipantObject(NotificationParticipant oNotificationParticipant, DataReader oReader) { base.SetObjectID(oNotificationParticipant, oReader.GetID("NotificationParticipantID")); oNotificationParticipant.NotificationID = oReader.GetID("NotificationID"); oNotificationParticipant.EmployeeID = oReader.GetID("EmployeeID"); oNotificationParticipant.Comments = oReader.GetString("Comments"); oNotificationParticipant.SentTime = (DateTime) oReader.GetDateTime("SentTime"); this.SetObjectState(oNotificationParticipant, Ease.CoreV35.ObjectState.Saved); } private NotificationParticipant CreateParticipant(DataReader oReader) { NotificationParticipant oNotificationParticipant = new NotificationParticipant(); MapParticipantObject(oNotificationParticipant, oReader); return oNotificationParticipant; } private ObjectsTemplate CreateParticipants(DataReader oReader) { ObjectsTemplate oNotificationParticipants = new ObjectsTemplate(); NotificationParticipant oNotificationParticipant; while (oReader.Read()) { oNotificationParticipant = new NotificationParticipant(); oNotificationParticipant = CreateParticipant(oReader); oNotificationParticipants.Add(oNotificationParticipant); } return oNotificationParticipants; } #endregion #region Map NotificationRule private void MapNotificationRuleObject(NotificationRule oNotificationRule, DataReader oReader) { base.SetObjectID(oNotificationRule, oReader.GetID("NotificationRuleID")); oNotificationRule.BatchNo = oReader.GetInt32("BatchNo").Value; oNotificationRule.NotificationID = oReader.GetID("NotificationID"); oNotificationRule.PerticipantID = oReader.GetID("PerticipantID"); oNotificationRule.PerticipantType = (EnumPerticipantType) oReader.GetInt32("PerticipantType"); oNotificationRule.SendDate = oReader.GetDateTime("SendDate").Value; oNotificationRule.ExtendedBody = oReader.GetString("ExtendedBody"); oNotificationRule.Remarks = oReader.GetString("Remarks"); oNotificationRule.EmailSendStatus = (EnumSendStatus)oReader.GetInt32("EmailSendStatus"); oNotificationRule.WebSendStatus = (EnumSendStatus)oReader.GetInt32("WebSendStatus"); oNotificationRule.SMSSendStatus = (EnumSendStatus)oReader.GetInt32("SMSSendStatus"); oNotificationRule.AllowWrite = (bool)oReader.GetBoolean("AllowWrite"); this.SetObjectState(oNotificationRule, Ease.CoreV35.ObjectState.Saved); } private NotificationRule CreateNotificationRule(DataReader oReader) { NotificationRule oNotificationRule = new NotificationRule(); MapNotificationRuleObject(oNotificationRule, oReader); return oNotificationRule; } private ObjectsTemplate CreateNotificationRules(DataReader oReader) { ObjectsTemplate oCreateNotificationRules = new ObjectsTemplate(); NotificationRule oCreateNotificationRule; while (oReader.Read()) { oCreateNotificationRule = CreateNotificationRule(oReader); oCreateNotificationRules.Add(oCreateNotificationRule); } return oCreateNotificationRules; } #endregion #endregion #region Implementation of INotificationService #region Get Functions public int GetNewID() { TransactionContext tc = null; tc = TransactionContext.Begin(true); try { return tc.GenerateID("Notification", "NotificationID"); } catch (Exception e) { throw e; } finally { tc.End(); } } public Notification Get(ID id) { Notification oNotification = null; #region Cache Header oNotification = _cache["Get", id] as Notification; if (oNotification != null) return oNotification; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NotificationDA.Get(tc,id)); if(oreader.Read()) { oNotification = CreateObject(oreader); } oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oNotification, "Get", id); #endregion return oNotification; } public ObjectsTemplate Get() { ObjectsTemplate oNotifications = new ObjectsTemplate(); #region Cache Header oNotifications = _cache["Get"] as ObjectsTemplate; if (oNotifications != null) return oNotifications; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NotificationDA.Get(tc)); oNotifications = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oNotifications, "Get"); #endregion return oNotifications; } public ObjectsTemplate Get(EnumNotificationStatus status) { ObjectsTemplate oNotifications = new ObjectsTemplate(); #region Cache Header oNotifications = _cache["Get"] as ObjectsTemplate; if (oNotifications != null) return oNotifications; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NotificationDA.Get(tc,status)); oNotifications = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oNotifications, "Get"); #endregion return oNotifications; } public ObjectsTemplate GetByDateRange(DateTime startDate, DateTime endDate) { ObjectsTemplate oNotifications = new ObjectsTemplate(); #region Cache Header oNotifications = _cache["Get"] as ObjectsTemplate; if (oNotifications != null) return oNotifications; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NotificationDA.GetByDateRange(tc,startDate,endDate)); oNotifications = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oNotifications, "Get"); #endregion return oNotifications; } public ObjectsTemplate GetNotificationParticipants(ID id) { ObjectsTemplate oNotificationParticipants = new ObjectsTemplate(); #region Cache Header oNotificationParticipants = _cache["Get", id] as ObjectsTemplate; if (oNotificationParticipants != null) return oNotificationParticipants; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NotificationDA.GetParticipants(tc, id)); oNotificationParticipants = CreateParticipants(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oNotificationParticipants, "Get", id); #endregion return oNotificationParticipants; } public ObjectsTemplate GetNotificationRules(ID id) { ObjectsTemplate oCreateNotificationRules = new ObjectsTemplate(); #region Cache Header oCreateNotificationRules = _cache["Get", id] as ObjectsTemplate; if (oCreateNotificationRules != null) return oCreateNotificationRules; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NotificationDA.GetNotificationRules(tc, id)); oCreateNotificationRules = CreateNotificationRules(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oCreateNotificationRules, "Get", id); #endregion return oCreateNotificationRules; } #endregion #region Get Scalars public int GetNewBatchNumber() { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); int id = tc.GenerateID("NotificationRule", "BatchNo"); return id; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } private int GetNewBatchNumber(TransactionContext tc) { try { int id = tc.GenerateID("NotificationRule", "BatchNo"); return id; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DateTime GetMinSendDateByID(ID iD) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DateTime minDate = NotificationDA.GetMinDateByID(tc, iD); return minDate; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } #endregion public ID Save(Notification oNotification) { TransactionContext tc = null; try { int startingBatchNo; int previousBatchNo = 0; tc = TransactionContext.Begin(true); if (oNotification.IsNew) { int id = tc.GenerateID("Notification", "NotificationID"); base.SetObjectID(oNotification, ID.FromInteger(id)); NotificationDA.Insert(tc, oNotification); startingBatchNo = GetNewBatchNumber(tc); foreach (NotificationRule notificationRule in oNotification.NotificationRules) { if(previousBatchNo!=notificationRule.BatchNo && (previousBatchNo!=0)) { startingBatchNo++; } previousBatchNo = notificationRule.BatchNo; notificationRule.BatchNo = startingBatchNo; base.SetObjectID(notificationRule, ID.FromInteger(tc.GenerateID("NotificationRule", "NotificationRuleID"))); notificationRule.NotificationID = ID.FromInteger(id); NotificationDA.InsertNotificationRule(tc, notificationRule); } } else { NotificationDA.Update(tc, oNotification); NotificationDA.DeleteNotificationRules(tc,oNotification.ID); startingBatchNo = GetNewBatchNumber(tc); foreach (NotificationRule notificationRule in oNotification.NotificationRules) { if (previousBatchNo != notificationRule.BatchNo && (previousBatchNo != 0)) { startingBatchNo++; } previousBatchNo = notificationRule.BatchNo; notificationRule.BatchNo = startingBatchNo; base.SetObjectID(notificationRule, ID.FromInteger(tc.GenerateID("NotificationRule", "NotificationRuleID"))); notificationRule.NotificationID = ID.FromInteger(oNotification.ID.Integer); NotificationDA.InsertNotificationRule(tc, notificationRule); } } return oNotification.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void SaveReply(NotificationParticipant oParticipant) { TransactionContext tc = null; tc = TransactionContext.Begin(true); try { if (oParticipant.IsNew) { int id = tc.GenerateID("NotificationParticipant", "NotificationParticipantID"); base.SetObjectID(oParticipant, ID.FromInteger(id)); NotificationDA.InsertReply(tc, oParticipant); } else { } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void UpdateNotificationRule(NotificationRule notfRule) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); NotificationDA.UpdateNotficationRule(tc, notfRule); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void Delete(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); NotificationDA.DeleteParticipants(tc,id); NotificationDA.DeleteNotificationRules(tc, id); NotificationDA.Delete(tc, id); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } #endregion } }