-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEntityIconService.cs
More file actions
77 lines (65 loc) · 2.92 KB
/
Copy pathEntityIconService.cs
File metadata and controls
77 lines (65 loc) · 2.92 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
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System.Reflection;
namespace Generator.Services
{
/// <summary>
/// Service responsible for retrieving entity icons from Dataverse and local files
/// </summary>
internal class EntityIconService
{
private readonly ServiceClient client;
public EntityIconService(ServiceClient client)
{
this.client = client;
}
/// <summary>
/// Retrieves entity icons, first from Dataverse webresources, then from local entityicons directory
/// </summary>
public async Task<Dictionary<string, string>> GetEntityIconMap(IEnumerable<EntityMetadata> entities)
{
var logicalNameToIconName =
entities
.Where(x => x.IconVectorName != null)
.ToDictionary(x => x.LogicalName, x => x.IconVectorName);
var iconNameToSvg = new Dictionary<string, string>();
if (logicalNameToIconName.Count > 0)
{
var query = new QueryExpression("webresource")
{
ColumnSet = new ColumnSet("content", "name"),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("name", ConditionOperator.In, logicalNameToIconName.Values.ToList())
}
}
};
var webresources = await client.RetrieveMultipleAsync(query);
iconNameToSvg = webresources.Entities.ToDictionary(x => x.GetAttributeValue<string>("name"), x => x.GetAttributeValue<string>("content"));
}
var logicalNameToSvg =
logicalNameToIconName
.Where(x => iconNameToSvg.ContainsKey(x.Value) && !string.IsNullOrEmpty(iconNameToSvg[x.Value]))
.ToDictionary(x => x.Key, x => iconNameToSvg.GetValueOrDefault(x.Value) ?? string.Empty);
var sourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var iconDirectory = Path.Combine(sourceDirectory ?? string.Empty, "../../../entityicons");
var iconFiles = Directory.GetFiles(iconDirectory).ToDictionary(x => Path.GetFileNameWithoutExtension(x), x => x);
foreach (var entity in entities)
{
if (logicalNameToSvg.ContainsKey(entity.LogicalName))
{
continue;
}
var iconKey = $"svg_{entity.ObjectTypeCode}";
if (iconFiles.ContainsKey(iconKey))
{
logicalNameToSvg[entity.LogicalName] = Convert.ToBase64String(File.ReadAllBytes(iconFiles[iconKey]));
}
}
return logicalNameToSvg;
}
}
}