public bool Drop()
        {
            // fail if the user is not in the sysadmin role
            if (!this.DataContainer.Server.ConnectionContext.IsInFixedServerRole(FixedServerRoles.SysAdmin))
            {
                return(false);
            }

            string alertName = GetAlertName(this.DataContainer);

            if (this.DataContainer.Server.JobServer.Alerts.Contains(alertName))
            {
                this.DataContainer.Server.JobServer.Alerts.Refresh();
                if (this.DataContainer.Server.JobServer.Alerts.Contains(alertName))
                {
                    Alert alert = this.DataContainer.Server.JobServer.Alerts[alertName];
                    if (alert != null)
                    {
                        alert.DropIfExists();
                        if (this.jobData != null)
                        {
                            JobAlertData alertData = new JobAlertData(alert);
                            this.jobData.JobAlerts.DeleteAlert(alertData);
                        }
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
 public void AddAlert(JobAlertData alert)
 {
     if (alert == null)
     {
         throw new ArgumentNullException("step");
     }
     this.jobAlerts.Add(alert);
 }
Ejemplo n.º 3
0
 public void DeleteAlert(JobAlertData alert)
 {
     if (alert == null)
     {
         throw new ArgumentNullException("step");
     }
     if (this.jobAlerts.Contains(alert))
     {
         this.jobAlerts.Remove(alert);
         this.deletedJobAlerts.Add(alert);
     }
 }
        public bool CreateOrUpdate()
        {
            Alert  alert          = null;
            string alertName      = GetAlertName(this.DataContainer);
            bool   createNewAlert = true;

            try
            {
                // check if alert already exists
                if (this.DataContainer.Server.JobServer.Alerts.Contains(alertName))
                {
                    this.DataContainer.Server.JobServer.Alerts.Refresh();               // Try to recover
                    if (this.DataContainer.Server.JobServer.Alerts.Contains(alertName)) // If still no luck
                    {
                        // use the existing alert
                        alert          = this.DataContainer.Server.JobServer.Alerts[alertName];
                        createNewAlert = false;
                    }
                }

                // create a new alert
                if (createNewAlert)
                {
                    alert = new Alert(this.DataContainer.Server.JobServer, alertName);
                }

                // apply changes from input parameter to SMO alert object
                UpdateAlertProperties(alert);

                if (createNewAlert)
                {
                    alert.Create();
                    if (this.jobData != null)
                    {
                        JobAlertData alertData = new JobAlertData(alert);
                        this.jobData.JobAlerts.AddAlert(alertData);
                    }
                }
                else
                {
                    // don't bother trying to update the alert unless they are sysadmin.
                    if (!this.DataContainer.Server.ConnectionContext.IsInFixedServerRole(FixedServerRoles.SysAdmin))
                    {
                        return(false);
                    }
                    alert.Alter();

                    if (!string.IsNullOrWhiteSpace(this.alertInfo.Name) && !string.Equals(alert.Name, this.alertInfo.Name))
                    {
                        alert.Rename(this.alertInfo.Name);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                ApplicationException applicationException;
                if (createNewAlert)
                {
                    applicationException = new ApplicationException(SR.CannotCreateNewAlert, e);
                }
                else
                {
                    applicationException = new ApplicationException(SR.CannotAlterAlert, e);
                }
                throw applicationException;
            }
        }