-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainControl.cs
384 lines (321 loc) · 14.8 KB
/
MainControl.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using FlowActivationOrder.Models;
using McTools.Xrm.Connection;
using Microsoft.Msagl.GraphmapsWithMesh;
using Microsoft.Msagl.Drawing;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
using QuikGraph;
using QuikGraph.Algorithms.TopologicalSort;
using QuikGraph.MSAGL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Xml.Linq;
using XrmToolBox.Extensibility;
using LocalModels = FlowActivationOrder.Models;
namespace FlowActivationOrder
{
public partial class MainControl : PluginControlBase
{
private Settings mySettings;
public MainControl()
{
InitializeComponent();
}
private void MyPluginControl_Load(object sender, EventArgs e)
{
//ShowInfoNotification("This is a notification that can lead to XrmToolBox repository", new Uri("https://github.com/MscrmTools/XrmToolBox"));
// Loads or creates the settings for the plugin
if (!SettingsManager.Instance.TryLoad(GetType(), out mySettings))
{
mySettings = new Settings();
LogWarning("Settings not found => a new settings file has been created!");
}
else
{
LogInfo("Settings found and loaded");
}
}
private void tsbClose_Click(object sender, EventArgs e)
{
CloseTool();
}
/// <summary>
/// This event occurs when the plugin is closed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyPluginControl_OnCloseTool(object sender, EventArgs e)
{
// Before leaving, save the settings
SettingsManager.Instance.Save(GetType(), mySettings);
}
/// <summary>
/// This event occurs when the connection has been updated in XrmToolBox
/// </summary>
public override void UpdateConnection(IOrganizationService newService, ConnectionDetail detail, string actionName, object parameter)
{
base.UpdateConnection(newService, detail, actionName, parameter);
if (mySettings != null && detail != null)
{
mySettings.LastUsedOrganizationWebappUrl = detail.WebApplicationUrl;
LogInfo("Connection has changed to: {0}", detail.WebApplicationUrl);
}
ExecuteMethod(LoadSolutions);
}
private void solutionsToolStripDropDownButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
var solution = e.ClickedItem.Tag as LocalModels.Solution;
LoadFlows(solution);
}
private void LoadFlows(LocalModels.Solution solution)
{
WorkAsync(
new WorkAsyncInfo
{
Message = "Retrieiving the Flows",
Work = (w, e) =>
{
List<LocalModels.FlowDefinition> flowList = new List<LocalModels.FlowDefinition>();
var qe = new QueryExpression("workflow");
qe.ColumnSet.AddColumns("ismanaged", "clientdata", "description", "name", "createdon", "modifiedon", "modifiedby", "createdby", "workflowidunique");
qe.Criteria.AddCondition("category", ConditionOperator.Equal, 5);
qe.AddOrder("name", OrderType.Ascending);
if (solution != null)
{
var solComp = qe.AddLink("solutioncomponent", "workflowid", "objectid", JoinOperator.Inner);
solComp.EntityAlias = "solComp";
solComp.LinkCriteria.AddCondition("solutionid", ConditionOperator.Equal, solution.Id);
}
var flowRecords = Service.RetrieveMultiple(qe);
foreach (var flowRecord in flowRecords.Entities)
{
var flowDef = new LocalModels.FlowDefinition
{
Id = flowRecord["workflowid"].ToString(),
Name = flowRecord["name"].ToString(),
ClientData = flowRecord["clientdata"].ToString(),
Description = !flowRecord.Attributes.Contains("description") ? string.Empty : flowRecord["description"].ToString(),
Solution = true,
Managed = (bool)flowRecord["ismanaged"],
UniqueId = flowRecord["workflowidunique"].ToString()
};
flowList.Add(flowDef);
}
e.Result = flowList;
},
ProgressChanged = e => { },
PostWorkCallBack = e =>
{
var returnFlows = e.Result as List<LocalModels.FlowDefinition>;
flowsDataGridView.AutoGenerateColumns = true;
flowsDataGridView.DataSource = returnFlows;
},
}
);
}
private void LoadSolutions()
{
solutionsToolStripDropDownButton.DropDown.Items.Clear();
WorkAsync(
new WorkAsyncInfo
{
Message = "Retrieiving the Solutions",
Work = (w, e) =>
{
List<LocalModels.Solution> solList = new List<LocalModels.Solution>();
QueryExpression solQry = new QueryExpression("solution");
solQry.Distinct = true;
solQry.ColumnSet = new ColumnSet("solutionid", "uniquename", "friendlyname", "version", "publisherid");
solQry.AddOrder("friendlyname", OrderType.Ascending);
solQry.Criteria = new FilterExpression();
solQry.Criteria.AddCondition(new ConditionExpression("isvisible", ConditionOperator.Equal, true));
solQry.Criteria.AddCondition(new ConditionExpression("uniquename", ConditionOperator.NotEqual, "Default"));
solQry.Criteria.AddCondition(new ConditionExpression("ismanaged", ConditionOperator.Equal, false));
var solutionEntities = Service.RetrieveMultiple(solQry);
foreach (var solution in solutionEntities.Entities)
{
solList.Add(new LocalModels.Solution
{
Id = solution["solutionid"].ToString(),
UniqueName = solution["uniquename"].ToString(),
FriendlyName = solution["friendlyname"].ToString(),
VersionNumber = solution["version"].ToString(),
PublisherId = ((EntityReference)solution["publisherid"]).Id.ToString(),
PublisherIdName = ((EntityReference)solution["publisherid"]).Name
});
}
e.Result = solList;
},
ProgressChanged = e => { },
PostWorkCallBack = e =>
{
if (e.Error != null)
{
LogError("An error happend while loading the solutions.", e.Error);
MessageBox.Show(e.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
var solList = e.Result as List<LocalModels.Solution>;
foreach (var solution in solList)
{
if (solution.UniqueName == "Active" || solution.UniqueName == "Default" || solution.UniqueName == "Basic")
continue;
ToolStripDropDownButton lstItem = new ToolStripDropDownButton();
lstItem.Text = solution.FriendlyName;
lstItem.Tag = solution;
solutionsToolStripDropDownButton.DropDown.Items.Add(lstItem);
}
},
}
);
}
private void generateToolStripButton_Click(object sender, EventArgs e)
{
var dependencyGraph = new BidirectionalGraph<FlowNode, Edge<FlowNode>>(true);
var flowNodeDictionary = new Dictionary<string, FlowNode>();
// add vertices
foreach (DataGridViewRow row in flowsDataGridView.SelectedRows)
{
var flow = new FlowNode(row.DataBoundItem as LocalModels.FlowDefinition);
dependencyGraph.AddVertex(flow);
flowNodeDictionary.Add(flow.Flow.Id, flow);
}
// add edges
foreach (var flow in dependencyGraph.Vertices)
{
var workflow = LocalModels.WorkflowObject.FromJson(flow.Flow.ClientData);
var childWorkflowReferenceNames = GetActions(workflow, LocalModels.ActionType.Workflow)
.Where(action => action.Inputs.HasValue && action.Inputs.Value.Object != null)
.Select(action => action.Inputs.Value.Object.Host.WorkflowReferenceName)
.Distinct();
foreach (var childWorkflowReference in childWorkflowReferenceNames)
{
if(flowNodeDictionary.TryGetValue(childWorkflowReference, out FlowNode childFlow))
{
dependencyGraph.AddEdge(new Edge<FlowNode>(childFlow, flow));
}
}
}
var sorter = new TopologicalSortAlgorithm<FlowNode, Edge<FlowNode>>(dependencyGraph);
sorter.Compute();
for (var index = 0; index < sorter.SortedVertices.Length; index++)
{
var flowNode = sorter.SortedVertices[index];
flowNode.Index = index;
}
flowsOrderedListTextBox.Text = GetOutputForFlowsOrderedList(sorter.SortedVertices);
almAcceleratorConfigsTextBox.Text = GetOutputForALMAcceleratorConfigs(sorter.SortedVertices);
GetOutputForVisual(dependencyGraph);
}
private IEnumerable<LocalModels.Action> GetActions(object obj, LocalModels.ActionType actionType)
{
if (obj == null) yield break;
if (obj is LocalModels.Action action)
{
if (action.Type == actionType)
yield return action;
}
Type objType = obj.GetType();
if (objType.Assembly != this.GetType().Assembly) yield break;
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(obj, null);
if (propValue is IList list)
{
foreach (var item in list)
{
foreach (var childItem in GetActions(item, actionType))
yield return childItem;
}
}
else if (propValue is IDictionary map)
{
foreach (var item in map.Values)
{
foreach (var childItem in GetActions(item, actionType))
yield return childItem;
}
}
else
{
foreach (var childItem in GetActions(propValue, actionType))
yield return childItem;
}
}
}
private void GetOutputForVisual(IEdgeListGraph<FlowNode, Edge<FlowNode>> graph)
{
Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
viewer.Graph = graph.ToMsaglGraph();
this.splitContainer2.Panel2.SuspendLayout();
viewer.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Panel2.Controls.Add(viewer);
this.splitContainer2.Panel2.ResumeLayout();
}
private string GetOutputForFlowsOrderedList(FlowNode[] flows)
{
var items = new List<string>();
for (var index = 0; index < flows.Length; index++)
{
var flow = flows[index];
items.Add(string.Format("[{0}] {1}", index, flow.Flow.Name));
}
return String.Join(Environment.NewLine, items);
}
private string GetOutputForALMAcceleratorConfigs(FlowNode[] flows)
{
var items = new List<string>();
for(var index = 0; index < flows.Length; index++)
{
var flow = flows[index];
var name = flow.Flow.Name.Replace(" ", "");
items.Add(
string.Format(
@"{{
""Name"": ""owner.ownerEmail.{0}.{1}"",
""Value"": """"
}},{{
""Name"": ""flow.sharing.{0}.{1}"",
""Value"": """"
}},{{
""Name"": ""activateflow.order.{0}.{1}"",
""Value"": ""{2}""
}},{{
""Name"": ""activateflow.activate.{0}.{1}"",
""Value"": ""true""
}}", name, flow.Flow.Id, index+1));
}
return String.Join(",", items);
}
}
class FlowNode
{
public FlowNode(LocalModels.FlowDefinition flow) : this(flow, 0)
{
}
public FlowNode(LocalModels.FlowDefinition flow, int index)
{
this.Flow = flow;
this.Index = index;
}
public int Index { get; set; }
public LocalModels.FlowDefinition Flow { get; set; }
public override string ToString()
{
return this.Index.ToString();
}
}
}