public void ApplyChanges()

in src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs [558:715]


        public void ApplyChanges(AgentOperatorInfo operatorInfo)
        {
            // do nothing if we are read only
            if (this.readOnly)
            {
                return;
            }

            JobServer jobServer = GetJobServer();

            // get the operator. This will create a new one if it does not already exist
            Microsoft.SqlServer.Management.Smo.Agent.Operator currentOperator = GetCurrentOperator();

            // general tab
            currentOperator.Enabled = operatorInfo.Enabled;

            if (!string.IsNullOrWhiteSpace(operatorInfo.EmailAddress))
            {
                currentOperator.EmailAddress = operatorInfo.EmailAddress;
            }

            if (!string.IsNullOrWhiteSpace(operatorInfo.PagerAddress))
            {
                currentOperator.PagerAddress = operatorInfo.PagerAddress;
            }
    
            currentOperator.PagerDays = this.pagerDays;

            if ((operatorInfo.PagerDays & Contracts.WeekDays.WeekDays) > 0)
            {
                TimeSpan weekdayPagerStartTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.WeekdayPagerStartTime))
                {
                    weekdayPagerStartTime = TimeSpan.Parse(operatorInfo.WeekdayPagerStartTime);
                }

                TimeSpan weekdayPagerEndTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.WeekdayPagerEndTime))
                {
                    weekdayPagerEndTime = TimeSpan.Parse(operatorInfo.WeekdayPagerEndTime);
                }

                currentOperator.WeekdayPagerStartTime = weekdayPagerStartTime;
                currentOperator.WeekdayPagerEndTime = weekdayPagerEndTime;
            }

            if ((operatorInfo.PagerDays & Contracts.WeekDays.Saturday) > 0)
            {
                TimeSpan saturdayPagerStartTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SaturdayPagerStartTime))
                {
                    saturdayPagerStartTime = TimeSpan.Parse(operatorInfo.SaturdayPagerStartTime);
                }

                TimeSpan saturdayPagerEndTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SaturdayPagerEndTime))
                {
                    saturdayPagerEndTime = TimeSpan.Parse(operatorInfo.SaturdayPagerEndTime);
                }

                currentOperator.SaturdayPagerStartTime = saturdayPagerStartTime;
                currentOperator.SaturdayPagerEndTime = saturdayPagerEndTime;
            }

            if ((operatorInfo.PagerDays & Contracts.WeekDays.Sunday) > 0)
            {
                TimeSpan sundayPagerStartTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SundayPagerStartTime))
                {
                    sundayPagerStartTime = TimeSpan.Parse(operatorInfo.SundayPagerStartTime);
                }

                TimeSpan sundayPagerEndTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SundayPagerEndTime))
                {
                    sundayPagerEndTime = TimeSpan.Parse(operatorInfo.SundayPagerEndTime);
                }

                currentOperator.SundayPagerStartTime = sundayPagerStartTime;
                currentOperator.SundayPagerEndTime = sundayPagerEndTime;
            }

            if (this.createMode)
            {
                // create the object
                currentOperator.Create();
                this.originalOperatorName = this.name;
            }
            else
            {
                // alter the object
                currentOperator.Alter();
            }

            // only set this up if the notifications has been set
            if (this.alertNotifications != null)
            {
                SqlServer.Management.Smo.Agent.NotifyMethods notifyMethods;
                for (int i = 0; i < alertNotifications.Count; ++i)
                {
                    notifyMethods = 0;

                    if (alertNotifications[i].NotifyEmail)
                    {
                        notifyMethods |= SqlServer.Management.Smo.Agent.NotifyMethods.NotifyEmail;
                    }
                    if (alertNotifications[i].NotifyPager)
                    {
                        notifyMethods |= SqlServer.Management.Smo.Agent.NotifyMethods.Pager;
                    }
                    
                    bool alertAlreadyNotifiesOperator = false;

                    // if we're not creating see if the current alert already notifies this operator
                    if (!createMode)
                    {
                        DataTable notifications = alertNotifications[i].Alert.EnumNotifications(this.originalOperatorName);
                        if (notifications.Rows.Count > 0)
                        {
                            alertAlreadyNotifiesOperator = true;
                        }
                    }

                    // either update or clear existing notifications
                    if (alertAlreadyNotifiesOperator)
                    {
                        if(notifyMethods != SqlServer.Management.Smo.Agent.NotifyMethods.None)
                        {
                            alertNotifications[i].Alert.UpdateNotification(this.originalOperatorName, notifyMethods);
                        }
                        else
                        {
                            alertNotifications[i].Alert.RemoveNotification(this.originalOperatorName);
                        }
                    }
                    else if(notifyMethods != SqlServer.Management.Smo.Agent.NotifyMethods.None)
                    {
                        // add a new notification
                        alertNotifications[i].Alert.AddNotification(this.originalOperatorName, notifyMethods);
                    }
                }
            }

            // see if we need to rename. This has to be done last otherwise any scripts generated will be incorrect.
            if (!this.createMode && currentOperator.Name != this.originalOperatorName)
            {
                currentOperator.Rename(this.name);
                if(this.dataContainer.Server.ConnectionContext.SqlExecutionModes != SqlExecutionModes.CaptureSql)
                {
                    this.originalOperatorName = this.name;
                }
            }
            // update state if we aren't scripting
            if (this.createMode && this.dataContainer.Server.ConnectionContext.SqlExecutionModes != SqlExecutionModes.CaptureSql)
            {
                this.createMode = false;
            }
        }