-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataverse-add-cd-column.linq
111 lines (97 loc) · 3.01 KB
/
dataverse-add-cd-column.linq
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
<Query Kind="Program" />
void Main()
{
/* TODO:
use datamodel and water data model entity folder
use list of tables to modify
for each table, check if CustomDimension column already exists
if exists, then update
if not, then add it.
could avoid update if we want to just revert changes in git and re-run add
need to add CustomDimension column manually to 1 table, then export to capture xml to add
*/
string rootFolder = @"C:\repos\CRM.Solutions.SustainabilityService\solutions\SustainabilityDataModel\Solution\Entities";
//string rootFolder = @"C:\repos\CRM.Solutions.SustainabilityService\solutions\SustainabilityWaterDataModel\Solution\Entities";
string cdAttributeXMLFilePath = @"C:\Users\zackmoore\OneDrive - Microsoft\Documents\CustomDimensions\dataverse-entity-customdimension-attribute-fragment.xml";
HashSet<string> set = new()
{
"msdyn_businesstravel",
"msdyn_capitalgood",
"msdyn_emission",
"msdyn_employeecommuting",
"msdyn_endoflifetreatmentofsoldproducts",
"msdyn_fugitiveemission",
"msdyn_industrialprocess",
"msdyn_mobilecombustion",
"msdyn_purchasedenergy",
"msdyn_purchasedgoodandservice",
"msdyn_stationarycombustion",
"msdyn_transportationanddistribution",
"msdyn_wastegeneratedinoperations",
"msdyn_waterquantity",
"msdyn_watersample",
"msdyn_watertestresult",
};
foreach (var subfolder in Directory.GetDirectories(rootFolder))
{
string tmpEntityFolderName = Path.GetFileName(subfolder);
if(!set.Contains(tmpEntityFolderName))
{
continue;
}
string entityFilePathName = Path.Combine(subfolder, "Entity.xml");
if(File.Exists(entityFilePathName))
{
XDocument xdoc = XDocument.Load(entityFilePathName);
var xEntity = xdoc.Element("Entity")?.Element("EntityInfo")?.Element("entity");
if(xEntity == null)
{
Console.WriteLine($"Skipped '{entityFilePathName}' b/c no Entity");
continue;
}
var entityName = xEntity.Attribute("Name").Value;
Console.WriteLine($"Entity '{entityFilePathName}'");
var xAttributes = xEntity.Element("attributes").Elements();
XElement xNewCd = XElement.Load(cdAttributeXMLFilePath);
string n = xNewCd?.Element("Name")?.Value;
XElement xprev = null;
if(xAttributes.Count() == 0)
{
xEntity.Element("attributes").Add(xNewCd);
}
else
{
foreach (var xcur in xAttributes)
{
string p = xprev?.Element("Name")?.Value;
string c = xcur.Element("Name")?.Value;
int compareNP = string.Compare(n, p);
int compareNC = string.Compare(n, c);
if (p == null && compareNC < 0)
{
//xcur.AddBeforeSelf(xNewCd);
break;
}
else if (compareNC == 0)
{
xcur.ReplaceWith(xNewCd);
break;
}
else if (compareNP >= 0 && compareNC < 0)
{
//xcur.AddBeforeSelf(xNewCd);
xprev.AddAfterSelf(xNewCd);
break;
}
else if (compareNC > 0 && xcur.NextNode == null)
{
xcur.AddAfterSelf(xNewCd);
break;
}
xprev = xcur;
}
}
xdoc.Save(entityFilePathName);
}
}
}