-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolutionService.cs
More file actions
113 lines (101 loc) · 4.26 KB
/
Copy pathSolutionService.cs
File metadata and controls
113 lines (101 loc) · 4.26 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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Generator.Services
{
/// <summary>
/// Service responsible for solution queries and component mapping
/// </summary>
internal class SolutionService
{
private readonly ServiceClient client;
private readonly IConfiguration configuration;
private readonly ILogger<SolutionService> logger;
public SolutionService(ServiceClient client, IConfiguration configuration, ILogger<SolutionService> logger)
{
this.client = client;
this.configuration = configuration;
this.logger = logger;
}
/// <summary>
/// Retrieves solution IDs based on configuration
/// </summary>
public async Task<(List<Guid> SolutionIds, List<Entity> SolutionEntities)> GetSolutionIds()
{
var solutionNameArg = configuration["DataverseSolutionNames"];
if (solutionNameArg == null)
{
throw new Exception("Specify one or more solutions");
}
var solutionNames = solutionNameArg.Split(",").Select(x => x.Trim().ToLower()).ToList();
var entities = await client.RetrieveAllAsync(new QueryExpression("solution")
{
ColumnSet = new ColumnSet("publisherid", "friendlyname", "uniquename", "solutionid"),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("uniquename", ConditionOperator.In, solutionNames)
}
}
});
return (entities.Select(e => e.GetAttributeValue<Guid>("solutionid")).ToList(), entities);
}
/// <summary>
/// Creates Solution DTOs with their components
/// </summary>
public async Task<Dictionary<Guid, (string Name, string Prefix)>> GetPublisherMapAsync(
List<Entity> solutionEntities)
{
// Fetch all unique publishers for the solutions
var publisherIds = solutionEntities
.Select(s => s.GetAttributeValue<EntityReference>("publisherid").Id)
.Distinct()
.ToList();
var publisherQuery = new QueryExpression("publisher")
{
ColumnSet = new ColumnSet("publisherid", "friendlyname", "customizationprefix"),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("publisherid", ConditionOperator.In, publisherIds)
}
}
};
var publishers = await client.RetrieveAllAsync(publisherQuery);
return publishers.ToDictionary(
p => p.GetAttributeValue<Guid>("publisherid"),
p => (
Name: p.GetAttributeValue<string>("friendlyname") ?? "Unknown Publisher",
Prefix: p.GetAttributeValue<string>("customizationprefix") ?? string.Empty
));
}
/// <summary>
/// Extracts publisher information from schema name
/// </summary>
public (string PublisherName, string PublisherPrefix) GetPublisherFromSchemaName(
string schemaName,
Dictionary<Guid, (string Name, string Prefix)> publisherLookup)
{
// Extract prefix from schema name (e.g., "contoso_entity" -> "contoso")
var parts = schemaName.Split('_', 2);
if (parts.Length == 2)
{
var prefix = parts[0];
// Find publisher by matching prefix
foreach (var publisher in publisherLookup.Values)
{
if (publisher.Prefix.Equals(prefix, StringComparison.OrdinalIgnoreCase))
{
return (publisher.Name, publisher.Prefix);
}
}
}
// Default to Microsoft if no prefix or prefix not found
return ("Microsoft", "");
}
}
}