Example #1
0
 /// <summary>
 /// Handles a single notification template that has been loaded, along with
 /// customer details and other metadata associated.
 /// </summary>
 /// <param name="notificationTemplate">The notification template to be handled.</param>
 /// <param name="options">The notification options used in this request.</param>
 public void Handle(INotificationTemplate notificationTemplate, NotificationOptions options)
 {
     if (notificationTemplate is TTemplate)
     {
         this.InternalHandle((TTemplate)notificationTemplate, options);
     }
 }
        /// <summary>
        /// Parse a template and replace placeholders with specified values.
        /// </summary>
        /// <param name="nodeName">Name of the node containing the template contents</param>
        /// <param name="template">Template to be parsed</param>
        /// <param name="templateParams">Object contaiing placeholder propreties to be replaced</param>
        /// <returns></returns>
        public IDictionary<string, string> ParseTemplate(string nodeName,
            INotificationTemplate template, object templateParams)
        {
            // Load template
            var templateNode = template.Read();
            if (templateNode == null)
            {
                throw new XmlException(
                    string.Format("Could not find template node {0} in template.", nodeName));
            }

            var parsedNodes = new Dictionary<string, string>();

            // Parse placeholders
            foreach (var node in templateNode.Descendants())
            {
                // Loop through template properties and replace placeholders with values
                if (templateParams != null)
                {
                    var properties = templateParams.GetType()
                        .GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    foreach (var prop in properties)
                    {
                        var value = (string)prop.GetValue(templateParams, null);
                        string placeholder = "{{" + prop.Name + "}}";
                        parsedNodes.Add(node.Name.ToString(), node.Value.Replace(placeholder, value));
                    }
                } else
                {
                    parsedNodes.Add(node.Name.ToString(), node.Value);
                }
            }

            return parsedNodes;
        }
Example #3
0
        private void ProcessUsingHandler(INotificationTemplate notificationTemplate, NotificationOptions options, INotificationHandler handler)
        {
            var handlerName = handler.GetType().Name;

            this._logger.LogDebug("Processing notification. handler={0}", handlerName);

            handler.Handle(notificationTemplate, options);
        }
        /// <summary>
        /// Send an email to the specified user
        /// </summary>
        /// <param name="recipient">Recipient of the email</param>
        /// <param name="template">Template used for email</param>
        /// <param name="messageParams">Values for placeholders used in template to replace with.</param>
        public async Task <bool> Notify(User recipient, INotificationTemplate template, object messageParams)
        {
            var message = new MailMessage(new MailAddress(_fromEmail, _fromName),
                                          new MailAddress(recipient.Email));

            IDictionary <string, string> templateValues =
                _templateService.ParseTemplate(TemplateNode, template, messageParams);

            message.Subject = templateValues["Subject"];

            // Wrap the body in the parent template
            var parentEmailTemplate =
                new NotificationTemplate(template.ContainingDirectory, "Email");

            IDictionary <string, string> parentTemplate =
                _templateService.ParseTemplate("Template", parentEmailTemplate, new
            {
                Body = templateValues["Body"]
            });

            message.Body       = parentTemplate["Message"];
            message.IsBodyHtml = true;

            try
            {
                _emailService.Send(message);

                _logService.CreateLog(new Log
                {
                    Category = LogCategory.Application,
                    Level    = LogLevel.Info,
                    Message  = string.Format("Email notification to {0} successful: {1}", message.To, message.Subject),
                    Details  = message.Body
                });
            }
            catch (SmtpException ex)
            {
                _logService.CreateLog(new Log
                {
                    Category = LogCategory.Application,
                    Level    = LogLevel.Error,
                    Message  = string.Format("Email notification to {0} failed: {1}", message.To, message.Subject),
                    Details  = ex.Message + " \n\n Original Email: " + message.Body
                });
            }

            await Task.Yield();

            return(true);
        }
        /// <summary>
        /// Send an email to the specified user
        /// </summary>
        /// <param name="recipient">Recipient of the email</param>
        /// <param name="template">Template used for email</param>
        /// <param name="messageParams">Values for placeholders used in template to replace with.</param>
        public async Task<bool> Notify(User recipient, INotificationTemplate template, object messageParams)
        {
            var message = new MailMessage(new MailAddress(_fromEmail, _fromName),
                new MailAddress(recipient.Email));

            IDictionary<string, string> templateValues = 
                _templateService.ParseTemplate(TemplateNode, template, messageParams);

            message.Subject = templateValues["Subject"];

            // Wrap the body in the parent template
            var parentEmailTemplate =
                new NotificationTemplate(template.ContainingDirectory, "Email");

            IDictionary<string, string> parentTemplate =
                _templateService.ParseTemplate("Template", parentEmailTemplate, new
                {
                    Body = templateValues["Body"]
                });

            message.Body = parentTemplate["Message"];
            message.IsBodyHtml = true;

            try
            {
                _emailService.Send(message);

                _logService.CreateLog(new Log
                {
                    Category = LogCategory.Application,
                    Level = LogLevel.Info,
                    Message = string.Format("Email notification to {0} successful: {1}", message.To, message.Subject),
                    Details = message.Body
                });
            }
            catch (SmtpException ex)
            {
                _logService.CreateLog(new Log
                {
                    Category = LogCategory.Application,
                    Level = LogLevel.Error,
                    Message = string.Format("Email notification to {0} failed: {1}", message.To, message.Subject),
                    Details = ex.Message + " \n\n Original Email: " + message.Body
                });
            }

            await Task.Yield();
            return true;
        }
Example #6
0
        /// <summary>
        /// Parse a template and replace placeholders with specified values.
        /// </summary>
        /// <param name="nodeName">Name of the node containing the template contents</param>
        /// <param name="template">Template to be parsed</param>
        /// <param name="templateParams">Object contaiing placeholder propreties to be replaced</param>
        /// <returns></returns>
        public IDictionary <string, string> ParseTemplate(string nodeName,
                                                          INotificationTemplate template, object templateParams)
        {
            // Load template
            var templateNode = template.Read();

            if (templateNode == null)
            {
                throw new XmlException(
                          string.Format("Could not find template node {0} in template.", nodeName));
            }

            var parsedNodes = new Dictionary <string, string>();

            // Parse placeholders
            foreach (var node in templateNode.Descendants())
            {
                // Loop through template properties and replace placeholders with values
                if (templateParams != null)
                {
                    var properties = templateParams.GetType()
                                     .GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    foreach (var prop in properties)
                    {
                        var    value       = (string)prop.GetValue(templateParams, null);
                        string placeholder = "{{" + prop.Name + "}}";
                        parsedNodes.Add(node.Name.ToString(), node.Value.Replace(placeholder, value));
                    }
                }
                else
                {
                    parsedNodes.Add(node.Name.ToString(), node.Value);
                }
            }

            return(parsedNodes);
        }
Example #7
0
 /// <summary>
 /// Parse a template that does not contain any placeholders
 /// </summary>
 /// <param name="nodeName">Name of the node containing the template contents</param>
 /// <param name="template">Template to be parsed</param>
 /// <returns></returns>
 public IDictionary <string, string> ParseTemplate(string nodeName,
                                                   INotificationTemplate template)
 {
     return(ParseTemplate(nodeName, template, null));
 }
Example #8
0
 /// <summary>
 /// Send a notification with placeholders replaced with specified parameters
 /// </summary>
 /// <param name="recipient">User that will receive the notification</param>
 /// <param name="template">Template to be parsed</param>
 /// <param name="messageParams">Object containing propreties to be replaced</param>
 /// <returns></returns>
 public abstract Task <bool> Notify(User recipient, INotificationTemplate template, object messageParams);
Example #9
0
 /// <summary>
 /// Send a notification that does not contain any placeholders
 /// </summary>
 /// <param name="recipient">User that will receive the notification</param>
 /// <param name="template">Template to be parsed</param>
 /// <returns></returns>
 public Task <bool> Notify(User recipient, INotificationTemplate template)
 {
     return(Notify(recipient, template, null));
 }
 /// <summary>
 /// Parse a template that does not contain any placeholders
 /// </summary>
 /// <param name="nodeName">Name of the node containing the template contents</param>
 /// <param name="template">Template to be parsed</param>
 /// <returns></returns>
 public IDictionary<string, string> ParseTemplate(string nodeName,
     INotificationTemplate template)
 {
     return ParseTemplate(nodeName, template, null);
 }
 /// <summary>
 /// Send a notification with placeholders replaced with specified parameters
 /// </summary>
 /// <param name="recipient">User that will receive the notification</param>
 /// <param name="template">Template to be parsed</param>
 /// <param name="messageParams">Object containing propreties to be replaced</param>
 /// <returns></returns>
 public abstract Task<bool> Notify(User recipient, INotificationTemplate template, object messageParams);
 /// <summary>
 /// Send a notification that does not contain any placeholders
 /// </summary>
 /// <param name="recipient">User that will receive the notification</param>
 /// <param name="template">Template to be parsed</param>
 /// <returns></returns>
 public Task<bool> Notify(User recipient, INotificationTemplate template)
 {
     return Notify(recipient, template, null);
 }