Пример #1
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);
            }

            AuditEventType auditEventType = _auditEventTypeRepository.GetByID(auditEvent.EventTypeID);

            MailMessage mail = new MailMessage();

            mail.Subject    = string.Format("{0}", 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, auditEventType);
            mail.IsBodyHtml = mail.Body.ToLower().Contains("<html>") && (mail.Body.ToLower().Contains("<head>") || mail.Body.ToLower().Contains("<head/>"));

            // Set list of recipients
            for (int index = 0; index < emailAuditEventSettings.NotificationSettings.RecipientAddresses.Count; index++)
            {
                mail.To.Add(new MailAddress(emailAuditEventSettings.NotificationSettings.RecipientAddresses[index], emailAuditEventSettings.NotificationSettings.RecipientAddresses[index]));
            }
            return(mail);
        }
Пример #2
0
        public bool Handles(AuditEvent auditEvent)
        {
            EmailAuditEventSettings emailAuditEventSettings = _emailAuditEventSettingsList == null ? null : _emailAuditEventSettingsList.Find(s => s.EventTypeId == auditEvent.EventTypeID && s.EmailCreatorId == this.Id);

            if (emailAuditEventSettings == null || emailAuditEventSettings.NotificationSettings == null || !emailAuditEventSettings.NotificationSettings.Enabled)
            {
                return(false);
            }
            return(true);
        }
Пример #3
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);
        }
Пример #4
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);
            }
        }