-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSchedulerJobViewerForm.cs
240 lines (216 loc) · 11.1 KB
/
SchedulerJobViewerForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using DevExpress.ReportServer.ServiceModel.ConnectionProviders;
using DevExpress.ReportServer.ServiceModel.DataContracts;
using DevExpress.XtraScheduler;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScheduledTasksAPIClientDemo {
public partial class SchedulerJobViewerForm : Form {
#region inner classes
class Info {
public string Name { get; set; }
public object Value { get; set; }
}
class Parameter {
public string Name { get; set; }
public SchedulerParametersSource Source { get; set; }
public object Value { get; set; }
}
#endregion
readonly ConnectionProvider serverConnection;
readonly int scheduledJobId;
public SchedulerJobViewerForm(int scheduledJobId, ConnectionProvider serverConnection) {
InitializeComponent();
this.serverConnection = serverConnection;
this.scheduledJobId = scheduledJobId;
}
// The following code obtains information about a specific scheduled job from the server
// and enables you to remotely manage scheduled jobs on the client.
// Please note that managing jobs requires that appropriate access permissions are attributed
// to the user account under which this application is connected to the Server.
private void SchedulerJobViewerForm_Load(object sender, EventArgs e) {
serverConnection.DoWithScheduledJobAsync(x => x.GetScheduledJobAsync(scheduledJobId, null))
.ContinueWith(taskFunc => {
if (taskFunc.IsFaulted) {
MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //
}
else {
FillScheduledJob(taskFunc.Result);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
void FillScheduledJob(ScheduledJobDto scheduledJob) {
id.Text = string.Format("{0}", scheduledJob.Id);
scheduledJobName.Text = scheduledJob.Name;
scheduledJobEnabled.Checked = scheduledJob.Enabled;
scheduledJobStartDate.DateTime = scheduledJob.StartDate.ToLocalTime();
reportId.Text = string.Format("{0}", scheduledJob.ReportId);
FillRecurrencyInfo(scheduledJob);
FillParametersBinding(scheduledJob);
FillParameters(scheduledJob);
FillExternalSubscribers(scheduledJob);
FillExportToShared(scheduledJob);
}
#region Appointment
void FillRecurrencyInfo(ScheduledJobDto scheduledJob) {
recurrencyInfo.Text = string.Empty;
var appointment = CreateAppointment(scheduledJob);
if (appointment == null) {
return;
}
var culture = Thread.CurrentThread.CurrentUICulture;
var infos = new List<Info>();
infos.Add(new Info { Name = "Description", Value = RecurrenceInfo.GetDescription(appointment, culture.DateTimeFormat.FirstDayOfWeek) });
infos.Add(new Info { Name = "Next start", Value = GetNextDateDisplayText(appointment) });
recurrencyInfoGrid.DataSource = infos;
recurrencyInfoView.BestFitColumns();
}
Appointment CreateAppointment(ScheduledJobDto scheduledJob) {
if (string.IsNullOrEmpty(scheduledJob.SerializedRecurrenceInfo)) {
return null;
}
var appointment = DevExpress.XtraScheduler.Compatibility.StaticAppointmentFactory.CreateAppointment(AppointmentType.Pattern);
appointment.Start = scheduledJob.StartDate.ToLocalTime();
appointment.RecurrenceInfo.FromXml(scheduledJob.SerializedRecurrenceInfo);
appointment.RecurrenceInfo.Start = appointment.Start;
return appointment;
}
string GetNextDateDisplayText(Appointment appointment) {
const string ScheduledJobExpired = "Never (expired)";
const string ScheduledJobExpiredFormat = "Never (expired on {0:d})";
var calculator = OccurrenceCalculator.CreateInstance(appointment.RecurrenceInfo);
var nextDate = calculator.FindNextOccurrenceTimeAfter(DateTime.Now, appointment);
if (nextDate != DateTime.MaxValue) {
return nextDate.ToString("g");
}
else {
var lastDate = GetLastDate(appointment, calculator);
return lastDate != null
? string.Format(ScheduledJobExpiredFormat, lastDate)
: ScheduledJobExpired;
}
}
DateTime? GetLastDate(Appointment appointment, OccurrenceCalculator calculator) {
var index = calculator.CalcLastOccurrenceIndex(appointment);
return index >= 0
? (DateTime?)appointment.GetOccurrence(index).Start
: null;
}
#endregion
#region Parameters Binding
void FillParametersBinding(ScheduledJobDto scheduledJob) {
var infos = new List<Info>();
if (scheduledJob.SchedulerParameters.Binding != null) {
infos.Add(new Info { Name = "Data Model (id)", Value = scheduledJob.SchedulerParameters.Binding.DataModelId });
infos.Add(new Info { Name = "Data Member", Value = scheduledJob.SchedulerParameters.Binding.DataMember });
infos.Add(new Info { Name = "Email Field", Value = scheduledJob.SchedulerParameters.Binding.EmailField });
infos.Add(new Info { Name = "Recipient Name Field", Value = scheduledJob.SchedulerParameters.Binding.DisplayNameField });
parametersBindingGrid.DataSource = infos;
parametersBindingView.BestFitColumns();
}
}
#endregion
#region Report Parameters
void FillParameters(ScheduledJobDto scheduledJob) {
var parameters = new List<Parameter>();
foreach (var item in scheduledJob.SchedulerParameters.Parameters) {
parameters.Add(new Parameter() { Name = item.Key, Source = item.Value.Source, Value = item.Value.Value });
}
reportParametersGrid.DataSource = parameters;
reportParametersView.BestFitColumns();
}
#endregion
#region External Subscribers
void FillExternalSubscribers(ScheduledJobDto scheduledJob) {
externalSubscribers.Text = scheduledJob.ExternalSubscribers;
}
#endregion
#region Export to Shared Folder
void FillExportToShared(ScheduledJobDto scheduledJob) {
exportToSharedFolder.Text = scheduledJob.ExportToSharedFolder;
}
#endregion
#region Delete Task
private void btnDelete_Click(object sender, EventArgs e) {
serverConnection
.DoWithScheduledJobAsync(x => x.DeleteScheduledJobAsync(scheduledJobId, null))
.ContinueWith(
taskFunc => MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error),
TaskContinuationOptions.OnlyOnFaulted);
}
#endregion
#region Update Task
private void btnUpdate_Click(object sender, EventArgs e) {
serverConnection
.DoWithScheduledJobAsync(x => x.GetScheduledJobAsync(scheduledJobId, null))
.ContinueWith(taskFunc => {
var scheduledJob = taskFunc.Result;
scheduledJob.Name = scheduledJobName.Text;
scheduledJob.Enabled = scheduledJobEnabled.Checked;
scheduledJob.StartDate = scheduledJobStartDate.DateTime;
scheduledJob.ExternalSubscribers = externalSubscribers.Text;
scheduledJob.ExportToSharedFolder = exportToSharedFolder.Text;
int selectedReportId;
if (int.TryParse(reportId.Text, out selectedReportId)) {
scheduledJob.ReportId = selectedReportId;
}
else {
scheduledJob.ReportId = null;
}
return serverConnection.DoWithScheduledJobAsync(x => x.UpdateScheduledJobAsync(scheduledJob, null));
})
.Unwrap()
.ContinueWith(taskFunc => MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
, new CancellationToken(), TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
}
#endregion
#region Create Task
private void btnCreate_Click(object sender, EventArgs e) {
var scheduledJob = new ScheduledJobDto();
scheduledJob.TaskMode = ScheduledTaskMode.BillingStatement;
scheduledJob.Name = scheduledJobName.Text;
scheduledJob.Enabled = scheduledJobEnabled.Checked;
scheduledJob.StartDate = scheduledJobStartDate.DateTime.ToUniversalTime();
using (var apt = DevExpress.XtraScheduler.Compatibility.StaticAppointmentFactory.CreateAppointment(AppointmentType.Pattern)) {
apt.RecurrenceInfo.Type = RecurrenceType.Daily;
apt.RecurrenceInfo.Start = scheduledJob.StartDate;
apt.RecurrenceInfo.WeekDays = WeekDays.WorkDays;
scheduledJob.SerializedRecurrenceInfo = apt.RecurrenceInfo.ToXml();
}
scheduledJob.SchedulerParameters = new SchedulerParameters() {
Binding = new ParametersBinding() {
DataModelId = 1,
DataMember = "vwEmployees",
EmailField = "Email",
DisplayNameField = "DisplayName"
}
};
scheduledJob.InternalSubscribers = null;
scheduledJob.ExternalSubscribers = externalSubscribers.Text;
scheduledJob.ExportToSharedFolder = exportToSharedFolder.Text;
int selectedReportId;
if (int.TryParse(reportId.Text, out selectedReportId)) {
scheduledJob.ReportId = selectedReportId;
}
else {
scheduledJob.ReportId = null;
}
serverConnection
.DoWithScheduledJobAsync(x => x.CreateScheduledJobAsync(scheduledJob, null))
.ContinueWith(taskFunc => {
if (taskFunc.IsFaulted) {
MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
id.Text = taskFunc.Result.ToString();
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
#endregion
private void btnExecute_Click(object sender, EventArgs e) {
serverConnection.DoWithScheduledJobAsync(x => x.ExecuteJobAsync(scheduledJobId, null, null));
}
}
}