public void ApplyChanges()

in src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/JobData.cs [1111:1294]


        public void ApplyChanges(bool creating)
        {
            Job job = null;
            bool scripting = this.context.Server.ConnectionContext.SqlExecutionModes == SqlExecutionModes.CaptureSql;
            bool targetServerSelected = false;

            this.mode = creating ? ActionMode.Create : ActionMode.Edit; 

            // Before any job posting if donem make sure that if this is an MSX job that the user has selected at
            // least one Target Server.
            if (!this.targetLocalServer)
            {
                for (int i = 0; i < this.AvailableTargetServers.Length; ++i)
                {
                    if (this.AvailableTargetServers[i].WillJobBeAppliedToTarget)
                    {
                        targetServerSelected = true;
                        break;
                    }
                }
                if (!targetServerSelected)
                {
                    // Not target servers selected.  Throw error.
                    throw new ApplicationException(SR.TargetServerNotSelected);
                }
            }

            if (creating)
            {
                job = new Job(this.context.Server.JobServer, this.Name, this.Category.SmoCategory.ID);
            }
            else
            {
                // just lookup the original object
                job = this.Job;
            }

            if (!this.IsReadOnly)
            {
                if (creating || job.OwnerLoginName != this.owner)
                {
                    job.OwnerLoginName = this.owner;
                }

                if (creating || (this.category != null
                                && this.category.SmoCategory.Name != job.Category))
                {
                    job.Category = this.category.SmoCategory.Name;
                }

                if (!string.IsNullOrWhiteSpace(this.description) && (creating || this.description != job.Description))
                {
                    job.Description = this.description;
                }

                SaveNotifications(job, creating);
            }

            if (this.AllowEnableDisable)
            {
                if (creating || this.enabled != job.IsEnabled)
                {
                    job.IsEnabled = this.enabled;
                }
            }

            // do the actual creation / alter
            if (creating)
            {
                ///Check to see if the job already exists
                JobExists(job.Name);
                job.Create();
                if (!scripting)
                {
                    this.urn = job.Urn;
                }
            }
            else
            {
                job.StartStepID = this.startStepID != -1 ? this.startStepID : job.StartStepID;
                job.Alter();
            }

            if (!this.IsReadOnly && !scripting)
            {
                if (this.targetLocalServer)
                {
                    if (!OriginallyTargetLocalServer || creating)
                    {
                        foreach (MsaJobTargetServer targetServer in this.AvailableTargetServers)
                        {
                            if (targetServer.IsJobAppliedToTarget)
                            {
                                job.RemoveFromTargetServer(targetServer.Name.ToUpperInvariant());
                                targetServer.IsJobAppliedToTarget = false;
                            }
                        }

                        OriginallyTargetLocalServer = true;
                        job.ApplyToTargetServer(this.context.Server.ConnectionContext.TrueName.ToUpperInvariant());
                    }
                }
                else if (this.IsMsx)
                {

                    if (!creating && OriginallyTargetLocalServer)
                    {
                        // Remove from target server only if actually does target the local server
                        string thisServerName = this.context.Server.ConnectionContext.TrueName.ToUpperInvariant();
                        DataTable targetServers = job.EnumTargetServers();
                        foreach (DataRow row in targetServers.Rows)
                        {
                            string targetServerName = row["ServerName"] as string;
                            if (String.Compare(targetServerName, thisServerName, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                job.RemoveFromTargetServer(thisServerName);
                                break;
                            }
                        }

                        OriginallyTargetLocalServer = false;
                    }

                    // add and remove target servers
                    for (int i = 0; i < this.AvailableTargetServers.Length; ++i)
                    {
                        if (this.AvailableTargetServers[i].WillJobBeAppliedToTarget && !this.AvailableTargetServers[i].IsJobAppliedToTarget)
                        {
                            job.ApplyToTargetServer(this.AvailableTargetServers[i].Name.ToUpperInvariant());
                            this.AvailableTargetServers[i].IsJobAppliedToTarget = true;
                        }
                        else if (!this.AvailableTargetServers[i].WillJobBeAppliedToTarget && this.AvailableTargetServers[i].IsJobAppliedToTarget)
                        {
                            job.RemoveFromTargetServer(this.AvailableTargetServers[i].Name);
                            this.AvailableTargetServers[i].IsJobAppliedToTarget = false;
                        }
                    }
                }

            }

            // Because some of the SMO methods above can update Job's CategoryID affecting the Urn
            this.urn = job.Urn;

            bool stepsChanged = false;
            // save steps,schedules etc
            if (this.jobSteps != null)
            {
                stepsChanged = this.jobSteps.ApplyChanges(job);
            }
            bool schedulesChanged = false;
            if (this.jobSchedules != null)
            {
                schedulesChanged = this.jobSchedules.ApplyChanges(job);
            }

            if ((stepsChanged || schedulesChanged) && !this.TargetLocalServer && !creating)
            {
                // TODO: this seems wrong. Why do it here and not in SMO?
                this.context.Server.ConnectionContext.ExecuteNonQuery(
                    string.Format(CultureInfo.InvariantCulture, "EXECUTE msdb.dbo.sp_post_msx_operation  N'INSERT', N'JOB', @job_id = '{0}'"
                    , job.JobID));
            }

            // Do not attempt to save the job alert if we are in scripting mode, since the job id does not
            // yet exists.
            if (jobAlerts != null && !scripting)
            {
                this.jobAlerts.ApplyChanges(job);
            }

            // check the name if we are not creating
            if (!creating && this.Name != job.Name )
            {
                // new name = rename
                job.Rename(this.Name);

                // get the new urn if we aren't scripting
                if (!scripting)
                {
                    this.urn = job.Urn;
                }
            }
        }