//public AddEditEmailNotificationRuleModel()
        //{
        //    mEmailNotificationRule = new EmailNotificationRule();
        //    OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanExecuteOkButtonHandler);
        //    CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);
        //}
        public AddEditEmailNotificationRuleModel(string code)
        {
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            cmsWebServiceClient.GetPluginEmailRuleCompleted += (s1, e1) =>
            {
                cmsWebServiceClient.GetEmailTokensCompleted += (s2, e2) =>
                {
                    mEmailNotificationRule = e1.Result;
                    mEmailNotificationRuleTokens = e2.Result;
                    RaisePropertyChanged("Code");
                    RaisePropertyChanged("Name");
                    RaisePropertyChanged("SubjectText");
                    ValidateSubjectText(SubjectText);

                    RaisePropertyChanged("SendTo");
                    ValidateSentTo(SendTo);

                    RaisePropertyChanged("CcTo");
                    ValidateCC(CcTo);

                    RaisePropertyChanged("IsActive");
                    RaisePropertyChanged("AllowCCIgnore");
                    RaisePropertyChanged("Ordinal");
                    RaisePropertyChanged("SendTo");
                    RaisePropertyChanged("SendTo");
                    if (DataLoaded != null) DataLoaded();

                };
                    cmsWebServiceClient.GetEmailTokensAsync();
                };
            cmsWebServiceClient.GetPluginEmailRuleAsync(code);

            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanExecuteOkButtonHandler);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);
        }
Ejemplo n.º 2
0
        public IList<EmailNotificationRule> GetIssueEmailRules()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            foreach (IssueEmailNotificationCode token in Enum.GetValues(typeof(IssueEmailNotificationCode)))
            {
                dic.Add(token.ToString(), CommonUtils.SplitCamelCase(token.ToString()));
            }

            string[] pluginCodeArray = (from x in dic select x.Key).ToArray();
            IList<EmailNotificationRule> matchingDbRules;
            using (CmsEntities cee = new CmsEntities())
            {
                matchingDbRules = (from x in cee.EmailNotificationRules where pluginCodeArray.Contains(x.Code) select x).ToList();

                string[] matchingDbCodeArray = (from x in matchingDbRules select x.Code).ToArray();

                List<string> unmatchedDbRuleCodes = (from x in pluginCodeArray where !matchingDbCodeArray.Contains(x) select x).ToList();

                int maxOrdinal = matchingDbRules.Any() ? (from x in matchingDbRules select x.Ordinal).Max() : 0;

                foreach (string ruleCode in unmatchedDbRuleCodes)
                {
                    maxOrdinal++;
                    EmailNotificationRule rule = new EmailNotificationRule
                        {
                            Name = (from x in dic where x.Key == ruleCode select x.Value).FirstOrDefault(),
                            Code = ruleCode,
                            SubjectText = "not set",
                            SendTo = "not set",
                            CcTo = "not set",
                            Ordinal = maxOrdinal,
                            IsActive = true //default
                        };

                    matchingDbRules.Add(rule);
                }
            }

            return matchingDbRules;
        }
Ejemplo n.º 3
0
        private IEnumerable<string> BuildIssueEmailRecepientsList(IssueModifications issueModifications, EmailNotificationRule triggeredRule, string[] tokens)
        {
            IList<string> emailRecepientsList = new List<string>();
            Hashtable ht = new Hashtable();

            foreach (string splitToken in tokens)
            {
                foreach (IssueEmailRecipientTextTokens token in Enum.GetValues(typeof(IssueEmailRecipientTextTokens)))
                {
                    string clearedBrackets = splitToken.Replace("{", "");
                    clearedBrackets = clearedBrackets.Replace("}", "");

                    if (string.Compare(token.ToString(), clearedBrackets, true, CultureInfo.CurrentCulture) == 0)
                    {
                        if (token == IssueEmailRecipientTextTokens.AssignedTo)
                        {
                            //{AssignedTo}
                            if (EmailCommon.IsValidEmailaddress(issueModifications.Issue.CurrentlyAssignedToUser.EmailAddress))
                            {
                                emailRecepientsList.Add(issueModifications.Issue.CurrentlyAssignedToUser.EmailAddress);
                            }
                            else
                            {
                                log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                    token.ToString(), issueModifications.Issue.CurrentlyAssignedToUser.FirstLastName,
                                    issueModifications.Issue.CurrentlyAssignedToUser.EmailAddress);
                            }
                        }
                        else if (token == IssueEmailRecipientTextTokens.DistributionUsers
                                 && issueModifications.Issue.IssueDistributionUsers != null
                                 && issueModifications.Issue.IssueDistributionUsers.Any())
                        {
                            //{DistributionUsers}
                            using (CmsEntities cee = new CmsEntities())
                            {
                                foreach (IssueDistributionUser user in issueModifications.Issue.IssueDistributionUsers)
                                {
                                    User distUser = (from x in cee.Users where x.Id == user.UserId && x.ActiveUser select x).FirstOrDefault();
                                    if (distUser != null)
                                    {
                                        if (EmailCommon.IsValidEmailaddress(distUser.EmailAddress))
                                        {
                                            if (!ht.ContainsKey(distUser.Id))
                                            {
                                                if (triggeredRule.AllowCCIgnore)
                                                {
                                                    var preference = (from x in cee.UserPreferences where x.Id == distUser.UserPreferenceId select x).FirstOrDefault();
                                                    if (preference == null || !preference.IgnoreCCEmail)
                                                    {
                                                        ht.Add(distUser.Id, distUser.EmailAddress);
                                                        emailRecepientsList.Add(distUser.EmailAddress);
                                                    }
                                                }
                                                else
                                                {
                                                    ht.Add(distUser.Id, distUser.EmailAddress);
                                                    emailRecepientsList.Add(distUser.EmailAddress);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                                token.ToString(), distUser.FirstLastName,
                                                distUser.EmailAddress);
                                        }
                                    }
                                }
                            }
                        }
                        else if (token == IssueEmailRecipientTextTokens.InitiatedBy)
                        {
                            //{InitiatedBy}
                            using (CmsEntities cee = new CmsEntities())
                            {
                                User initiatedBy = (from x in cee.Users where x.Id == issueModifications.Issue.InitiatedById && x.ActiveUser select x).FirstOrDefault();
                                if (initiatedBy != null)
                                {
                                    if (EmailCommon.IsValidEmailaddress(initiatedBy.EmailAddress))
                                    {
                                        if (!ht.ContainsKey(initiatedBy.Id))
                                        {
                                            ht.Add(initiatedBy.Id, initiatedBy.EmailAddress);
                                            emailRecepientsList.Add(initiatedBy.EmailAddress);
                                        }
                                    }
                                    else
                                    {
                                        log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                            token.ToString(), initiatedBy.FirstLastName,
                                            initiatedBy.EmailAddress);
                                    }
                                }
                            }
                        }
                        else if (token == IssueEmailRecipientTextTokens.ModifiedBy)
                        {
                            //{ModifiedBy}
                            using (CmsEntities cee = new CmsEntities())
                            {
                                User modifiedBy = (from x in cee.Users where x.Id == issueModifications.Issue.ModifiedById && x.ActiveUser select x).FirstOrDefault();
                                if (modifiedBy != null)
                                {
                                    if (EmailCommon.IsValidEmailaddress(modifiedBy.EmailAddress))
                                    {
                                        if (!ht.ContainsKey(modifiedBy.Id))
                                        {
                                            ht.Add(modifiedBy.Id, modifiedBy.EmailAddress);
                                            emailRecepientsList.Add(modifiedBy.EmailAddress);
                                        }
                                    }
                                    else
                                    {
                                        log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                            token.ToString(), modifiedBy.FirstLastName,
                                            modifiedBy.EmailAddress);
                                    }
                                }
                            }
                        }
                        else if (token == IssueEmailRecipientTextTokens.PreviouslyAssignedTo)
                        {
                            //{PreviouslyAssignedTo}
                            using (CmsEntities cee = new CmsEntities())
                            {
                                User previouslyAssignedTo = (from x in cee.Users where x.Id == issueModifications.OriginalIssue.CurrentlyAssignedToId && x.ActiveUser select x).FirstOrDefault();
                                if (previouslyAssignedTo != null)
                                {
                                    if (EmailCommon.IsValidEmailaddress(previouslyAssignedTo.EmailAddress))
                                    {
                                        if (!ht.ContainsKey(previouslyAssignedTo.Id))
                                        {
                                            ht.Add(previouslyAssignedTo.Id, previouslyAssignedTo.EmailAddress);
                                            emailRecepientsList.Add(previouslyAssignedTo.EmailAddress);
                                        }
                                    }
                                    else
                                    {
                                        log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                            token.ToString(), previouslyAssignedTo.FirstLastName,
                                            previouslyAssignedTo.EmailAddress);
                                    }
                                }
                            }
                        }
                        else if (token == IssueEmailRecipientTextTokens.AddedDistributionUsers)
                        {
                            //Added Distribution User

                            var addedUsers = new List<User>();
                            using (CmsEntities cee = new CmsEntities())
                            {
                                foreach (var newDistUser in issueModifications.Issue.IssueDistributionUsers)
                                {
                                    var oldDistUser = (from x in issueModifications.OriginalIssue.IssueDistributionUsers
                                                       where x.IssueId == newDistUser.IssueId
                                                             && x.UserId == newDistUser.UserId
                                                       select x).FirstOrDefault();
                                    if (oldDistUser == null)
                                    {
                                        var user = (from x in cee.Users where x.Id == newDistUser.UserId && x.ActiveUser select x).FirstOrDefault();

                                        if (user != null)
                                        {
                                            //New User was added
                                            addedUsers.Add(user);
                                        }
                                    }
                                }

                                foreach (User user in addedUsers)
                                {
                                    if (EmailCommon.IsValidEmailaddress(user.EmailAddress))
                                    {
                                        if (!ht.ContainsKey(user.Id))
                                        {
                                            if (triggeredRule.AllowCCIgnore)
                                            {
                                                var preference = (from x in cee.UserPreferences where x.Id == user.UserPreferenceId select x).FirstOrDefault();
                                                if (preference == null || !preference.IgnoreCCEmail)
                                                {
                                                    ht.Add(user.Id, user.EmailAddress);
                                                    emailRecepientsList.Add(user.EmailAddress);
                                                }
                                            }
                                            else
                                            {
                                                ht.Add(user.Id, user.EmailAddress);
                                                emailRecepientsList.Add(user.EmailAddress);
                                            }
                                        }
                                        else
                                        {
                                            log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                                token.ToString(), user.FirstLastName,
                                                user.EmailAddress);
                                        }
                                    }
                                }
                            }
                        }
                        else if (token == IssueEmailRecipientTextTokens.RemovedDistributionUsers)
                        {
                            //Removed Distribution User
                            var removedUsers = new List<User>();
                            using (CmsEntities cee = new CmsEntities())
                            {
                                foreach (var oldDistUser in issueModifications.OriginalIssue.IssueDistributionUsers)
                                {
                                    var newDistUser = (from x in issueModifications.Issue.IssueDistributionUsers
                                                       where x.IssueId == oldDistUser.IssueId
                                                             && x.UserId == oldDistUser.UserId
                                                       select x).FirstOrDefault();
                                    if (newDistUser == null)
                                    {
                                        var user = (from x in cee.Users where x.Id == oldDistUser.UserId && x.ActiveUser select x).FirstOrDefault();

                                        if (user != null)
                                        {
                                            //User was removed
                                            removedUsers.Add(user);
                                        }
                                    }
                                }

                                foreach (User user in removedUsers)
                                {
                                    if (EmailCommon.IsValidEmailaddress(user.EmailAddress))
                                    {
                                        if (!ht.ContainsKey(user.Id))
                                        {
                                            if (triggeredRule.AllowCCIgnore)
                                            {
                                                var preference = (from x in cee.UserPreferences where x.Id == user.UserPreferenceId select x).FirstOrDefault();
                                                if (preference == null || !preference.IgnoreCCEmail)
                                                {
                                                    ht.Add(user.Id, user.EmailAddress);
                                                    emailRecepientsList.Add(user.EmailAddress);
                                                }
                                            }
                                            else
                                            {
                                                ht.Add(user.Id, user.EmailAddress);
                                                emailRecepientsList.Add(user.EmailAddress);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        log.Error("", "{0} user '{1}' does not have valid email address '{2}'",
                                            token.ToString(), user.FirstLastName,
                                            user.EmailAddress);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return emailRecepientsList;
        }
Ejemplo n.º 4
0
        public DbOperationResult<EmailNotificationRule> SaveEmailNotificationRule(EmailNotificationRule emailNotificationRule)
        {
            var result = new DbOperationResult<EmailNotificationRule>();

            try
            {
                using (var cee = new CmsEntities())
                {
                    EmailNotificationRule originalObject = (from x in cee.EmailNotificationRules where x.Id == emailNotificationRule.Id select x).FirstOrDefault();

                    if (originalObject == null)
                    {
                        originalObject = new EmailNotificationRule();
                        originalObject.Name = emailNotificationRule.Name;
                        originalObject.Code = emailNotificationRule.Code;
                        cee.EmailNotificationRules.Add(originalObject);
                    }

                    originalObject.SendTo = emailNotificationRule.SendTo;
                    originalObject.CcTo = emailNotificationRule.CcTo;
                    originalObject.SubjectText = emailNotificationRule.SubjectText;
                    originalObject.IsActive = emailNotificationRule.IsActive;
                    originalObject.AllowCCIgnore = emailNotificationRule.AllowCCIgnore;
                    originalObject.Ordinal = emailNotificationRule.Ordinal;

                    cee.SaveChanges();
                    result.EntityResult = originalObject;
                }
            }
            catch (Exception ex)
            {
                return BuildOperationalErrorResults<EmailNotificationRule>(ex);
            }

            return result;
        }