-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkEntitiesByBatchId
84 lines (69 loc) · 3.05 KB
/
LinkEntitiesByBatchId
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
using Stylelabs.M.Base.Querying;
using Stylelabs.M.Base.Querying.Linq;
using Stylelabs.M.Framework.Essentials.LoadOptions;
using Stylelabs.M.Sdk.Contracts.Base;
using Stylelabs.M.Sdk.Contracts.Querying;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
MClient.Logger.Info("Getting Entities");
int totalMappedProducts = default;
int totalSkipped = default;
int total = default;
CultureInfo defaultCulture = await MClient.Cultures.GetDefaultCultureAsync();
var query = Query.CreateQuery(entities => from e in entities
where e.DefinitionName == "definitionName"
&& e.Property("BatchId") == "batch1" // Remove this line if you want to run it on all entities of the definition
select e);
var result = await MClient.Querying.QueryIdsAsync(query);
long totalNumberInCH = result.TotalNumberOfResults;
MClient.Logger.Info($"Total Number of entities - {totalNumberInCH}");
IIdIterator iterator = MClient.Querying.CreateEntityIdIterator(query);
while (await iterator.MoveNextAsync())
{
var entities = iterator.Current.Items;
foreach (var item in entities)
{
++total;
var entity = await MClient.Entities.GetAsync(item);
if(entity == null)
{
MClient.Logger.Info($"Entity {item} not found.");
continue;
}
MClient.Logger.Info($"Processing {entity.Identifier} - {entity.Id} . Total processed - {total}. Total in CH - {totalNumberInCH}.");
#region Load Options
var propertyLoadOption = new PropertyLoadOption("Catalog", "ProductName");
var relationLoadOption = new RelationLoadOption("Products");
var isLoaded = await entity.LoadMembersAsync(propertyLoadOption, relationLoadOption);
if (!isLoaded)
continue;
#endregion
var relation = entity.GetRelation("Products", RelationRole.Parent);
var productName = entity.GetPropertyValue("ProductName");
MClient.Logger.Info(productName.ToString());
var productId = await GetProductId(productName.ToString().Trim());
if (!productId.HasValue)
{
MClient.Logger.Info($"Product {productName} was not found in CH");
continue;
}
relation.SetIds(new List<long> { productId.Value });
var id = await MClient.Entities.SaveAsync(entity);
MClient.Logger.Info($"Relation [{productId}] added to {entity.DefinitionName} | {entity.Id}");
MClient.Logger.Info(productName.ToString());
}
}
async Task<long?> GetProductId(string product)
{
MClient.Logger.Info($"Getting Product - {product}");
var query = Query.CreateQuery(entities =>
from e in entities
where e.Property("ProductName") == product
&& e.DefinitionName == "M.PCM.Product"
select e);
var productId = await MClient.Querying.SingleIdAsync(query);
MClient.Logger.Info($"Product {product} found. Id - {productId}");
return productId;
}