Exemple #1
0
        /// <summary>
        /// Returns email body. Email has header details (event type, event type etc) and table for each audit event value.
        /// </summary>
        /// <param name="auditEvent"></param>
        /// <returns></returns>
        private string GetBody(AuditEvent auditEvent, string subEvent, List <AuditEventValueConverter> valueConverters, AuditEventValueTypeRepository auditEventValueTypeRepository)
        {
            AuditEventType auditEventType = new AuditEventTypeRepository().GetByID(auditEvent.EventTypeID);
            StringBuilder  html           = new StringBuilder(string.Format("<html><head>{0}</head><body>", GetStyle()));

            html.Append("<table>");
            //html.Append("<tr><th>Item</th><th>Value</th></tr>");
            DataTable valuesDataTable = GetValuesDataTable(auditEvent, auditEventType, subEvent, auditEventValueTypeRepository);

            for (int rowIndex = 0; rowIndex < valuesDataTable.Rows.Count; rowIndex++)
            {
                html.Append(string.Format("<tr><td><B>{0}</B></td><td>{1}</td></tr>", valuesDataTable.Rows[rowIndex]["Item"].ToString(), valuesDataTable.Rows[rowIndex]["Value"].ToString()));
            }
            html.Append("</table>");
            html.Append("</body>");
            html.Append("</html>");
            return(html.ToString());
        }
Exemple #2
0
        public MailMessage CreateEmail(AuditEvent auditEvent)
        {
            // Determine if we need to generate email
            EmailAuditEventSettings emailAuditEventSettings = _emailAuditEventSettingsList.Find(s => s.EventTypeId == auditEvent.EventTypeID);

            if (!Handles(auditEvent))
            {
                return(null);
            }

            AuditEventValueTypeRepository auditEventValueTypeRepository = new AuditEventValueTypeRepository();
            AuditEventType auditEventType = new AuditEventTypeRepository().GetByID(auditEvent.EventTypeID);

            // Determine if sub event is set
            string subEvent = "";

            foreach (int valueTypeId in new int[] { AuditEventValueTypes.GamePlanPipelineEventID, AuditEventValueTypes.GamePlanAutoBookEventID })
            {
                subEvent = GetValueDescription(auditEvent, valueTypeId, auditEventValueTypeRepository, _valueConverters);
                if (!String.IsNullOrEmpty(subEvent))
                {
                    break;
                }
            }

            MailMessage mail = new MailMessage();

            if (!String.IsNullOrEmpty(subEvent))   // Sub event indicated
            {
                mail.Subject = string.Format("[{0}] {1}: {2}: {3}", _environmentId, Globals.ProductName, auditEventType.Description, subEvent);
            }
            else
            {
                mail.Subject = string.Format("[{0}] {1}: {2}", _environmentId, Globals.ProductName, auditEventType.Description);
            }
            mail.Sender     = new MailAddress(emailAuditEventSettings.NotificationSettings.SenderAddress, emailAuditEventSettings.NotificationSettings.SenderAddress);
            mail.From       = new MailAddress(emailAuditEventSettings.NotificationSettings.SenderAddress, emailAuditEventSettings.NotificationSettings.SenderAddress);
            mail.Body       = GetBody(auditEvent, subEvent, _valueConverters, auditEventValueTypeRepository);
            mail.IsBodyHtml = mail.Body.ToLower().Contains("<html>") && (mail.Body.ToLower().Contains("<head>") || mail.Body.ToLower().Contains("<head/>"));

            // Check if specific email address indicated in audit event
            List <AuditEventValue> recipientAuditEventValues = new List <AuditEventValue>();
            AuditEventValue        recipientAuditEventValue  = auditEvent.GetValueByValueTypeId(AuditEventValueTypes.RecipientEmailAddress);

            if (recipientAuditEventValue != null)
            {
                recipientAuditEventValues.Add(recipientAuditEventValue);
            }
            if (recipientAuditEventValues.Count == 0)
            {
                for (int index = 0; index < emailAuditEventSettings.NotificationSettings.RecipientAddresses.Count; index++)
                {
                    mail.To.Add(new MailAddress(emailAuditEventSettings.NotificationSettings.RecipientAddresses[index], emailAuditEventSettings.NotificationSettings.RecipientAddresses[index]));
                }
            }
            else
            {
                foreach (AuditEventValue auditEventValue in recipientAuditEventValues)
                {
                    mail.To.Add(new MailAddress(auditEventValue.Value.ToString()));
                }
            }
            if (emailAuditEventSettings.NotificationSettings.CCAddresses != null)
            {
                emailAuditEventSettings.NotificationSettings.CCAddresses.ForEach(address => mail.CC.Add(new MailAddress(address, address)));
            }

            /*
             * foreach (AuditEventValue auditEventValue in auditEvent.Values.Where(item => item.TypeID == AuditEventValueTypes.EmailAttachment))
             * {
             *  mail.Attachments.Add(new Attachment(auditEventValue.Value.ToString()));
             * }
             */
            return(mail);
        }
Exemple #3
0
        public MailMessage CreateEmail(AuditEvent auditEvent)
        {
            // Determine if we need to generate email
            if (!Handles(auditEvent))
            {
                return(null);
            }

            using (var scope = _repositoryFactory.BeginRepositoryScope())
            {
                EmailAuditEventSettings emailAuditEventSettings = _emailAuditEventSettingsList.Find(s => s.EventTypeId == auditEvent.EventTypeID);

                AuditEventValueTypeRepository auditEventValueTypeRepository = new AuditEventValueTypeRepository();

                // Determine if sub event is set
                string subEvent = "";
                foreach (int valueTypeId in new int[] { AuditEventValueTypes.GamePlanPipelineEventID, AuditEventValueTypes.GamePlanAutoBookEventID })
                {
                    subEvent = GetValueDescription(auditEvent, valueTypeId, auditEventValueTypeRepository, _valueConverters);
                    if (!String.IsNullOrEmpty(subEvent))
                    {
                        break;
                    }
                }

                // Get run details, check if specific scenario specified
                var             runRepository             = scope.CreateRepository <IRunRepository>();
                AuditEventValue runIdAuditEventValue      = auditEvent.GetValueByValueTypeId(AuditEventValueTypes.GamePlanRunID);
                AuditEventValue scenarioIdAuditEventValue = auditEvent.GetValueByValueTypeId(AuditEventValueTypes.GamePlanScenarioID);
                Run             run = runRepository.Find((Guid)runIdAuditEventValue.Value);

                AuditEventType auditEventType = new AuditEventTypeRepository().GetByID(auditEvent.EventTypeID);
                MailMessage    mail           = new MailMessage();
                if (!String.IsNullOrEmpty(subEvent))   // No pipeline event
                {
                    mail.Subject = string.Format("[{0}] {1}: {2}: {3}", _environmentId, Globals.ProductName, auditEventType.Description, subEvent);
                }
                else
                {
                    mail.Subject = string.Format("[{0}] {1}: {2}", _environmentId, Globals.ProductName, auditEventType.Description);
                }

                // Indicate if run failed in subject, bit of a hack as we have to check the message string
                if (auditEvent.EventTypeID == AuditEventTypes.GamePlanRunCompleted)
                {
                    string message = GetValueDescription(auditEvent, AuditEventValueTypes.Message, auditEventValueTypeRepository, _valueConverters);
                    if (!String.IsNullOrEmpty(message) && message.ToLower().Contains("errors"))
                    {
                        mail.Subject = mail.Subject + string.Format(" ({0})", message);
                    }
                }

                // Load scenarios
                var scenarioRepository = scope.CreateRepository <IScenarioRepository>();

                var             scenarioIds = run.Scenarios.Select(e => e.Id).Distinct().ToArray();
                List <Scenario> scenarios   = scenarioRepository.FindByIds(scenarioIds).ToList();

                mail.Sender = new MailAddress(emailAuditEventSettings.NotificationSettings.SenderAddress, emailAuditEventSettings.NotificationSettings.SenderAddress);
                mail.From   = new MailAddress(emailAuditEventSettings.NotificationSettings.SenderAddress, emailAuditEventSettings.NotificationSettings.SenderAddress);
                mail.Body   = scenarioIdAuditEventValue == null?GetBody(auditEvent, auditEventType, run, scenarios, subEvent, auditEventValueTypeRepository) : GetBody(auditEvent, auditEventType, run, (Guid)scenarioIdAuditEventValue.Value, scenarios.First(s => s.Id == (Guid)scenarioIdAuditEventValue.Value), subEvent, auditEventValueTypeRepository);

                mail.IsBodyHtml = mail.Body.ToLower().Contains("<html>") && (mail.Body.ToLower().Contains("<head>") || mail.Body.ToLower().Contains("<head/>"));

                // Check if specific email address indicated in audit event

                List <AuditEventValue> recipientAuditEventValues = new List <AuditEventValue>();
                AuditEventValue        recipientAuditEventValue  = auditEvent.GetValueByValueTypeId(AuditEventValueTypes.RecipientEmailAddress);
                if (recipientAuditEventValue != null)
                {
                    recipientAuditEventValues.Add(recipientAuditEventValue);
                }
                if (recipientAuditEventValues.Count == 0)
                {
                    for (int index = 0; index < emailAuditEventSettings.NotificationSettings.RecipientAddresses.Count; index++)
                    {
                        mail.To.Add(new MailAddress(emailAuditEventSettings.NotificationSettings.RecipientAddresses[index], emailAuditEventSettings.NotificationSettings.RecipientAddresses[index]));
                    }
                }
                else
                {
                    foreach (AuditEventValue auditEventValue in recipientAuditEventValues)
                    {
                        mail.To.Add(new MailAddress(auditEventValue.Value.ToString()));
                    }
                }
                if (emailAuditEventSettings.NotificationSettings.CCAddresses != null)
                {
                    emailAuditEventSettings.NotificationSettings.CCAddresses.ForEach(address => mail.CC.Add(new MailAddress(address, address)));
                }

                /*
                 * foreach (AuditEventValue auditEventValue in auditEvent.Values.Where(item => item.TypeID == AuditEventValueTypes.EmailAttachment))
                 * {
                 *  mail.Attachments.Add(new Attachment(auditEventValue.Value.ToString()));
                 * }
                 */
                return(mail);
            }
        }