-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
SelectAttributesDialog.cs
363 lines (337 loc) · 13.8 KB
/
SelectAttributesDialog.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
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Rappen.XRM.Helpers.Extensions;
using Rappen.XTB.XmlEditorUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using XrmToolBox.Extensibility;
namespace Rappen.XTB.FetchXmlBuilder.Forms
{
public partial class SelectAttributesDialog : Form
{
private int sortcolumn = 0;
private List<ListViewItem> allItems;
private List<string> viewcolumns;
private List<string> formcolumns;
private FetchXmlBuilder fxb;
private string entity;
private bool initiating = true;
private bool working = false;
public SelectAttributesDialog(FetchXmlBuilder fxb, string entity, List<string> selectedAttributes)
{
this.fxb = fxb;
this.entity = entity;
var attributes = new List<AttributeMetadata>(fxb.GetDisplayAttributes(entity));
InitializeComponent();
GenerateAllItems(attributes, selectedAttributes);
PopulateAttributes();
initiating = false;
SetSelectedNos();
ActiveControl = txtFilter;
}
private void GenerateAllItems(List<AttributeMetadata> attributes, List<string> selectedAttributes)
{
chkMetamore_CheckedChanged(null, null);
allItems = new List<ListViewItem>();
foreach (var attribute in attributes)
{
var name = attribute.DisplayName?.UserLocalizedLabel?.Label ?? attribute.LogicalName;
var item = new ListViewItem(new string[] {
name,
attribute.LogicalName,
attribute.ToTypeName(),
attribute.IsValidForRead.HasValue ? attribute.IsValidForRead.Value.ToString() : "",
attribute.IsValidForGrid.HasValue ? attribute.IsValidForGrid.Value.ToString() : "",
attribute.IsValidForAdvancedFind.Value.ToString(),
attribute.IsRetrievable.HasValue ? attribute.IsRetrievable.Value.ToString() : ""
});
item.Name = attribute.LogicalName;
item.Text = name;
item.Tag = attribute;
item.Checked = selectedAttributes.Contains(attribute.LogicalName);
allItems.Add(item);
}
}
private void PopulateAttributes(bool required = false, bool isonanyview = false, bool isonanyform = false)
{
initiating = true;
lvAttributes.Items.Clear();
foreach (var item in allItems)
{
var filter = txtFilter.Text.ToUpperInvariant();
if (string.IsNullOrWhiteSpace(filter) || item.Name.ToUpperInvariant().Contains(filter) || item.Text.ToUpperInvariant().Contains(filter))
{
if (required && !IsRequired(item.Tag as AttributeMetadata))
{
continue;
}
if (isonanyview && !IsOnAnyView(item.Tag as AttributeMetadata))
{
continue;
}
if (isonanyform && !IsOnAnyForm(item.Tag as AttributeMetadata))
{
continue;
}
lvAttributes.Items.Add(item);
}
}
initiating = false;
SetSelectedNos();
}
private bool IsRequired(AttributeMetadata meta)
{
return (meta.RequiredLevel?.Value == AttributeRequiredLevel.ApplicationRequired ||
meta.RequiredLevel?.Value == AttributeRequiredLevel.SystemRequired) &&
string.IsNullOrEmpty(meta.AttributeOf);
}
private bool IsOnAnyView(AttributeMetadata meta)
{
if (viewcolumns == null)
{
if (working)
{
return false;
}
working = true;
Enabled = false;
fxb.WorkAsync(new WorkAsyncInfo
{
Message = "Loading views...",
Work = (w, a) =>
{
if (fxb.Service == null)
{
throw new Exception("Need a connection to load views.");
}
var qexs = new QueryExpression("savedquery");
qexs.ColumnSet = new ColumnSet("name", "returnedtypecode", "layoutxml", "iscustomizable");
qexs.Criteria.AddCondition("returnedtypecode", ConditionOperator.Equal, entity);
qexs.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
qexs.Criteria.AddCondition("layoutxml", ConditionOperator.NotNull);
a.Result = fxb.RetrieveMultiple(qexs);
},
PostWorkCallBack = (a) =>
{
if (a.Error != null)
{
fxb.ShowErrorDialog(a.Error);
}
else if (a.Result is EntityCollection views)
{
SetViewColumns(views);
PopulateAttributes(false, true);
}
Enabled = true;
working = false;
}
});
return false;
}
return viewcolumns.Contains(meta.LogicalName);
}
private bool IsOnAnyForm(AttributeMetadata meta)
{
if (formcolumns == null)
{
if (working)
{
return false;
}
working = true;
Enabled = false;
fxb.WorkAsync(new WorkAsyncInfo
{
Message = "Loading forms...",
Work = (w, a) =>
{
if (fxb.Service == null)
{
throw new Exception("Need a connection to load forms.");
}
var query = new QueryExpression("systemform");
query.ColumnSet.AddColumns("name", "type", "formxml");
query.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, entity);
query.Criteria.AddCondition("formactivationstate", ConditionOperator.Equal, 1);
query.Criteria.AddCondition("formxml", ConditionOperator.NotNull);
a.Result = fxb.RetrieveMultiple(query);
},
PostWorkCallBack = (a) =>
{
if (a.Error != null)
{
fxb.ShowErrorDialog(a.Error);
}
else if (a.Result is EntityCollection forms)
{
SetFormColumns(forms);
PopulateAttributes(isonanyform: true);
}
Enabled = true;
working = false;
}
});
return false;
}
return formcolumns.Contains(meta.LogicalName);
}
private void SetViewColumns(EntityCollection views)
{
viewcolumns = new List<string>();
foreach (var view in views.Entities)
{
var layout = view.GetAttributeValue<string>("layoutxml");
if (layout.ToXml().SelectSingleNode("grid") is XmlElement grid &&
grid.SelectSingleNode("row") is XmlElement row)
{
viewcolumns.AddRange(row.ChildNodes
.Cast<XmlNode>()
.Where(n => n.Name == "cell")
.Select(c => c.AttributeValue("name")));
}
}
viewcolumns = viewcolumns.Distinct().ToList();
}
private void SetFormColumns(EntityCollection forms)
{
formcolumns = new List<string>();
foreach (var form in forms.Entities)
{
var formxml = form.GetAttributeValue<string>("formxml");
var nodes = formxml.ToXml().ChildNodes;
foreach (XmlNode node in nodes)
{
formcolumns.AddRange(FindCellControlFields(node));
}
}
formcolumns = formcolumns.Distinct().ToList();
}
private IEnumerable<string> FindCellControlFields(XmlNode node)
{
var result = new List<string>();
if (node.Name == "cell" &&
node.SelectSingleNode("control") is XmlNode control &&
control.AttributeValue("datafieldname") is string field &&
!string.IsNullOrEmpty(field))
{
result.Add(field);
}
node.ChildNodes.Cast<XmlNode>().ToList().ForEach(n => result.AddRange(FindCellControlFields(n)));
return result;
}
public List<AttributeMetadata> GetSelectedAttributes()
{
var result = new List<AttributeMetadata>();
foreach (var item in allItems)
{
if (item.Checked)
{
result.Add((AttributeMetadata)item.Tag);
}
}
return result;
}
private void lvAttributes_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column == sortcolumn)
{
lvAttributes.Sorting = lvAttributes.Sorting == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
}
else
{
lvAttributes.Sorting = SortOrder.Ascending;
}
lvAttributes.ListViewItemSorter = new ListViewItemComparer(e.Column, lvAttributes.Sorting);
sortcolumn = e.Column;
}
private void txtFilter_TextChanged(object sender, EventArgs e)
{
PopulateAttributes();
}
private void chkMetamore_CheckedChanged(object sender, EventArgs e)
{
foreach (var col in from ColumnHeader col in lvAttributes.Columns
where col.Tag?.ToString() == "meta"
select col)
{
ShowHideMeta(chkMetamore.Checked, col);
}
splitContainer1.Panel2Collapsed = !chkMetamore.Checked;
var width = (from ColumnHeader col in lvAttributes.Columns select col.Width).Sum() + 70;
Width = Math.Max(width, 450) + splitContainer1.Panel2.Width;
}
private void ShowHideMeta(bool on, ColumnHeader col)
{
if (on)
{
col.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
}
else
{
col.Width = 0;
}
}
private void lvAttributes_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvAttributes.SelectedItems.Cast<ListViewItem>().FirstOrDefault() is ListViewItem item &&
item.Tag is AttributeMetadata meta)
{
metadataControl1.SelectedObject = meta;
}
}
private void lvAttributes_ItemChecked(object sender, ItemCheckedEventArgs e)
{
timerSummary.Stop();
timerSummary.Start();
}
private void timerSummary_Tick(object sender, EventArgs e)
{
timerSummary.Enabled = false;
SetSelectedNos();
}
private void SetSelectedNos()
{
if (initiating || working)
{
return;
}
//try
{
lblSelectedNo.Text = $"Selected {allItems.Where(a => a.Checked).Count()}/{allItems.Count}";
}
//catch { }
}
private void lnkShowAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PopulateAttributes();
}
private void lnkShowRequired_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PopulateAttributes(required: true);
}
private void lnkShowOnViews_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PopulateAttributes(isonanyview: true);
}
private void lnkShowOnForms_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PopulateAttributes(isonanyform: true);
}
private void lnkCheckShown_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
lvAttributes.Items.Cast<ListViewItem>().ToList().ForEach(a => a.Checked = true);
}
private void lnkUncheckShown_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
lvAttributes.Items.Cast<ListViewItem>().ToList().ForEach(a => a.Checked = false);
}
private void lnkUncheckAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
allItems.ForEach(a => a.Checked = false);
}
}
}