private static Message GenerateMessage(Subscription subscription, Dictionary<string, NotificationConfig> configs)
{
if (subscription.RelatedEvents.Count == 0)
return null;
// we will process those content only for which no config is available. if there is no such content, we are done.
if (!subscription.RelatedEvents.Any(e => configs[e.ContentPath] == null))
return null;
var template = new MessageTemplate(subscription.Language);
var cultureInfo = CultureInfo.CreateSpecificCulture(subscription.Language);
string subjectTemplate = null;
switch (subscription.Frequency)
{
case NotificationFrequency.Immediately: subjectTemplate = template.ImmediatelySubject; break;
case NotificationFrequency.Daily: subjectTemplate = template.DailySubject; break;
case NotificationFrequency.Weekly: subjectTemplate = template.WeeklySubject; break;
case NotificationFrequency.Monthly: subjectTemplate = template.MonthlySubject; break;
default: throw GetUnknownFrequencyException(subscription.Frequency);
}
var subject = ReplaceParameters(subjectTemplate, subscription);
var body = new StringBuilder();
string headTemplate = null;
switch (subscription.Frequency)
{
case NotificationFrequency.Immediately: headTemplate = template.ImmediatelyHeader; break;
case NotificationFrequency.Daily: headTemplate = template.DailyHeader; break;
case NotificationFrequency.Weekly: headTemplate = template.WeeklyHeader; break;
case NotificationFrequency.Monthly: headTemplate = template.MonthlyHeader; break;
default: throw GetUnknownFrequencyException(subscription.Frequency);
}
body.Append(ReplaceParameters(headTemplate, subscription));
foreach (var @event in subscription.RelatedEvents)
{
// a custom config exists for this contentpath, email for that has already been processed previously
if (configs[@event.ContentPath] != null)
continue;
body.Append(ReplaceParameters(GetEventTemplate(template, @event.NotificationType), subscription.SitePath, subscription.SiteUrl, @event, cultureInfo));
}
string footTemplate = null;
switch (subscription.Frequency)
{
case NotificationFrequency.Immediately: footTemplate = template.ImmediatelyFooter; break;
case NotificationFrequency.Daily: footTemplate = template.DailyFooter; break;
case NotificationFrequency.Weekly: footTemplate = template.WeeklyFooter; break;
case NotificationFrequency.Monthly: footTemplate = template.MonthlyFooter; break;
default: throw GetUnknownFrequencyException(subscription.Frequency);
}
body.Append(ReplaceParameters(footTemplate, subscription));
return new Message
{
Address = subscription.UserEmail,
Subject = subject,
Body = body.ToString()
};
}