Add initial schedule request handlers (#638)

This commit is contained in:
Karl Burtram
2018-06-18 10:18:23 -07:00
committed by GitHub
parent d2cc376b87
commit 29fb715ad5
10 changed files with 555 additions and 279 deletions

View File

@@ -4,27 +4,61 @@
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
using Microsoft.SqlTools.ServiceLayer.Management;
namespace Microsoft.SqlTools.ServiceLayer.Agent
{
/// <summary>
/// Summary description for JobSchedules.
/// Summary description for JobSchedulesActions.
/// </summary>
internal class JobSchedulesActions : ManagementActionBase
{
private bool sharedSchedulesSupported = false;
private JobData data;
private JobScheduleData scheduleData = null;
private AgentScheduleInfo scheduleInfo;
private ConfigAction configAction;
public JobSchedulesActions(CDataContainer dataContainer, JobData data)
public JobSchedulesActions(CDataContainer dataContainer, JobData data, AgentScheduleInfo scheduleInfo, ConfigAction configAction)
{
this.DataContainer = dataContainer;
this.data = data;
this.configAction = configAction;
this.scheduleInfo = scheduleInfo;
this.sharedSchedulesSupported = this.DataContainer.Server.Information.Version.Major >= 9;
if (configAction == ConfigAction.Create)
{
this.scheduleData = new JobScheduleData(this.data.Job);
this.scheduleData.SetJobSchedule(new JobSchedule());
}
else
{
// get the JobScheduleData from the urn
string urn = null;
STParameters parameters = new STParameters();
parameters.SetDocument(this.DataContainer.Document);
parameters.GetParam("urn", ref urn);
JobSchedule jobStep = this.data.Job.Parent.Parent.GetSmoObject(urn) as JobSchedule;
if (jobStep != null)
{
this.scheduleData = new JobScheduleData(jobStep);
}
if (configAction == ConfigAction.Update && this.scheduleData == null)
{
throw new Exception("Schedule urn parameter cannot be null");
}
}
// copy properties from AgentScheduelInfo
if (this.scheduleData != null)
{
this.scheduleData.Name = scheduleInfo.Name;
}
}
/// <summary>
@@ -38,263 +72,36 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
base.Dispose(disposing);
}
#region ui stuff
private void InitializeData()
{
if (this.data == null)
{
return;
}
// load the grid
//PopulateGrid(this.data.JobSchedules);
}
// private void PopulateGrid(JobSchedulesData schedules)
// {
// // add non-shared schedules
// for (int i = 0; i < schedules.Schedules.Count; i++)
// {
// JobScheduleData schedule = schedules.Schedules[i] as JobScheduleData;
// if (schedule != null)
// {
// // add rows to the grid
// GridCellCollection row = new GridCellCollection();
// GridCell cell;
// // ID
// cell = new GridCell(ConvertIdToDisplayName(schedule.ID));
// row.Add(cell);
// // Name
// cell = new GridCell(schedule.Name);
// row.Add(cell);
// // Enabled
// cell = new GridCell(schedule.Enabled ? JobSR.Yes : JobSR.No);
// row.Add(cell);
// // Description
// cell = new GridCell(schedule.Description);
// row.Add(cell);
// // Hyperlink 'Jobs in Schedule'
// if (this.sharedSchedulesSupported)
// {
// // don't add a hyperlink if the schedule has not yet been created
// cell = new GridCell(schedule.Created ? JobSR.ViewJobsInScheduleHyperlink : String.Empty);
// row.Add(cell);
// }
// this.scheduleList.AddRow(row);
// }
// }
// }
/// <summary>
/// Convert an id into a user friendly name. Converts new id to "new"
/// called by PreProcessExecution to enable derived classes to take over execution
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private string ConvertIdToDisplayName(int id)
/// <param name="runType"></param>
/// <param name="executionResult"></param>
/// <returns>
/// true if regular execution should take place, false if everything,
/// has been done by this function
/// </returns>
protected override bool DoPreProcessExecution(RunType runType, out ExecutionMode executionResult)
{
string rv;
// convert -1 into New
if (id < 0)
base.DoPreProcessExecution(runType, out executionResult);
if (this.scheduleData == null)
{
rv = "JobSR.New";
return false;
}
if (this.configAction == ConfigAction.Drop)
{
var jobSchedule = this.scheduleData.SourceSchedule;
if (jobSchedule != null)
{
jobSchedule.DropIfExists();
}
}
else
{
rv = Convert.ToString(id, System.Globalization.CultureInfo.CurrentCulture);
this.scheduleData.ApplyChanges();
}
return rv;
}
#endregion
#region ui event handlers
// private void addSchedule_Click(object sender, System.EventArgs e)
// {
// System.Diagnostics.Debug.Assert(this.DataContainer.Server.Information.Version.Major >= 9, "Shared Schedules supported only for Yukon - this button should be disabled if target server is Shiloh/Sphinx");
// StringBuilder excludedSchedules = this.BuildListOfScheduleId(this.data.JobSchedules.Schedules);
// StringBuilder removedSchedules = this.BuildListOfScheduleId(this.data.JobSchedules.RemovedSchedules);
// STParameters param = new STParameters();
// param.SetDocument(this.DataContainer.Document);
// param.SetParam("excludedschedules", excludedSchedules.ToString());
// if (this.data.Mode == JobData.DialogMode.Properties)
// {
// param.SetParam("removedschedules", removedSchedules.ToString());
// param.SetParam("joburn", this.data.Urn);
// }
// ManageSchedulesForm formPickUpSharedSchedule = new ManageSchedulesForm
// (
// this.DataContainer,
// ((this.data != null) && (this.data.Name != null)) ? this.data.Name : String.Empty,
// this.ServiceProvider
// );
// using (formPickUpSharedSchedule)
// {
// DialogResult dr = formPickUpSharedSchedule.ShowDialog();
// // cleanup the datacontainer
// param.SetParam("excludedschedules", String.Empty);
// if (this.data.Mode == JobData.DialogMode.Properties)
// {
// param.SetParam("removedschedules", String.Empty);
// param.SetParam("joburn", String.Empty);
// }
// if (dr == DialogResult.OK)
// {
// JobSchedule schedule = formPickUpSharedSchedule.SelectedSchedule;
// System.Diagnostics.Debug.Assert(schedule != null);
// System.Diagnostics.Debug.Assert(schedule.Name != null);
// bool scheduleAlreadyAdded = false;
// foreach (object o in this.data.JobSchedules.Schedules)
// {
// JobScheduleData jsd = o as JobScheduleData;
// System.Diagnostics.Debug.Assert(jsd != null, "non JobScheduleData found in this.data.Schedules");
// if ((jsd != null) && (jsd.ID == schedule.ID))
// {
// scheduleAlreadyAdded = true;
// break;
// }
// }
// if (scheduleAlreadyAdded == false)
// {
// JobScheduleData scheduleData = new JobScheduleData(schedule);
// this.data.JobSchedules.AddSchedule(scheduleData);
// this.scheduleList.DeleteAllRows();
// PopulateGrid(this.data.JobSchedules);
// UpdateControlStatus();
// }
// }
// }
// }
private void editSchedule_Click(object sender, System.EventArgs e)
{
// EditSelectedSchedule();
}
// private void deleteSchedule_Click(object sender, System.EventArgs e)
// {
// // check that a row is selected first
// STrace.Assert(this.scheduleList.SelectedCells.Count > 0, "there are no selected rows");
// if (this.scheduleList.SelectedCells.Count == 0)
// {
// return;
// }
// int row = (int)this.scheduleList.SelectedCells[0].Y;
// // check that this is a valid row
// STrace.Assert(row <= this.data.JobSchedules.Schedules.Count, "selected row does not exist in data structures");
// if (row > this.data.JobSchedules.Schedules.Count)
// {
// return;
// }
// JobScheduleData data = this.data.JobSchedules.Schedules[row] as JobScheduleData;
// if (data != null)
// {
// this.data.JobSchedules.DeleteSchedule(data); // if is non-shared it will be marked for deletion here
// this.scheduleList.DeleteAllRows();
// PopulateGrid(this.data.JobSchedules);
// }
// UpdateControlStatus();
// }
// private void EditSelectedSchedule()
// {
// // check that a row is selected first
// STrace.Assert(this.scheduleList.SelectedCells.Count > 0, "there are no selected rows");
// if (this.scheduleList.SelectedCells.Count == 0)
// {
// return;
// }
// int row = (int)this.scheduleList.SelectedCells[0].Y;
// // check that this is a valid row
// STrace.Assert(row <= this.data.JobSchedules.Schedules.Count, "selected row does not exist in data structures");
// if (row > this.data.JobSchedules.Schedules.Count)
// {
// return;
// }
// JobScheduleData data = this.data.JobSchedules.Schedules[row] as JobScheduleData;
// if (data != null)
// {
// try
// {
// using (ScheduleDialog jobSchedule = new ScheduleDialog(data,
// this.data.JobSchedules.Schedules,
// this.DataContainer,
// this.ServiceProvider))
// {
// jobSchedule.Text = JobSR.EditSchedule(data.Name);
// jobSchedule.SetSite(this.ServiceProvider);
// try
// {
// if (DialogResult.OK == jobSchedule.ShowDialog())
// {
// this.scheduleList.DeleteAllRows();
// PopulateGrid(this.data.JobSchedules);
// UpdateControlStatus();
// }
// }
// catch (ApplicationException error)
// {
// DisplayExceptionMessage(error);
// jobSchedule.DialogResult = DialogResult.None;
// }
// }
// }
// catch (ApplicationException error)
// {
// DisplayExceptionMessage(error);
// }
// }
// }
// }
#endregion
/// <summary>
/// enumerates schedules list and returns back as s string in format 1,2,3
/// </summary>
/// <param name="schedules"></param>
/// <returns></returns>
private StringBuilder BuildListOfScheduleId(List<JobScheduleData> schedules)
{
if (schedules == null)
{
throw new ArgumentNullException("schedules");
}
StringBuilder scheduleIdList = new StringBuilder();
foreach (JobScheduleData schedule in schedules)
{
if (schedule != null)
{
if (scheduleIdList.Length > 0)
{
scheduleIdList.AppendFormat(CultureInfo.InvariantCulture, ", {0}", schedule.ID);
}
else
{
scheduleIdList.Append(schedule.ID);
}
}
}
return scheduleIdList;
return false;
}
}
}

View File

@@ -305,10 +305,3 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
}
}
}

View File

@@ -801,6 +801,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.id = value;
}
}
public string Name
{
get
@@ -812,6 +813,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.currentName = value;
}
}
public bool Enabled
{
get
@@ -823,6 +825,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.enabled = value;
}
}
public bool AllowEnableDisable
{
get
@@ -830,6 +833,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return this.allowEnableDisable;
}
}
public string Description
{
get
@@ -837,6 +841,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return this.ToString();
}
}
public bool Created
{
get
@@ -848,6 +853,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.alreadyCreated = value;
}
}
public DateTime ActiveStartDate
{
get
@@ -859,6 +865,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.startDate = value;
}
}
public TimeSpan ActiveStartTime
{
get
@@ -870,6 +877,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.startTime = value;
}
}
public DateTime ActiveEndDate
{
get
@@ -883,6 +891,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
: value;
}
}
public TimeSpan ActiveEndTime
{
get
@@ -896,6 +905,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
: value;
}
}
public bool HasEndDate
{
get
@@ -903,6 +913,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return this.endDate < JobScheduleData.MaxAgentDateValue;
}
}
public bool HasEndTime
{
get
@@ -910,6 +921,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return this.endTime < JobScheduleData.MaxAgentTimeValue;
}
}
public FrequencyTypes FrequencyTypes
{
get
@@ -921,6 +933,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.frequencyType = value;
}
}
public int FrequencyInterval
{
get
@@ -932,6 +945,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.frequencyInterval = value;
}
}
public int FrequencyRecurranceFactor
{
get
@@ -943,6 +957,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.frequencyRecurranceFactor = value;
}
}
public FrequencyRelativeIntervals FrequencyRelativeIntervals
{
get
@@ -954,6 +969,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.frequencyRelativeInterval = value;
}
}
public int FrequencySubDayInterval
{
get
@@ -965,6 +981,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.frequencySubDayInterval = value;
}
}
public FrequencySubDayTypes FrequencySubDayTypes
{
get
@@ -976,6 +993,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
this.frequencySubDayTypes = value;
}
}
public JobSchedule SourceSchedule
{
get
@@ -983,6 +1001,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return this.source;
}
}
public bool IsReadOnly
{
get { return isReadOnly; }
@@ -1328,7 +1347,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
private void SetDefaults()
{
this.alreadyCreated = false;
currentName = originalName = String.Empty;
currentName = originalName = string.Empty;
this.enabled = true;
this.frequencyType = FrequencyTypes.Weekly; //SQL2K default value