-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDataPartIndexer.cs
More file actions
105 lines (84 loc) · 2.94 KB
/
DataPartIndexer.cs
File metadata and controls
105 lines (84 loc) · 2.94 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
using System.Collections.ObjectModel;
using ByteSync.Business.Actions.Local;
using ByteSync.Business.Comparisons;
using ByteSync.Interfaces.Services.Sessions;
using ByteSync.Models.Inventories;
namespace ByteSync.Services.Sessions;
public class DataPartIndexer : IDataPartIndexer
{
public DataPartIndexer()
{
Inventories = new List<Inventory>();
DataPartsByNames = new Dictionary<string, DataPart>();
}
private List<Inventory> Inventories { get; }
private Dictionary<string, DataPart> DataPartsByNames { get; set; }
public void BuildMap(List<Inventory> inventories)
{
Inventories.Clear();
Inventories.AddAll(inventories);
DataPartsByNames.Clear();
bool isInventoryWithMultipleParts = inventories.Any(i => i.InventoryParts.Count > 1);
var cptInventory = 0;
foreach (var inventory in Inventories)
{
var inventoryLetter = ((char)('A' + cptInventory)).ToString();
if (!isInventoryWithMultipleParts)
{
var dataPart = new DataPart(inventoryLetter, inventory);
DataPartsByNames.Add(dataPart.Name, dataPart);
}
else
{
var cptPart = 1;
foreach (var inventoryPart in inventory.InventoryParts)
{
var name = $"{inventoryLetter}{cptPart}";
var dataPart = new DataPart(name, inventoryPart);
DataPartsByNames.Add(dataPart.Name, dataPart);
cptPart += 1;
}
}
cptInventory += 1;
}
}
public ReadOnlyCollection<DataPart> GetAllDataParts()
{
return DataPartsByNames.Values.ToList().AsReadOnly();
}
public DataPart? GetDataPart(DataPart? dataPart)
{
return GetDataPart(dataPart?.Name);
}
public DataPart? GetDataPart(string? dataPartName)
{
if (dataPartName == null)
{
return null;
}
if (DataPartsByNames.TryGetValue(dataPartName, out var dataPart))
{
return dataPart;
}
else
{
return null;
}
}
public void Remap(ICollection<SynchronizationRule> synchronizationRules)
{
foreach (var synchronizationRule in synchronizationRules)
{
foreach (var action in synchronizationRule.Actions)
{
action.Source = GetDataPart(action.Source);
action.Destination = GetDataPart(action.Destination);
}
foreach (var condition in synchronizationRule.Conditions)
{
condition.Source = GetDataPart(condition.Source)!;
condition.Destination = GetDataPart(condition.Destination);
}
}
}
}