-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDataverseMetadataFetcher.cs
More file actions
755 lines (671 loc) · 32.8 KB
/
DataverseMetadataFetcher.cs
File metadata and controls
755 lines (671 loc) · 32.8 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
using DataverseProxyGenerator.Core.Domain;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
namespace DataverseProxyGenerator.Core.Metadata;
public class DataverseMetadataFetcher : IDataverseMetadataFetcher
{
private const int MaxParallelism = 8;
private readonly ServiceClient serviceClient;
private readonly XrmFetchConfig config;
public DataverseMetadataFetcher(ServiceClient serviceClient, XrmFetchConfig config)
{
this.serviceClient = serviceClient;
this.config = config;
}
public async Task<IEnumerable<TableModel>> FetchMetadataAsync()
{
var tables = new List<TableModel>();
// Fetch from solutions
var metadataFromSolution = await GetEntityMetadataFromSolutionsAsync();
var fetchedLogicalNames = metadataFromSolution.Select(m => m.LogicalName).ToHashSet(StringComparer.InvariantCulture);
// Fetch by logical names not already fetched
var toolLogicalNames = new List<string>() { "activityparty" };
var logicalNamesToFetch =
config.Entities
.Concat(toolLogicalNames)
.Where(name => !string.IsNullOrWhiteSpace(name) && !fetchedLogicalNames.Contains(name))
.Distinct(StringComparer.InvariantCulture)
.ToList();
var metadataFromLogicalNames = await GetEntityMetadataFromLogicalNamesAsync(logicalNamesToFetch);
var allMetadata = metadataFromSolution.Concat(metadataFromLogicalNames).ToList();
var logicalNameToMetadata = allMetadata.ToDictionary(m => m.LogicalName, m => m, StringComparer.InvariantCulture);
// Merge all metadata
foreach (var metadata in allMetadata)
{
var table = BuildTableModelFromMetadata(logicalNameToMetadata, metadata);
tables.Add(table);
}
return tables;
}
private async Task<List<EntityMetadata>> GetEntityMetadataFromSolutionsAsync()
{
var logicalNameToMetadata = new Dictionary<string, EntityMetadata>(StringComparer.InvariantCulture);
foreach (var solutionUniqueName in config.Solutions)
{
var solutionId = GetSolutionId(solutionUniqueName);
if (solutionId == Guid.Empty)
continue;
var entityIds = GetEntityIdsFromSolution(solutionId);
using var semaphore = new SemaphoreSlim(MaxParallelism);
var metadataTasks = entityIds.Select(async entityId =>
{
await semaphore.WaitAsync();
try
{
return await GetEntityMetadataFromIdAsync(entityId);
}
finally
{
semaphore.Release();
}
});
var metadata = await Task.WhenAll(metadataTasks);
foreach (var m in metadata)
{
if (!string.IsNullOrEmpty(m.LogicalName) && !logicalNameToMetadata.ContainsKey(m.LogicalName))
logicalNameToMetadata.Add(m.LogicalName, m);
}
}
return logicalNameToMetadata.Values.ToList();
}
private async Task<List<EntityMetadata>> GetEntityMetadataFromLogicalNamesAsync(IEnumerable<string> logicalNames)
{
var metadataList = new List<EntityMetadata>();
using var semaphore = new SemaphoreSlim(MaxParallelism);
var tasks = logicalNames.Select(async logicalName =>
{
await semaphore.WaitAsync();
try
{
var entityRequest = new RetrieveEntityRequest
{
LogicalName = logicalName,
EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships,
RetrieveAsIfPublished = true,
};
var entityResponse = (RetrieveEntityResponse)await serviceClient.ExecuteAsync(entityRequest);
if (entityResponse?.EntityMetadata != null)
{
return entityResponse.EntityMetadata;
}
return null;
}
finally
{
semaphore.Release();
}
});
var results = await Task.WhenAll(tasks);
metadataList.AddRange(results.Where(m => m != null)!);
return metadataList;
}
private Guid GetSolutionId(string solutionUniqueName)
{
var solutionQuery = new QueryExpression("solution")
{
ColumnSet = new ColumnSet("solutionid"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("uniquename", ConditionOperator.Equal, solutionUniqueName),
},
},
};
var solutionEntity = serviceClient.RetrieveMultiple(solutionQuery).Entities.FirstOrDefault();
return solutionEntity?.Id ?? Guid.Empty;
}
private List<Guid> GetEntityIdsFromSolution(Guid solutionId)
{
var componentQuery = new QueryExpression("solutioncomponent")
{
ColumnSet = new ColumnSet("objectid"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("solutionid", ConditionOperator.Equal, solutionId),
new ConditionExpression("componenttype", ConditionOperator.Equal, 1), // 1 = Entity
},
},
};
return serviceClient.RetrieveMultiple(componentQuery).Entities
.Where(c => c.Contains("objectid") && c["objectid"] is Guid)
.Select(c => (Guid)c["objectid"])
.ToList();
}
private async Task<EntityMetadata> GetEntityMetadataFromIdAsync(Guid entityId)
{
var entityRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
{
MetadataId = entityId,
EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships,
RetrieveAsIfPublished = true,
};
var entityResponse = (Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse)await serviceClient.ExecuteAsync(entityRequest);
return entityResponse.EntityMetadata;
}
private TableModel BuildTableModelFromMetadata(Dictionary<string, EntityMetadata> logicalNameToMetadata, EntityMetadata entityMetadata)
{
var table = new TableModel
{
LogicalName = entityMetadata.LogicalName,
SchemaName = entityMetadata.SchemaName,
DisplayName = ApplyLabelMapping(entityMetadata.DisplayName?.UserLocalizedLabel?.Label ?? entityMetadata.LogicalName),
Description = entityMetadata.Description?.UserLocalizedLabel?.Label ?? string.Empty,
EntityTypeCode = entityMetadata.ObjectTypeCode ?? 0,
PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute,
PrimaryIdAttribute = entityMetadata.PrimaryIdAttribute,
IsIntersect = entityMetadata.IsIntersect ?? false,
Columns = new List<ColumnModel>(),
Relationships = new List<RelationshipModel>(),
};
var validAttributes = entityMetadata.Attributes
.Where(x => x.AttributeOf == null && x.LogicalName != entityMetadata.PrimaryIdAttribute)
.ToList();
foreach (var attr in validAttributes)
{
var column = BuildColumnModel(attr);
if (column != null)
{
table.Columns.Add(column);
}
}
AddPrimaryIdColumn(table, entityMetadata);
MapRelationships(logicalNameToMetadata, entityMetadata, table);
return table;
}
private ColumnModel? BuildColumnModel(AttributeMetadata attr)
{
ColumnModel? column = attr switch
{
StringAttributeMetadata stringAttr => BuildStringColumn(stringAttr),
MemoAttributeMetadata memoAttr => BuildMemoColumn(memoAttr),
IntegerAttributeMetadata intAttr => BuildIntegerColumn(intAttr),
BigIntAttributeMetadata bigIntAttr => BuildBigIntColumn(bigIntAttr),
BooleanAttributeMetadata boolAttr => BuildBooleanColumn(boolAttr),
DateTimeAttributeMetadata dateAttr => BuildDateTimeColumn(dateAttr),
DecimalAttributeMetadata decAttr => BuildDecimalColumn(decAttr),
DoubleAttributeMetadata dblAttr => BuildDoubleColumn(dblAttr),
MoneyAttributeMetadata moneyAttr => BuildMoneyColumn(moneyAttr),
EnumAttributeMetadata enumAttribute when enumAttribute.AttributeType == AttributeTypeCode.EntityName => BuildStringColumn(enumAttribute),
EnumAttributeMetadata enumAttr => BuildEnumColumn(enumAttr),
LookupAttributeMetadata lookupAttr when lookupAttr.AttributeType == AttributeTypeCode.PartyList => BuildPartyListColumn(lookupAttr),
LookupAttributeMetadata lookupAttr => BuildLookupColumn(lookupAttr),
FileAttributeMetadata fileAttr => BuildFileColumn(fileAttr),
ImageAttributeMetadata imageAttr => BuildImageColumn(imageAttr),
ManagedPropertyAttributeMetadata managedAttr => BuildManagedPropertyColumn(managedAttr),
UniqueIdentifierAttributeMetadata uniqueAttr => BuildUniqueIdentifierColumn(uniqueAttr),
AttributeMetadata attrAttr when attrAttr.AttributeType == AttributeTypeCode.Uniqueidentifier => BuildUniqueIdentifierColumn(attrAttr),
_ => null,
};
if (column != null)
{
column = column with
{
IsObsolete =
!string.IsNullOrEmpty(column.DisplayName) &&
!string.IsNullOrEmpty(config.DeprecatedPrefix) &&
column.DisplayName.StartsWith(config.DeprecatedPrefix, StringComparison.OrdinalIgnoreCase),
};
}
return column;
}
private string ApplyLabelMapping(string label)
{
if (string.IsNullOrEmpty(label) || config.LabelMapping.Count == 0)
return label;
foreach (var kvp in config.LabelMapping)
{
if (!string.IsNullOrEmpty(kvp.Key))
label = label.Replace(kvp.Key, kvp.Value, StringComparison.InvariantCulture);
}
return label;
}
private void AddPrimaryIdColumn(TableModel table, EntityMetadata entityMetadata)
{
var primaryIdAttribute = Array.Find(entityMetadata.Attributes, x => x.LogicalName == entityMetadata.PrimaryIdAttribute);
var primaryIdColumn = new PrimaryIdColumnModel
{
LogicalName = entityMetadata.PrimaryIdAttribute,
SchemaName = primaryIdAttribute?.SchemaName ?? entityMetadata.PrimaryIdAttribute,
DisplayName = ApplyLabelMapping(primaryIdAttribute?.DisplayName?.UserLocalizedLabel?.Label ?? entityMetadata.PrimaryIdAttribute),
IsObsolete = !string.IsNullOrEmpty(entityMetadata.PrimaryNameAttribute) &&
!string.IsNullOrEmpty(config.DeprecatedPrefix) &&
entityMetadata.PrimaryNameAttribute.StartsWith(config.DeprecatedPrefix, StringComparison.OrdinalIgnoreCase),
};
table.Columns.Add(primaryIdColumn);
}
private StringColumnModel BuildStringColumn(StringAttributeMetadata attr) => new StringColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
MaxLength = attr.MaxLength,
};
private StringColumnModel BuildStringColumn(EnumAttributeMetadata attr) => new StringColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private MemoColumnModel BuildMemoColumn(MemoAttributeMetadata attr) => new MemoColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
MaxLength = attr.MaxLength,
};
private IntegerColumnModel BuildIntegerColumn(IntegerAttributeMetadata attr) => new IntegerColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
Min = attr.MinValue ?? int.MinValue,
Max = attr.MaxValue ?? int.MaxValue,
};
private BigIntColumnModel BuildBigIntColumn(BigIntAttributeMetadata attr) => new BigIntColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private BooleanColumnModel BuildBooleanColumn(BooleanAttributeMetadata attr) => new BooleanColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private DateTimeColumnModel BuildDateTimeColumn(DateTimeAttributeMetadata attr) => new DateTimeColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private DecimalColumnModel BuildDecimalColumn(DecimalAttributeMetadata attr) => new DecimalColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
Precision = attr.Precision,
};
private DoubleColumnModel BuildDoubleColumn(DoubleAttributeMetadata attr) => new DoubleColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private MoneyColumnModel BuildMoneyColumn(MoneyAttributeMetadata attr) => new MoneyColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
Precision = attr.Precision,
};
private EnumColumnModel BuildEnumColumn(EnumAttributeMetadata attr)
{
var optionsetValues = attr.OptionSet?.Options?
.Where(o => o.Value != null)
.ToDictionary(
o => o.Value.GetValueOrDefault(),
o =>
{
var label = o.Label?.UserLocalizedLabel?.Label;
label = ApplyLabelMapping(label ?? string.Empty);
return label;
}) ?? [];
// Build OptionLocalizations: option value -> (LCID -> label)
var optionLocalizations = BuildOptionLocalizations(attr);
return new EnumColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
OptionsetName = attr.OptionSet?.Name ?? attr.LogicalName,
IsGlobalOptionset = attr.OptionSet?.IsGlobal ?? false,
IsMultiSelect = attr.AttributeTypeName == "MultiSelectPicklistType",
OptionsetValues = optionsetValues,
OptionLocalizations = optionLocalizations,
};
}
private static Dictionary<int, Dictionary<int, string>> BuildOptionLocalizations(EnumAttributeMetadata attr)
{
var optionLocalizations = new Dictionary<int, Dictionary<int, string>>();
if (attr.OptionSet?.Options != null)
{
foreach (var o in attr.OptionSet.Options)
{
if (o.Value == null)
continue;
var value = o.Value.GetValueOrDefault();
var localizations = new Dictionary<int, string>();
if (o.Label?.LocalizedLabels != null)
{
foreach (var loc in o.Label.LocalizedLabels)
{
if (!string.IsNullOrWhiteSpace(loc.Label))
{
localizations[loc.LanguageCode] = loc.Label;
}
}
}
// Always include the user localized label if present
if (o.Label?.UserLocalizedLabel != null && !string.IsNullOrWhiteSpace(o.Label.UserLocalizedLabel.Label))
{
localizations[o.Label.UserLocalizedLabel.LanguageCode] = o.Label.UserLocalizedLabel.Label;
}
if (localizations.Count > 0)
{
optionLocalizations[value] = localizations;
}
}
}
return optionLocalizations;
}
private PartyListColumnModel BuildPartyListColumn(LookupAttributeMetadata attr) => new PartyListColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private LookupColumnModel BuildLookupColumn(LookupAttributeMetadata attr) => new LookupColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
TargetTable = attr.Targets?.FirstOrDefault() ?? "Unknown",
};
private FileColumnModel BuildFileColumn(FileAttributeMetadata attr) => new FileColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private ImageColumnModel BuildImageColumn(ImageAttributeMetadata attr) => new ImageColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private ManagedColumnModel? BuildManagedPropertyColumn(ManagedPropertyAttributeMetadata attr)
{
return attr.ValueAttributeTypeCode switch
{
AttributeTypeCode.Boolean => BuildBooleanManagedColumnModel(attr),
AttributeTypeCode.DateTime => BuildManagedColumnModel(attr, "DateTime", nullable: true),
AttributeTypeCode.Decimal => BuildManagedColumnModel(attr, "decimal", nullable: true),
AttributeTypeCode.Double => BuildManagedColumnModel(attr, "double", nullable: true),
AttributeTypeCode.Integer => BuildManagedColumnModel(attr, "int", nullable: true),
AttributeTypeCode.BigInt => BuildManagedColumnModel(attr, "long", nullable: true),
AttributeTypeCode.Lookup => BuildManagedColumnModel(attr, "EntityReference", nullable: true),
AttributeTypeCode.Money => BuildManagedColumnModel(attr, "decimal", nullable: true),
AttributeTypeCode.Memo => BuildManagedColumnModel(attr, "string"),
AttributeTypeCode.PartyList => BuildManagedColumnModel(attr, "IEnumerable<ActivityParty>"),
AttributeTypeCode.String => BuildManagedColumnModel(attr, "string"),
_ => null,
};
}
private ManagedColumnModel BuildManagedColumnModel(AttributeMetadata attr, string returnType, bool nullable = false) => new ManagedColumnModel(returnType, nullable)
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private BooleanManagedColumnModel BuildBooleanManagedColumnModel(AttributeMetadata attr) => new BooleanManagedColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private UniqueIdentifierColumnModel BuildUniqueIdentifierColumn(AttributeMetadata attr) => new UniqueIdentifierColumnModel
{
LogicalName = attr.LogicalName,
SchemaName = attr.SchemaName,
DisplayName = ApplyLabelMapping(attr.DisplayName?.UserLocalizedLabel?.Label ?? attr.LogicalName),
Description = ApplyLabelMapping(attr.Description?.UserLocalizedLabel?.Label ?? string.Empty),
};
private static void MapRelationships(Dictionary<string, EntityMetadata> logicalNameToMetadata, EntityMetadata entityMetadata, TableModel table)
{
MapManyToOne(logicalNameToMetadata, entityMetadata, table);
MapOneToMany(logicalNameToMetadata, entityMetadata, table);
MapManyToMany(logicalNameToMetadata, entityMetadata, table);
table = table with
{
Relationships = [.. table.Relationships.DistinctBy(x => x.SchemaName)],
};
}
private static void MapManyToOne(Dictionary<string, EntityMetadata> logicalNameToMetadata, EntityMetadata entityMetadata, TableModel table)
{
foreach (var rel in entityMetadata.ManyToOneRelationships)
{
if (!logicalNameToMetadata.TryGetValue(rel.ReferencedEntity, out var relatedMetadata))
continue;
table.Relationships.Add(new RelationshipModel
{
SchemaName = rel.SchemaName,
RelationshipType = "ManyToOne",
ThisEntityRole = "Referencing",
ThisEntityAttribute = rel.ReferencingAttribute,
RelatedEntity = rel.ReferencedEntity,
RelatedEntityAttribute = rel.ReferencedAttribute,
RelatedEntitySchemaName = relatedMetadata.SchemaName,
});
}
}
private static void MapOneToMany(Dictionary<string, EntityMetadata> logicalNameToMetadata, EntityMetadata entityMetadata, TableModel table)
{
foreach (var rel in entityMetadata.OneToManyRelationships.Where(x => x.ReferencingEntity != entityMetadata.LogicalName))
{
if (!logicalNameToMetadata.TryGetValue(rel.ReferencingEntity, out var relatedMetadata))
continue;
table.Relationships.Add(new RelationshipModel
{
SchemaName = rel.SchemaName,
RelationshipType = "OneToMany",
ThisEntityRole = "Referenced",
ThisEntityAttribute = rel.ReferencedAttribute,
RelatedEntity = rel.ReferencingEntity,
RelatedEntityAttribute = rel.ReferencingAttribute,
RelatedEntitySchemaName = relatedMetadata.SchemaName,
});
}
}
private static void MapManyToMany(Dictionary<string, EntityMetadata> logicalNameToMetadata, EntityMetadata entityMetadata, TableModel table)
{
foreach (var rel in entityMetadata.ManyToManyRelationships.Where(x => logicalNameToMetadata.ContainsKey(x.Entity1LogicalName) && logicalNameToMetadata.ContainsKey(x.Entity2LogicalName)))
{
if (rel.Entity2LogicalName != entityMetadata.LogicalName)
{
table.Relationships.Add(new RelationshipModel
{
SchemaName = rel.SchemaName,
RelationshipType = "ManyToMany",
ThisEntityRole = "Entity1",
ThisEntityAttribute = rel.Entity1IntersectAttribute,
RelatedEntity = rel.Entity2LogicalName,
RelatedEntityAttribute = rel.Entity2IntersectAttribute,
RelatedEntitySchemaName = logicalNameToMetadata.TryGetValue(rel.Entity2LogicalName, out var relatedMetadata2) ? relatedMetadata2.SchemaName : "Entity",
});
continue;
}
table.Relationships.Add(new RelationshipModel
{
SchemaName = rel.SchemaName,
RelationshipType = "ManyToMany",
ThisEntityRole = "Entity2",
ThisEntityAttribute = rel.Entity2IntersectAttribute,
RelatedEntity = rel.Entity1LogicalName,
RelatedEntityAttribute = rel.Entity1IntersectAttribute,
RelatedEntitySchemaName = logicalNameToMetadata.TryGetValue(rel.Entity1LogicalName, out var relatedMetadata1) ? relatedMetadata1.SchemaName : "Entity",
});
}
}
public async Task<IEnumerable<CustomApiModel>> FetchCustomApisAsync()
{
var customApis = new List<CustomApiModel>();
// Fetch custom APIs from solutions
foreach (var solutionUniqueName in config.Solutions)
{
var solutionId = GetSolutionId(solutionUniqueName);
if (solutionId == Guid.Empty)
continue;
var customApiIds = await GetCustomApiIdsFromSolutionAsync(solutionId);
using var semaphore = new SemaphoreSlim(MaxParallelism);
var customApiTasks = customApiIds.Select(async customApiId =>
{
await semaphore.WaitAsync();
try
{
return await GetCustomApiFromIdAsync(customApiId);
}
finally
{
semaphore.Release();
}
});
var results = await Task.WhenAll(customApiTasks);
customApis.AddRange(results.Where(api => api != null)!);
}
return customApis;
}
private async Task<List<Guid>> GetCustomApiIdsFromSolutionAsync(Guid solutionId)
{
var componentQuery = new QueryExpression("solutioncomponent")
{
ColumnSet = new ColumnSet("objectid"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("solutionid", ConditionOperator.Equal, solutionId),
new ConditionExpression("componenttype", ConditionOperator.Equal, 10026), // Custom API component type
},
},
};
var result = await serviceClient.RetrieveMultipleAsync(componentQuery);
return result.Entities
.Where(c => c.Contains("objectid") && c["objectid"] is Guid)
.Select(c => (Guid)c["objectid"])
.ToList();
}
private async Task<CustomApiModel?> GetCustomApiFromIdAsync(Guid customApiId)
{
try
{
// Fetch the custom API
var customApiQuery = new QueryExpression("customapi")
{
ColumnSet = new ColumnSet("uniquename", "displayname", "description", "isfunction"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("customapiid", ConditionOperator.Equal, customApiId),
},
},
};
var customApiResult = await serviceClient.RetrieveMultipleAsync(customApiQuery);
var customApiEntity = customApiResult.Entities.FirstOrDefault();
if (customApiEntity == null)
return null;
var customApi = new CustomApiModel
{
UniqueName = customApiEntity.GetAttributeValue<string>("uniquename") ?? string.Empty,
DisplayName = customApiEntity.GetAttributeValue<string>("displayname") ?? string.Empty,
Description = customApiEntity.GetAttributeValue<string>("description") ?? string.Empty,
IsFunction = customApiEntity.GetAttributeValue<bool>("isfunction"),
};
var requestParameters = await FetchCustomApiRequestParametersAsync(customApiId);
var responseProperties = await FetchCustomApiResponsePropertiesAsync(customApiId);
return customApi with
{
RequestParameters = requestParameters,
ResponseProperties = responseProperties,
};
}
catch (InvalidOperationException)
{
// Log or handle the exception as needed
return null;
}
catch (ArgumentException)
{
// Log or handle the exception as needed
return null;
}
}
private async Task<IList<CustomApiParameterModel>> FetchCustomApiRequestParametersAsync(Guid customApiId)
{
var requestParametersQuery = new QueryExpression("customapirequestparameter")
{
ColumnSet = new ColumnSet("name", "uniquename", "displayname", "description", "type", "isoptional", "logicalentityname"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("customapiid", ConditionOperator.Equal, customApiId),
},
},
};
var requestParametersResult = await serviceClient.RetrieveMultipleAsync(requestParametersQuery);
return requestParametersResult.Entities
.Select(param => new CustomApiParameterModel
{
Name = param.GetAttributeValue<string>("name") ?? string.Empty,
UniqueName = param.GetAttributeValue<string>("uniquename") ?? string.Empty,
DisplayName = param.GetAttributeValue<string>("displayname") ?? string.Empty,
Description = param.GetAttributeValue<string>("description") ?? string.Empty,
Type = (CustomApiParameterType)param.GetAttributeValue<OptionSetValue>("type").Value,
IsOptional = param.GetAttributeValue<bool>("isoptional"),
LogicalEntityName = param.GetAttributeValue<string>("logicalentityname"),
})
.ToList();
}
private async Task<IList<CustomApiParameterModel>> FetchCustomApiResponsePropertiesAsync(Guid customApiId)
{
var responsePropertiesQuery = new QueryExpression("customapiresponseproperty")
{
ColumnSet = new ColumnSet("name", "uniquename", "displayname", "description", "type", "logicalentityname"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("customapiid", ConditionOperator.Equal, customApiId),
},
},
};
var responsePropertiesResult = await serviceClient.RetrieveMultipleAsync(responsePropertiesQuery);
return responsePropertiesResult.Entities
.Select(prop => new CustomApiParameterModel
{
Name = prop.GetAttributeValue<string>("name") ?? string.Empty,
UniqueName = prop.GetAttributeValue<string>("uniquename") ?? string.Empty,
DisplayName = prop.GetAttributeValue<string>("displayname") ?? string.Empty,
Description = prop.GetAttributeValue<string>("description") ?? string.Empty,
Type = (CustomApiParameterType)prop.GetAttributeValue<OptionSetValue>("type").Value,
IsOptional = false, // Response properties are always required
LogicalEntityName = prop.GetAttributeValue<string>("logicalentityname"),
})
.ToList();
}
}