|
1 |
| -using Dataverse.ConfigurationMigrationTool.Console.Common; |
2 |
| -using Dataverse.ConfigurationMigrationTool.Console.Features.Export.Mappers; |
3 |
| -using Dataverse.ConfigurationMigrationTool.Console.Features.Shared; |
| 1 | +using Dataverse.ConfigurationMigrationTool.Console.Features.Shared; |
4 | 2 | using Dataverse.ConfigurationMigrationTool.Console.Features.Shared.Domain;
|
5 | 3 | using Microsoft.Extensions.Logging;
|
6 |
| -using Microsoft.PowerPlatform.Dataverse.Client; |
7 |
| -using Microsoft.Xrm.Sdk; |
8 |
| -using Microsoft.Xrm.Sdk.Metadata; |
9 |
| -using Microsoft.Xrm.Sdk.Query; |
10 | 4 |
|
11 | 5 | namespace Dataverse.ConfigurationMigrationTool.Console.Features.Export;
|
12 | 6 | public interface IDataExportService
|
13 | 7 | {
|
14 |
| - Task<TaskResult> ExportToFile(DataSchema Schema, string outputfile); |
| 8 | + Task<IEnumerable<EntityImport>> ExportEntitiesFromSchema(DataSchema Schema); |
15 | 9 | }
|
16 | 10 | public class DataExportService : IDataExportService
|
17 | 11 | {
|
18 | 12 | private readonly ILogger<DataExportService> _logger;
|
19 |
| - private readonly IOrganizationServiceAsync2 _organizationServiceAsync2; |
20 | 13 | private readonly IMetadataService _metadataService;
|
21 |
| - private static readonly IMapper<(EntityMetadata, EntitySchema, Entity), Record> _recordMapper = new DataverseRecordToRecordMapper(); |
| 14 | + private readonly IDomainService _domainService; |
| 15 | + |
22 | 16 | public DataExportService(ILogger<DataExportService> logger,
|
23 |
| - IOrganizationServiceAsync2 organizationServiceAsync2, |
24 |
| - IMetadataService metadataService) |
| 17 | + IMetadataService metadataService, |
| 18 | + IDomainService domainService) |
25 | 19 | {
|
26 | 20 | _logger = logger;
|
27 |
| - _organizationServiceAsync2 = organizationServiceAsync2; |
28 | 21 | _metadataService = metadataService;
|
| 22 | + _domainService = domainService; |
29 | 23 | }
|
30 | 24 |
|
31 |
| - public async Task<TaskResult> ExportToFile(DataSchema Schema, string outputfile) |
| 25 | + public async Task<IEnumerable<EntityImport>> ExportEntitiesFromSchema(DataSchema Schema) |
32 | 26 | {
|
33 | 27 | var entityImports = new Dictionary<string, EntityImport>();
|
34 | 28 | foreach (var entitySchema in Schema.Entity)
|
35 | 29 | {
|
36 | 30 | _logger.LogInformation("Exporting entity {entityName}", entitySchema.Displayname);
|
37 |
| - |
38 |
| - var exportfields = entitySchema.Fields.Field.Select(f => f.Name).ToList(); |
39 | 31 | var metadata = await _metadataService.GetEntity(entitySchema.Name);
|
40 |
| - var query = new QueryExpression(entitySchema.Name) |
41 |
| - { |
42 |
| - ColumnSet = new ColumnSet(exportfields.ToArray()), |
43 |
| - |
44 |
| - }; |
45 |
| - var entityCollection = await _organizationServiceAsync2.RetrieveAll(query, page: 5000, _logger); |
46 |
| - |
47 |
| - var data = entityCollection.Entities.Select(e => _recordMapper.Map((metadata, entitySchema, e))).ToList(); |
48 |
| - |
| 32 | + var data = await _domainService.GetRecords(metadata, entitySchema); |
49 | 33 | //Add Relationships export
|
50 |
| - |
| 34 | + var entityRelationShips = new List<M2mrelationship>(); |
| 35 | + foreach (var relationship in entitySchema.Relationships.Relationship) |
| 36 | + { |
| 37 | + if (!relationship.ManyToMany) |
| 38 | + { |
| 39 | + continue; |
| 40 | + } |
| 41 | + var relMD = metadata.ManyToManyRelationships.FirstOrDefault(r => r.IntersectEntityName == relationship.RelatedEntityName); |
| 42 | + var relationships = await _domainService.GetM2mRelationships(relMD); |
| 43 | + entityRelationShips.AddRange(relationships); |
| 44 | + |
| 45 | + } |
51 | 46 | entityImports[entitySchema.Name] = new EntityImport
|
52 | 47 | {
|
53 | 48 | Name = entitySchema.Name,
|
54 | 49 | Displayname = entitySchema.Displayname,
|
55 |
| - Records = new Records { Record = data } |
| 50 | + Records = new Records { Record = data.ToList() }, |
| 51 | + M2mrelationships = new M2mrelationships |
| 52 | + { |
| 53 | + M2mrelationship = entityRelationShips |
| 54 | + } |
56 | 55 | };
|
57 | 56 |
|
58 | 57 | }
|
59 | 58 | // Write To File
|
60 |
| - |
61 |
| - return TaskResult.Completed; |
| 59 | + return entityImports.Select(kv => kv.Value).ToList(); |
62 | 60 | }
|
63 | 61 | }
|
0 commit comments