/// <summary> /// Retrieves the message of the given Message code and replaces all ReplacementCodes with the proper value /// </summary> /// <param name="textDefinitionCode">The code of the text definition to retrieve</param> /// <param name="objectsForParsing">Any objects that are available should be passed on as they may be used as part of the parsing process.</param> /// <returns>The text definition with both it's HTML and Text element parsed</returns> public TextDefinition ParseMessage(string textDefinitionCode, Dictionary <ReplaceableObjectKeys, object> objectsForParsing) { StringBuilder message = new StringBuilder(); TextDefinition td = this.ahm.MessageHandler.GetTextDefinitionByCode(textDefinitionCode); if (td == null) { return(new TextDefinition()); } ReplaceableRetriever retriever = new ReplaceableRetriever(this.ahm, objectsForParsing); TextParser <ReplaceableObjectKeys> parser = new TextParser <ReplaceableObjectKeys>(); string text = parser.ParseMessage(td.Text, retriever); string html = parser.ParseMessage(td.Html, retriever); TextDefinition result = new TextDefinition(); result.DefinitionCode = td.DefinitionCode; result.Text = text; result.Html = html; return(result); }
/// <summary> /// Runs the notification checker and sends outstanding notifications and reminders /// </summary> public static void SendNotifications() { AccessHandlerManager ahm = new AccessHandlerManager(); List <Notification> notifications = ahm.NotificationHandler.GetNotifications(); Dictionary <string, NotificationData> notificationData = new Dictionary <string, NotificationData>(); foreach (Notification n in notifications) { switch (n.NotificationType) { case NotificationType.RegistrationComplete: Task <User> t = ahm.UserAccessHandler.FindByIdAsync(n.NotificationObjectId); t.Wait(); User user = t.Result; NotifcationManager.AssignNotification(user, n.NotificationType, notificationData); n.NotificationSendTime = DateTime.Now; n.NotificationCompleted = true; ahm.NotificationHandler.UpdateNotification(n); break; case NotificationType.NewQuestionnaire: QuestionnaireUserResponseGroup group = ahm.QuestionnaireAccessHandler.GetSmallQuestionnaireUserResponseGroupById(int.Parse(n.NotificationObjectId)); Patient patient = ahm.UserAccessHandler.FindPatient(group.Patient.Id); if (group != null && !group.Completed && group.Patient.ProxyUserPatientMap.Any(m => m.User.EmailConfirmed) && (n.NotificationSendTime == null || n.NotificationSendTime < DateTime.Now.AddHours(-4))) { NotifcationManager.AssignNotification(patient, n.NotificationType, notificationData); n.NotificationSendTime = DateTime.Now; n.NotificationCompleted = true; ahm.NotificationHandler.UpdateNotification(n); } else { n.NotificationCompleted = true; ahm.NotificationHandler.UpdateNotification(n); } break; } } TextParser parser = new TextParser(ahm); foreach (string email in notificationData.Keys) { NotificationData data = notificationData[email]; StringBuilder textBuilder = new StringBuilder(); StringBuilder htmlBuilder = new StringBuilder(); ReplaceableObjectKeys objectKey = data.NotificationTarget.GetType() == typeof(User) ? ReplaceableObjectKeys.User : ReplaceableObjectKeys.Patient; TextDefinition start = parser.ParseMessage("NotificationStart", new Dictionary <ReplaceableObjectKeys, object>() { { objectKey, data.NotificationTarget } }); TextDefinition end = parser.ParseMessage("NotificationEnd", new Dictionary <ReplaceableObjectKeys, object>() { { objectKey, data.NotificationTarget } }); textBuilder.Append(start.Text); htmlBuilder.Append(start.Html); foreach (NotificationType t in data.Notifications) { TextDefinition td; td = parser.ParseMessage(t.ToString(), new Dictionary <ReplaceableObjectKeys, object>() { { objectKey, data.NotificationTarget } }); textBuilder.Append(td.Text); htmlBuilder.Append(td.Html); /* * switch (t) * { * case NotificationType.RegistrationComplete: * //textBuilder.AppendLine(Text) * td = parser.ParseMessage(NotificationType.RegistrationComplete.ToString(), null); * textBuilder.Append(td.Text); * htmlBuilder.Append(td.Html); * break; * case NotificationType.NewQuestionnaire: * td = parser.ParseMessage(NotificationType.RegistrationComplete.ToString(), null); * textBuilder.Append(td.Text); * htmlBuilder.Append(td.Html); * break; * }*/ } textBuilder.Append(end.Text); htmlBuilder.Append(end.Html); SmtpMailClient.SendMail(email, "Replay Notification", textBuilder.ToString(), htmlBuilder.ToString()); } }