/
SchedulerJobViewerForm.vb
243 lines (218 loc) · 11.9 KB
/
SchedulerJobViewerForm.vb
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
241
242
243
Imports DevExpress.ReportServer.ServiceModel.ConnectionProviders
Imports DevExpress.ReportServer.ServiceModel.DataContracts
Imports DevExpress.XtraScheduler
Imports System
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace ScheduledTasksAPIClientDemo
Partial Public Class SchedulerJobViewerForm
Inherits Form
#Region "inner classes"
Private Class Info
Public Property Name() As String
Public Property Value() As Object
End Class
Private Class Parameter
Public Property Name() As String
Public Property Source() As SchedulerParametersSource
Public Property Value() As Object
End Class
#End Region
Private ReadOnly serverConnection As ConnectionProvider
Private ReadOnly scheduledJobId As Integer
Public Sub New(ByVal scheduledJobId As Integer, ByVal serverConnection As ConnectionProvider)
InitializeComponent()
Me.serverConnection = serverConnection
Me.scheduledJobId = scheduledJobId
End Sub
' 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 Sub SchedulerJobViewerForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
serverConnection.DoWithScheduledJobAsync(Function(x) x.GetScheduledJobAsync(scheduledJobId, Nothing)).ContinueWith(Sub(taskFunc)
If taskFunc.IsFaulted Then
MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
FillScheduledJob(taskFunc.Result)
End If
End Sub, TaskScheduler.FromCurrentSynchronizationContext())
End Sub
Private Sub FillScheduledJob(ByVal scheduledJob As ScheduledJobDto)
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)
End Sub
#Region "Appointment"
Private Sub FillRecurrencyInfo(ByVal scheduledJob As ScheduledJobDto)
recurrencyInfo.Text = String.Empty
Dim appointment = CreateAppointment(scheduledJob)
If appointment Is Nothing Then
Return
End If
Dim culture = Thread.CurrentThread.CurrentUICulture
Dim infos = New List(Of Info)()
infos.Add(New Info With { _
.Name = "Description", _
.Value = RecurrenceInfo.GetDescription(appointment, culture.DateTimeFormat.FirstDayOfWeek) _
})
infos.Add(New Info With { _
.Name = "Next start", _
.Value = GetNextDateDisplayText(appointment) _
})
recurrencyInfoGrid.DataSource = infos
recurrencyInfoView.BestFitColumns()
End Sub
Private Function CreateAppointment(ByVal scheduledJob As ScheduledJobDto) As Appointment
If String.IsNullOrEmpty(scheduledJob.SerializedRecurrenceInfo) Then
Return Nothing
End If
Dim 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
End Function
Private Function GetNextDateDisplayText(ByVal appointment As Appointment) As String
Const ScheduledJobExpired As String = "Never (expired)"
Const ScheduledJobExpiredFormat As String = "Never (expired on {0:d})"
Dim calculator = OccurrenceCalculator.CreateInstance(appointment.RecurrenceInfo)
Dim nextDate = calculator.FindNextOccurrenceTimeAfter(Date.Now, appointment)
If nextDate <> DateTime.MaxValue Then
Return nextDate.ToString("g")
Else
Dim lastDate = GetLastDate(appointment, calculator)
Return If(lastDate IsNot Nothing, String.Format(ScheduledJobExpiredFormat, lastDate), ScheduledJobExpired)
End If
End Function
Private Function GetLastDate(ByVal appointment As Appointment, ByVal calculator As OccurrenceCalculator) As Date?
Dim index = calculator.CalcLastOccurrenceIndex(appointment)
Return If(index >= 0, CType(appointment.GetOccurrence(index).Start, Date?), Nothing)
End Function
#End Region
#Region "Parameters Binding"
Private Sub FillParametersBinding(ByVal scheduledJob As ScheduledJobDto)
Dim infos = New List(Of Info)()
If scheduledJob.SchedulerParameters.Binding IsNot Nothing Then
infos.Add(New Info With { _
.Name = "Data Model (id)", _
.Value = scheduledJob.SchedulerParameters.Binding.DataModelId _
})
infos.Add(New Info With { _
.Name = "Data Member", _
.Value = scheduledJob.SchedulerParameters.Binding.DataMember _
})
infos.Add(New Info With { _
.Name = "Email Field", _
.Value = scheduledJob.SchedulerParameters.Binding.EmailField _
})
infos.Add(New Info With { _
.Name = "Recipient Name Field", _
.Value = scheduledJob.SchedulerParameters.Binding.DisplayNameField _
})
parametersBindingGrid.DataSource = infos
parametersBindingView.BestFitColumns()
End If
End Sub
#End Region
#Region "Report Parameters"
Private Sub FillParameters(ByVal scheduledJob As ScheduledJobDto)
Dim parameters = New List(Of Parameter)()
For Each item In scheduledJob.SchedulerParameters.Parameters
parameters.Add(New Parameter() With { _
.Name = item.Key, _
.Source = item.Value.Source, _
.Value = item.Value.Value _
})
Next item
reportParametersGrid.DataSource = parameters
reportParametersView.BestFitColumns()
End Sub
#End Region
#Region "External Subscribers"
Private Sub FillExternalSubscribers(ByVal scheduledJob As ScheduledJobDto)
externalSubscribers.Text = scheduledJob.ExternalSubscribers
End Sub
#End Region
#Region "Export to Shared Folder"
Private Sub FillExportToShared(ByVal scheduledJob As ScheduledJobDto)
exportToSharedFolder.Text = scheduledJob.ExportToSharedFolder
End Sub
#End Region
#Region "Delete Task"
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
serverConnection.DoWithScheduledJobAsync(Function(x) x.DeleteScheduledJobAsync(scheduledJobId, Nothing)).ContinueWith(Function(taskFunc) MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error), TaskContinuationOptions.OnlyOnFaulted)
End Sub
#End Region
#Region "Update Task"
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
serverConnection.DoWithScheduledJobAsync(Function(x) x.GetScheduledJobAsync(scheduledJobId, Nothing)).ContinueWith(Function(taskFunc)
Dim scheduledJob = taskFunc.Result
scheduledJob.Name = scheduledJobName.Text
scheduledJob.Enabled = scheduledJobEnabled.Checked
scheduledJob.StartDate = scheduledJobStartDate.DateTime
scheduledJob.ExternalSubscribers = externalSubscribers.Text
scheduledJob.ExportToSharedFolder = exportToSharedFolder.Text
Dim selectedReportId As Integer = Nothing
If Integer.TryParse(reportId.Text, selectedReportId) Then
scheduledJob.ReportId = selectedReportId
Else
scheduledJob.ReportId = Nothing
End If
Return serverConnection.DoWithScheduledJobAsync(Function(x) x.UpdateScheduledJobAsync(scheduledJob, Nothing))
End Function).Unwrap().ContinueWith(Function(taskFunc) MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error), New CancellationToken(), TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext())
End Sub
#End Region
#Region "Create Task"
Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreate.Click
Dim scheduledJob = New ScheduledJobDto()
scheduledJob.TaskMode = ScheduledTaskMode.BillingStatement
scheduledJob.Name = scheduledJobName.Text
scheduledJob.Enabled = scheduledJobEnabled.Checked
scheduledJob.StartDate = scheduledJobStartDate.DateTime.ToUniversalTime()
Using 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()
End Using
scheduledJob.SchedulerParameters = New SchedulerParameters() With { _
.Binding = New ParametersBinding() With { _
.DataModelId = 1, _
.DataMember = "vwEmployees", _
.EmailField = "Email", _
.DisplayNameField = "DisplayName" _
} _
}
scheduledJob.InternalSubscribers = Nothing
scheduledJob.ExternalSubscribers = externalSubscribers.Text
scheduledJob.ExportToSharedFolder = exportToSharedFolder.Text
Dim selectedReportId As Integer = Nothing
If Integer.TryParse(reportId.Text, selectedReportId) Then
scheduledJob.ReportId = selectedReportId
Else
scheduledJob.ReportId = Nothing
End If
serverConnection.DoWithScheduledJobAsync(Function(x) x.CreateScheduledJobAsync(scheduledJob, Nothing)).ContinueWith(Sub(taskFunc)
If taskFunc.IsFaulted Then
MessageBox.Show(taskFunc.Exception.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
id.Text = taskFunc.Result.ToString()
End If
End Sub, TaskScheduler.FromCurrentSynchronizationContext())
End Sub
#End Region
Private Sub btnExecute_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExecute.Click
serverConnection.DoWithScheduledJobAsync(Function(x) x.ExecuteJobAsync(scheduledJobId, Nothing, Nothing))
End Sub
End Class
End Namespace