-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolutionComponentExtractor.cs
More file actions
386 lines (342 loc) · 16 KB
/
Copy pathSolutionComponentExtractor.cs
File metadata and controls
386 lines (342 loc) · 16 KB
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
385
386
using Generator.DTO;
using Microsoft.Extensions.Logging;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Generator.Services;
/// <summary>
/// Extracts all solution components for the insights visualization.
/// This is separate from SolutionComponentService which filters entity metadata extraction.
/// </summary>
public class SolutionComponentExtractor
{
private readonly ServiceClient _client;
private readonly ILogger<SolutionComponentExtractor> _logger;
/// <summary>
/// All component types we want to extract for insights.
/// </summary>
private static readonly int[] SupportedComponentTypes = new[]
{
1, // Entity
2, // Attribute
9, // OptionSet
10, // Relationship
14, // EntityKey
20, // SecurityRole
26, // SavedQuery
29, // Workflow
50, // RibbonCustomization
59, // SavedQueryVisualization
60, // SystemForm
61, // WebResource
62, // SiteMap
63, // ConnectionRole
65, // HierarchyRule
66, // CustomControl
70, // FieldSecurityProfile
80, // ModelDrivenApp
91, // PluginAssembly
92, // SDKMessageProcessingStep
300, // CanvasApp
372, // ConnectionReference
380, // EnvironmentVariableDefinition
381, // EnvironmentVariableValue
418, // Dataflow
};
/// <summary>
/// Maps component type codes to their Dataverse table, name column, primary key column, and optional entity column for name resolution.
/// Primary key is optional - if null, defaults to tablename + "id".
/// EntityColumn is used to get the related table for components like forms and views.
/// </summary>
private static readonly Dictionary<int, (string TableName, string NameColumn, string? PrimaryKey, string? EntityColumn)> ComponentTableMap = new()
{
{ 20, ("role", "name", null, null) },
{ 26, ("savedquery", "name", null, "returnedtypecode") }, // Views have returnedtypecode for the entity
{ 29, ("workflow", "name", null, null) },
{ 50, ("ribboncustomization", "entity", null, null) },
{ 59, ("savedqueryvisualization", "name", null, null) },
{ 60, ("systemform", "name", "formid", "objecttypecode") }, // Forms have objecttypecode for the entity
{ 61, ("webresource", "name", null, null) },
{ 62, ("sitemap", "sitemapname", null, null) },
{ 63, ("connectionrole", "name", null, null) },
{ 65, ("hierarchyrule", "name", null, null) },
{ 66, ("customcontrol", "name", null, null) },
{ 70, ("fieldsecurityprofile", "name", null, null) },
{ 80, ("appmodule", "name", "appmoduleid", null) }, // appmodule uses appmoduleid
{ 91, ("pluginassembly", "name", null, null) },
{ 92, ("sdkmessageprocessingstep", "name", null, null) },
{ 300, ("canvasapp", "name", null, null) },
{ 372, ("connectionreference", "connectionreferencedisplayname", null, null) },
{ 380, ("environmentvariabledefinition", "displayname", null, null) },
{ 381, ("environmentvariablevalue", "schemaname", null, null) },
{ 418, ("workflow", "name", null, null) }, // Dataflows are stored in workflow table with category=6
};
/// <summary>
/// Component types that should have a related table displayed.
/// </summary>
private static readonly HashSet<int> ComponentTypesWithRelatedTable = new() { 2, 10, 14, 26, 60 }; // Attribute, Relationship, EntityKey, SavedQuery (View), SystemForm
public SolutionComponentExtractor(ServiceClient client, ILogger<SolutionComponentExtractor> logger)
{
_client = client;
_logger = logger;
}
/// <summary>
/// Extracts all solution components grouped by solution for the insights view.
/// </summary>
public async Task<List<SolutionComponentCollection>> ExtractSolutionComponentsAsync(
List<Guid> solutionIds,
Dictionary<Guid, string> solutionNameLookup,
Dictionary<Guid, string>? entityNameLookup = null,
Dictionary<Guid, string>? attributeNameLookup = null,
Dictionary<Guid, string>? relationshipNameLookup = null,
Dictionary<Guid, string>? attributeEntityLookup = null,
Dictionary<Guid, string>? relationshipEntityLookup = null,
Dictionary<Guid, string>? keyEntityLookup = null)
{
_logger.LogInformation($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] Extracting solution components for {solutionIds.Count} solutions");
if (solutionIds == null || !solutionIds.Any())
{
_logger.LogWarning("No solution IDs provided for component extraction");
return new List<SolutionComponentCollection>();
}
// Query all solution components
var rawComponents = await QuerySolutionComponentsAsync(solutionIds);
_logger.LogInformation($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] Found {rawComponents.Count} raw solution components");
// Group by solution
var componentsBySolution = rawComponents
.GroupBy(c => c.SolutionId)
.ToDictionary(g => g.Key, g => g.ToList());
// Resolve names for each component type
var nameCache = await BuildNameCacheAsync(rawComponents, entityNameLookup, attributeNameLookup, relationshipNameLookup, attributeEntityLookup, relationshipEntityLookup, keyEntityLookup);
// Build the result collections
var result = new List<SolutionComponentCollection>();
foreach (var solutionId in solutionIds)
{
var solutionName = solutionNameLookup.GetValueOrDefault(solutionId, solutionId.ToString());
if (!componentsBySolution.TryGetValue(solutionId, out var components))
{
result.Add(new SolutionComponentCollection(solutionId, solutionName, new List<SolutionComponentData>()));
continue;
}
var componentDataList = components
.Select(c => new SolutionComponentData(
Name: ResolveComponentName(c, nameCache),
SchemaName: ResolveComponentSchemaName(c, nameCache),
ComponentType: (SolutionComponentType)c.ComponentType,
ObjectId: c.ObjectId,
IsExplicit: c.IsExplicit,
RelatedTable: ResolveRelatedTable(c, nameCache)))
.OrderBy(c => c.ComponentType)
.ThenBy(c => c.Name)
.ToList();
result.Add(new SolutionComponentCollection(solutionId, solutionName, componentDataList));
_logger.LogInformation($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] Solution '{solutionName}': {componentDataList.Count} components");
}
return result;
}
private async Task<List<RawComponentInfo>> QuerySolutionComponentsAsync(List<Guid> solutionIds)
{
var results = new List<RawComponentInfo>();
var query = new QueryExpression("solutioncomponent")
{
ColumnSet = new ColumnSet("objectid", "componenttype", "solutionid", "rootcomponentbehavior"),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("componenttype", ConditionOperator.In, SupportedComponentTypes),
new ConditionExpression("solutionid", ConditionOperator.In, solutionIds)
}
}
};
try
{
var entities = await _client.RetrieveAllAsync(query);
foreach (var entity in entities)
{
var componentType = entity.GetAttributeValue<OptionSetValue>("componenttype")?.Value ?? 0;
var objectId = entity.GetAttributeValue<Guid>("objectid");
var solutionId = entity.GetAttributeValue<EntityReference>("solutionid")?.Id ?? Guid.Empty;
var rootBehavior = entity.Contains("rootcomponentbehavior")
? entity.GetAttributeValue<OptionSetValue>("rootcomponentbehavior")?.Value ?? -1
: -1;
// RootComponentBehaviour: 0, 1, 2 = explicit, other = implicit
var isExplicit = rootBehavior >= 0 && rootBehavior <= 2;
results.Add(new RawComponentInfo(componentType, objectId, solutionId, isExplicit));
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to query solution components");
}
return results;
}
private async Task<Dictionary<(int ComponentType, Guid ObjectId), (string Name, string SchemaName, string? RelatedTable)>> BuildNameCacheAsync(
List<RawComponentInfo> components,
Dictionary<Guid, string>? entityNameLookup,
Dictionary<Guid, string>? attributeNameLookup,
Dictionary<Guid, string>? relationshipNameLookup,
Dictionary<Guid, string>? attributeEntityLookup,
Dictionary<Guid, string>? relationshipEntityLookup,
Dictionary<Guid, string>? keyEntityLookup)
{
var cache = new Dictionary<(int, Guid), (string Name, string SchemaName, string? RelatedTable)>();
// Group components by type for batch queries
var componentsByType = components
.GroupBy(c => c.ComponentType)
.ToDictionary(g => g.Key, g => g.Select(c => c.ObjectId).Distinct().ToList());
foreach (var (componentType, objectIds) in componentsByType)
{
// Use provided lookups for metadata-based types
if (componentType == 1 && entityNameLookup != null) // Entity
{
foreach (var objectId in objectIds)
{
if (entityNameLookup.TryGetValue(objectId, out var name))
{
cache[(componentType, objectId)] = (name, name, null);
}
}
continue;
}
if (componentType == 2 && attributeNameLookup != null) // Attribute
{
foreach (var objectId in objectIds)
{
if (attributeNameLookup.TryGetValue(objectId, out var name))
{
var relatedTable = attributeEntityLookup?.GetValueOrDefault(objectId);
cache[(componentType, objectId)] = (name, name, relatedTable);
}
}
continue;
}
if (componentType == 10 && relationshipNameLookup != null) // Relationship
{
foreach (var objectId in objectIds)
{
if (relationshipNameLookup.TryGetValue(objectId, out var name))
{
var relatedTable = relationshipEntityLookup?.GetValueOrDefault(objectId);
cache[(componentType, objectId)] = (name, name, relatedTable);
}
}
continue;
}
// EntityKey - use keyEntityLookup for related table
if (componentType == 14)
{
foreach (var objectId in objectIds)
{
var relatedTable = keyEntityLookup?.GetValueOrDefault(objectId);
cache[(componentType, objectId)] = (objectId.ToString(), objectId.ToString(), relatedTable);
}
continue;
}
// Skip OptionSet - use ObjectId as fallback, no related table
if (componentType == 9)
{
foreach (var objectId in objectIds)
{
cache[(componentType, objectId)] = (objectId.ToString(), objectId.ToString(), null);
}
continue;
}
// Query Dataverse tables for other types
if (ComponentTableMap.TryGetValue(componentType, out var tableInfo))
{
var primaryKey = tableInfo.PrimaryKey ?? tableInfo.TableName + "id";
var namesAndEntities = await QueryComponentNamesWithEntityAsync(tableInfo.TableName, tableInfo.NameColumn, primaryKey, tableInfo.EntityColumn, objectIds);
foreach (var (objectId, name, relatedTable) in namesAndEntities)
{
cache[(componentType, objectId)] = (name, name, relatedTable);
}
}
}
return cache;
}
private async Task<List<(Guid ObjectId, string Name, string? RelatedTable)>> QueryComponentNamesWithEntityAsync(
string tableName, string nameColumn, string primaryKey, string? entityColumn, List<Guid> objectIds)
{
var result = new List<(Guid, string, string?)>();
if (!objectIds.Any())
return result;
try
{
var columns = new List<string> { primaryKey, nameColumn };
if (!string.IsNullOrEmpty(entityColumn))
{
columns.Add(entityColumn);
}
var query = new QueryExpression(tableName)
{
ColumnSet = new ColumnSet(columns.ToArray()),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression(primaryKey, ConditionOperator.In, objectIds)
}
}
};
var entities = await _client.RetrieveAllAsync(query);
foreach (var entity in entities)
{
var id = entity.GetAttributeValue<Guid>(primaryKey);
var name = entity.GetAttributeValue<string>(nameColumn) ?? id.ToString();
string? relatedTable = null;
if (!string.IsNullOrEmpty(entityColumn) && entity.Contains(entityColumn))
{
// The entity column can be a string (logical name) or an int (object type code)
var entityValue = entity[entityColumn];
if (entityValue is string strValue)
{
relatedTable = strValue;
}
else if (entityValue is int intValue)
{
// Object type code - we'd need entity metadata to resolve this
// For now, just store the numeric value as string
relatedTable = intValue.ToString();
}
}
result.Add((id, name, relatedTable));
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to query names from {tableName}. Using ObjectId as fallback.");
// Return empty - fallback will use ObjectId
}
return result;
}
private string ResolveComponentName(RawComponentInfo component, Dictionary<(int, Guid), (string Name, string SchemaName, string? RelatedTable)> cache)
{
if (cache.TryGetValue((component.ComponentType, component.ObjectId), out var names))
{
return names.Name;
}
return component.ObjectId.ToString();
}
private string ResolveComponentSchemaName(RawComponentInfo component, Dictionary<(int, Guid), (string Name, string SchemaName, string? RelatedTable)> cache)
{
if (cache.TryGetValue((component.ComponentType, component.ObjectId), out var names))
{
return names.SchemaName;
}
return component.ObjectId.ToString();
}
private string? ResolveRelatedTable(RawComponentInfo component, Dictionary<(int, Guid), (string Name, string SchemaName, string? RelatedTable)> cache)
{
if (!ComponentTypesWithRelatedTable.Contains(component.ComponentType))
{
return null;
}
if (cache.TryGetValue((component.ComponentType, component.ObjectId), out var names))
{
return names.RelatedTable;
}
return null;
}
private record RawComponentInfo(int ComponentType, Guid ObjectId, Guid SolutionId, bool IsExplicit);
}