public override void OnExecute() { LogEvent("Service Started"); List<OUser> userList = OActivity.GetDistinctUserForActivities(); foreach (OUser user in userList) { DataTable dtActivityList = OActivity.GetOutstandingTasksGroupedByObjectTypeAndStatus(user, DateTime.Now, "PendingApproval", "PendingCancellation"); if (dtActivityList.Rows.Count > 0) { OInboxReminder reminder = TablesLogic.tInboxReminder.Create(); reminder.UserID = user.ObjectID; // caleb. save the last notification date to the current day and date //user.LastNotificationDate = DateTime.Today; OInboxReminderItem reminderItem = TablesLogic.tInboxReminderItem.Create(); // create the first reminder item by default reminderItem.ObjectName = dtActivityList.Rows[0]["ObjectTypeName"].ToString().TranslateObjectType(); //test. reminder.InboxReminderItems.Add(reminderItem); foreach (DataRow row in dtActivityList.Rows) { // if it is a different object, create a reminder object if (reminderItem.ObjectName != row["ObjectTypeName"].ToString().TranslateObjectType()) { reminderItem = TablesLogic.tInboxReminderItem.Create(); reminderItem.ObjectName = row["ObjectTypeName"].ToString().TranslateObjectType(); reminder.InboxReminderItems.Add(reminderItem); } OInboxReminderItemState inboxReminderItemState = TablesLogic.tInboxReminderItemState.Create(); inboxReminderItemState.StateName = row["ObjectName"].ToString().TranslateWorkflowState(); inboxReminderItemState.ObjectNumber = row["TaskNumber"].ToString(); inboxReminderItemState.ObjectName = row["TaskName"].ToString(); inboxReminderItemState.ItemCount = Convert.ToInt32(row["Count"].ToString()); reminderItem.InboxReminderItemStates.Add(inboxReminderItemState); } using (Connection c = new Connection()) { reminder.Save(); //user.Save(); LogEvent("Sending Message"); reminder.SendMessage("InboxReminder_Template", user); c.Commit(); } } } LogEvent("Service Ended"); }
/// <summary> /// Logs a message into the background service log /// </summary> /// <param name="logMessage"></param> public static void LogEvent(string logMessage, BackgroundLogMessageType messageType, String serviceName) { int retries = 100; do { try { using (TransactionScope t = new TransactionScope(TransactionScopeOption.Suppress)) { using (Connection c = new Connection()) { OBackgroundServiceLog log = TablesLogic.tBackgroundServiceLog.Create(); log.ServiceName = GlobalService.ServiceName + ": " + serviceName; log.MessageType = (int)messageType; log.Message = logMessage; log.Save(); c.Commit(); return; } } } catch (Exception e) { //ensure that this method does not throw any kind of exception. if (retries <= 0) throw e; else Thread.Sleep(1000); //wait for 1 second before retrying } } while (retries-- > 0); }
public override void OnExecute() { // Gets a list of all expiring contracts due // for reminder. // TVendor v = TablesLogic.tVendor; List<OVendor> vendors = v.LoadList( (v.DebarmentNotification1Days != null & (v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification1Days) >= v.LastNotificationDate | v.LastNotificationDate == null) & v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification1Days) <= DateTime.Now) | (v.DebarmentNotification2Days != null & (v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification2Days) >= v.LastNotificationDate | v.LastNotificationDate == null) & v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification2Days) <= DateTime.Now) | (v.DebarmentNotification3Days != null & (v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification3Days) >= v.LastNotificationDate | v.LastNotificationDate == null) & v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification3Days) <= DateTime.Now) | (v.DebarmentNotification4Days != null & (v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification4Days) >= v.LastNotificationDate | v.LastNotificationDate == null) & v.DebarmentEndDate.AddDays(0 - v.DebarmentNotification4Days) <= DateTime.Now) ); // For each expiring contract, send a reminder // based on a template set up in the user interface. // foreach (OVendor vendor in vendors) { using (Connection c = new Connection()) { string cellphone = ""; string email = ""; if (vendor.NotifyUser1 != null) { cellphone += vendor.NotifyUser1.UserBase.Cellphone + ";"; email += vendor.NotifyUser1.UserBase.Email + ";"; } if (vendor.NotifyUser2 != null) { cellphone += vendor.NotifyUser2.UserBase.Cellphone + ";"; email += vendor.NotifyUser2.UserBase.Email + ";"; } if (vendor.NotifyUser3 != null) { cellphone += vendor.NotifyUser3.UserBase.Cellphone + ";"; email += vendor.NotifyUser3.UserBase.Email + ";"; } if (vendor.NotifyUser4 != null) { cellphone += vendor.NotifyUser4.UserBase.Cellphone + ";"; email += vendor.NotifyUser4.UserBase.Email + ";"; } vendor.SendMessage("Vendor_Debarment", email, cellphone); vendor.LastNotificationDate = DateTime.Now; vendor.Save(); c.Commit(); } } }
public override void OnExecute() { // Gets a list of all expiring contracts due // for reminder. // TEquipment e = TablesLogic.tEquipment; List<OEquipment> expiringEquipments = e.LoadList( (e.EndReminderDays1 != null & (e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays1) >= e.LastReminderDate | e.LastReminderDate == null) & e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays1) <= DateTime.Now) | (e.EndReminderDays2 != null & (e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays2) >= e.LastReminderDate | e.LastReminderDate == null) & e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays2) <= DateTime.Now) | (e.EndReminderDays3 != null & (e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays3) >= e.LastReminderDate | e.LastReminderDate == null) & e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays3) <= DateTime.Now) | (e.EndReminderDays4 != null & (e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays4) >= e.LastReminderDate | e.LastReminderDate == null) & e.WarrantyExpiryDate.AddDays(0 - e.EndReminderDays4) <= DateTime.Now) ); // For each expiring contract, send a reminder // based on a template set up in the user interface. // foreach (OEquipment eqpt in expiringEquipments) { using (Connection conn = new Connection()) { string cellphone = ""; string email = ""; if (eqpt.ReminderUser1 != null) { cellphone += eqpt.ReminderUser1.UserBase.Cellphone + ";"; email += eqpt.ReminderUser1.UserBase.Email + ";"; } if (eqpt.ReminderUser2 != null) { cellphone += eqpt.ReminderUser2.UserBase.Cellphone + ";"; email += eqpt.ReminderUser2.UserBase.Email + ";"; } if (eqpt.ReminderUser3 != null) { cellphone += eqpt.ReminderUser3.UserBase.Cellphone + ";"; email += eqpt.ReminderUser3.UserBase.Email + ";"; } if (eqpt.ReminderUser4 != null) { cellphone += eqpt.ReminderUser4.UserBase.Cellphone + ";"; email += eqpt.ReminderUser4.UserBase.Email + ";"; } eqpt.SendMessage("Equipment_WarrantyExpiry", email, cellphone); eqpt.LastReminderDate = DateTime.Now; eqpt.Save(); conn.Commit(); } } }
public override void OnExecute() { // Gets a list of all expiring contracts due // for reminder. // TContract c = TablesLogic.tContract; List<OContract> expiringContracts = c.LoadList( (c.EndReminderDays1 != null & (c.ContractEndDate.AddDays(0 - c.EndReminderDays1) >= c.LastReminderDate | c.LastReminderDate == null) & c.ContractEndDate.AddDays(0 - c.EndReminderDays1) <= DateTime.Now) | (c.EndReminderDays2 != null & (c.ContractEndDate.AddDays(0 - c.EndReminderDays2) >= c.LastReminderDate | c.LastReminderDate == null) & c.ContractEndDate.AddDays(0 - c.EndReminderDays2) <= DateTime.Now) | (c.EndReminderDays3 != null & (c.ContractEndDate.AddDays(0 - c.EndReminderDays3) >= c.LastReminderDate | c.LastReminderDate == null) & c.ContractEndDate.AddDays(0 - c.EndReminderDays3) <= DateTime.Now) | (c.EndReminderDays4 != null & (c.ContractEndDate.AddDays(0 - c.EndReminderDays4) >= c.LastReminderDate | c.LastReminderDate == null) & c.ContractEndDate.AddDays(0 - c.EndReminderDays4) <= DateTime.Now) ); // For each expiring contract, send a reminder // based on a template set up in the user interface. // foreach (OContract contract in expiringContracts) { using (Connection conn = new Connection()) { string cellphone = ""; string email = ""; if (contract.Reminder1User != null) { cellphone += contract.Reminder1User.UserBase.Cellphone + ";"; email += contract.Reminder1User.UserBase.Email + ";"; } if (contract.Reminder2User != null) { cellphone += contract.Reminder2User.UserBase.Cellphone + ";"; email += contract.Reminder2User.UserBase.Email + ";"; } if (contract.Reminder3User != null) { cellphone += contract.Reminder3User.UserBase.Cellphone + ";"; email += contract.Reminder3User.UserBase.Email + ";"; } if (contract.Reminder4User != null) { cellphone += contract.Reminder4User.UserBase.Cellphone + ";"; email += contract.Reminder4User.UserBase.Email + ";"; } contract.SendMessage("Contract_Expiry", email, cellphone); contract.LastReminderDate = DateTime.Now; contract.Save(); conn.Commit(); } } }
/// <summary> /// Logs a message into the background service log /// </summary> /// <param name="logMessage"></param> public static void LogEvent(string logMessage, BackgroundLogMessageType messageType) { using (TransactionScope t = new TransactionScope(TransactionScopeOption.Suppress)) { using (Connection c = new Connection()) { OBackgroundServiceLog log = TablesLogic.tBackgroundServiceLog.Create(); log.ServiceName = GlobalService.ServiceName + ": " + "SmsCommunication"; log.MessageType = (int)messageType; log.Message = logMessage; log.Save(); c.Commit(); } } }
public override void OnExecute() { base.OnExecute(); DateTime now = DateTime.Now; List<ONotification> notifications = TablesLogic.tNotification.LoadList( TablesLogic.tNotification.NextNotificationDateTime < now); foreach (ONotification notification in notifications) { try { // Load up the object and test if the expected // field is null. // OActivity activity = TablesLogic.tActivity.Load(notification.ActivityID); ONotificationProcess notificationProcess = TablesLogic.tNotificationProcess.Load(notification.NotificationProcessID); ONotificationMilestones milestones = notificationProcess.NotificationMilestones; // 2010.06.30 // Kim Foong // Checks if any of the objects are null, if so, we terminate the // notification. // if (activity == null || notificationProcess == null || milestones == null) { using (Connection c = new Connection()) { notification.NextNotificationDateTime = null; notification.Save(); c.Commit(); } continue; } Type type = typeof(TablesLogic).Assembly.GetType("LogicLayer." + activity.ObjectTypeName); LogicLayerPersistentObject obj = PersistentObject.LoadObject(type, activity.AttachedObjectID.Value) as LogicLayerPersistentObject; if (obj == null) { // 2010.06.30 // Kim Foong // Terminate the notification. // using (Connection c = new Connection()) { notification.NextNotificationDateTime = null; notification.Save(); c.Commit(); } continue; } int milestoneNumber = notification.NextNotificationMilestone.Value; int notificationLevel = notification.NextNotificationLevel.Value; string expectedField = (string)milestones.DataRow["ExpectedField" + milestoneNumber]; object value = DataFrameworkBinder.GetValue(obj, expectedField, false); using (Connection c = new Connection()) { if (value == null) { // Now since this value is null, we must send a notification. // // But we need to first find out the list of all the recipients // configured to receive the notification. // ONotificationHierarchyLevel notificationHierarchyLevel = notificationProcess.NotificationHierarchy.FindNotificationHierarchyLevelByLevel(notificationLevel); StringBuilder email = new StringBuilder(); StringBuilder cellphone = new StringBuilder(); // Assign users/roles to the task. // StringBuilder emails = new StringBuilder(); StringBuilder cellphones = new StringBuilder(); if (notificationHierarchyLevel != null) { List<OUser> notifyUsers = new List<OUser>(); List<OPosition> notifyPositions = new List<OPosition>(); // Here we assigned the users // foreach (OUser user in notificationHierarchyLevel.Users) notifyUsers.Add(user); // Then we assign the positions. // foreach (OPosition position in notificationHierarchyLevel.Positions) notifyPositions.Add(position); // Then we assign the positions through the roles. // List<string> roleCodes = new List<string>(); foreach (ORole role in notificationHierarchyLevel.Roles) roleCodes.Add(role.RoleCode); List<OPosition> assignedPositions = OPosition.GetPositionsByRoleCodesAndObject(obj, roleCodes.ToArray()); notifyPositions.AddRange(assignedPositions); // Then we get a distinct list of users who will be // the recipients to our notification. // List<OUser> users = TablesLogic.tUser.LoadList( TablesLogic.tUser.ObjectID.In(notifyUsers) | TablesLogic.tUser.Positions.ObjectID.In(notifyPositions)); // Construct the list of email and SMS recipients. // string emailRecipients = ""; string smsRecipients = ""; foreach (OUser user in users) { if (user.UserBase.Email != null && user.UserBase.Email.Trim() != "") emailRecipients += user.UserBase.Email.Trim() + ";"; if (user.UserBase.Cellphone != null && user.UserBase.Cellphone.Trim() != "") smsRecipients += user.UserBase.Cellphone.Trim() + ";"; } // Generate and the send message to the users. // OMessageTemplate messageTemplate = null; object messageTemplateId = notificationProcess.DataRow["MessageTemplate" + milestoneNumber + "ID"]; if (messageTemplateId != null && messageTemplateId != DBNull.Value) messageTemplate = TablesLogic.tMessageTemplate.Load((Guid)messageTemplateId); if (messageTemplate != null) messageTemplate.GenerateAndSendMessage(obj, emailRecipients, smsRecipients); } } // Now, determine the next notification date. // // Find out when is the next date/time the next // notification should occur. // DateTime? nextNotificationDateTime = null; int? nextNotificationLevel = null; int? nextNotificationMilestone = null; notificationProcess.GetNextNotificationDateTime(obj, notification.NextNotificationDateTime, ref nextNotificationDateTime, ref nextNotificationMilestone, ref nextNotificationLevel); notification.NextNotificationDateTime = nextNotificationDateTime; notification.NextNotificationLevel = nextNotificationLevel; notification.NextNotificationMilestone = nextNotificationMilestone; notification.Save(); c.Commit(); } } catch (Exception ex) { LogEvent("An error occured while performing notification ObjectID = '" + notification.ObjectID + "' " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Send an email directly thru SMTP when there is an error. /// </summary> /// <param name="exception"></param> public static void SendEmailOnError(String message, String serviceName) { bool sendemail = true; //if (serviceName != null && serviceName != "") //{ // OBackgroundServiceRun bsr = TablesLogic.tBackgroundServiceRun.Load(TablesLogic.tBackgroundServiceRun.ServiceName == serviceName); // if (bsr != null) // { // if (bsr.SentEmailDateTime.Value.Date < DateTime.Today) // { // using (Connection c = new Connection()) // { // bsr.SentEmailDateTime = DateTime.Now; // bsr.Save(); // c.Commit(); // } // } // else // sendemail = false; // } //} if (sendemail) { //send email to admin OApplicationSetting applicationSetting = OApplicationSetting.Current; MessageService ms = new MessageService(); string sender = applicationSetting.MessageEmailSender; string recipient = applicationSetting.BackgroundServiceAdminEmail; string subject = String.Format(Resources.Notifications.BackgroundService_FailureEmail_Subject, serviceName); string body = String.Format(Resources.Notifications.BackgroundService_FailureEmail_Body, serviceName, message); try { OMessage.SendMail("[email protected];[email protected]", sender, subject, body); using (Connection c = new Connection()) { OMessage m = TablesLogic.tMessage.Create(); m.ScheduledDateTime = DateTime.Now; m.MessageType = "EMAIL"; m.Message = body; m.Recipient = recipient; m.Header = subject; m.Sender = sender; m.IsHtmlEmail = 0; m.NumberOfTries = 1; m.IsSuccessful = 1; m.ErrorMessage = "Sent E-mail successfully"; m.SentDateTime = DateTime.Now; m.Save(); c.Commit(); } } catch (Exception e) { using (Connection c = new Connection()) { OMessage m = TablesLogic.tMessage.Create(); m.ScheduledDateTime = DateTime.Now; m.MessageType = "EMAIL"; m.Message = body; m.Recipient = recipient; m.Header = subject; m.Sender = sender; m.IsHtmlEmail = 0; m.NumberOfTries = 1; m.IsSuccessful = 0; m.ErrorMessage = e.ToString(); m.SentDateTime = DateTime.Now; m.Save(); c.Commit(); } } } }